Skip to content

Commit af339c5

Browse files
committed
Allow original preservation.
1 parent 30c9c24 commit af339c5

File tree

3 files changed

+915
-74
lines changed

3 files changed

+915
-74
lines changed

react-native/services/FileStore/index.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import * as Crypto from 'expo-crypto'
21
import * as FileSystem from 'expo-file-system'
32
import type { FileStoreInterface } from '../../types/FileStoreInterface'
3+
import type { UuidGenerator } from '../UuidGenerator'
44

55
/**
66
* A wrapper around expo-file-system which stores files in a subdirectory of the
77
* document directory and provides a mockable interface.
88
*/
99
export class FileStore implements FileStoreInterface {
10+
constructor (private readonly uuidGenerator: UuidGenerator) {}
11+
1012
private subdirectoryName: null | string = null
1113
private loading = false
1214
private operationsInProgress = 0
@@ -99,7 +101,7 @@ export class FileStore implements FileStoreInterface {
99101
try {
100102
this.operationsInProgress++
101103

102-
const output = Crypto.randomUUID().toLowerCase()
104+
const output = this.uuidGenerator.generate()
103105

104106
await FileSystem.moveAsync({
105107
from: fileUri,
@@ -112,4 +114,27 @@ export class FileStore implements FileStoreInterface {
112114
}
113115
}
114116
}
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+
}
115140
}

react-native/services/FileStore/readme.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ document directory and provides a mockable interface.
66
## Usage
77

88
```tsx
9-
import type { FileStore } from "react-native-app-helpers";
9+
import type { UuidGenerator, FileStore } from "react-native-app-helpers";
1010

11-
const fileStore = new FileStore();
11+
const fileStore = new FileStore(new UuidGenerator());
1212

1313
await fileStore.load(`example-subdirectory-name`);
1414

@@ -24,6 +24,9 @@ fileStore.generatePath(`9dd60263-682d-41b9-bf39-c3a1183da1b1`);
2424
// `9dd60263-682d-41b9-bf39-c3a1183da1b1`
2525
await fileStore.import(`example-file-uri`);
2626

27+
// `9dd60263-682d-41b9-bf39-c3a1183da1b1`
28+
await fileStore.importPreservingOriginal(`example-file-uri`);
29+
2730
fileStore.unload();
2831
```
2932

0 commit comments

Comments
 (0)