forked from CherryHQ/cherry-studio-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileService.ts
More file actions
263 lines (218 loc) · 6.93 KB
/
FileService.ts
File metadata and controls
263 lines (218 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { fileDatabase } from '@database'
import { Directory, File, Paths } from 'expo-file-system'
import * as FileSystem from 'expo-file-system/legacy'
import * as Sharing from 'expo-sharing'
import { DEFAULT_DOCUMENTS_STORAGE, DEFAULT_IMAGES_STORAGE, DEFAULT_STORAGE } from '@/constants/storage'
import { loggerService } from '@/services/LoggerService'
import type { FileMetadata } from '@/types/file'
import { FileTypes } from '@/types/file'
import { uuid } from '@/utils'
export interface ShareFileResult {
success: boolean
message: string
}
const logger = loggerService.withContext('File Service')
const { getAllFiles, getFileById } = fileDatabase
// 辅助函数,确保目录存在
async function ensureDirExists(dir: Directory) {
const dirInfo = dir.info()
if (!dirInfo.exists) {
dir.create({ intermediates: true })
}
}
export function readFile(file: FileMetadata): string {
return new File(file.path).textSync()
}
export function readBase64File(file: FileMetadata): string {
return new File(file.path).base64Sync()
}
export async function writeBase64File(data: string): Promise<FileMetadata> {
if (!DEFAULT_IMAGES_STORAGE.exists) {
DEFAULT_IMAGES_STORAGE.create({ intermediates: true, overwrite: true })
}
const cleanedBase64 = data.includes('data:image') ? data.split(',')[1] : data
const fileName = uuid()
const fileUri = DEFAULT_IMAGES_STORAGE.uri + `${fileName}.png`
// Use legacy API to write base64 data directly
await FileSystem.writeAsStringAsync(fileUri, cleanedBase64, {
encoding: FileSystem.EncodingType.Base64
})
const file = new File(fileUri)
return {
id: fileName,
name: fileName,
origin_name: fileName,
path: fileUri,
size: file.size,
ext: '.png',
type: FileTypes.IMAGE,
created_at: Date.now(),
count: 1
}
}
export function readStreamFile(file: FileMetadata): ReadableStream {
return new File(file.path).readableStream()
}
export async function uploadFiles(
files: Omit<FileMetadata, 'md5'>[],
uploadedDir?: Directory
): Promise<FileMetadata[]> {
const filePromises = files.map(async file => {
try {
const storageDir = uploadedDir
? uploadedDir
: file.type === FileTypes.IMAGE
? DEFAULT_IMAGES_STORAGE
: DEFAULT_DOCUMENTS_STORAGE
await ensureDirExists(storageDir)
const sourceUri = file.path
const sourceFile = new File(sourceUri)
// ios upload image will be .JPG
const destinationUri = `${storageDir.uri}${file.id}.${file.ext.toLowerCase()}`
const destinationFile = new File(destinationUri)
if (destinationFile.exists) {
destinationFile.delete()
}
sourceFile.move(destinationFile)
if (!sourceFile.exists) {
throw new Error('Failed to copy file or get info.')
}
const finalFile: FileMetadata = {
...file,
path: destinationUri,
size: sourceFile.size
}
console.log('finalFile', finalFile)
fileDatabase.upsertFiles([finalFile])
return finalFile
} catch (error) {
logger.error('Error uploading file:', error)
throw new Error(`Failed to upload file: ${file.name}`)
}
})
return await Promise.all(filePromises)
}
async function deleteFile(id: string, force: boolean = false): Promise<void> {
try {
const file = await fileDatabase.getFileById(id)
if (!file) return
const sourceFile = new File(file.path)
if (!force && file.count > 1) {
fileDatabase.upsertFiles([{ ...file, count: file.count - 1 }])
return
}
fileDatabase.deleteFileById(id)
sourceFile.delete()
} catch (error) {
logger.error('Error deleting file:', error)
throw new Error(`Failed to delete file: ${id}`)
}
}
export async function deleteFiles(files: FileMetadata[]): Promise<void> {
await Promise.all(files.map(file => deleteFile(file.id)))
}
export async function resetCacheDirectory() {
try {
if (DEFAULT_STORAGE.exists) {
DEFAULT_STORAGE.delete()
}
// Delete ImagePicker directory
const imagePickerDirectory = new Directory(Paths.cache, 'ImagePicker')
if (imagePickerDirectory.exists) {
imagePickerDirectory.delete()
}
// Delete DocumentPicker directory
const documentPickerDirectory = new Directory(Paths.cache, 'DocumentPicker')
if (documentPickerDirectory.exists) {
documentPickerDirectory.delete()
}
// Recreate Files directory
DEFAULT_STORAGE.create({ intermediates: true })
} catch (error) {
logger.error('resetCacheDirectory', error)
}
}
export async function getDirectorySizeAsync(directoryUri: string): Promise<number> {
try {
const directory = new Directory(directoryUri)
if (!directory.exists) {
return 0
}
let totalSize = 0
const contents = directory.list()
for (const item of contents) {
if (item instanceof Directory) {
totalSize += await getDirectorySizeAsync(item.uri)
} else {
totalSize += item.size || 0
}
}
return totalSize
} catch (error) {
console.error('Cannot get directory size:', error)
return 0
}
}
/**
* Get Cache Directory Size
* @returns Cache Directory Size
*/
export async function getCacheDirectorySize() {
// imagePicker and documentPicker will copy files to File, so size will double compututaion
// this is not equal to ios system cache storage
// const imagePickerDirectory = new Directory(Paths.cache, 'ImagePicker')
// const documentPickerDirectory = new Directory(Paths.cache, 'DocumentPicker')
const filesSize = await getDirectorySizeAsync(DEFAULT_STORAGE.uri)
// const imageSize = await getDirectorySizeAsync(imagePickerDirectory.uri)
// const documentSize = await getDirectorySizeAsync(documentPickerDirectory.uri)
// return filesSize + imageSize + documentSize
return filesSize
}
export async function shareFile(uri: string): Promise<ShareFileResult> {
try {
if (!(await Sharing.isAvailableAsync())) {
logger.warn('Sharing is not available on this device')
return {
success: false,
message: 'Sharing is not available on this device.'
}
}
const fileInfo = new File(uri).info()
if (!fileInfo.exists) {
logger.error('File not found:', uri)
return {
success: false,
message: 'File not found.'
}
}
await Sharing.shareAsync(uri)
logger.info('File shared successfully')
return {
success: true,
message: 'File shared successfully.'
}
} catch (error) {
logger.error('Error sharing file:', error)
return {
success: false,
message: 'Failed to share file. Please try again.'
}
}
}
export async function downloadFileAsync(url: string, destination: File) {
return File.downloadFileAsync(url, destination)
}
export default {
readFile,
readBase64File,
readStreamFile,
getFile: getFileById,
getAllFiles,
uploadFiles,
deleteFiles,
resetCacheDirectory,
getDirectorySizeAsync,
getCacheDirectorySize,
shareFile,
downloadFileAsync
}