-> Add ConsoleTools::fileExtensionFromContentType

-> Update cachePathForMediaURL with this new method
This commit is contained in:
ylecollen 2015-01-16 09:54:37 +01:00
parent a4d25edae0
commit 213f162e93
4 changed files with 59 additions and 14 deletions

View file

@ -29,6 +29,9 @@
// largeFilesFirst: move the largest file to the list head (large > 100KB). It can be combined isTimeSorted // largeFilesFirst: move the largest file to the list head (large > 100KB). It can be combined isTimeSorted
+ (NSArray*)listFiles:(NSString *)folderPath timeSorted:(BOOL)isTimeSorted largeFilesFirst:(BOOL)largeFilesFirst; + (NSArray*)listFiles:(NSString *)folderPath timeSorted:(BOOL)isTimeSorted largeFilesFirst:(BOOL)largeFilesFirst;
// return the file extension from a contentType
+ (NSString*) fileExtensionFromContentType:(NSString*)contentType;
// Image // Image
+ (UIImage *)resize:(UIImage *)image toFitInSize:(CGSize)size; + (UIImage *)resize:(UIImage *)image toFitInSize:(CGSize)size;

View file

@ -16,6 +16,8 @@
#import "ConsoleTools.h" #import "ConsoleTools.h"
#import <MobileCoreServices/MobileCoreServices.h>
@implementation ConsoleTools @implementation ConsoleTools
#pragma mark - Time interval #pragma mark - Time interval
@ -137,6 +139,47 @@
} }
} }
// return the file extension from a contentType
+ (NSString*) fileExtensionFromContentType:(NSString*)contentType
{
if (!contentType)
{
return @"";
}
CFStringRef mimeType = (__bridge CFStringRef)contentType;
CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType, NULL);
NSString* extension = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension);
CFRelease(uti);
if (extension)
{
return [NSString stringWithFormat:@".%@", extension];
}
// else undefined type
if ([contentType isEqualToString:@"application/jpeg"])
{
return @".jpg";
}
else if ([contentType isEqualToString:@"audio/x-alaw-basic"])
{
return @".alaw";
}
else if ([contentType isEqualToString:@"audio/x-caf"])
{
return @".caf";
}
else if ([contentType isEqualToString:@"audio/aac"])
{
return @".aac";
}
return @"";
}
#pragma mark - Image #pragma mark - Image
+ (UIImage *)resize:(UIImage *)image toFitInSize:(CGSize)size { + (UIImage *)resize:(UIImage *)image toFitInSize:(CGSize)size {

View file

@ -18,6 +18,7 @@
#import "MediaLoader.h" #import "MediaLoader.h"
extern NSString *const kMediaManagerPrefixForDummyURL; extern NSString *const kMediaManagerPrefixForDummyURL;
extern NSString *const kMediaManagerThumbnailDirectory;
// notify when a media download is finished (object: URL) // notify when a media download is finished (object: URL)
extern NSString *const kMediaDownloadDidFinishNotification; extern NSString *const kMediaDownloadDidFinishNotification;

View file

@ -23,6 +23,8 @@ NSString *const kMediaManagerPrefixForDummyURL = @"dummyUrl-";
NSString *const kMediaDownloadDidFinishNotification = @"kMediaDownloadDidFinishNotification"; NSString *const kMediaDownloadDidFinishNotification = @"kMediaDownloadDidFinishNotification";
NSString *const kMediaDownloadDidFailNotification = @"kMediaDownloadDidFailNotification"; NSString *const kMediaDownloadDidFailNotification = @"kMediaDownloadDidFailNotification";
NSString *const kMediaManagerThumbnailDirectory = @"kMediaManagerThumbnailDirectory";
static NSString* mediaCachePath = nil; static NSString* mediaCachePath = nil;
static NSString *mediaDir = @"mediacache"; static NSString *mediaDir = @"mediacache";
@ -126,22 +128,18 @@ static NSMutableDictionary* uploadTableById = nil;
} }
+ (NSString*)cachePathForMediaURL:(NSString*)mediaURL andType:(NSString *)mimeType { + (NSString*)cachePathForMediaURL:(NSString*)mediaURL andType:(NSString *)mimeType {
NSString *fileName;
if ([mimeType isEqualToString:@"image/jpeg"]) { NSString* fileExt = [ConsoleTools fileExtensionFromContentType:mimeType];
fileName = [NSString stringWithFormat:@"ima%lu.jpg", (unsigned long)mediaURL.hash]; NSString* fileBase = @"";
} else if ([mimeType isEqualToString:@"video/mp4"]) {
fileName = [NSString stringWithFormat:@"video%lu.mp4", (unsigned long)mediaURL.hash]; // use the mime type to extract a base filename
} else if ([mimeType isEqualToString:@"video/quicktime"]) { if ([mimeType rangeOfString:@"/"].location != NSNotFound){
fileName = [NSString stringWithFormat:@"video%lu.mov", (unsigned long)mediaURL.hash]; NSArray *components = [mimeType componentsSeparatedByString:@"/"];
} else { fileBase = [components objectAtIndex:0];
NSString *extension = @"";
NSArray *components = [mediaURL componentsSeparatedByString:@"."];
if (components && components.count > 1) {
extension = [components lastObject];
}
fileName = [NSString stringWithFormat:@"%lu.%@", (unsigned long)mediaURL.hash, extension];
} }
NSString *fileName = [NSString stringWithFormat:@"%@%lu%@", [fileBase substringToIndex:3], (unsigned long)mediaURL.hash, fileExt];
return [[MediaManager getCachePath] stringByAppendingPathComponent:fileName]; return [[MediaManager getCachePath] stringByAppendingPathComponent:fileName];
} }