Revision: 29557 http://sourceforge.net/p/bibdesk/svn/29557 Author: hofman Date: 2025-09-08 16:12:38 +0000 (Mon, 08 Sep 2025) Log Message: ----------- save suffested filename instead of full response in download wrapper
Modified Paths: -------------- trunk/bibdesk/BDSKDownloadManager.h trunk/bibdesk/BDSKDownloadManager.m trunk/bibdesk/BDSKTextImportController.m Modified: trunk/bibdesk/BDSKDownloadManager.h =================================================================== --- trunk/bibdesk/BDSKDownloadManager.h 2025-09-08 15:32:17 UTC (rev 29556) +++ trunk/bibdesk/BDSKDownloadManager.h 2025-09-08 16:12:38 UTC (rev 29557) @@ -65,7 +65,7 @@ - (void)cancel:(NSInteger)uniqueID; - (void)remove:(NSInteger)uniqueID; -- (void)addDownload:(id)download; +- (void)addDownload:(id)download request:(NSURLRequest *)request; @end @@ -82,12 +82,12 @@ NSURL *URL; NSURL *fileURL; NSURL *tempFileURL; + NSString *suggestedFilename; BDSKDownloadStatus status; - NSURLResponse *response; id download; } -- (instancetype)initWithDownload:(id)aDownload; +- (instancetype)initWithDownload:(id)aDownload URL:(NSURL *)aURL; @property (nonatomic, readonly) id download; @property (nonatomic, readonly) NSInteger uniqueID; @@ -95,7 +95,7 @@ @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, nullable, strong) NSString *suggestedFilename; @property (nonatomic) BDSKDownloadStatus status; - (void)cancel; Modified: trunk/bibdesk/BDSKDownloadManager.m =================================================================== --- trunk/bibdesk/BDSKDownloadManager.m 2025-09-08 15:32:17 UTC (rev 29556) +++ trunk/bibdesk/BDSKDownloadManager.m 2025-09-08 16:12:38 UTC (rev 29557) @@ -118,8 +118,8 @@ [[NSNotificationCenter defaultCenter] postNotificationName:BDSKDownloadsDidChangeNotification object:self]; } -- (void)addDownload:(id)download { - [downloads addObject:[[BDSKWebDownload alloc] initWithDownload:download]]; +- (void)addDownload:(id)download request:(NSURLRequest *)request { + [downloads addObject:[[BDSKWebDownload alloc] initWithDownload:download URL:[request URL]]]; [self notifyUpdate]; } @@ -281,20 +281,20 @@ @implementation BDSKWebDownload -@synthesize download, uniqueID, URL, fileURL, tempFileURL, status, response; +@synthesize download, uniqueID, URL, fileURL, tempFileURL, status, suggestedFilename; @dynamic fileName; static NSInteger currentUniqueID = 0; -- (instancetype)initWithDownload:(id)aDownload { +- (instancetype)initWithDownload:(id)aDownload URL:(NSURL *)aURL { self = [super init]; if (self) { uniqueID = ++currentUniqueID; - URL = [([aDownload respondsToSelector:@selector(originalRequest)] ? [aDownload originalRequest] : [aDownload respondsToSelector:@selector(request)] ? [aDownload request] : [[aDownload task] originalRequest]) URL]; + URL = aURL; fileURL = nil; status = BDSKDownloadStatusDownloading; download = aDownload; - response = nil; + suggestedFilename = nil; } return self; } @@ -302,7 +302,9 @@ - (NSString *)fileName { NSString *fileName = [fileURL lastPathComponent]; if (fileName == nil) { - if ([[URL path] length] > 1) { + if (suggestedFilename) { + fileName = suggestedFilename; + } else if ([[URL path] length] > 1) { fileName = [URL lastPathComponent]; } else { fileName = [URL host]; @@ -318,7 +320,7 @@ status = newStatus; if (status != BDSKDownloadStatusDownloading) { download = nil; - response = nil; + suggestedFilename = nil; } if (status == BDSKDownloadStatusFailed) [self setFileURL:nil]; @@ -354,13 +356,13 @@ @implementation BDSKWebDownloadDelegate - (void)downloadDidBegin:(NSURLDownload *)download { - [downloadManager addDownload:download]; + [downloadManager addDownload:download request:[download request]]; } - (void)downloadDidFinish:(NSURLDownload *)download { BDSKWebDownload *webDownload = [downloadManager webDownloadForDownload:download]; - NSString *filename = [[webDownload response] suggestedFilename]; + NSString *filename = [webDownload suggestedFilename]; NSString *disposition = [[download request] valueForHTTPHeaderField:@"Content-Disposition"]; if ([disposition hasPrefix:@"attachment; filename*=UTF-8''"]) { @@ -392,7 +394,20 @@ } - (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response { - [[downloadManager webDownloadForDownload:download] setResponse:response]; + NSString *filename = [response suggestedFilename]; + NSString *disposition = [[download request] valueForHTTPHeaderField:@"Content-Disposition"]; + if ([disposition hasPrefix:@"attachment; filename*=UTF-8''"]) { + disposition = [disposition substringFromIndex:29]; + if ([disposition length]) + filename = [disposition stringByRemovingPercentEncoding]; + } else if ([disposition hasPrefix:@"attachment; filename="]) { + disposition = [disposition substringFromIndex:21]; + if ([disposition hasPrefix:@"\""] && [disposition hasSuffix:@"\""]) + disposition = [disposition substringWithRange:NSMakeRange(1, [disposition length] - 2)]; + if ([disposition length]) + filename = disposition; + } + [[downloadManager webDownloadForDownload:download] setSuggestedFilename:filename]; } // -download:decideDestinationWithSuggestedFilename: is not functional anymore Modified: trunk/bibdesk/BDSKTextImportController.m =================================================================== --- trunk/bibdesk/BDSKTextImportController.m 2025-09-08 15:32:17 UTC (rev 29556) +++ trunk/bibdesk/BDSKTextImportController.m 2025-09-08 16:12:38 UTC (rev 29557) @@ -957,8 +957,9 @@ } else if (@available(macOS 11.3, *)) { decisionHandler(WKNavigationResponsePolicyDownload); } else { - BDSKDownload *aDownload = [[BDSKDownloader sharedDownloader] startFileDownloadWithRequest:[NSURLRequest requestWithURL:[[navigationResponse response] URL]] delegate:[[BDSKDownloadManager sharedManager] bdskDownloadDelegate]]; - [[BDSKDownloadManager sharedManager] addDownload:aDownload]; + NSURLRequest *request = [NSURLRequest requestWithURL:[[navigationResponse response] URL]]; + BDSKDownload *aDownload = [[BDSKDownloader sharedDownloader] startFileDownloadWithRequest:request delegate:[[BDSKDownloadManager sharedManager] bdskDownloadDelegate]]; + [[BDSKDownloadManager sharedManager] addDownload:aDownload request:request]; decisionHandler(WKNavigationResponsePolicyCancel); } } @@ -970,8 +971,9 @@ else decisionHandler(WKNavigationActionPolicyAllow, preferences); } else if ([navigationAction navigationType] == WKNavigationTypeFormSubmitted || [[[navigationAction request] valueForHTTPHeaderField:@"Content-Disposition"] hasPrefix:@"attachment"]) { - BDSKDownload *aDownload = [[BDSKDownloader sharedDownloader] startFileDownloadWithRequest:[navigationAction request] delegate:[[BDSKDownloadManager sharedManager] bdskDownloadDelegate]]; - [[BDSKDownloadManager sharedManager] addDownload:aDownload]; + NSURLRequest *request = [navigationAction request]; + BDSKDownload *aDownload = [[BDSKDownloader sharedDownloader] startFileDownloadWithRequest:request delegate:[[BDSKDownloadManager sharedManager] bdskDownloadDelegate]]; + [[BDSKDownloadManager sharedManager] addDownload:aDownload request:request]; decisionHandler(WKNavigationActionPolicyCancel, preferences); } else { decisionHandler(WKNavigationActionPolicyAllow, preferences); @@ -980,12 +982,12 @@ - (void)webView:(WKWebView *)aWebView navigationAction:(WKNavigationAction *)navigationAction didBecomeDownload:(WKDownload *)aDownload API_AVAILABLE(macos(11.3)) { [aDownload setDelegate:[[BDSKDownloadManager sharedManager] wkDownloadDelegate]]; - [[BDSKDownloadManager sharedManager] addDownload:aDownload]; + [[BDSKDownloadManager sharedManager] addDownload:aDownload request:[aDownload originalRequest]]; } - (void)webView:(WKWebView *)aWebView navigationResponse:(WKNavigationResponse *)navigationResponse didBecomeDownload:(WKDownload *)aDownload API_AVAILABLE(macos(11.3)) { [aDownload setDelegate:[[BDSKDownloadManager sharedManager] wkDownloadDelegate]]; - [[BDSKDownloadManager sharedManager] addDownload:aDownload]; + [[BDSKDownloadManager sharedManager] addDownload:aDownload request:[aDownload originalRequest]]; } #pragma mark BDSKUIDelegate protocol @@ -1086,11 +1088,11 @@ if (@available(macOS 11.3, *)) { [webView startDownloadUsingRequest:request completionHandler:^(WKDownload *aDownload){ [aDownload setDelegate:[[BDSKDownloadManager sharedManager] wkDownloadDelegate]]; - [[BDSKDownloadManager sharedManager] addDownload:aDownload]; + [[BDSKDownloadManager sharedManager] addDownload:aDownload request:request]; }]; } else { BDSKDownload *aDownload = [[BDSKDownloader sharedDownloader] startFileDownloadWithRequest:request delegate:[[BDSKDownloadManager sharedManager] bdskDownloadDelegate]]; - [[BDSKDownloadManager sharedManager] addDownload:aDownload]; + [[BDSKDownloadManager sharedManager] addDownload:aDownload request:request]; } } else { [[NSWorkspace sharedWorkspace] openURLWithDefaultApp:url]; 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