Revision: 28125
http://sourceforge.net/p/bibdesk/svn/28125
Author: hofman
Date: 2022-12-16 17:14:47 +0000 (Fri, 16 Dec 2022)
Log Message:
-----------
Cancel automatically started item downloads when the returned MIME type does
not match the types to automatically download. The extension of the remote URL
may not be the same as the downloaded type, for instance due to redirection or
errors.
Modified Paths:
--------------
trunk/bibdesk/BDSKEditor.m
trunk/bibdesk/BDSKItemDownload.h
trunk/bibdesk/BDSKItemDownload.m
trunk/bibdesk/BibDocument.m
trunk/bibdesk/BibDocument_Actions.m
trunk/bibdesk/BibDocument_DataSource.m
trunk/bibdesk/BibItem+Scripting.m
trunk/bibdesk/BibItem.h
trunk/bibdesk/BibItem.m
Modified: trunk/bibdesk/BDSKEditor.m
===================================================================
--- trunk/bibdesk/BDSKEditor.m 2022-12-16 07:30:21 UTC (rev 28124)
+++ trunk/bibdesk/BDSKEditor.m 2022-12-16 17:14:47 UTC (rev 28125)
@@ -620,11 +620,11 @@
if ([[NSUserDefaults standardUserDefaults]
boolForKey:BDSKUseLocalUrlAndUrlKey] == NO) {
for (BDSKLinkedFile *file in [publication remoteURLs]) {
if ([publication canDownloadLinkedFile:file])
- [publication downloadLinkedFile:file replace:NO];
+ [publication downloadLinkedFile:file
options:BDSKDownloadDefault];
}
} else if ([publication canDownloadURLForField:BDSKUrlString]) {
if ([NSString isEmptyString:[publication
valueOfField:BDSKLocalUrlString inherit:NO]]) {
- [publication downloadURLForField:BDSKUrlString];
+ [publication downloadURLForField:BDSKUrlString
options:BDSKDownloadDefault];
} else {
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setMessageText:NSLocalizedString(@"Local-Url is set",
@"Message in alert dialog")];
@@ -633,7 +633,7 @@
[alert addButtonWithTitle:NSLocalizedString(@"Cancel", @"Button
title")];
[alert beginSheetModalForWindow:[self window]
completionHandler:^(NSInteger returnCode){
if (returnCode == NSAlertFirstButtonReturn)
- [publication downloadURLForField:BDSKUrlString];
+ [publication downloadURLForField:BDSKUrlString
options:BDSKDownloadDefault];
}];
}
}
@@ -1857,7 +1857,7 @@
- (BOOL)fileView:(FVFileView *)aFileView shouldDownloadURL:(NSURL *)aURL
atIndex:(NSUInteger)anIndex replace:(BOOL)replace {
BDSKLinkedFile *file = [publication objectInFilesAtIndex:anIndex];
if ([publication canDownloadLinkedFile:file])
- [publication downloadLinkedFile:file replace:replace];
+ [publication downloadLinkedFile:file options:replace ?
BDSKDownloadReplace : BDSKDownloadDefault];
return NO;
}
Modified: trunk/bibdesk/BDSKItemDownload.h
===================================================================
--- trunk/bibdesk/BDSKItemDownload.h 2022-12-16 07:30:21 UTC (rev 28124)
+++ trunk/bibdesk/BDSKItemDownload.h 2022-12-16 17:14:47 UTC (rev 28125)
@@ -49,7 +49,7 @@
BDSKLinkedFile *linkedFile;
NSURL *URL;
NSUInteger index;
- BOOL replace;
+ NSUInteger options;
int64_t expectedLength, receivedLength;
NSURL *fileURL;
}
@@ -57,13 +57,14 @@
@property (nonatomic, assign) id <BDSKItemDownloadDelegate> delegate;
@property (nonatomic, readonly) BDSKLinkedFile *linkedFile;
@property (nonatomic, readonly) NSURL *URL;
+@property (nonatomic, readonly) NSURLResponse *response;
@property (nonatomic) NSUInteger index;
-@property (nonatomic, readonly) BOOL replace;
+@property (nonatomic, readonly) NSUInteger options;
@property (nonatomic, readonly) double progress;
@property (nonatomic, readonly) NSURL *fileURL;
-- (id)initWithLinkedFile:(BDSKLinkedFile *)aLinkedFile
atIndex:(NSUInteger)anIndex replace:(BOOL)aReplace;
-- (id)initWithURL:(NSURL *)aURL;
+- (id)initWithLinkedFile:(BDSKLinkedFile *)aLinkedFile
atIndex:(NSUInteger)anIndex options:(NSUInteger)anOptions;
+- (id)initWithURL:(NSURL *)aURL options:(NSUInteger)anOptions;
- (void)start;
- (void)cancel;
@@ -72,6 +73,7 @@
@protocol BDSKItemDownloadDelegate <NSObject>
+- (void)itemDownloadDidStart:(BDSKItemDownload *)download;
- (void)itemDownloadDidUpdate:(BDSKItemDownload *)download;
- (void)itemDownloadDidFinish:(BDSKItemDownload *)download;
- (NSWindow *)itemDownloadWindowForAuthenticationSheet:(BDSKItemDownload
*)download;
Modified: trunk/bibdesk/BDSKItemDownload.m
===================================================================
--- trunk/bibdesk/BDSKItemDownload.m 2022-12-16 07:30:21 UTC (rev 28124)
+++ trunk/bibdesk/BDSKItemDownload.m 2022-12-16 17:14:47 UTC (rev 28125)
@@ -43,16 +43,16 @@
@implementation BDSKItemDownload
-@synthesize delegate, linkedFile, URL, index, replace, fileURL;
-@dynamic progress;
+@synthesize delegate, linkedFile, URL, index, options, fileURL;
+@dynamic response, progress;
-- (id)initWithURL:(NSURL *)aURL {
+- (id)initWithURL:(NSURL *)aURL options:(NSUInteger)anOptions {
self = [super init];
if (self) {
linkedFile = nil;
URL = [aURL retain];
index = NSNotFound;
- replace = NO;
+ options = anOptions;
download = nil;
expectedLength = 0;
receivedLength = 0;
@@ -60,13 +60,13 @@
return self;
}
-- (id)initWithLinkedFile:(BDSKLinkedFile *)aLinkedFile
atIndex:(NSUInteger)anIndex replace:(BOOL)aReplace {
+- (id)initWithLinkedFile:(BDSKLinkedFile *)aLinkedFile
atIndex:(NSUInteger)anIndex options:(NSUInteger)anOptions {
self = [super init];
if (self) {
linkedFile = [aLinkedFile retain];
URL = [[linkedFile URL] retain];
index = anIndex;
- replace = aReplace;
+ options = anOptions;
download = nil;
expectedLength = 0;
receivedLength = 0;
@@ -98,6 +98,10 @@
return expectedLength > 0 ? (double)receivedLength /
(double)expectedLength : (CGFloat)NSURLResponseUnknownLength;
}
+- (NSURLResponse *)response {
+ return [download response];
+}
+
#pragma mark BDSKDownloaderDelegate
- (void)downloader:(BDSKDownloader *)downloader download:(BDSKDownload
*)aDownload decideDestinationWithSuggestedFilename:(NSString *)destinationName
completionHandler:(void (^)(NSURL *destinationURL, BOOL
allowOverwrite))completionHandler {
@@ -132,7 +136,7 @@
- (void)downloader:(BDSKDownloader *)downloader download:(BDSKDownload
*)aDownload didReceiveExpectedContentLength:(int64_t)expectedContentLength {
expectedLength = expectedContentLength;
- [[self delegate] itemDownloadDidUpdate:self];
+ [[self delegate] itemDownloadDidStart:self];
}
- (void)downloader:(BDSKDownloader *)downloader download:(BDSKDownload
*)aDownload didReceiveDataOfLength:(uint64_t)length {
Modified: trunk/bibdesk/BibDocument.m
===================================================================
--- trunk/bibdesk/BibDocument.m 2022-12-16 07:30:21 UTC (rev 28124)
+++ trunk/bibdesk/BibDocument.m 2022-12-16 17:14:47 UTC (rev 28125)
@@ -2218,17 +2218,19 @@
}
if ([sud boolForKey:BDSKDownloadImportedURLsKey]) {
- BOOL replace = [sud boolForKey:BDSKReplaceDownloadedURLsKey];
NSArray *types = [sud stringArrayForKey:BDSKURLTypesToDownloadKey];
+ BDSKDownloadOption dlOptions = BDSKDownloadCheckType;
+ if ([sud boolForKey:BDSKReplaceDownloadedURLsKey])
+ options |= BDSKDownloadReplace;
for (pub in newPubs) {
if (useLocalUrlAndUrl) {
if ([pub canDownloadURLForField:BDSKUrlString] && [NSString
isEmptyString:[pub valueOfField:BDSKLocalUrlString inherit:NO]] && [types
containsObject:[[[pub remoteURLForField:BDSKUrlString] pathExtension]
lowercaseString] ?: @""])
- [pub downloadURLForField:BDSKUrlString];
+ [pub downloadURLForField:BDSKUrlString options:dlOptions];
} else if ([[pub localFiles] count] == 0) {
for (BDSKLinkedFile *linkedURL in [pub remoteURLs]) {
if ([pub canDownloadLinkedFile:linkedURL] &&
[types containsObject:[[[linkedURL URL] pathExtension]
lowercaseString] ?: @""]) {
- [pub downloadLinkedFile:linkedURL replace:replace];
+ [pub downloadLinkedFile:linkedURL options:dlOptions];
}
}
}
Modified: trunk/bibdesk/BibDocument_Actions.m
===================================================================
--- trunk/bibdesk/BibDocument_Actions.m 2022-12-16 07:30:21 UTC (rev 28124)
+++ trunk/bibdesk/BibDocument_Actions.m 2022-12-16 17:14:47 UTC (rev 28125)
@@ -1135,11 +1135,11 @@
if ([[NSUserDefaults standardUserDefaults]
boolForKey:BDSKUseLocalUrlAndUrlKey] == NO) {
for (BDSKLinkedFile *file in [pub remoteURLs]) {
if ([pub canDownloadLinkedFile:file])
- [pub downloadLinkedFile:file replace:NO];
+ [pub downloadLinkedFile:file options:BDSKDownloadDefault];
}
} else if ([pub canDownloadURLForField:BDSKUrlString]) {
if ([NSString isEmptyString:[pub valueOfField:BDSKLocalUrlString
inherit:NO]]) {
- [pub downloadURLForField:BDSKUrlString];
+ [pub downloadURLForField:BDSKUrlString
options:BDSKDownloadDefault];
} else {
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setMessageText:NSLocalizedString(@"Local-Url is set",
@"Message in alert dialog")];
@@ -1148,7 +1148,7 @@
[alert addButtonWithTitle:NSLocalizedString(@"Cancel", @"Button
title")];
[alert beginSheetModalForWindow:[self windowForSheet]
completionHandler:^(NSInteger returnCode){
if (returnCode == NSAlertFirstButtonReturn)
- [pub downloadURLForField:BDSKUrlString];
+ [pub downloadURLForField:BDSKUrlString
options:BDSKDownloadDefault];
}];
}
}
Modified: trunk/bibdesk/BibDocument_DataSource.m
===================================================================
--- trunk/bibdesk/BibDocument_DataSource.m 2022-12-16 07:30:21 UTC (rev
28124)
+++ trunk/bibdesk/BibDocument_DataSource.m 2022-12-16 17:14:47 UTC (rev
28125)
@@ -1723,7 +1723,7 @@
BibItem *publication = [self singleSelectedPublication];
if (publication) {
BDSKLinkedFile *linkedFile = [publication
objectInFilesAtIndex:anIndex];
- [publication downloadLinkedFile:linkedFile replace:replace];
+ [publication downloadLinkedFile:linkedFile options:replace ?
BDSKDownloadReplace : BDSKDownloadDefault];
}
return NO;
}
Modified: trunk/bibdesk/BibItem+Scripting.m
===================================================================
--- trunk/bibdesk/BibItem+Scripting.m 2022-12-16 07:30:21 UTC (rev 28124)
+++ trunk/bibdesk/BibItem+Scripting.m 2022-12-16 17:14:47 UTC (rev 28125)
@@ -794,7 +794,7 @@
else if (start > 0 || end < count - 1)
linkedURLs = [linkedURLs subarrayWithRange:NSMakeRange(start, end
+ 1 - start)];
for (BDSKLinkedFile *linkedURL in linkedURLs)
- [self downloadLinkedFile:linkedURL replace:replace];
+ [self downloadLinkedFile:linkedURL options:replace ?
BDSKDownloadReplace : BDSKDownloadDefault];
} else {
if ([self remoteURLForField:field] == nil) {
[command setScriptErrorNumber:NSArgumentsWrongScriptError];
@@ -801,7 +801,7 @@
[command setScriptErrorString:@"Invalid remote URL field."];
return nil;
}
- [self downloadURLForField:field];
+ [self downloadURLForField:field options:BDSKDownloadDefault];
}
return nil;
Modified: trunk/bibdesk/BibItem.h
===================================================================
--- trunk/bibdesk/BibItem.h 2022-12-16 07:30:21 UTC (rev 28124)
+++ trunk/bibdesk/BibItem.h 2022-12-16 17:14:47 UTC (rev 28125)
@@ -81,6 +81,12 @@
BDSKFieldActionAsk = 3
};
+typedef NS_OPTIONS(NSUInteger, BDSKDownloadOption) {
+ BDSKDownloadDefault = 0,
+ BDSKDownloadReplace = 1 << 0,
+ BDSKDownloadCheckType = 1 << 1,
+};
+
@class BibDocument, BDSKCategoryGroup, BibAuthor, BDSKFieldCollection,
BDSKTemplate, BDSKPublicationsArray, BDSKMacroResolver;
@protocol BDSKParseableItem, BDSKOwner;
@@ -760,8 +766,8 @@
- (NSString *)fieldValueForFileURL:(NSURL *)aURL withFormat:(NSInteger)format;
- (NSString *)fieldValueForFileURL:(NSURL *)aURL withFormat:(NSInteger)format
basePath:(NSString *)basePath;
-- (void)downloadURLForField:(NSString *)field;
-- (void)downloadLinkedFile:(BDSKLinkedFile *)linkedFile replace:(BOOL)replace;
+- (void)downloadURLForField:(NSString *)field
options:(BDSKDownloadOption)options;
+- (void)downloadLinkedFile:(BDSKLinkedFile *)linkedFile
options:(BDSKDownloadOption)options;
- (BOOL)canDownloadURLForField:(NSString *)field;
- (BOOL)canDownloadLinkedFile:(BDSKLinkedFile *)linkedFile;
Modified: trunk/bibdesk/BibItem.m
===================================================================
--- trunk/bibdesk/BibItem.m 2022-12-16 07:30:21 UTC (rev 28124)
+++ trunk/bibdesk/BibItem.m 2022-12-16 17:14:47 UTC (rev 28125)
@@ -3303,6 +3303,28 @@
#pragma mark Download
+- (void)itemDownloadDidStart:(BDSKItemDownload *)download {
+ NSDictionary *userInfo = [NSDictionary dictionaryWithObject:download
forKey:BDSKItemDownloadKey];
+ if (([download options] & BDSKDownloadCheckType)) {
+ NSSet *types = [NSSet setWithArray:[[NSUserDefaults
standardUserDefaults] stringArrayForKey:BDSKURLTypesToDownloadKey]];
+ NSString *MIMEType = [[download response] MIMEType];
+ if (MIMEType) {
+ CFStringRef theUTI =
UTTypeCreatePreferredIdentifierForTag((CFStringRef)MIMEType,
kUTTagClassMIMEType, NULL);
+ if (theUTI) {
+ NSSet *extensions = [NSSet setWithArray:[[(NSArray
*)UTTypeCopyAllTagsWithClass(theUTI, kUTTagClassFilenameExtension) autorelease]
valueForKey:@"lowercaseString"]];
+ CFRelease(theUTI);
+ if ([extensions count] && [types intersectsSet:extensions] ==
NO) {
+ [download cancel];
+ [[NSNotificationCenter defaultCenter]
postNotificationName:BDSKItemDownloadFinishedNotification object:self
userInfo:userInfo];
+ [downloads removeObject:download];
+ return;
+ }
+ }
+ }
+ }
+ [[NSNotificationCenter defaultCenter]
postNotificationName:BDSKItemDownloadUpdatedNotification object:self
userInfo:userInfo];
+}
+
- (void)itemDownloadDidUpdate:(BDSKItemDownload *)download {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:download
forKey:BDSKItemDownloadKey];
[[NSNotificationCenter defaultCenter]
postNotificationName:BDSKItemDownloadUpdatedNotification object:self
userInfo:userInfo];
@@ -3310,7 +3332,7 @@
- (void)itemDownloadDidFinish:(BDSKItemDownload *)download {
NSUInteger idx = [download index];
- BOOL replace = [download replace];
+ BOOL replace = 0 != ([download options] & BDSKDownloadReplace);
NSURL *fileURL = [download fileURL];
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:download
forKey:BDSKItemDownloadKey];
// download is retained by userInfo
@@ -3353,10 +3375,10 @@
return [[self owner] windowForSheetForObject:self];
}
-- (void)downloadURLForField:(NSString *)field {
+- (void)downloadURLForField:(NSString *)field
options:(BDSKDownloadOption)options {
NSURL *url = [self remoteURLForField:field];
if (url && [[downloads valueForKey:@"URL"] containsObject:url] == NO) {
- BDSKItemDownload *download = [[BDSKItemDownload alloc]
initWithURL:url];
+ BDSKItemDownload *download = [[BDSKItemDownload alloc] initWithURL:url
options:options];
if (downloads == nil)
downloads = [[NSMutableArray alloc] init];
[downloads addObject:download];
@@ -3365,10 +3387,10 @@
}
}
-- (void)downloadLinkedFile:(BDSKLinkedFile *)linkedFile replace:(BOOL)replace {
+- (void)downloadLinkedFile:(BDSKLinkedFile *)linkedFile
options:(BDSKDownloadOption)options {
NSUInteger idx = [files indexOfObject:linkedFile];
if ([linkedFile isFile] == NO && idx != NSNotFound && [[downloads
valueForKey:@"URL"] containsObject:[linkedFile URL]] == NO) {
- BDSKItemDownload *download = [[BDSKItemDownload alloc]
initWithLinkedFile:linkedFile atIndex:idx replace:replace];
+ BDSKItemDownload *download = [[BDSKItemDownload alloc]
initWithLinkedFile:linkedFile atIndex:idx options:options];
if (downloads == nil)
downloads = [[NSMutableArray alloc] init];
[downloads addObject:download];
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit