Explorar o código

HTTP Import: Respect folder structure

(closes #381)
Soomin Lee %!s(int64=6) %!d(string=hai) anos
pai
achega
941379b029

+ 1 - 2
Resources/web/jquery.fileupload.js

@@ -425,8 +425,7 @@
                                     that._isInstanceOf('Blob', file)) {
                                 formData.append(
                                     options.paramName[index] || paramName,
-                                    file,
-                                    file.name
+                                    file, file.path
                                 );
                             }
                         });

+ 3 - 2
Sources/VLCHTTPConnection.m

@@ -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])

+ 25 - 7
Sources/VLCHTTPUploaderController.m

@@ -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;