Commit ecf9eb37 authored by Gleb Pinigin's avatar Gleb Pinigin
Browse files

Network: use HEAD method to check availability of subtitles; follow code convention

parent 1d9bf526
...@@ -265,37 +265,39 @@ forRowAtIndexPath:(NSIndexPath *)indexPath ...@@ -265,37 +265,39 @@ forRowAtIndexPath:(NSIndexPath *)indexPath
- (NSString *)_checkURLofSubtitle:(NSString *)url - (NSString *)_checkURLofSubtitle:(NSString *)url
{ {
NSString *SubtitleFileExtensions = kSupportedSubtitleFileExtensions; NSString *subtitleFileExtensions = kSupportedSubtitleFileExtensions;
NSCharacterSet *characterFilter = [NSCharacterSet characterSetWithCharactersInString:@"\\.():$"]; NSCharacterSet *characterFilter = [NSCharacterSet characterSetWithCharactersInString:@"\\.():$"];
SubtitleFileExtensions = [[SubtitleFileExtensions componentsSeparatedByCharactersInSet:characterFilter] componentsJoinedByString:@""]; subtitleFileExtensions = [[subtitleFileExtensions componentsSeparatedByCharactersInSet:characterFilter] componentsJoinedByString:@""];
NSArray *arraySubtitleFileExtensions = [SubtitleFileExtensions componentsSeparatedByString:@"|"]; NSArray *arraySubtitleFileExtensions = [subtitleFileExtensions componentsSeparatedByString:@"|"];
NSString *urlTemp = [[url stringByDeletingPathExtension] stringByAppendingString:@"."]; NSString *urlTemp = [[url stringByDeletingPathExtension] stringByAppendingString:@"."];
for (int cnt = 0; cnt < arraySubtitleFileExtensions.count; cnt++) { for (int cnt = 0; cnt < arraySubtitleFileExtensions.count; cnt++) {
NSString *CheckURL = [urlTemp stringByAppendingString:arraySubtitleFileExtensions[cnt]]; NSString *checkAddress = [urlTemp stringByAppendingString:arraySubtitleFileExtensions[cnt]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:CheckURL]]; NSURL *checkURL = [NSURL URLWithString:checkAddress];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:checkURL];
request.HTTPMethod = @"HEAD";
NSURLResponse *response = nil; NSURLResponse *response = nil;
NSError *error = nil; [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSData *receivedData = [[NSData alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]];
NSInteger httpStatus = [(NSHTTPURLResponse *)response statusCode]; NSInteger httpStatus = [(NSHTTPURLResponse *)response statusCode];
receivedData = nil;
if (httpStatus == 200) { if (httpStatus == 200) {
NSString *receivedSub = [NSString stringWithContentsOfURL:[NSURL URLWithString:CheckURL] encoding:NSASCIIStringEncoding error:nil]; NSString *receivedSub = [NSString stringWithContentsOfURL:checkURL encoding:NSASCIIStringEncoding error:nil];
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *directoryPath = searchPaths[0]; NSString *directoryPath = searchPaths[0];
NSString *FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[CheckURL lastPathComponent]]; NSString *fileSubtitlePath = [directoryPath stringByAppendingPathComponent:[checkURL lastPathComponent]];
NSFileManager *fileManager = [NSFileManager defaultManager]; NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:FileSubtitlePath]) { if (![fileManager fileExistsAtPath:fileSubtitlePath]) {
//create local subtitle file //create local subtitle file
[fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil]; [fileManager createFileAtPath:fileSubtitlePath contents:nil attributes:nil];
if (![fileManager fileExistsAtPath:FileSubtitlePath]) if (![fileManager fileExistsAtPath:fileSubtitlePath])
APLog(@"file creation failed, no data was saved"); APLog(@"file creation failed, no data was saved");
} }
[receivedSub writeToFile:FileSubtitlePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
return FileSubtitlePath; [receivedSub writeToFile:fileSubtitlePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
return fileSubtitlePath;
} }
} }
return nil; return nil;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment