Bgerstle has submitted this change and it was merged.
Change subject: Fix for Bug: T99122 - exception on non-wmf image urls
......................................................................
Fix for Bug: T99122 - exception on non-wmf image urls
Once again throwing an exception where none was needed.
Removed the exception and returned nil instead.
Followed code paths to the change and ensured nil case can be handled by each
caller.
Also removed an unnecessary check for "/" when is ensured to be gone by the
method provided by the SDK
Added assertion to ensure non-nil filename.
Change-Id: Iee8f5858c90e2ee0455567aa391e0452e00db389
---
M MediaWikiKit/MediaWikiKit/MWKArticle.m
M MediaWikiKit/MediaWikiKit/MWKDataStore.m
2 files changed, 26 insertions(+), 28 deletions(-)
Approvals:
Bgerstle: Looks good to me, approved
jenkins-bot: Verified
diff --git a/MediaWikiKit/MediaWikiKit/MWKArticle.m
b/MediaWikiKit/MediaWikiKit/MWKArticle.m
index 424da87..acc13f5 100644
--- a/MediaWikiKit/MediaWikiKit/MWKArticle.m
+++ b/MediaWikiKit/MediaWikiKit/MWKArticle.m
@@ -188,14 +188,17 @@
* else return nil
*/
- (MWKImage*)existingImageWithURL:(NSString*)url {
- NSString* thisImageCacheFolderPath = [self.dataStore pathForImageURL:url
title:self.title];
- BOOL isDirectory = NO;
- BOOL thisImageCacheFolderPathExists = [[NSFileManager defaultManager]
fileExistsAtPath:thisImageCacheFolderPath isDirectory:&isDirectory];
- if (!thisImageCacheFolderPathExists) {
+ NSString* imageCacheFolderPath = [self.dataStore pathForImageURL:url
title:self.title];
+ if (!imageCacheFolderPath) {
return nil;
- } else {
- return [self imageWithURL:url];
}
+
+ BOOL imageCacheFolderPathExists = [[NSFileManager defaultManager]
fileExistsAtPath:imageCacheFolderPath isDirectory:NULL];
+ if (!imageCacheFolderPathExists) {
+ return nil;
+ }
+
+ return [self imageWithURL:url];
}
#pragma mark - Save
diff --git a/MediaWikiKit/MediaWikiKit/MWKDataStore.m
b/MediaWikiKit/MediaWikiKit/MWKDataStore.m
index cc7b972..c5a145c 100644
--- a/MediaWikiKit/MediaWikiKit/MWKDataStore.m
+++ b/MediaWikiKit/MediaWikiKit/MWKDataStore.m
@@ -82,7 +82,7 @@
- (NSString*)pathForImageURL:(NSString*)url title:(MWKTitle*)title {
NSString* imagesPath = [self pathForImagesWithTitle:title];
NSString* encURL = [self safeFilenameWithImageURL:url];
- return [imagesPath stringByAppendingPathComponent:encURL];
+ return encURL ? [imagesPath stringByAppendingPathComponent : encURL] : nil;
}
- (NSString*)pathForImage:(MWKImage*)image {
@@ -108,28 +108,19 @@
- (NSString*)safeFilenameWithImageURL:(NSString*)str {
str = [str wmf_schemelessURL];
- if ([str hasPrefix:MWKDataStoreValidImageSitePrefix]) {
- NSString* suffix = [str
substringFromIndex:[MWKDataStoreValidImageSitePrefix length]];
- NSString* fileName = [suffix lastPathComponent];
-
- // Image URLs are already percent-encoded, so don't double-encode em.
- // In fact, we want to decode them...
- // If we don't, long Unicode filenames may not fit in the filesystem.
- NSString* decodedFileName = [fileName
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
-
- // Just to be safe, confirm no path explostions!
- if ([decodedFileName rangeOfString:@"/"].location != NSNotFound) {
- @throw [NSException exceptionWithName:@"MWKDataStoreException"
- reason:@"Tried to save URL with
encoded slash"
- userInfo:@{@"str": str}];
- }
-
- return decodedFileName;
- } else {
- @throw [NSException exceptionWithName:@"MWKDataStoreException"
- reason:@"Tried to save
non-upload.wikimedia.org URL as image"
- userInfo:@{@"str": str ? str : [NSNull
null]}];
+ if (![str hasPrefix:MWKDataStoreValidImageSitePrefix]) {
+ return nil;
}
+
+ NSString* suffix = [str
substringFromIndex:[MWKDataStoreValidImageSitePrefix length]];
+ NSString* fileName = [suffix lastPathComponent];
+
+ // Image URLs are already percent-encoded, so don't double-encode em.
+ // In fact, we want to decode them...
+ // If we don't, long Unicode filenames may not fit in the filesystem.
+ NSString* decodedFileName = [fileName
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
+
+ return decodedFileName;
}
#pragma mark - save methods
@@ -146,6 +137,10 @@
}
- (BOOL)saveData:(NSData*)data toFile:(NSString*)filename
atPath:(NSString*)path error:(NSError**)error {
+ NSAssert([filename length] > 0, @"No file path given for saving data");
+ if (!filename) {
+ return NO;
+ }
[self ensurePathExists:path error:error];
NSString* absolutePath = [path stringByAppendingPathComponent:filename];
return [data writeToFile:absolutePath options:NSDataWritingAtomic
error:error];
--
To view, visit https://gerrit.wikimedia.org/r/215357
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Iee8f5858c90e2ee0455567aa391e0452e00db389
Gerrit-PatchSet: 3
Gerrit-Project: apps/ios/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Fjalapeno <[email protected]>
Gerrit-Reviewer: Bgerstle <[email protected]>
Gerrit-Reviewer: Fjalapeno <[email protected]>
Gerrit-Reviewer: Mhurd <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits