Skip to content

Commit

Permalink
feat: add type to assets (#1808)
Browse files Browse the repository at this point in the history
  • Loading branch information
hhunaid committed Sep 21, 2021
1 parent c4b462f commit 383c123
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
29 changes: 15 additions & 14 deletions android/src/main/java/com/imagepicker/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,25 +346,24 @@ public static boolean isCameraPermissionFulfilled(Context context, Activity acti
}

static boolean isImageType(Uri uri, Context context) {
String imageMimeType = "image/";
final String imageMimeType = "image/";

if (uri.getScheme().equals("file")) {
return getMimeTypeFromFileUri(uri).contains(imageMimeType);
}

ContentResolver contentResolver = context.getContentResolver();
return contentResolver.getType(uri).contains(imageMimeType);
return getMimeType(uri, context).contains(imageMimeType);
}

static boolean isVideoType(Uri uri, Context context) {
String videoMimeType = "video/";
final String videoMimeType = "video/";

if (uri.getScheme().equals("file")) {
return getMimeTypeFromFileUri(uri).contains(videoMimeType);
}

ContentResolver contentResolver = context.getContentResolver();
return contentResolver.getType(uri).contains("video/");
return getMimeType(uri, context).contains(videoMimeType);
}

static String getMimeType(Uri uri, Context context) {
if (uri.getScheme().equals("file")) {
return getMimeTypeFromFileUri(uri);
}

ContentResolver contentResolver = context.getContentResolver();
return contentResolver.getType(uri);
}

static List<Uri> collectUrisFromData(Intent data) {
Expand Down Expand Up @@ -395,6 +394,7 @@ static ReadableMap getImageResponseMap(Uri uri, Options options, Context context
map.putString("type", getMimeTypeFromFileUri(uri));
map.putInt("width", dimensions[0]);
map.putInt("height", dimensions[1]);
map.putString("type", getMimeType(uri, context));

if (options.includeBase64) {
map.putString("base64", getBase64String(uri, context));
Expand All @@ -409,6 +409,7 @@ static ReadableMap getVideoResponseMap(Uri uri, Context context) {
map.putDouble("fileSize", getFileSize(uri, context));
map.putInt("duration", getDuration(uri, context));
map.putString("fileName", fileName);
map.putString("type", getMimeType(uri, context));
return map;
}

Expand Down
1 change: 1 addition & 0 deletions ios/ImagePickerManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ -(NSMutableDictionary *)mapVideoToAsset:(NSURL *)url error:(NSError **)error {
NSMutableDictionary *asset = [[NSMutableDictionary alloc] init];
asset[@"duration"] = @(roundf(CMTimeGetSeconds([AVAsset assetWithURL:videoDestinationURL].duration)));
asset[@"uri"] = videoDestinationURL.absoluteString;
asset[@"type"] = [ImagePickerUtils getFileTypeFromUrl:videoDestinationURL];

return asset;
}
Expand Down
2 changes: 2 additions & 0 deletions ios/ImagePickerUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
+ (NSString*)getFileType:(NSData*)imageData;

+ (UIImage*)resizeImage:(UIImage*)image maxWidth:(float)maxWidth maxHeight:(float)maxHeight;

+ (NSString *) getFileTypeFromUrl:(NSURL *)url;

@end
8 changes: 8 additions & 0 deletions ios/ImagePickerUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ + (NSString*) getFileType:(NSData *)imageData
}
}

+ (NSString *) getFileTypeFromUrl:(NSURL *)url {
CFStringRef fileExtension = (__bridge CFStringRef)[url pathExtension];
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
CFRelease(UTI);
return (__bridge_transfer NSString *)MIMEType;
}

+ (UIImage*)resizeImage:(UIImage*)image maxWidth:(float)maxWidth maxHeight:(float)maxHeight
{
if ((maxWidth == 0) || (maxHeight == 0)) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface Asset {
width?: number;
height?: number;
fileSize?: number;
type?: string; //TODO
type?: string;
fileName?: string;
duration?: number;
}
Expand Down

0 comments on commit 383c123

Please sign in to comment.