mirror of
https://github.com/vector-im/element-ios.git
synced 2024-09-30 16:22:39 +00:00
-> Add ConsoleTools::fileExtensionFromContentType
-> Update cachePathForMediaURL with this new method
This commit is contained in:
parent
a4d25edae0
commit
213f162e93
4 changed files with 59 additions and 14 deletions
|
@ -29,6 +29,9 @@
|
|||
// 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;
|
||||
|
||||
// return the file extension from a contentType
|
||||
+ (NSString*) fileExtensionFromContentType:(NSString*)contentType;
|
||||
|
||||
// Image
|
||||
+ (UIImage *)resize:(UIImage *)image toFitInSize:(CGSize)size;
|
||||
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
#import "ConsoleTools.h"
|
||||
|
||||
#import <MobileCoreServices/MobileCoreServices.h>
|
||||
|
||||
@implementation ConsoleTools
|
||||
|
||||
#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
|
||||
|
||||
+ (UIImage *)resize:(UIImage *)image toFitInSize:(CGSize)size {
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
#import "MediaLoader.h"
|
||||
|
||||
extern NSString *const kMediaManagerPrefixForDummyURL;
|
||||
extern NSString *const kMediaManagerThumbnailDirectory;
|
||||
|
||||
// notify when a media download is finished (object: URL)
|
||||
extern NSString *const kMediaDownloadDidFinishNotification;
|
||||
|
|
|
@ -23,6 +23,8 @@ NSString *const kMediaManagerPrefixForDummyURL = @"dummyUrl-";
|
|||
NSString *const kMediaDownloadDidFinishNotification = @"kMediaDownloadDidFinishNotification";
|
||||
NSString *const kMediaDownloadDidFailNotification = @"kMediaDownloadDidFailNotification";
|
||||
|
||||
NSString *const kMediaManagerThumbnailDirectory = @"kMediaManagerThumbnailDirectory";
|
||||
|
||||
static NSString* mediaCachePath = nil;
|
||||
static NSString *mediaDir = @"mediacache";
|
||||
|
||||
|
@ -126,22 +128,18 @@ static NSMutableDictionary* uploadTableById = nil;
|
|||
}
|
||||
|
||||
+ (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];
|
||||
|
||||
NSString* fileExt = [ConsoleTools fileExtensionFromContentType:mimeType];
|
||||
NSString* fileBase = @"";
|
||||
|
||||
// use the mime type to extract a base filename
|
||||
if ([mimeType rangeOfString:@"/"].location != NSNotFound){
|
||||
NSArray *components = [mimeType componentsSeparatedByString:@"/"];
|
||||
fileBase = [components objectAtIndex:0];
|
||||
}
|
||||
|
||||
NSString *fileName = [NSString stringWithFormat:@"%@%lu%@", [fileBase substringToIndex:3], (unsigned long)mediaURL.hash, fileExt];
|
||||
|
||||
return [[MediaManager getCachePath] stringByAppendingPathComponent:fileName];
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue