1
- import * as Crypto from 'expo-crypto'
2
1
import * as FileSystem from 'expo-file-system'
3
2
import type { FileStoreInterface } from '../../types/FileStoreInterface'
3
+ import type { UuidGenerator } from '../UuidGenerator'
4
4
5
5
/**
6
6
* A wrapper around expo-file-system which stores files in a subdirectory of the
7
7
* document directory and provides a mockable interface.
8
8
*/
9
9
export class FileStore implements FileStoreInterface {
10
+ constructor ( private readonly uuidGenerator : UuidGenerator ) { }
11
+
10
12
private subdirectoryName : null | string = null
11
13
private loading = false
12
14
private operationsInProgress = 0
@@ -99,7 +101,7 @@ export class FileStore implements FileStoreInterface {
99
101
try {
100
102
this . operationsInProgress ++
101
103
102
- const output = Crypto . randomUUID ( ) . toLowerCase ( )
104
+ const output = this . uuidGenerator . generate ( )
103
105
104
106
await FileSystem . moveAsync ( {
105
107
from : fileUri ,
@@ -112,4 +114,27 @@ export class FileStore implements FileStoreInterface {
112
114
}
113
115
}
114
116
}
117
+
118
+ async importPreservingOriginal ( fileUri : string ) : Promise < string > {
119
+ if ( this . loading ) {
120
+ throw new Error ( 'The file store is currently loading.' )
121
+ } else if ( this . subdirectoryName === null ) {
122
+ throw new Error ( 'The file store is not loaded.' )
123
+ } else {
124
+ try {
125
+ this . operationsInProgress ++
126
+
127
+ const output = this . uuidGenerator . generate ( )
128
+
129
+ await FileSystem . copyAsync ( {
130
+ from : fileUri ,
131
+ to : this . generatePath ( output )
132
+ } )
133
+
134
+ return output
135
+ } finally {
136
+ this . operationsInProgress --
137
+ }
138
+ }
139
+ }
115
140
}
0 commit comments