Revision: 29552
          http://sourceforge.net/p/bibdesk/svn/29552
Author:   hofman
Date:     2025-09-08 09:36:37 +0000 (Mon, 08 Sep 2025)
Log Message:
-----------
Move existing file at download target location to tmp location, move back at 
failure or delete at success

Modified Paths:
--------------
    trunk/bibdesk/BDSKDownloadManager.h
    trunk/bibdesk/BDSKDownloadManager.m
    trunk/bibdesk/BDSKDownloader.m

Modified: trunk/bibdesk/BDSKDownloadManager.h
===================================================================
--- trunk/bibdesk/BDSKDownloadManager.h 2025-09-08 09:03:57 UTC (rev 29551)
+++ trunk/bibdesk/BDSKDownloadManager.h 2025-09-08 09:36:37 UTC (rev 29552)
@@ -81,6 +81,7 @@
     NSInteger uniqueID;
     NSURL *URL;
     NSURL *fileURL;
+    NSURL *tempFileURL;
     BDSKDownloadStatus status;
     NSURLResponse *response;
     id download;
@@ -93,6 +94,7 @@
 @property (nonatomic, readonly) NSURL *URL;
 @property (nonatomic, nullable, strong) NSURL *fileURL;
 @property (nonatomic, nullable, readonly) NSString *fileName;
+@property (nonatomic, nullable, strong) NSURL *tempFileURL;
 @property (nonatomic, nullable, strong) NSURLResponse *response;
 @property (nonatomic) BDSKDownloadStatus status;
 

Modified: trunk/bibdesk/BDSKDownloadManager.m
===================================================================
--- trunk/bibdesk/BDSKDownloadManager.m 2025-09-08 09:03:57 UTC (rev 29551)
+++ trunk/bibdesk/BDSKDownloadManager.m 2025-09-08 09:36:37 UTC (rev 29552)
@@ -213,6 +213,14 @@
     if (webDownload == nil)
         return;
     
+    NSURL *tmpURL = [webDownload tempFileURL];
+    if (tmpURL) {
+        if (error && [webDownload fileURL])
+            [[NSFileManager defaultManager] moveItemAtURL:tmpURL 
toURL:[webDownload fileURL] error:NULL];
+        else
+            [[NSFileManager defaultManager] removeItemAtURL:tmpURL error:NULL];
+    }
+    
     [webDownload setStatus:error ? BDSKDownloadStatusFailed : 
BDSKDownloadStatusFinished];
     
     if ([[NSUserDefaults standardUserDefaults] boolForKey:(error ? 
BDSKRemoveFailedDownloadsKey : BDSKRemoveFinishedDownloadsKey)])
@@ -229,7 +237,7 @@
     }
 }
 
-- (void)decideDestinationWithSuggestedFilename:(NSString *)filename 
modalForWindow:(NSWindow *)window completionHandler:(void (^)(NSURL * 
destinationURL))completionHandler {
+- (void)download:(id)download decideDestinationWithSuggestedFilename:(NSString 
*)filename modalForWindow:(NSWindow *)window completionHandler:(void (^)(NSURL 
* destinationURL))completionHandler {
     NSString *extension = [filename pathExtension];
    
     NSString *downloadsDirectory = [[[NSUserDefaults standardUserDefaults] 
stringForKey:BDSKDownloadsDirectoryKey] stringByExpandingTildeInPath];
@@ -251,8 +259,11 @@
         if (result == NSModalResponseOK) {
             NSURL *fileURL = [sPanel URL];
             // if a file exists, the user should have confirmed to overwrite
-            if ([fileURL checkResourceIsReachableAndReturnError:NULL])
-                [[NSFileManager defaultManager] removeItemAtURL:fileURL 
error:NULL];
+            if ([fileURL checkResourceIsReachableAndReturnError:NULL]) {
+                NSURL *tmpURL = [[NSFileManager defaultManager] 
temporaryFileURLWithBasename:[fileURL lastPathComponent]];
+                [[NSFileManager defaultManager] moveItemAtURL:fileURL 
toURL:tmpURL error:NULL];
+                [[self webDownloadForDownload:download] setTempFileURL:tmpURL];
+            }
             completionHandler(fileURL);
         } else {
             completionHandler(nil);
@@ -270,7 +281,7 @@
 
 @implementation BDSKWebDownload
 
-@synthesize download, uniqueID, URL, fileURL, status, response;
+@synthesize download, uniqueID, URL, fileURL, tempFileURL, status, response;
 @dynamic fileName;
 
 static NSInteger currentUniqueID = 0;
@@ -311,6 +322,7 @@
         }
         if (status == BDSKDownloadStatusFailed)
             [self setFileURL:nil];
+        [self setTempFileURL:nil];
     }
 }
 
