Browse Source

VLCServerBrowsingController: add the subtitle download locally when download the media and if subtitle URL is not nil

Pierre SAGASPE 7 years ago
parent
commit
e127aa5905

+ 1 - 0
SharedSources/ServerBrowsing/Plex/VLCNetworkServerBrowserPlex.h

@@ -29,6 +29,7 @@ NS_ASSUME_NONNULL_BEGIN
 
 @property (nonatomic, readonly, nullable) NSString *filename;
 @property (nonatomic, readonly, nullable) NSString *duration;
+@property (nonatomic, readonly, nullable) NSString *subtitleType;
 @property (nonatomic, readonly, nullable) NSURL *subtitleURL;
 @property (nonatomic, readonly, nullable) NSURL *thumbnailURL;
 @property (nonatomic, readonly, nullable) NSURL *URLcontainer;

+ 3 - 0
SharedSources/ServerBrowsing/Plex/VLCNetworkServerBrowserPlex.m

@@ -222,6 +222,9 @@
             subtitleURLString = [VLCPlexWebAPI urlAuth:subtitleURLString authentification:auth];
 
         _subtitleURL = subtitleURLString.length ? [NSURL URLWithString:subtitleURLString] : nil;
+
+        NSString *subType = dictionary[@"codecSubtitle"];
+        _subtitleType = subType.length ? subType : nil;
     }
     return self;
 }

+ 3 - 2
SharedSources/ServerBrowsing/VLCNetworkServerBrowser-Protocol.h

@@ -2,7 +2,7 @@
  * VLCNetworkServerBrowser-Protocol.h
  * VLC for iOS
  *****************************************************************************
- * Copyright (c) 2015 VideoLAN. All rights reserved.
+ * Copyright (c) 2015-2018 VideoLAN. All rights reserved.
  * $Id$
  *
  * Authors: Tobias Conradi <videolan # tobias-conradi.de>
@@ -49,10 +49,11 @@ NS_ASSUME_NONNULL_BEGIN
 @optional
 @property (nonatomic, readonly, nullable) NSString *filename;
 @property (nonatomic, readonly, nullable) NSString *duration;
+@property (nonatomic, readonly, nullable) NSString *subtitleType;
 @property (nonatomic, readonly, nullable) NSURL *subtitleURL;
 @property (nonatomic, readonly, nullable) NSURL *thumbnailURL;
 @property (nonatomic, getter=isDownloadable, readonly) BOOL downloadable;
 @end
 
 
-NS_ASSUME_NONNULL_END
+NS_ASSUME_NONNULL_END

+ 39 - 4
SharedSources/ServerBrowsing/VLCServerBrowsingController.m

@@ -1,7 +1,7 @@
 /*****************************************************************************
  * VLC for iOS
  *****************************************************************************
- * Copyright (c) 2015 VideoLAN. All rights reserved.
+ * Copyright (c) 2015-2018 VideoLAN. All rights reserved.
  * $Id$
  *
  * Authors: Tobias Conradi <videolan # tobias-conradi.de>
@@ -270,10 +270,45 @@
         NSString *extension = urlExtension.length != 0 ? urlExtension : @"vlc";
         filename = [filename stringByAppendingPathExtension:extension];
     }
-    [[VLCDownloadViewController sharedInstance] addURLToDownloadList:item.URL
-                                                     fileNameOfMedia:filename];
+    [[VLCDownloadViewController sharedInstance] addURLToDownloadList:item.URL fileNameOfMedia:filename];
+    if (item.subtitleURL)
+        [self getFileSubtitleFromServer:item];
 }
 
-#endif
+- (void)getFileSubtitleFromServer:(id<VLCNetworkServerBrowserItem>)item
+{
+    NSString *filename = nil;
+    if ([item respondsToSelector:@selector(filename)])
+        filename = item.filename;
+    else
+        filename = item.name;
+
+    NSString *FileSubtitlePath = nil;
+    NSURL *subtitleURL = item.subtitleURL;
+    NSString *extension = [subtitleURL pathExtension];
+    if ([extension isEqualToString:@""])
+        extension = item.subtitleType;
+
+    filename = [NSString stringWithFormat:@"%@.%@", [filename stringByDeletingPathExtension], extension];
+
+    NSData *receivedSub = [NSData dataWithContentsOfURL:subtitleURL]; // TODO: fix synchronous load
+
+    if (receivedSub.length < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
+        NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+        NSString *directoryPath = searchPaths[0];
+        FileSubtitlePath = [directoryPath stringByAppendingPathComponent:filename];
+
+        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
+        APLog(@"Disk full");
+}
 
+#endif
 @end