VLCOneDriveObject.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*****************************************************************************
  2. * VLCOneDriveObject.h
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015-2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: 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 "VLCOneDriveObject.h"
  14. #import "VLCHTTPFileDownloader.h"
  15. #import "NSString+SupportedMedia.h"
  16. #import "UIDevice+VLC.h"
  17. #if TARGET_OS_IOS
  18. @interface VLCOneDriveObject () <VLCHTTPFileDownloader>
  19. {
  20. VLCHTTPFileDownloader *_fileDownloader;
  21. }
  22. @end
  23. #else
  24. @interface VLCOneDriveObject ()
  25. @end
  26. #endif
  27. @implementation VLCOneDriveObject
  28. #pragma mark properties
  29. - (BOOL)isFolder
  30. {
  31. return [self.type isEqual:@"folder"] || [self.type isEqual:@"album"];
  32. }
  33. - (BOOL)isVideo
  34. {
  35. return [self.type isEqual:@"video"];
  36. }
  37. - (BOOL)isAudio
  38. {
  39. return [self.type isEqual:@"audio"];
  40. }
  41. - (NSString *)filesPath
  42. {
  43. return [self.objectId stringByAppendingString:@"/files"];
  44. }
  45. - (BOOL)hasFullFolderTree
  46. {
  47. BOOL hasFullTree = YES;
  48. if (self.folders != nil) {
  49. NSUInteger count = self.folders.count;
  50. for (NSUInteger x = 0; x < count; x++) {
  51. VLCOneDriveObject *folder = self.folders[x];
  52. if (!folder.hasFullFolderTree) {
  53. hasFullTree = NO;
  54. break;
  55. }
  56. }
  57. } else
  58. hasFullTree = NO;
  59. return hasFullTree;
  60. }
  61. #pragma mark - actions
  62. - (void)loadFolderContent
  63. {
  64. if (!self.isFolder) {
  65. APLog(@"%@ is no folder, can't load content", self.objectId);
  66. return;
  67. }
  68. if (self.folders == nil) {
  69. [self.liveClient getWithPath:self.filesPath
  70. delegate:self
  71. userState:@"load-folder-content"];
  72. } else {
  73. NSUInteger count = self.folders.count;
  74. for (NSUInteger x = 0; x < count; x++) {
  75. VLCOneDriveObject *folder = self.folders[x];
  76. if (!folder.hasFullFolderTree) {
  77. folder.delegate = self.delegate;
  78. [folder loadFolderContent];
  79. return;
  80. }
  81. }
  82. [self.delegate fullFolderTreeLoaded:self];
  83. }
  84. }
  85. #pragma mark - live operations
  86. - (void)liveOperationSucceeded:(LiveDownloadOperation *)operation
  87. {
  88. NSString *userState = operation.userState;
  89. if ([userState isEqualToString:@"load-folder-content"]) {
  90. NSMutableArray *subFolders = [[NSMutableArray alloc] init];
  91. NSMutableArray *folderFiles = [[NSMutableArray alloc] init];
  92. NSMutableArray *items = [[NSMutableArray alloc] init];
  93. NSArray *rawFolderObjects = operation.result[@"data"];
  94. BOOL hasSubFolders = NO;
  95. NSUInteger count = rawFolderObjects.count;
  96. for (NSUInteger x = 0; x < count; x++) {
  97. NSDictionary *rawObject = rawFolderObjects[x];
  98. VLCOneDriveObject *oneDriveObject = [[VLCOneDriveObject alloc] init];
  99. oneDriveObject.parent = self;
  100. oneDriveObject.objectId = rawObject[@"id"];
  101. oneDriveObject.name = rawObject[@"name"];
  102. oneDriveObject.type = rawObject[@"type"];
  103. oneDriveObject.liveClient = self.liveClient;
  104. if (oneDriveObject.isFolder) {
  105. hasSubFolders = YES;
  106. [subFolders addObject:oneDriveObject];
  107. } else {
  108. oneDriveObject.size = rawObject[@"size"];
  109. oneDriveObject.thumbnailURL = rawObject[@"picture"];
  110. oneDriveObject.downloadPath = rawObject[@"source"];
  111. if (oneDriveObject.isVideo)
  112. oneDriveObject.subtitleURL = [self configureSubtitle:oneDriveObject.name folderItems:rawFolderObjects];
  113. oneDriveObject.duration = rawObject[@"duration"];
  114. [folderFiles addObject:oneDriveObject];
  115. }
  116. //Display only folders and supported files.
  117. if (oneDriveObject.isFolder || [oneDriveObject.name isSupportedFormat])
  118. [items addObject:oneDriveObject];
  119. }
  120. self.folders = subFolders;
  121. self.files = folderFiles;
  122. self.items = items;
  123. [self.delegate folderContentLoaded:self];
  124. }
  125. }
  126. - (void)liveOperationFailed:(NSError *)error operation:(LiveDownloadOperation *)operation
  127. {
  128. NSString *userState = operation.userState;
  129. APLog(@"liveOperationFailed %@ (%@)", userState, error);
  130. if ([userState isEqualToString:@"load-folder-content"])
  131. [self.delegate folderContentLoadingFailed:error sender:self];
  132. }
  133. #pragma - subtitle
  134. - (NSString *)configureSubtitle:(NSString *)fileName folderItems:(NSArray *)folderItems
  135. {
  136. NSString *subtitleURL = nil;
  137. NSString *subtitlePath = [self _searchSubtitle:fileName folderItems:folderItems];
  138. if (subtitlePath)
  139. subtitleURL = [self _getFileSubtitleFromServer:[NSURL URLWithString:subtitlePath]];
  140. return subtitleURL;
  141. }
  142. - (NSString *)_searchSubtitle:(NSString *)fileName folderItems:(NSArray *)folderItems
  143. {
  144. NSString *urlTemp = [[fileName lastPathComponent] stringByDeletingPathExtension];
  145. NSString *itemPath = nil;
  146. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", urlTemp];
  147. NSArray *results = [folderItems filteredArrayUsingPredicate:predicate];
  148. for (int cnt = 0; cnt < results.count; cnt++) {
  149. NSDictionary *dictObject = results[cnt];
  150. NSString *itemName = dictObject[@"name"];
  151. if ([itemName isSupportedSubtitleFormat])
  152. itemPath = dictObject[@"source"];
  153. }
  154. return itemPath;
  155. }
  156. - (NSString *)_getFileSubtitleFromServer:(NSURL *)subtitleURL
  157. {
  158. NSString *FileSubtitlePath = nil;
  159. NSData *receivedSub = [NSData dataWithContentsOfURL:subtitleURL]; // TODO: fix synchronous load
  160. if (receivedSub.length < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  161. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  162. NSString *directoryPath = searchPaths[0];
  163. FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[subtitleURL lastPathComponent]];
  164. NSFileManager *fileManager = [NSFileManager defaultManager];
  165. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  166. //create local subtitle file
  167. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  168. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  169. APLog(@"file creation failed, no data was saved");
  170. return nil;
  171. }
  172. }
  173. [receivedSub writeToFile:FileSubtitlePath atomically:YES];
  174. } else {
  175. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  176. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [subtitleURL lastPathComponent], [[UIDevice currentDevice] model]]
  177. preferredStyle:UIAlertControllerStyleAlert];
  178. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_OK", nil)
  179. style:UIAlertActionStyleCancel
  180. handler:nil];
  181. [alertController addAction:okAction];
  182. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
  183. }
  184. return FileSubtitlePath;
  185. }
  186. #pragma mark - delegation
  187. - (void)folderContentLoaded:(VLCOneDriveObject *)sender
  188. {
  189. }
  190. - (void)folderContentLoadingFailed:(NSError *)error sender:(VLCOneDriveObject *)sender
  191. {
  192. if (self.delegate)
  193. [self.delegate folderContentLoadingFailed:error sender:self];
  194. }
  195. - (void)fullFolderTreeLoaded:(VLCOneDriveObject *)sender
  196. {
  197. [self loadFolderContent];
  198. }
  199. #pragma mark - file downloading
  200. - (void)saveObjectToDocuments
  201. {
  202. #if TARGET_OS_IOS
  203. _fileDownloader = [[VLCHTTPFileDownloader alloc] init];
  204. _fileDownloader.delegate = self;
  205. [_fileDownloader downloadFileFromURL:[NSURL URLWithString:self.downloadPath] withFileName:self.name];
  206. #endif
  207. }
  208. - (void)downloadStarted
  209. {
  210. if ([self.downloadDelegate respondsToSelector:@selector(downloadStarted:)])
  211. [self.downloadDelegate downloadStarted:self];
  212. }
  213. - (void)downloadEnded
  214. {
  215. if ([self.downloadDelegate respondsToSelector:@selector(downloadEnded:)])
  216. [self.downloadDelegate downloadEnded:self];
  217. }
  218. - (void)downloadFailedWithErrorDescription:(NSString *)description
  219. {
  220. APLog(@"download failed (%@)", description);
  221. }
  222. - (void)progressUpdatedTo:(CGFloat)percentage receivedDataSize:(CGFloat)receivedDataSize expectedDownloadSize:(CGFloat)expectedDownloadSize
  223. {
  224. if ([self.downloadDelegate respondsToSelector:@selector(progressUpdated:)])
  225. [self.downloadDelegate progressUpdated:percentage];
  226. if ([self.downloadDelegate respondsToSelector:@selector(calculateRemainingTime:expectedDownloadSize:)])
  227. [self.downloadDelegate calculateRemainingTime:receivedDataSize expectedDownloadSize:expectedDownloadSize];
  228. }
  229. @end