Skip to content

Commit 677b316

Browse files
committed
✨ feat: add check image for cropper
1 parent 9227ce5 commit 677b316

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

android/src/main/java/com/margelo/nitro/multipleimagepicker/MultipleImagePickerImp.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.margelo.nitro.multipleimagepicker
22

33
import android.app.Activity
4+
import android.content.ContentResolver
45
import android.content.Context
56
import android.content.Intent
67
import android.graphics.Color
@@ -167,6 +168,18 @@ class MultipleImagePickerImp(reactContext: ReactApplicationContext?) :
167168
resolved: (result: CropResult) -> Unit,
168169
rejected: (reject: Double) -> Unit
169170
) {
171+
172+
173+
fun isImage(uri: Uri, contentResolver: ContentResolver): Boolean {
174+
val mimeType: String? = contentResolver.getType(uri)
175+
return mimeType?.startsWith("image/") == true
176+
}
177+
178+
val uri = Uri.parse(image)
179+
val isImageFile = isImage(uri, appContext.contentResolver)
180+
181+
if (!isImageFile) return rejected(0.0)
182+
170183
cropOption = Options()
171184

172185
setCropOption(
@@ -216,7 +229,12 @@ class MultipleImagePickerImp(reactContext: ReactApplicationContext?) :
216229
// start edit
217230

218231
val cropActivityEventListener = object : BaseActivityEventListener() {
219-
override fun onActivityResult(activity: Activity, requestCode: Int, resultCode: Int, data: Intent?) {
232+
override fun onActivityResult(
233+
activity: Activity,
234+
requestCode: Int,
235+
resultCode: Int,
236+
data: Intent?
237+
) {
220238
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CROP) {
221239
val resultUri = UCrop.getOutput(data!!)
222240
val width = UCrop.getOutputImageWidth(data).toDouble()

docs/docs/CROP.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
id: crop
3-
title: Crop
4-
sidebar_label: Crop
3+
title: Open Crop
4+
sidebar_label: Open Crop
55
slug: /crop
66
---
77

example/src/components/CodeTag.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function CodeTag({ children, textProps, ...props }: CodeTagProps) {
2020

2121
const style = StyleSheet.create({
2222
text: {
23-
fontFamily: 'monospace',
23+
// fontFamily: 'monospace',
2424
fontWeight: 600,
2525
fontSize: 16,
2626
},

ios/HybridMultipleImagePicker+Crop.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import HXPhotoPicker
9+
import MobileCoreServices
910

1011
// class CropConfig: PickerCropConfig {
1112
// //
@@ -15,6 +16,8 @@ extension HybridMultipleImagePicker {
1516
func openCrop(image: String, config: NitroCropConfig, resolved: @escaping ((CropResult) -> Void), rejected: @escaping ((Double) -> Void)) throws {
1617
let asset: EditorAsset
1718

19+
if !isImage(image) { return rejected(0) }
20+
1821
if image.hasPrefix("http://") || image.hasPrefix("https://") || image.hasPrefix("file://") {
1922
guard let url = URL(string: image),
2023
let data = try? Data(contentsOf: url)
@@ -101,3 +104,14 @@ extension HybridMultipleImagePicker {
101104
return config
102105
}
103106
}
107+
108+
private func isImage(_ urlString: String) -> Bool {
109+
guard let url = URL(string: urlString),
110+
let pathExtension = url.pathExtension as CFString?,
111+
let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, nil)?.takeRetainedValue()
112+
else {
113+
return false
114+
}
115+
116+
return UTTypeConformsTo(uti, kUTTypeImage)
117+
}

0 commit comments

Comments
 (0)