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

wifi upload: robustify multi-file uploads

parent a603b2ec
......@@ -19,13 +19,13 @@
#import "HTTPDataResponse.h"
#import "HTTPFileResponse.h"
#import "MultipartMessageHeaderField.h"
#import "VLCHTTPUploaderController.h"
@interface VLCHTTPConnection()
{
MultipartFormDataParser *_parser;
NSFileHandle *_storeFile;
NSString *_filepath;
NSString *_filename;
UInt64 _contentLength;
UInt64 _receivedContent;
}
......@@ -138,7 +138,6 @@
// an empty form sent. we won't handle it.
return;
}
_filename = filename;
// create the path where to store the media temporarily
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
......@@ -177,50 +176,15 @@
APLog(@"closing file");
[_storeFile closeFile];
_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) {
_storeFile = nil;
[[NSFileManager defaultManager] removeItemAtPath:_filepath error:nil];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[(VLCAppDelegate*)[UIApplication sharedApplication].delegate activateIdleTimer];
if (_filepath) {
if (_filepath.length > 0)
[[(VLCAppDelegate*)[UIApplication sharedApplication].delegate uploadController] moveFileFrom:_filepath];
}
[super die];
return [super shouldDie];
}
@end
......@@ -23,4 +23,6 @@
- (BOOL)changeHTTPServerState:(BOOL)state;
- (NSString *)currentIPAddress;
- (void)moveFileFrom:(NSString *)filepath;
@end
......@@ -126,4 +126,40 @@
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
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