From 7cde7722b4a5e4b824daa3018c5d158ec5a77db8 Mon Sep 17 00:00:00 2001 From: jhen Date: Tue, 29 Aug 2023 10:31:15 +0800 Subject: [PATCH] feat(example): create dir for model cache --- README.md | 2 +- example/src/App.tsx | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b01bc3d..24ad152 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ yarn example android --mode release This example used [react-native-document-picker](https://github.com/rnmods/react-native-document-picker) for select model. - iOS: You can move the model to iOS Simulator, or iCloud for real device. -- Android: Selected file will be copied or downloaded to document directory so it may be slow. +- Android: Selected file will be copied or downloaded to cache directory so it may be slow. ## Mock `llama.rn` diff --git a/example/src/App.tsx b/example/src/App.tsx index 8a923cf..b98cd20 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -137,11 +137,15 @@ export default function App() { let [file] = res if (file) { if (Platform.OS === 'android' && file.uri.startsWith('content://')) { - const filename = `${file.uri.split('/').pop() || 'model'}.gguf` - const filepath = `${ReactNativeBlobUtil.fs.dirs.DocumentDir}/${filename}` + const dir = `${ReactNativeBlobUtil.fs.dirs.CacheDir}/models` + if (!(await ReactNativeBlobUtil.fs.isDir(dir))) await ReactNativeBlobUtil.fs.mkdir(dir) + + const filepath = `${dir}/${file.uri.split('/').pop() || 'model'}.gguf` if (await ReactNativeBlobUtil.fs.exists(filepath)) { handleInitContext({ uri: filepath } as DocumentPickerResponse) return + } else { + await ReactNativeBlobUtil.fs.unlink(dir) // Clean up old files in models } addSystemMessage('Copying model to internal storage...') await ReactNativeBlobUtil.MediaCollection.copyToInternal(