Skip to content

Commit

Permalink
Allow original preservation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswilddev committed Dec 12, 2023
1 parent 30c9c24 commit af339c5
Show file tree
Hide file tree
Showing 3 changed files with 915 additions and 74 deletions.
29 changes: 27 additions & 2 deletions react-native/services/FileStore/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as Crypto from 'expo-crypto'
import * as FileSystem from 'expo-file-system'
import type { FileStoreInterface } from '../../types/FileStoreInterface'
import type { UuidGenerator } from '../UuidGenerator'

/**
* A wrapper around expo-file-system which stores files in a subdirectory of the
* document directory and provides a mockable interface.
*/
export class FileStore implements FileStoreInterface {
constructor (private readonly uuidGenerator: UuidGenerator) {}

private subdirectoryName: null | string = null
private loading = false
private operationsInProgress = 0
Expand Down Expand Up @@ -99,7 +101,7 @@ export class FileStore implements FileStoreInterface {
try {
this.operationsInProgress++

const output = Crypto.randomUUID().toLowerCase()
const output = this.uuidGenerator.generate()

await FileSystem.moveAsync({
from: fileUri,
Expand All @@ -112,4 +114,27 @@ export class FileStore implements FileStoreInterface {
}
}
}

async importPreservingOriginal (fileUri: string): Promise<string> {
if (this.loading) {
throw new Error('The file store is currently loading.')
} else if (this.subdirectoryName === null) {
throw new Error('The file store is not loaded.')
} else {
try {
this.operationsInProgress++

const output = this.uuidGenerator.generate()

await FileSystem.copyAsync({
from: fileUri,
to: this.generatePath(output)
})

return output
} finally {
this.operationsInProgress--
}
}
}
}
7 changes: 5 additions & 2 deletions react-native/services/FileStore/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ document directory and provides a mockable interface.
## Usage

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

const fileStore = new FileStore();
const fileStore = new FileStore(new UuidGenerator());

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

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

// `9dd60263-682d-41b9-bf39-c3a1183da1b1`
await fileStore.importPreservingOriginal(`example-file-uri`);

fileStore.unload();
```

Expand Down
Loading

0 comments on commit af339c5

Please sign in to comment.