mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {
2 Image as RNImage,
3 openCropper as openCropperFn,
4} from 'react-native-image-crop-picker'
5import {
6 documentDirectory,
7 getInfoAsync,
8 readDirectoryAsync,
9} from 'expo-file-system'
10
11import {compressIfNeeded} from './manip'
12import {CropperOptions} from './types'
13
14async function getFile() {
15 const imagesDir = documentDirectory!
16 .split('/')
17 .slice(0, -6)
18 .concat(['Media', 'DCIM', '100APPLE'])
19 .join('/')
20
21 let files = await readDirectoryAsync(imagesDir)
22 files = files.filter(file => file.endsWith('.JPG'))
23 const file = `${imagesDir}/${files[0]}`
24
25 const fileInfo = await getInfoAsync(file)
26
27 if (!fileInfo.exists) {
28 throw new Error('Failed to get file info')
29 }
30
31 return await compressIfNeeded({
32 path: file,
33 mime: 'image/jpeg',
34 size: fileInfo.size,
35 width: 4288,
36 height: 2848,
37 })
38}
39
40export async function openPicker(): Promise<RNImage[]> {
41 return [await getFile()]
42}
43
44export async function openCamera(): Promise<RNImage> {
45 return await getFile()
46}
47
48export async function openCropper(opts: CropperOptions) {
49 const item = await openCropperFn({
50 ...opts,
51 forceJpg: true, // ios only
52 })
53
54 return {
55 path: item.path,
56 mime: item.mime,
57 size: item.size,
58 width: item.width,
59 height: item.height,
60 }
61}