@@ -359,11 +371,11 @@
             filename = disposition;
     }
     
-    [downloadManager decideDestinationWithSuggestedFilename:filename 
modalForWindow:nil completionHandler:^(NSURL *destinationURL){
+    [downloadManager download:download 
decideDestinationWithSuggestedFilename:filename modalForWindow:nil 
completionHandler:^(NSURL *destinationURL){
         NSError *error = nil;
         if (destinationURL) {
-            if ([[NSFileManager defaultManager] moveItemAtURL:[webDownload 
fileURL] toURL:destinationURL error:&error])
-                [webDownload setFileURL:destinationURL];
+            [webDownload setFileURL:destinationURL];
+            [[NSFileManager defaultManager] moveItemAtURL:[webDownload 
fileURL] toURL:destinationURL error:&error];
         } else {
             error = [NSError errorWithDomain:NSURLErrorDomain 
code:NSURLErrorCancelled userInfo:nil];
         }
@@ -405,7 +417,7 @@
 }
 
 - (void)download:(WKDownload *)download 
decideDestinationUsingResponse:(NSURLResponse *)response 
suggestedFilename:(NSString *)filename completionHandler:(void (^)(NSURL * 
destinationURL))completionHandler API_AVAILABLE(macos(11.3)) {
-    [downloadManager decideDestinationWithSuggestedFilename:filename 
modalForWindow:[[download webView] window] completionHandler:^(NSURL 
*destinationURL){
+    [downloadManager download:download 
decideDestinationWithSuggestedFilename:filename modalForWindow:[[download 
webView] window] completionHandler:^(NSURL *destinationURL){
         if (destinationURL)
             [[downloadManager webDownloadForDownload:download] 
setFileURL:destinationURL];
         completionHandler(destinationURL);
@@ -423,7 +435,7 @@
 }
 
 - (void)download:(BDSKDownload *)download 
decideDestinationWithSuggestedFilename:(NSString *)filename 
completionHandler:(void (^)(NSURL *destinationURL))completionHandler {
-    [downloadManager decideDestinationWithSuggestedFilename:filename 
modalForWindow:nil completionHandler:completionHandler];
+    [downloadManager download:download 
decideDestinationWithSuggestedFilename:filename modalForWindow:nil 
completionHandler:completionHandler];
 }
 
 - (void)download:(BDSKDownload *)download didCreateDestination:(NSURL 
*)destinationURL {

Modified: trunk/bibdesk/BDSKDownloader.m
===================================================================
--- trunk/bibdesk/BDSKDownloader.m      2025-09-08 09:03:57 UTC (rev 29551)
+++ trunk/bibdesk/BDSKDownloader.m      2025-09-08 09:36:37 UTC (rev 29552)
@@ -177,8 +177,8 @@
             if ([[destinationURL URLByDeletingLastPathComponent] 
checkResourceIsReachableAndReturnError:NULL] == NO) {
                 [fm createDirectoryAtPath:[[destinationURL 
URLByDeletingLastPathComponent] path] withIntermediateDirectories:YES 
attributes:nil error:NULL];
             }
+            [download _didCreateDestination:destinationURL];
             if ([fm moveItemAtURL:location toURL:destinationURL error:&error]) 
{
-                [download _didCreateDestination:destinationURL];
                 [download _didCompleteWithError:nil];
             } else {
                 [download _didCompleteWithError:error];

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to