VLCHTTPConnection.m 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*****************************************************************************
  2. * VLCHTTPConnection.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Pierre Sagaspe <pierre.sagaspe # me.com>
  10. * Carola Nitz <caro # videolan.org>
  11. * Jean-Baptiste Kempf <jb # videolan.org>
  12. *
  13. * Refer to the COPYING file of the official project for license.
  14. *****************************************************************************/
  15. #import "VLCActivityManager.h"
  16. #import "VLCHTTPConnection.h"
  17. #import "MultipartFormDataParser.h"
  18. #import "HTTPMessage.h"
  19. #import "HTTPDataResponse.h"
  20. #import "HTTPFileResponse.h"
  21. #import "MultipartMessageHeaderField.h"
  22. #import "HTTPDynamicFileResponse.h"
  23. #import "NSString+SupportedMedia.h"
  24. #import "UIDevice+VLC.h"
  25. #import "VLCHTTPUploaderController.h"
  26. #if TARGET_OS_IOS
  27. #import "VLCThumbnailsCache.h"
  28. #endif
  29. @interface VLCHTTPConnection()
  30. {
  31. MultipartFormDataParser *_parser;
  32. NSFileHandle *_storeFile;
  33. NSString *_filepath;
  34. UInt64 _contentLength;
  35. UInt64 _receivedContent;
  36. }
  37. @end
  38. @implementation VLCHTTPConnection
  39. - (BOOL)supportsMethod:(NSString *)method atPath:(NSString *)path
  40. {
  41. // Add support for POST
  42. if ([method isEqualToString:@"POST"] && [path isEqualToString:@"/upload.json"])
  43. return YES;
  44. return [super supportsMethod:method atPath:path];
  45. }
  46. - (BOOL)expectsRequestBodyFromMethod:(NSString *)method atPath:(NSString *)path
  47. {
  48. // Inform HTTP server that we expect a body to accompany a POST request
  49. if ([method isEqualToString:@"POST"] && [path isEqualToString:@"/upload.json"]) {
  50. // here we need to make sure, boundary is set in header
  51. NSString* contentType = [request headerField:@"Content-Type"];
  52. NSUInteger paramsSeparator = [contentType rangeOfString:@";"].location;
  53. if (NSNotFound == paramsSeparator)
  54. return NO;
  55. if (paramsSeparator >= contentType.length - 1)
  56. return NO;
  57. NSString* type = [contentType substringToIndex:paramsSeparator];
  58. if (![type isEqualToString:@"multipart/form-data"]) {
  59. // we expect multipart/form-data content type
  60. return NO;
  61. }
  62. // enumerate all params in content-type, and find boundary there
  63. NSArray* params = [[contentType substringFromIndex:paramsSeparator + 1] componentsSeparatedByString:@";"];
  64. NSUInteger count = params.count;
  65. for (NSUInteger i = 0; i < count; i++) {
  66. NSString *param = params[i];
  67. paramsSeparator = [param rangeOfString:@"="].location;
  68. if ((NSNotFound == paramsSeparator) || paramsSeparator >= param.length - 1)
  69. continue;
  70. NSString* paramName = [param substringWithRange:NSMakeRange(1, paramsSeparator-1)];
  71. NSString* paramValue = [param substringFromIndex:paramsSeparator+1];
  72. if ([paramName isEqualToString: @"boundary"])
  73. // let's separate the boundary from content-type, to make it more handy to handle
  74. [request setHeaderField:@"boundary" value:paramValue];
  75. }
  76. // check if boundary specified
  77. if (nil == [request headerField:@"boundary"])
  78. return NO;
  79. return YES;
  80. }
  81. return [super expectsRequestBodyFromMethod:method atPath:path];
  82. }
  83. - (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path
  84. {
  85. if ([method isEqualToString:@"POST"] && [path isEqualToString:@"/upload.json"])
  86. return [[HTTPDataResponse alloc] initWithData:[@"\"OK\"" dataUsingEncoding:NSUTF8StringEncoding]];
  87. #if TARGET_OS_IOS
  88. if ([path hasPrefix:@"/download/"]) {
  89. NSString *filePath = [[path stringByReplacingOccurrencesOfString:@"/download/" withString:@""]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  90. HTTPFileResponse *fileResponse = [[HTTPFileResponse alloc] initWithFilePath:filePath forConnection:self];
  91. fileResponse.contentType = @"application/octet-stream";
  92. return fileResponse;
  93. }
  94. if ([path hasPrefix:@"/thumbnail"]) {
  95. NSString *filePath = [[path stringByReplacingOccurrencesOfString:@"/thumbnail/" withString:@""]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  96. filePath = [filePath stringByReplacingOccurrencesOfString:@".png" withString:@""];
  97. NSManagedObjectContext *moc = [[MLMediaLibrary sharedMediaLibrary] managedObjectContext];
  98. if (moc) {
  99. NSPersistentStoreCoordinator *psc = [moc persistentStoreCoordinator];
  100. if (psc) {
  101. NSManagedObject *mo = nil;
  102. @try {
  103. mo = [moc existingObjectWithID:[psc managedObjectIDForURIRepresentation:[NSURL URLWithString:filePath]] error:nil];
  104. }@catch (NSException *exeption) {
  105. // somebody gave us a malformed or stale URIRepresentation
  106. }
  107. NSData *theData;
  108. NSString *contentType;
  109. /* devices category 3 and faster include HW accelerated JPEG encoding
  110. * so we can make our transfers faster by using waaay smaller images */
  111. if ([[UIDevice currentDevice] speedCategory] < 3) {
  112. theData = UIImagePNGRepresentation([VLCThumbnailsCache thumbnailForManagedObject:mo]);
  113. contentType = @"image/png";
  114. } else {
  115. theData = UIImageJPEGRepresentation([VLCThumbnailsCache thumbnailForManagedObject:mo], .9);
  116. contentType = @"image/jpg";
  117. }
  118. if (theData) {
  119. HTTPDataResponse *dataResponse = [[HTTPDataResponse alloc] initWithData:theData];
  120. dataResponse.contentType = contentType;
  121. return dataResponse;
  122. }
  123. }
  124. }
  125. }
  126. NSString *filePath = [self filePathForURI:path];
  127. NSString *documentRoot = [config documentRoot];
  128. NSString *relativePath = [filePath substringFromIndex:[documentRoot length]];
  129. BOOL shouldReturnLibVLCXML = [relativePath isEqualToString:@"/libMediaVLC.xml"];
  130. if ([relativePath isEqualToString:@"/index.html"] || shouldReturnLibVLCXML) {
  131. NSMutableArray *allMedia = [[NSMutableArray alloc] init];
  132. /* add all albums */
  133. NSArray *allAlbums = [MLAlbum allAlbums];
  134. for (MLAlbum *album in allAlbums) {
  135. if (album.name.length > 0 && album.tracks.count > 1)
  136. [allMedia addObject:album];
  137. }
  138. /* add all shows */
  139. NSArray *allShows = [MLShow allShows];
  140. for (MLShow *show in allShows) {
  141. if (show.name.length > 0 && show.episodes.count > 1)
  142. [allMedia addObject:show];
  143. }
  144. /* add all folders*/
  145. NSArray *allFolders = [MLLabel allLabels];
  146. for (MLLabel *folder in allFolders)
  147. [allMedia addObject:folder];
  148. /* add all remaining files */
  149. NSArray *allFiles = [MLFile allFiles];
  150. for (MLFile *file in allFiles) {
  151. if (file.labels.count > 0) continue;
  152. if (!file.isShowEpisode && !file.isAlbumTrack)
  153. [allMedia addObject:file];
  154. else if (file.isShowEpisode) {
  155. if (file.showEpisode.show.episodes.count < 2)
  156. [allMedia addObject:file];
  157. } else if (file.isAlbumTrack) {
  158. if (file.albumTrack.album.tracks.count < 2)
  159. [allMedia addObject:file];
  160. }
  161. }
  162. NSUInteger mediaCount = allMedia.count;
  163. NSMutableArray *mediaInHtml = [[NSMutableArray alloc] initWithCapacity:mediaCount];
  164. NSMutableArray *mediaInXml = [[NSMutableArray alloc] initWithCapacity:mediaCount];
  165. NSString *hostName = [[VLCHTTPUploaderController sharedInstance] hostname];
  166. NSString *duration;
  167. for (NSManagedObject *mo in allMedia) {
  168. if ([mo isKindOfClass:[MLFile class]]) {
  169. MLFile *file = (MLFile *)mo;
  170. duration = [[VLCTime timeWithNumber:file.duration] stringValue];
  171. [mediaInHtml addObject:[NSString stringWithFormat:
  172. @"<div style=\"background-image:url('thumbnail/%@.png')\"> \
  173. <a href=\"download/%@\" class=\"inner\"> \
  174. <div class=\"down icon\"></div> \
  175. <div class=\"infos\"> \
  176. <span class=\"first-line\">%@</span> \
  177. <span class=\"second-line\">%@ - %0.2f MB</span> \
  178. </div> \
  179. </a> \
  180. </div>",
  181. file.objectID.URIRepresentation,
  182. [file.url.path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
  183. file.title,
  184. duration, (float)(file.fileSizeInBytes / 1e6)]];
  185. if (shouldReturnLibVLCXML) {
  186. NSString *pathSub = [self _checkIfSubtitleWasFound:file.path];
  187. if (pathSub)
  188. pathSub = [NSString stringWithFormat:@"http://%@/download/%@", hostName, pathSub];
  189. [mediaInXml addObject:[NSString stringWithFormat:@"<Media title=\"%@\" thumb=\"http://%@/thumbnail/%@.png\" duration=\"%@\" size=\"%li\" pathfile=\"http://%@/download/%@\" pathSubtitle=\"%@\"/>", file.title, hostName, file.objectID.URIRepresentation.absoluteString, duration, file.fileSizeInBytes, hostName, [file.url.path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], pathSub]];
  190. }
  191. }
  192. else if ([mo isKindOfClass:[MLShow class]]) {
  193. MLShow *show = (MLShow *)mo;
  194. NSArray *episodes = [show sortedEpisodes];
  195. [mediaInHtml addObject:[NSString stringWithFormat:
  196. @"<div style=\"background-image:url('thumbnail/%@.png')\"> \
  197. <a href=\"#\" class=\"inner folder\"> \
  198. <div class=\"open icon\"></div> \
  199. <div class=\"infos\"> \
  200. <span class=\"first-line\">%@</span> \
  201. <span class=\"second-line\">%lu items</span> \
  202. </div> \
  203. </a> \
  204. <div class=\"content\">",
  205. mo.objectID.URIRepresentation,
  206. show.name,
  207. (unsigned long)[episodes count]]];
  208. for (MLShowEpisode *showEp in episodes) {
  209. MLFile *anyFileFromEpisode = (MLFile *)[[showEp files] anyObject];
  210. duration = [[VLCTime timeWithNumber:[anyFileFromEpisode duration]] stringValue];
  211. [mediaInHtml addObject:[NSString stringWithFormat:
  212. @"<div style=\"background-image:url('thumbnail/%@.png')\"> \
  213. <a href=\"download/%@\" class=\"inner\"> \
  214. <div class=\"down icon\"></div> \
  215. <div class=\"infos\"> \
  216. <span class=\"first-line\">S%@E%@ - %@</span> \
  217. <span class=\"second-line\">%@ - %0.2f MB</span> \
  218. </div> \
  219. </a> \
  220. </div>",
  221. showEp.objectID.URIRepresentation,
  222. [anyFileFromEpisode.url.path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
  223. showEp.seasonNumber,
  224. showEp.episodeNumber,
  225. showEp.name,
  226. duration, (float)([anyFileFromEpisode fileSizeInBytes] / 1e6)]];
  227. if (shouldReturnLibVLCXML) {
  228. NSString *pathSub = [self _checkIfSubtitleWasFound:[anyFileFromEpisode path]];
  229. if (![pathSub isEqualToString:@""])
  230. pathSub = [NSString stringWithFormat:@"http://%@/download/%@", hostName, pathSub];
  231. [mediaInXml addObject:[NSString stringWithFormat:@"<Media title=\"%@ - S%@E%@\" thumb=\"http://%@/thumbnail/%@.png\" duration=\"%@\" size=\"%li\" pathfile=\"http://%@/download/%@\" pathSubtitle=\"%@\"/>", show.name, showEp.seasonNumber, showEp.episodeNumber, hostName, showEp.objectID.URIRepresentation, duration, [anyFileFromEpisode fileSizeInBytes], hostName, [anyFileFromEpisode.url.path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], pathSub]];
  232. }
  233. }
  234. [mediaInHtml addObject:@"</div></div>"];
  235. } else if ([mo isKindOfClass:[MLLabel class]]) {
  236. MLLabel *label = (MLLabel *)mo;
  237. NSArray *folderItems = [label sortedFolderItems];
  238. [mediaInHtml addObject:[NSString stringWithFormat:
  239. @"<div style=\"background-image:url('thumbnail/%@.png')\"> \
  240. <a href=\"#\" class=\"inner folder\"> \
  241. <div class=\"open icon\"></div> \
  242. <div class=\"infos\"> \
  243. <span class=\"first-line\">%@</span> \
  244. <span class=\"second-line\">%lu items</span> \
  245. </div> \
  246. </a> \
  247. <div class=\"content\">",
  248. label.objectID.URIRepresentation,
  249. label.name,
  250. (unsigned long)folderItems.count]];
  251. for (MLFile *file in folderItems) {
  252. duration = [[VLCTime timeWithNumber:[file duration]] stringValue];
  253. [mediaInHtml addObject:[NSString stringWithFormat:
  254. @"<div style=\"background-image:url('thumbnail/%@.png')\"> \
  255. <a href=\"download/%@\" class=\"inner\"> \
  256. <div class=\"down icon\"></div> \
  257. <div class=\"infos\"> \
  258. <span class=\"first-line\">%@</span> \
  259. <span class=\"second-line\">%@ - %0.2f MB</span> \
  260. </div> \
  261. </a> \
  262. </div>",
  263. file.objectID.URIRepresentation,
  264. [file.url.path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
  265. file.title,
  266. duration, (float)(file.fileSizeInBytes / 1e6)]];
  267. if (shouldReturnLibVLCXML) {
  268. NSString *pathSub = [self _checkIfSubtitleWasFound:file.path];
  269. if (pathSub)
  270. pathSub = [NSString stringWithFormat:@"http://%@/download/%@", hostName, pathSub];
  271. [mediaInXml addObject:[NSString stringWithFormat:@"<Media title=\"%@\" thumb=\"http://%@/thumbnail/%@.png\" duration=\"%@\" size=\"%li\" pathfile=\"http://%@/download/%@\" pathSubtitle=\"%@\"/>", file.title, hostName, file.objectID.URIRepresentation, duration, file.fileSizeInBytes, hostName, [file.url.path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], pathSub]];
  272. }
  273. }
  274. [mediaInHtml addObject:@"</div></div>"];
  275. } else if ([mo isKindOfClass:[MLAlbum class]]) {
  276. MLAlbum *album = (MLAlbum *)mo;
  277. NSArray *albumTracks = [album sortedTracks];
  278. [mediaInHtml addObject:[NSString stringWithFormat:
  279. @"<div style=\"background-image:url('thumbnail/%@.png')\"> \
  280. <a href=\"#\" class=\"inner folder\"> \
  281. <div class=\"open icon\"></div> \
  282. <div class=\"infos\"> \
  283. <span class=\"first-line\">%@</span> \
  284. <span class=\"second-line\">%lu items</span> \
  285. </div> \
  286. </a> \
  287. <div class=\"content\">",
  288. album.objectID.URIRepresentation,
  289. album.name,
  290. (unsigned long)albumTracks.count]];
  291. for (MLAlbumTrack *track in albumTracks) {
  292. MLFile *anyFileFromTrack = [track anyFileFromTrack];
  293. duration = [[VLCTime timeWithNumber:[anyFileFromTrack duration]] stringValue];
  294. [mediaInHtml addObject:[NSString stringWithFormat:
  295. @"<div style=\"background-image:url('thumbnail/%@.png')\"> \
  296. <a href=\"download/%@\" class=\"inner\"> \
  297. <div class=\"down icon\"></div> \
  298. <div class=\"infos\"> \
  299. <span class=\"first-line\">%@</span> \
  300. <span class=\"second-line\">%@ - %0.2f MB</span> \
  301. </div> \
  302. </a> \
  303. </div>",
  304. track.objectID.URIRepresentation,
  305. [anyFileFromTrack.url.path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
  306. track.title,
  307. duration, (float)([anyFileFromTrack fileSizeInBytes] / 1e6)]];
  308. if (shouldReturnLibVLCXML)
  309. [mediaInXml addObject:[NSString stringWithFormat:@"<Media title=\"%@\" thumb=\"http://%@/thumbnail/%@.png\" duration=\"%@\" size=\"%li\" pathfile=\"http://%@/download/%@\" pathSubtitle=\"\"/>", track.title, hostName, track.objectID.URIRepresentation, duration, [anyFileFromTrack fileSizeInBytes], hostName, [anyFileFromTrack.url.path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
  310. }
  311. [mediaInHtml addObject:@"</div></div>"];
  312. }
  313. }
  314. UIDevice *currentDevice = [UIDevice currentDevice];
  315. NSString *deviceModel = [currentDevice model];
  316. NSDictionary *replacementDict;
  317. HTTPDynamicFileResponse *fileResponse;
  318. if (shouldReturnLibVLCXML) {
  319. replacementDict = @{@"FILES" : [mediaInXml componentsJoinedByString:@" "],
  320. @"NB_FILE" : [NSString stringWithFormat:@"%li", (unsigned long)mediaInXml.count],
  321. @"LIB_TITLE" : [currentDevice name]};
  322. fileResponse = [[HTTPDynamicFileResponse alloc] initWithFilePath:[self filePathForURI:path]
  323. forConnection:self
  324. separator:@"%%"
  325. replacementDictionary:replacementDict];
  326. fileResponse.contentType = @"application/xml";
  327. } else {
  328. replacementDict = @{@"FILES" : [mediaInHtml componentsJoinedByString:@" "],
  329. @"WEBINTF_TITLE" : NSLocalizedString(@"WEBINTF_TITLE", nil),
  330. @"WEBINTF_DROPFILES" : NSLocalizedString(@"WEBINTF_DROPFILES", nil),
  331. @"WEBINTF_DROPFILES_LONG" : [NSString stringWithFormat:NSLocalizedString(@"WEBINTF_DROPFILES_LONG", nil), deviceModel],
  332. @"WEBINTF_DOWNLOADFILES" : NSLocalizedString(@"WEBINTF_DOWNLOADFILES", nil),
  333. @"WEBINTF_DOWNLOADFILES_LONG" : [NSString stringWithFormat: NSLocalizedString(@"WEBINTF_DOWNLOADFILES_LONG", nil), deviceModel]};
  334. fileResponse = [[HTTPDynamicFileResponse alloc] initWithFilePath:[self filePathForURI:path]
  335. forConnection:self
  336. separator:@"%%"
  337. replacementDictionary:replacementDict];
  338. fileResponse.contentType = @"text/html";
  339. }
  340. #else
  341. UIDevice *currentDevice = [UIDevice currentDevice];
  342. NSString *deviceModel = [currentDevice model];
  343. NSString *filePath = [self filePathForURI:path];
  344. NSString *documentRoot = [config documentRoot];
  345. NSString *relativePath = [filePath substringFromIndex:[documentRoot length]];
  346. NSDictionary *replacementDict = @{@"WEBINTF_TITLE" : NSLocalizedString(@"WEBINTF_TITLE_ATV", nil),
  347. @"WEBINTF_DROPFILES" : NSLocalizedString(@"WEBINTF_DROPFILES", nil),
  348. @"WEBINTF_DROPFILES_LONG" : [NSString stringWithFormat:NSLocalizedString(@"WEBINTF_DROPFILES_LONG_ATV", nil), deviceModel]};
  349. HTTPDynamicFileResponse *fileResponse;
  350. if ([relativePath isEqualToString:@"/index.html"]) {
  351. fileResponse = [[HTTPDynamicFileResponse alloc] initWithFilePath:[self filePathForURI:path]
  352. forConnection:self
  353. separator:@"%%"
  354. replacementDictionary:replacementDict];
  355. fileResponse.contentType = @"text/html";
  356. #endif
  357. return fileResponse;
  358. } else if ([relativePath isEqualToString:@"/style.css"]) {
  359. #if TARGET_OS_IOS
  360. NSDictionary *replacementDict = @{@"WEBINTF_TITLE" : NSLocalizedString(@"WEBINTF_TITLE", nil)};
  361. #else
  362. NSDictionary *replacementDict = @{@"WEBINTF_TITLE" : NSLocalizedString(@"WEBINTF_TITLE_ATV", nil)};
  363. #endif
  364. HTTPDynamicFileResponse *fileResponse = [[HTTPDynamicFileResponse alloc] initWithFilePath:[self filePathForURI:path]
  365. forConnection:self
  366. separator:@"%%"
  367. replacementDictionary:replacementDict];
  368. fileResponse.contentType = @"text/css";
  369. return fileResponse;
  370. }
  371. return [super httpResponseForMethod:method URI:path];
  372. }
  373. - (void)prepareForBodyWithSize:(UInt64)contentLength
  374. {
  375. // set up mime parser
  376. NSString* boundary = [request headerField:@"boundary"];
  377. _parser = [[MultipartFormDataParser alloc] initWithBoundary:boundary formEncoding:NSUTF8StringEncoding];
  378. _parser.delegate = self;
  379. APLog(@"expecting file of size %lli kB", contentLength / 1024);
  380. _contentLength = contentLength;
  381. }
  382. - (void)processBodyData:(NSData *)postDataChunk
  383. {
  384. /* append data to the parser. It will invoke callbacks to let us handle
  385. * parsed data. */
  386. [_parser appendData:postDataChunk];
  387. _receivedContent += postDataChunk.length;
  388. APLog(@"received %lli kB (%lli %%)", _receivedContent / 1024, ((_receivedContent * 100) / _contentLength));
  389. }
  390. //-----------------------------------------------------------------
  391. #pragma mark multipart form data parser delegate
  392. - (void)processStartOfPartWithHeader:(MultipartMessageHeader*) header
  393. {
  394. /* in this sample, we are not interested in parts, other then file parts.
  395. * check content disposition to find out filename */
  396. MultipartMessageHeaderField* disposition = (header.fields)[@"Content-Disposition"];
  397. NSString* filename = [(disposition.params)[@"filename"] lastPathComponent];
  398. if ((nil == filename) || [filename isEqualToString: @""]) {
  399. // it's either not a file part, or
  400. // an empty form sent. we won't handle it.
  401. return;
  402. }
  403. // create the path where to store the media temporarily
  404. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  405. NSString *uploadDirPath = [searchPaths[0] stringByAppendingPathComponent:@"Upload"];
  406. NSFileManager *fileManager = [NSFileManager defaultManager];
  407. BOOL isDir = YES;
  408. if (![fileManager fileExistsAtPath:uploadDirPath isDirectory:&isDir])
  409. [fileManager createDirectoryAtPath:uploadDirPath withIntermediateDirectories:YES attributes:nil error:nil];
  410. _filepath = [uploadDirPath stringByAppendingPathComponent: filename];
  411. NSNumber *freeSpace = [[UIDevice currentDevice] freeDiskspace];
  412. if (_contentLength >= freeSpace.longLongValue) {
  413. /* avoid deadlock since we are on a background thread */
  414. [self performSelectorOnMainThread:@selector(notifyUserAboutEndOfFreeStorage:) withObject:filename waitUntilDone:NO];
  415. [self handleResourceNotFound];
  416. [self stop];
  417. return;
  418. }
  419. APLog(@"Saving file to %@", _filepath);
  420. if (![fileManager createDirectoryAtPath:uploadDirPath withIntermediateDirectories:true attributes:nil error:nil])
  421. APLog(@"Could not create directory at path: %@", _filepath);
  422. if (![fileManager createFileAtPath:_filepath contents:nil attributes:nil])
  423. APLog(@"Could not create file at path: %@", _filepath);
  424. _storeFile = [NSFileHandle fileHandleForWritingAtPath:_filepath];
  425. VLCActivityManager *activityManager = [VLCActivityManager defaultManager];
  426. [activityManager networkActivityStarted];
  427. [activityManager disableIdleTimer];
  428. }
  429. - (void)notifyUserAboutEndOfFreeStorage:(NSString *)filename
  430. {
  431. #if TARGET_OS_IOS
  432. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  433. message:[NSString stringWithFormat:
  434. NSLocalizedString(@"DISK_FULL_FORMAT", nil),
  435. filename,
  436. [[UIDevice currentDevice] model]]
  437. delegate:self
  438. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  439. otherButtonTitles:nil];
  440. [alert show];
  441. #else
  442. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  443. message:[NSString stringWithFormat:
  444. NSLocalizedString(@"DISK_FULL_FORMAT", nil),
  445. filename,
  446. [[UIDevice currentDevice] model]]
  447. preferredStyle:UIAlertControllerStyleAlert];
  448. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  449. style:UIAlertActionStyleCancel
  450. handler:nil]];
  451. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
  452. #endif
  453. }
  454. - (void)processContent:(NSData*)data WithHeader:(MultipartMessageHeader*) header
  455. {
  456. // here we just write the output from parser to the file.
  457. if (_storeFile) {
  458. @try {
  459. [_storeFile writeData:data];
  460. }
  461. @catch (NSException *exception) {
  462. APLog(@"File to write further data because storage is full.");
  463. [_storeFile closeFile];
  464. _storeFile = nil;
  465. /* don't block */
  466. [self performSelector:@selector(stop) withObject:nil afterDelay:0.1];
  467. }
  468. }
  469. }
  470. - (void)processEndOfPartWithHeader:(MultipartMessageHeader*)header
  471. {
  472. // as the file part is over, we close the file.
  473. APLog(@"closing file");
  474. [_storeFile closeFile];
  475. _storeFile = nil;
  476. }
  477. - (BOOL)shouldDie
  478. {
  479. if (_filepath) {
  480. if (_filepath.length > 0)
  481. [[VLCHTTPUploaderController sharedInstance] moveFileFrom:_filepath];
  482. }
  483. return [super shouldDie];
  484. }
  485. #pragma mark subtitle
  486. - (NSMutableArray *)_listOfSubtitles
  487. {
  488. NSMutableArray *listOfSubtitles = [[NSMutableArray alloc] init];
  489. NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  490. NSArray *allFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
  491. NSString *filePath;
  492. NSUInteger count = allFiles.count;
  493. for (NSUInteger i = 0; i < count; i++) {
  494. filePath = [[NSString stringWithFormat:@"%@/%@", documentsDirectory, allFiles[i]] stringByReplacingOccurrencesOfString:@"file://"withString:@""];
  495. if ([filePath isSupportedSubtitleFormat])
  496. [listOfSubtitles addObject:filePath];
  497. }
  498. return listOfSubtitles;
  499. }
  500. - (NSString *)_checkIfSubtitleWasFound:(NSString *)filePath
  501. {
  502. NSString *subtitlePath;
  503. NSString *fileSub;
  504. NSString *currentPath;
  505. NSString *fileName = [[filePath lastPathComponent] stringByDeletingPathExtension];
  506. if (fileName == nil)
  507. return nil;
  508. NSMutableArray *listOfSubtitles = [self _listOfSubtitles];
  509. NSUInteger count = listOfSubtitles.count;
  510. for (NSUInteger i = 0; i < count; i++) {
  511. currentPath = listOfSubtitles[i];
  512. fileSub = [NSString stringWithFormat:@"%@", currentPath];
  513. if ([fileSub rangeOfString:fileName].location != NSNotFound)
  514. subtitlePath = currentPath;
  515. }
  516. return subtitlePath;
  517. }
  518. @end