Commit 941379b0 authored by Soomin Lee's avatar Soomin Lee

HTTP Import: Respect folder structure

(closes #381)
parent 997a1ee8
......@@ -425,8 +425,7 @@
that._isInstanceOf('Blob', file)) {
formData.append(
options.paramName[index] || paramName,
file,
file.name
file, file.path
);
}
});
......
......@@ -612,7 +612,7 @@
* check content disposition to find out filename */
MultipartMessageHeaderField* disposition = (header.fields)[@"Content-Disposition"];
NSString* filename = [(disposition.params)[@"filename"] lastPathComponent];
NSString* filename = (disposition.params)[@"filename"];
if ((nil == filename) || [filename isEqualToString: @""]) {
// it's either not a file part, or
......@@ -641,7 +641,8 @@
}
APLog(@"Saving file to %@", _filepath);
if (![fileManager createDirectoryAtPath:uploadDirPath withIntermediateDirectories:true attributes:nil error:nil])
if (![fileManager createDirectoryAtPath:[_filepath stringByDeletingLastPathComponent]
withIntermediateDirectories:true attributes:nil error:nil])
APLog(@"Could not create directory at path: %@", _filepath);
if (![fileManager createFileAtPath:_filepath contents:nil attributes:nil])
......
......@@ -303,22 +303,40 @@
* while on iOS we have persistent storage, so move it there */
#if TARGET_OS_IOS
NSString *fileName = [filepath lastPathComponent];
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *libraryPath = searchPaths[0];
NSString *finalFilePath = [libraryPath stringByAppendingPathComponent:fileName];
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *uploadPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)
firstObject] stringByAppendingPathComponent:@"Upload"];
NSString *finalFilePath = [libraryPath
stringByAppendingString:[filepath
stringByReplacingOccurrencesOfString:uploadPath
withString:@""]];
NSFileManager *fileManager = [NSFileManager defaultManager];
// Re-create the folder structure of the user
if (![fileManager createDirectoryAtPath:[finalFilePath stringByDeletingLastPathComponent]
withIntermediateDirectories:YES attributes:nil error:nil])
APLog(@"Could not create directory at path: %@", finalFilePath);
if ([fileManager fileExistsAtPath:finalFilePath]) {
/* we don't want to over-write existing files, so add an integer to the file name */
NSString *potentialFilename;
NSString *potentialFullPath;
NSString *currentPath = [finalFilePath stringByDeletingLastPathComponent];
NSString *fileExtension = [fileName pathExtension];
NSString *rawFileName = [fileName stringByDeletingPathExtension];
for (NSUInteger x = 1; x < 100; x++) {
potentialFilename = [NSString stringWithFormat:@"%@ %lu.%@", rawFileName, (unsigned long)x, fileExtension];
if (![[NSFileManager defaultManager] fileExistsAtPath:[libraryPath stringByAppendingPathComponent:potentialFilename]])
potentialFullPath = [currentPath stringByAppendingString:[NSString
stringWithFormat:@"/%@-%lu.%@",
rawFileName,
(unsigned long)x,
fileExtension]];
if (![[NSFileManager defaultManager] fileExistsAtPath:potentialFullPath]) {
finalFilePath = potentialFullPath;
break;
}
}
finalFilePath = [libraryPath stringByAppendingPathComponent:potentialFilename];
}
NSError *error;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment