2014-11-24 09:38:23 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "MediaManager.h"
|
|
|
|
|
2015-01-16 08:01:59 +00:00
|
|
|
#import "ConsoleTools.h"
|
|
|
|
|
2014-11-24 09:38:23 +00:00
|
|
|
NSString *const kMediaManagerPrefixForDummyURL = @"dummyUrl-";
|
|
|
|
|
2015-01-07 16:26:37 +00:00
|
|
|
NSString *const kMediaDownloadDidFinishNotification = @"kMediaDownloadDidFinishNotification";
|
|
|
|
NSString *const kMediaDownloadDidFailNotification = @"kMediaDownloadDidFailNotification";
|
|
|
|
|
2014-11-24 09:38:23 +00:00
|
|
|
static NSString* mediaCachePath = nil;
|
|
|
|
static NSString *mediaDir = @"mediacache";
|
|
|
|
|
|
|
|
static MediaManager *sharedMediaManager = nil;
|
|
|
|
|
|
|
|
@implementation MediaManager
|
|
|
|
|
2015-01-12 17:07:39 +00:00
|
|
|
// Table of downloads in progress
|
2015-01-12 10:00:53 +00:00
|
|
|
static NSMutableDictionary* downloadTableByURL = nil;
|
2015-01-12 17:07:39 +00:00
|
|
|
// Table of uploads in progress
|
|
|
|
static NSMutableDictionary* uploadTableById = nil;
|
2014-11-24 09:38:23 +00:00
|
|
|
|
2015-01-12 10:00:53 +00:00
|
|
|
#pragma mark - Media Download
|
2014-11-24 09:38:23 +00:00
|
|
|
|
2015-01-12 17:07:39 +00:00
|
|
|
+ (MediaLoader*)downloadMediaFromURL:(NSString*)mediaURL withType:(NSString *)mimeType {
|
|
|
|
if (mediaURL && [mediaURL hasPrefix:kMediaManagerPrefixForDummyURL] == NO) {
|
2015-01-12 10:00:53 +00:00
|
|
|
// Create a media loader to download data
|
2014-11-24 09:38:23 +00:00
|
|
|
MediaLoader *mediaLoader = [[MediaLoader alloc] init];
|
2015-01-12 10:00:53 +00:00
|
|
|
// Report this loader
|
2015-01-12 17:07:39 +00:00
|
|
|
if (!downloadTableByURL) {
|
|
|
|
downloadTableByURL = [[NSMutableDictionary alloc] init];
|
|
|
|
}
|
2015-01-12 10:00:53 +00:00
|
|
|
[downloadTableByURL setValue:mediaLoader forKey:mediaURL];
|
|
|
|
|
|
|
|
// Launch download
|
|
|
|
[mediaLoader downloadMedia:mediaURL mimeType:mimeType success:^(NSString *cacheFilePath) {
|
|
|
|
[downloadTableByURL removeObjectForKey:mediaURL];
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:kMediaDownloadDidFinishNotification object:mediaURL userInfo:nil];
|
2015-01-08 17:20:34 +00:00
|
|
|
} failure:^(NSError *error) {
|
2015-01-12 10:00:53 +00:00
|
|
|
[downloadTableByURL removeObjectForKey:mediaURL];
|
|
|
|
NSLog(@"Failed to download image (%@): %@", mediaURL, error);
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:kMediaDownloadDidFailNotification object:mediaURL userInfo:nil];
|
2015-01-08 17:20:34 +00:00
|
|
|
}];
|
|
|
|
return mediaLoader;
|
2014-11-24 09:38:23 +00:00
|
|
|
}
|
2015-01-12 10:00:53 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:kMediaDownloadDidFailNotification object:mediaURL userInfo:nil];
|
2015-01-08 17:20:34 +00:00
|
|
|
return nil;
|
2014-11-24 09:38:23 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 17:20:34 +00:00
|
|
|
// try to find out a media loader from a media URL
|
2015-01-12 10:00:53 +00:00
|
|
|
+ (id)existingDownloaderForURL:(NSString*)url {
|
|
|
|
if (downloadTableByURL && url) {
|
|
|
|
return [downloadTableByURL valueForKey:url];
|
2015-01-08 17:20:34 +00:00
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2015-01-12 17:07:39 +00:00
|
|
|
#pragma mark - Media Uploader
|
|
|
|
|
|
|
|
+ (MediaLoader*)prepareUploaderWithId:(NSString *)uploadId initialRange:(CGFloat)initialRange andRange:(CGFloat)range {
|
|
|
|
if (uploadId) {
|
|
|
|
// Create a media loader to upload data
|
|
|
|
MediaLoader *mediaLoader = [[MediaLoader alloc] initWithUploadId:uploadId initialRange:initialRange andRange:range];
|
|
|
|
// Report this loader
|
|
|
|
if (!uploadTableById) {
|
|
|
|
uploadTableById = [[NSMutableDictionary alloc] init];
|
|
|
|
}
|
|
|
|
[uploadTableById setValue:mediaLoader forKey:uploadId];
|
|
|
|
return mediaLoader;
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (MediaLoader*)existingUploaderWithId:(NSString*)uploadId {
|
|
|
|
if (uploadTableById && uploadId) {
|
|
|
|
return [uploadTableById valueForKey:uploadId];
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)removeUploaderWithId:(NSString*)uploadId {
|
|
|
|
if (uploadTableById && uploadId) {
|
|
|
|
return [uploadTableById removeObjectForKey:uploadId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 10:00:53 +00:00
|
|
|
#pragma mark - Cache Handling
|
2015-01-08 17:20:34 +00:00
|
|
|
|
2015-01-12 10:00:53 +00:00
|
|
|
+ (UIImage*)loadCachePictureForURL:(NSString*)pictureURL {
|
|
|
|
UIImage* res = nil;
|
|
|
|
NSString* filename = [MediaManager cachePathForMediaURL:pictureURL andType:@"image/jpeg"];
|
|
|
|
|
|
|
|
if ([[NSFileManager defaultManager] fileExistsAtPath:filename]) {
|
|
|
|
NSData* imageContent = [NSData dataWithContentsOfFile:filename options:(NSDataReadingMappedAlways | NSDataReadingUncached) error:nil];
|
|
|
|
if (imageContent) {
|
|
|
|
res = [[UIImage alloc] initWithData:imageContent];
|
|
|
|
}
|
2015-01-08 17:20:34 +00:00
|
|
|
}
|
2015-01-12 10:00:53 +00:00
|
|
|
|
|
|
|
return res;
|
2015-01-07 16:26:37 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 10:00:53 +00:00
|
|
|
+ (NSString*)cacheMediaData:(NSData*)mediaData forURL:(NSString *)mediaURL andType:(NSString *)mimeType {
|
|
|
|
NSString* filename = [MediaManager cachePathForMediaURL:mediaURL andType:mimeType];
|
2014-11-24 09:38:23 +00:00
|
|
|
|
|
|
|
if ([mediaData writeToFile:filename atomically:YES]) {
|
|
|
|
return filename;
|
|
|
|
} else {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 10:00:53 +00:00
|
|
|
+ (NSString*)cachePathForMediaURL:(NSString*)mediaURL andType:(NSString *)mimeType {
|
|
|
|
NSString *fileName;
|
|
|
|
if ([mimeType isEqualToString:@"image/jpeg"]) {
|
|
|
|
fileName = [NSString stringWithFormat:@"ima%lu.jpg", (unsigned long)mediaURL.hash];
|
|
|
|
} else if ([mimeType isEqualToString:@"video/mp4"]) {
|
|
|
|
fileName = [NSString stringWithFormat:@"video%lu.mp4", (unsigned long)mediaURL.hash];
|
|
|
|
} else if ([mimeType isEqualToString:@"video/quicktime"]) {
|
|
|
|
fileName = [NSString stringWithFormat:@"video%lu.mov", (unsigned long)mediaURL.hash];
|
|
|
|
} else {
|
|
|
|
NSString *extension = @"";
|
|
|
|
NSArray *components = [mediaURL componentsSeparatedByString:@"."];
|
|
|
|
if (components && components.count > 1) {
|
|
|
|
extension = [components lastObject];
|
|
|
|
}
|
|
|
|
fileName = [NSString stringWithFormat:@"%lu.%@", (unsigned long)mediaURL.hash, extension];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [[MediaManager getCachePath] stringByAppendingPathComponent:fileName];
|
|
|
|
}
|
|
|
|
|
2014-11-24 09:38:23 +00:00
|
|
|
+ (void)clearCache {
|
|
|
|
NSError *error = nil;
|
|
|
|
|
|
|
|
if (!mediaCachePath) {
|
|
|
|
// compute the path
|
|
|
|
mediaCachePath = [MediaManager getCachePath];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mediaCachePath) {
|
|
|
|
if (![[NSFileManager defaultManager] removeItemAtPath:mediaCachePath error:&error]) {
|
|
|
|
NSLog(@"Fails to delete media cache dir : %@", error);
|
|
|
|
} else {
|
|
|
|
NSLog(@"Media cache : deleted !");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NSLog(@"Media cache does not exist");
|
|
|
|
}
|
|
|
|
|
|
|
|
mediaCachePath = nil;
|
|
|
|
}
|
|
|
|
|
2015-01-09 16:14:09 +00:00
|
|
|
+ (NSUInteger)cacheSize {
|
|
|
|
|
|
|
|
if (!mediaCachePath) {
|
|
|
|
// compute the path
|
|
|
|
mediaCachePath = [MediaManager getCachePath];
|
|
|
|
}
|
2015-01-16 08:01:59 +00:00
|
|
|
|
|
|
|
return (NSUInteger)[ConsoleTools folderSize:mediaCachePath];
|
2015-01-09 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 09:38:23 +00:00
|
|
|
+ (NSString*)getCachePath {
|
|
|
|
NSString *cachePath = nil;
|
|
|
|
|
|
|
|
if (!mediaCachePath) {
|
|
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
|
|
|
|
NSString *cacheRoot = [paths objectAtIndex:0];
|
|
|
|
|
|
|
|
mediaCachePath = [cacheRoot stringByAppendingPathComponent:mediaDir];
|
|
|
|
|
|
|
|
if (![[NSFileManager defaultManager] fileExistsAtPath:mediaCachePath]) {
|
|
|
|
[[NSFileManager defaultManager] createDirectoryAtPath:mediaCachePath withIntermediateDirectories:NO attributes:nil error:nil];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cachePath = mediaCachePath;
|
|
|
|
|
|
|
|
return cachePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|