Browse Source

check to see if there is enough free space on the device to the subtitles downloader

Signed-off-by: Felix Paul Kühne <fkuehne@videolan.org>
Pierre SAGASPE 10 years ago
parent
commit
48967c6f67

+ 22 - 15
Sources/VLCLocalServerFolderListViewController.m

@@ -582,7 +582,7 @@
 
 - (NSString *)_credentials
 {
-    NSString * cred;
+    NSString *cred;
 
     if (_ftpServerUserName.length > 0) {
         if (_ftpServerPassword.length > 0)
@@ -648,7 +648,7 @@
 
 - (void)requestFailed:(WRRequest *)request
 {
-    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil) message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:nil];
+    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil) message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:nil];
     [alert show];
 
     APLog(@"request %@ failed with error %i", request, request.error.errorCode);
@@ -739,20 +739,27 @@
 - (NSString *)_getFileSubtitleFromFtpServer:(NSString *)fileName
 {
     NSURL *url = [NSURL URLWithString:[[@"ftp" stringByAppendingFormat:@"://%@%@/%@/%@", [self _credentials], _ftpServerAddress, _ftpServerPath, fileName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
-
-    NSString *receivedSub = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];
-    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
-    NSString *directoryPath = searchPaths[0];
-    NSString *FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[fileName lastPathComponent]];
-
-    NSFileManager *fileManager = [NSFileManager defaultManager];
-    if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
-        //create local subtitle file
-        [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
-        if (![fileManager fileExistsAtPath:FileSubtitlePath])
-            APLog(@"file creation failed, no data was saved");
+    NSString *FileSubtitlePath = nil;
+    NSData *receivedSub = [NSData dataWithContentsOfURL:url];
+
+    if (receivedSub.length < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
+        NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
+        NSString *directoryPath = searchPaths[0];
+        FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[fileName lastPathComponent]];
+
+        NSFileManager *fileManager = [NSFileManager defaultManager];
+        if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
+            //create local subtitle file
+            [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
+            if (![fileManager fileExistsAtPath:FileSubtitlePath])
+                APLog(@"file creation failed, no data was saved");
+        }
+        [receivedSub writeToFile:FileSubtitlePath atomically:YES];
+    } else {
+        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [fileName lastPathComponent], [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
+        [alert show];
     }
-    [receivedSub writeToFile:FileSubtitlePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
+
     return FileSubtitlePath;
 }
 

+ 27 - 15
Sources/VLCOpenNetworkStreamViewController.m

@@ -18,6 +18,7 @@
 #import "UIBarButtonItem+Theme.h"
 #import "UINavigationController+Theme.h"
 #import "VLCMenuTableViewController.h"
+#import "UIDevice+VLC.h"
 
 @interface VLCOpenNetworkStreamViewController () <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>
 {
@@ -283,27 +284,38 @@ forRowAtIndexPath:(NSIndexPath *)indexPath
         NSInteger httpStatus = [(NSHTTPURLResponse *)response statusCode];
 
         if (httpStatus == 200) {
-            NSData *receivedSub = [NSData dataWithContentsOfURL:checkURL];
-
-            NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
-            NSString *directoryPath = searchPaths[0];
-            NSString *fileSubtitlePath = [directoryPath stringByAppendingPathComponent:[checkURL lastPathComponent]];
-
-            NSFileManager *fileManager = [NSFileManager defaultManager];
-            if (![fileManager fileExistsAtPath:fileSubtitlePath]) {
-                //create local subtitle file
-                [fileManager createFileAtPath:fileSubtitlePath contents:nil attributes:nil];
-                if (![fileManager fileExistsAtPath:fileSubtitlePath])
-                    APLog(@"file creation failed, no data was saved");
-            }
-
-            [receivedSub writeToFile:fileSubtitlePath atomically:YES];
+            NSString *fileSubtitlePath = [self _getFileSubtitleFromServer:checkURL];
             return fileSubtitlePath;
         }
     }
     return nil;
 }
 
+- (NSString *)_getFileSubtitleFromServer:(NSURL *)url
+{
+    NSString *FileSubtitlePath = nil;
+    NSString *fileName = [[url path] lastPathComponent];
+    NSData *receivedSub = [NSData dataWithContentsOfURL:url];
+
+    if (receivedSub.length < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
+        NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
+        NSString *directoryPath = [searchPaths objectAtIndex:0];
+        FileSubtitlePath = [directoryPath stringByAppendingPathComponent:fileName];
+        NSFileManager *fileManager = [NSFileManager defaultManager];
+        if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
+            [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
+            if (![fileManager fileExistsAtPath:FileSubtitlePath])
+                APLog(@"file creation failed, no data was saved");
+        }
+        [receivedSub writeToFile:FileSubtitlePath atomically:YES];
+    } else {
+        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), fileName, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
+        [alert show];
+    }
+
+    return FileSubtitlePath;
+}
+
 #pragma mark - text view delegate
 - (BOOL)textFieldShouldReturn:(UITextField *)textField
 {

+ 25 - 14
Sources/VLCPlexParser.m

@@ -10,6 +10,7 @@
  *****************************************************************************/
 
 #import "VLCPlexParser.h"
+#import "UIDevice+VLC.h"
 
 #define kPlexMediaServerDirInit @"library/sections"
 
@@ -137,23 +138,33 @@
 
 - (NSString *)getFileSubtitleFromPlexServer:(NSMutableArray *)mutableMediaObject modeStream:(BOOL)modeStream
 {
+    NSString *FileSubtitlePath = nil;
+    NSString *fileName = [[[[mutableMediaObject objectAtIndex:0] objectForKey:@"namefile"] stringByDeletingPathExtension] stringByAppendingPathExtension:[[mutableMediaObject objectAtIndex:0] objectForKey:@"codecSubtitle"]];
+
     NSURL *url = [NSURL URLWithString:[[mutableMediaObject objectAtIndex:0] objectForKey:@"keySubtitle"]];
     NSData *receivedSub = [NSData dataWithContentsOfURL:url];
-    NSArray *searchPaths =  nil;
-    if (modeStream)
-        searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
-    else
-        searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
-
-    NSString *directoryPath = [searchPaths objectAtIndex:0];
-    NSString *FileSubtitlePath = [[directoryPath stringByAppendingPathComponent:[[[mutableMediaObject objectAtIndex:0] objectForKey:@"namefile"] stringByDeletingPathExtension]] stringByAppendingPathExtension:[[mutableMediaObject objectAtIndex:0] objectForKey:@"codecSubtitle"]];
-    NSFileManager *fileManager = [NSFileManager defaultManager];
-    if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
-        [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
-        if (![fileManager fileExistsAtPath:FileSubtitlePath])
-            APLog(@"file creation failed, no data was saved");
+
+    if (receivedSub.length < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
+        NSArray *searchPaths =  nil;
+        if (modeStream)
+            searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
+        else
+            searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+
+        NSString *directoryPath = [searchPaths objectAtIndex:0];
+        FileSubtitlePath = [directoryPath stringByAppendingPathComponent:fileName];
+        NSFileManager *fileManager = [NSFileManager defaultManager];
+        if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
+            [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
+            if (![fileManager fileExistsAtPath:FileSubtitlePath])
+                APLog(@"file creation failed, no data was saved");
+        }
+        [receivedSub writeToFile:FileSubtitlePath atomically:YES];
+    } else {
+        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), fileName, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
+        [alert show];
     }
-    [receivedSub writeToFile:FileSubtitlePath atomically:YES];
+
     return FileSubtitlePath;
 }
 

+ 23 - 14
Sources/VLCSharedLibraryListViewController.m

@@ -259,22 +259,31 @@
 
 - (NSString *)_getFileSubtitleFromServer:(NSURL *)url modeStream:(BOOL)modeStream
 {
+    NSString *FileSubtitlePath = nil;
+    NSString *fileName = [[url path] lastPathComponent];
     NSData *receivedSub = [NSData dataWithContentsOfURL:url];
-    NSArray *searchPaths =  nil;
-    if (modeStream)
-        searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
-    else
-        searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
-
-    NSString *directoryPath = [searchPaths objectAtIndex:0];
-    NSString *FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[[url path] lastPathComponent]];
-    NSFileManager *fileManager = [NSFileManager defaultManager];
-    if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
-        [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
-        if (![fileManager fileExistsAtPath:FileSubtitlePath])
-            APLog(@"file creation failed, no data was saved");
+
+    if (receivedSub.length < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
+        NSArray *searchPaths =  nil;
+        if (modeStream)
+            searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
+        else
+            searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+
+        NSString *directoryPath = [searchPaths objectAtIndex:0];
+        FileSubtitlePath = [directoryPath stringByAppendingPathComponent:fileName];
+        NSFileManager *fileManager = [NSFileManager defaultManager];
+        if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
+            [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
+            if (![fileManager fileExistsAtPath:FileSubtitlePath])
+                APLog(@"file creation failed, no data was saved");
+        }
+        [receivedSub writeToFile:FileSubtitlePath atomically:YES];
+    } else {
+        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), fileName, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
+        [alert show];
     }
-    [receivedSub writeToFile:FileSubtitlePath atomically:YES];
+
     return FileSubtitlePath;
 }