forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {
2 documentDirectory,
3 getInfoAsync,
4 readDirectoryAsync,
5} from 'expo-file-system/legacy'
6import ExpoImageCropTool, {type OpenCropperOptions} from 'expo-image-crop-tool'
7
8import {compressIfNeeded} from './manip'
9import {type PickerImage} from './picker.shared'
10import {ImagePickerResult} from 'expo-image-picker'
11
12async function getFile() {
13 const imagesDir = documentDirectory!
14 .split('/')
15 .slice(0, -6)
16 .concat(['Media', 'DCIM', '100APPLE'])
17 .join('/')
18
19 let files = await readDirectoryAsync(imagesDir)
20 files = files.filter(file => file.endsWith('.JPG'))
21 const file = `${imagesDir}/${files[0]}`
22
23 const fileInfo = await getInfoAsync(file)
24
25 if (!fileInfo.exists) {
26 throw new Error('Failed to get file info')
27 }
28
29 return await compressIfNeeded({
30 path: file,
31 mime: 'image/jpeg',
32 size: fileInfo.size,
33 width: 4288,
34 height: 2848,
35 })
36}
37
38export async function openPicker(): Promise<PickerImage[]> {
39 return [await getFile()]
40}
41
42export async function openUnifiedPicker(): Promise<ImagePickerResult> {
43 const file = await getFile()
44
45 return {
46 assets: [
47 {
48 type: 'image',
49 uri: file.path,
50 mimeType: file.mime,
51 ...file,
52 },
53 ],
54 canceled: false,
55 }
56}
57
58export async function openCamera(): Promise<PickerImage> {
59 return await getFile()
60}
61
62export async function openCropper(opts: OpenCropperOptions) {
63 const item = await ExpoImageCropTool.openCropperAsync({
64 ...opts,
65 format: 'jpeg',
66 })
67
68 return {
69 path: item.path,
70 mime: item.mimeType,
71 size: item.size,
72 width: item.width,
73 height: item.height,
74 }
75}