VLCServerBrowsingController.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2015 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  8. * Felix Paul Kühne <fkuehne # videolan.org>
  9. * Pierre SAGASPE <pierre.sagaspe # me.com>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCServerBrowsingController.h"
  14. #import "NSString+SupportedMedia.h"
  15. #import "UIDevice+VLC.h"
  16. #import "VLCPlaybackController.h"
  17. #if TARGET_OS_TV
  18. #import "VLCFullscreenMovieTVViewController.h"
  19. #import "MetaDataFetcherKit.h"
  20. #endif
  21. #if DOWNLOAD_SUPPORTED
  22. #import "VLCDownloadViewController.h"
  23. #endif
  24. @implementation VLCServerBrowsingController
  25. - (instancetype)initWithViewController:(UIViewController *)viewController serverBrowser:(id<VLCNetworkServerBrowser>)browser
  26. {
  27. self = [super init];
  28. if (self) {
  29. _viewController = viewController;
  30. _serverBrowser = browser;
  31. #if TARGET_OS_TV
  32. MDFMovieDBSessionManager *movieDBSessionManager = [MDFMovieDBSessionManager sharedInstance];
  33. movieDBSessionManager.apiKey = kVLCfortvOSMovieDBKey;
  34. [movieDBSessionManager fetchProperties];
  35. #endif
  36. }
  37. return self;
  38. }
  39. #pragma mark -
  40. - (NSByteCountFormatter *)byteCounterFormatter
  41. {
  42. if (!_byteCountFormatter)
  43. _byteCountFormatter = [[NSByteCountFormatter alloc] init];
  44. return _byteCountFormatter;
  45. }
  46. - (UIImage *)genericFileImage
  47. {
  48. if (!_genericFileImage)
  49. _genericFileImage = [UIImage imageNamed:@"blank"];
  50. return _genericFileImage;
  51. }
  52. - (UIImage *)folderImage
  53. {
  54. if (!_folderImage)
  55. _folderImage = [UIImage imageNamed:@"folder"];
  56. return _folderImage;
  57. }
  58. #pragma mark - cell configuration
  59. - (void)configureCell:(id<VLCRemoteBrowsingCell>)cell withItem:(id<VLCNetworkServerBrowserItem>)item
  60. {
  61. if (item.isContainer) {
  62. cell.isDirectory = YES;
  63. cell.thumbnailImage = self.folderImage;
  64. } else {
  65. cell.isDirectory = NO;
  66. cell.thumbnailImage = self.genericFileImage;
  67. NSString *sizeString = item.fileSizeBytes ? [self.byteCounterFormatter stringFromByteCount:item.fileSizeBytes.longLongValue] : nil;
  68. NSString *duration = nil;
  69. if ([item respondsToSelector:@selector(duration)])
  70. duration = item.duration;
  71. NSString *subtitle = nil;
  72. if (sizeString && duration) {
  73. subtitle = [NSString stringWithFormat:@"%@ (%@)",duration, sizeString];
  74. } else if (sizeString) {
  75. subtitle = sizeString;
  76. } else if (duration) {
  77. subtitle = duration;
  78. }
  79. cell.subtitle = subtitle;
  80. #if DOWNLOAD_SUPPORTED
  81. if ([item respondsToSelector:@selector(isDownloadable)])
  82. cell.isDownloadable = item.isDownloadable;
  83. else
  84. cell.isDownloadable = NO;
  85. #endif
  86. }
  87. cell.title = item.name;
  88. NSURL *thumbnailURL = nil;
  89. if ([item respondsToSelector:@selector(thumbnailURL)])
  90. thumbnailURL = item.thumbnailURL;
  91. [cell setThumbnailURL:thumbnailURL];
  92. }
  93. #pragma mark - subtitles
  94. - (NSArray<NSURL*> *)_searchSubtitle:(NSString *)url
  95. {
  96. NSString *urlTemp = [[url lastPathComponent] stringByDeletingPathExtension];
  97. NSMutableArray<NSURL*> *urls = [NSMutableArray arrayWithArray:[self.serverBrowser.items valueForKey:@"URL"]];
  98. NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"SELF.path contains[c] %@", urlTemp];
  99. [urls filterUsingPredicate:namePredicate];
  100. NSPredicate *formatPrediate = [NSPredicate predicateWithBlock:^BOOL(NSURL *_Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
  101. return [evaluatedObject.path isSupportedSubtitleFormat];
  102. }];
  103. [urls filterUsingPredicate:formatPrediate];
  104. return [NSArray arrayWithArray:urls];
  105. }
  106. - (NSString *)_getFileSubtitleFromServer:(NSURL *)subtitleURL
  107. {
  108. NSString *FileSubtitlePath = nil;
  109. NSData *receivedSub = [NSData dataWithContentsOfURL:subtitleURL]; // TODO: fix synchronous load
  110. if (receivedSub.length < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  111. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  112. NSString *directoryPath = searchPaths[0];
  113. FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[subtitleURL lastPathComponent]];
  114. NSFileManager *fileManager = [NSFileManager defaultManager];
  115. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  116. //create local subtitle file
  117. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  118. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  119. APLog(@"file creation failed, no data was saved");
  120. return nil;
  121. }
  122. }
  123. [receivedSub writeToFile:FileSubtitlePath atomically:YES];
  124. } else {
  125. [self.viewController vlc_showAlertWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  126. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [subtitleURL lastPathComponent], [[UIDevice currentDevice] model]]
  127. buttonTitle:NSLocalizedString(@"BUTTON_OK", nil)];
  128. }
  129. return FileSubtitlePath;
  130. }
  131. - (void)configureSubtitlesInMediaList:(VLCMediaList *)mediaList
  132. {
  133. NSArray *items = self.serverBrowser.items;
  134. id<VLCNetworkServerBrowserItem> loopItem;
  135. [mediaList lock];
  136. NSUInteger count = mediaList.count;
  137. for (NSUInteger i = 0; i < count; i++) {
  138. loopItem = items[i];
  139. NSString *URLofSubtitle = nil;
  140. NSURL *remoteSubtitleURL = nil;
  141. if ([loopItem respondsToSelector:@selector(subtitleURL)]) {
  142. remoteSubtitleURL = [loopItem subtitleURL];
  143. }
  144. if (remoteSubtitleURL == nil) {
  145. NSArray *subtitlesList = [self _searchSubtitle:loopItem.URL.lastPathComponent];
  146. remoteSubtitleURL = subtitlesList.firstObject;
  147. }
  148. if(remoteSubtitleURL != nil) {
  149. URLofSubtitle = [self _getFileSubtitleFromServer:remoteSubtitleURL];
  150. if (URLofSubtitle != nil)
  151. [[mediaList mediaAtIndex:i] addOptions:@{ kVLCSettingSubtitlesFilePath : URLofSubtitle }];
  152. }
  153. }
  154. [mediaList unlock];
  155. }
  156. #pragma mark - File Streaming
  157. - (void)showMovieViewController
  158. {
  159. #if TARGET_OS_TV
  160. VLCFullscreenMovieTVViewController *moviewVC = [VLCFullscreenMovieTVViewController fullscreenMovieTVViewController];
  161. [self.viewController presentViewController:moviewVC
  162. animated:YES
  163. completion:nil];
  164. #endif
  165. }
  166. - (void)streamMediaList:(VLCMediaList *)mediaList startingAtIndex:(NSInteger)startIndex
  167. {
  168. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  169. [vpc playMediaList:mediaList firstIndex:startIndex];
  170. [self showMovieViewController];
  171. }
  172. - (void)streamFileForItem:(id<VLCNetworkServerBrowserItem>)item
  173. {
  174. NSString *URLofSubtitle = nil;
  175. NSURL *remoteSubtitleURL = nil;
  176. if ([item respondsToSelector:@selector(subtitleURL)])
  177. remoteSubtitleURL = [item subtitleURL];
  178. if (!remoteSubtitleURL) {
  179. NSArray *SubtitlesList = [self _searchSubtitle:item.URL.lastPathComponent];
  180. remoteSubtitleURL = SubtitlesList.firstObject;
  181. }
  182. if(remoteSubtitleURL)
  183. URLofSubtitle = [self _getFileSubtitleFromServer:remoteSubtitleURL];
  184. NSURL *URLToPlay = item.URL;
  185. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  186. [vpc playURL:URLToPlay subtitlesFilePath:URLofSubtitle];
  187. [self showMovieViewController];
  188. }
  189. #pragma mark - Downloads
  190. #if DOWNLOAD_SUPPORTED
  191. - (BOOL)triggerDownloadForItem:(id<VLCNetworkServerBrowserItem>)item
  192. {
  193. // is item supposed to be not downloadable?
  194. if ([item respondsToSelector:@selector(isDownloadable)] && ![item isDownloadable]) {
  195. return NO;
  196. }
  197. // if the item has no URL we can't download it
  198. if (!item.URL) {
  199. return NO;
  200. }
  201. if (item.fileSizeBytes.longLongValue < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  202. [self _downloadItem:item];
  203. return YES;
  204. } else {
  205. NSString *title = NSLocalizedString(@"DISK_FULL", nil);
  206. NSString *messsage = [NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), item.name, [[UIDevice currentDevice] model]];
  207. NSString *button = NSLocalizedString(@"BUTTON_OK", nil);
  208. [self.viewController vlc_showAlertWithTitle:title message:messsage buttonTitle:button];
  209. return NO;
  210. }
  211. }
  212. - (void)_downloadItem:(id<VLCNetworkServerBrowserItem>)item
  213. {
  214. NSString *filename;
  215. if ([item respondsToSelector:@selector(filename)])
  216. filename = item.filename;
  217. else
  218. filename = item.name;
  219. if (filename.pathExtension.length == 0) {
  220. /* there are few crappy UPnP servers who don't reveal the correct file extension, so we use a generic fake (#11123) */
  221. NSString *urlExtension = item.URL.pathExtension;
  222. NSString *extension = urlExtension.length != 0 ? urlExtension : @"vlc";
  223. filename = [filename stringByAppendingPathExtension:extension];
  224. }
  225. [[VLCDownloadViewController sharedInstance] addURLToDownloadList:item.URL
  226. fileNameOfMedia:filename];
  227. }
  228. #endif
  229. @end