Commit 68a4b708 authored by Felix Paul Kühne's avatar Felix Paul Kühne
Browse files

wifi upload: robustify multi-file uploads

parent a603b2ec
...@@ -19,13 +19,13 @@ ...@@ -19,13 +19,13 @@
#import "HTTPDataResponse.h" #import "HTTPDataResponse.h"
#import "HTTPFileResponse.h" #import "HTTPFileResponse.h"
#import "MultipartMessageHeaderField.h" #import "MultipartMessageHeaderField.h"
#import "VLCHTTPUploaderController.h"
@interface VLCHTTPConnection() @interface VLCHTTPConnection()
{ {
MultipartFormDataParser *_parser; MultipartFormDataParser *_parser;
NSFileHandle *_storeFile; NSFileHandle *_storeFile;
NSString *_filepath; NSString *_filepath;
NSString *_filename;
UInt64 _contentLength; UInt64 _contentLength;
UInt64 _receivedContent; UInt64 _receivedContent;
} }
...@@ -138,7 +138,6 @@ ...@@ -138,7 +138,6 @@
// an empty form sent. we won't handle it. // an empty form sent. we won't handle it.
return; return;
} }
_filename = filename;
// create the path where to store the media temporarily // create the path where to store the media temporarily
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
...@@ -177,50 +176,15 @@ ...@@ -177,50 +176,15 @@
APLog(@"closing file"); APLog(@"closing file");
[_storeFile closeFile]; [_storeFile closeFile];
_storeFile = nil; _storeFile = nil;
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *libraryPath = searchPaths[0];
NSString *finalFilePath = [libraryPath stringByAppendingPathComponent:_filename];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:finalFilePath]) {
/* we don't want to over-write existing files, so add an integer to the file name */
NSString *potentialFilename;
NSString *fileExtension = [_filename pathExtension];
NSString *rawFileName = [_filename stringByDeletingPathExtension];
for (NSUInteger x = 1; x < 100; x++) {
potentialFilename = [NSString stringWithFormat:@"%@ %i.%@", rawFileName, x, fileExtension];
if (![[NSFileManager defaultManager] fileExistsAtPath:[libraryPath stringByAppendingPathComponent:potentialFilename]])
break;
}
finalFilePath = [libraryPath stringByAppendingPathComponent:potentialFilename];
}
NSError *error;
[fileManager moveItemAtPath:_filepath toPath:finalFilePath error:&error];
if (error) {
APLog(@"Moving received media %@ to library folder failed (%i), deleting", _filename, error.code);
[fileManager removeItemAtPath:_filepath error:nil];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[(VLCAppDelegate*)[UIApplication sharedApplication].delegate activateIdleTimer];
/* update media library when file upload was completed */
VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate updateMediaList];
} }
- (void)die - (BOOL)shouldDie
{ {
if (_storeFile) { if (_filepath) {
_storeFile = nil; if (_filepath.length > 0)
[[NSFileManager defaultManager] removeItemAtPath:_filepath error:nil]; [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate uploadController] moveFileFrom:_filepath];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[(VLCAppDelegate*)[UIApplication sharedApplication].delegate activateIdleTimer];
} }
return [super shouldDie];
[super die];
} }
@end @end
...@@ -23,4 +23,6 @@ ...@@ -23,4 +23,6 @@
- (BOOL)changeHTTPServerState:(BOOL)state; - (BOOL)changeHTTPServerState:(BOOL)state;
- (NSString *)currentIPAddress; - (NSString *)currentIPAddress;
- (void)moveFileFrom:(NSString *)filepath;
@end @end
...@@ -126,4 +126,40 @@ ...@@ -126,4 +126,40 @@
return address; return address;
} }
- (void)moveFileFrom:(NSString *)filepath
{
NSString *fileName = [filepath lastPathComponent];
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *libraryPath = searchPaths[0];
NSString *finalFilePath = [libraryPath stringByAppendingPathComponent:fileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:finalFilePath]) {
/* we don't want to over-write existing files, so add an integer to the file name */
NSString *potentialFilename;
NSString *fileExtension = [fileName pathExtension];
NSString *rawFileName = [fileName stringByDeletingPathExtension];
for (NSUInteger x = 1; x < 100; x++) {
potentialFilename = [NSString stringWithFormat:@"%@ %i.%@", rawFileName, x, fileExtension];
if (![[NSFileManager defaultManager] fileExistsAtPath:[libraryPath stringByAppendingPathComponent:potentialFilename]])
break;
}
finalFilePath = [libraryPath stringByAppendingPathComponent:potentialFilename];
}
NSError *error;
[fileManager moveItemAtPath:filepath toPath:finalFilePath error:&error];
if (error) {
APLog(@"Moving received media %@ to library folder failed (%i), deleting", fileName, error.code);
[fileManager removeItemAtPath:filepath error:nil];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[(VLCAppDelegate*)[UIApplication sharedApplication].delegate activateIdleTimer];
/* update media library when file upload was completed */
VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate updateMediaList];
}
@end @end
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