feat(files): refactor circular deps
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { AbstractService } from '../Service/AbstractService'
|
||||
import { Uuid } from '@standardnotes/common'
|
||||
import { Role } from '@standardnotes/auth'
|
||||
import { FilesApiInterface } from '../Files/FilesApiInterface'
|
||||
import { FilesApiInterface } from '@standardnotes/files'
|
||||
|
||||
/* istanbul ignore file */
|
||||
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import { ApplicationIdentifier, ContentType } from '@standardnotes/common'
|
||||
import { BackupFile, DecryptedItemInterface, ItemStream, PrefKey, PrefValue } from '@standardnotes/models'
|
||||
import { FilesClientInterface } from '@standardnotes/files'
|
||||
import { AlertService } from '../Alert/AlertService'
|
||||
|
||||
import { ComponentManagerInterface } from '../Component/ComponentManagerInterface'
|
||||
import { Platform } from '../Device/Environments'
|
||||
import { ApplicationEvent } from '../Event/ApplicationEvent'
|
||||
import { ApplicationEventCallback } from '../Event/ApplicationEventCallback'
|
||||
import { FeaturesClientInterface } from '../Feature/FeaturesClientInterface'
|
||||
@@ -12,6 +15,7 @@ import { StorageValueModes } from '../Storage/StorageTypes'
|
||||
import { DeinitMode } from './DeinitMode'
|
||||
import { DeinitSource } from './DeinitSource'
|
||||
import { UserClientInterface } from './UserClientInterface'
|
||||
import { DeviceInterface } from '../Device/DeviceInterface'
|
||||
|
||||
export interface ApplicationInterface {
|
||||
deinit(mode: DeinitMode, source: DeinitSource): void
|
||||
@@ -21,6 +25,7 @@ export interface ApplicationInterface {
|
||||
addEventObserver(callback: ApplicationEventCallback, singleEvent?: ApplicationEvent): () => void
|
||||
hasProtectionSources(): boolean
|
||||
createEncryptedBackupFileForAutomatedDesktopBackups(): Promise<BackupFile | undefined>
|
||||
createEncryptedBackupFile(): Promise<BackupFile | undefined>
|
||||
createDecryptedBackupFile(): Promise<BackupFile | undefined>
|
||||
hasPasscode(): boolean
|
||||
lock(): Promise<void>
|
||||
@@ -31,14 +36,20 @@ export interface ApplicationInterface {
|
||||
getPreference<K extends PrefKey>(key: K): PrefValue[K] | undefined
|
||||
getPreference<K extends PrefKey>(key: K, defaultValue: PrefValue[K]): PrefValue[K]
|
||||
getPreference<K extends PrefKey>(key: K, defaultValue?: PrefValue[K]): PrefValue[K] | undefined
|
||||
setPreference<K extends PrefKey>(key: K, value: PrefValue[K]): Promise<void>
|
||||
streamItems<I extends DecryptedItemInterface = DecryptedItemInterface>(
|
||||
contentType: ContentType | ContentType[],
|
||||
stream: ItemStream<I>,
|
||||
): () => void
|
||||
hasAccount(): boolean
|
||||
get features(): FeaturesClientInterface
|
||||
get componentManager(): ComponentManagerInterface
|
||||
get items(): ItemsClientInterface
|
||||
get mutator(): MutatorClientInterface
|
||||
get user(): UserClientInterface
|
||||
get files(): FilesClientInterface
|
||||
readonly identifier: ApplicationIdentifier
|
||||
readonly platform: Platform
|
||||
deviceInterface: DeviceInterface
|
||||
alertService: AlertService
|
||||
}
|
||||
|
||||
169
packages/services/src/Domain/Backups/BackupService.ts
Normal file
169
packages/services/src/Domain/Backups/BackupService.ts
Normal file
@@ -0,0 +1,169 @@
|
||||
import { ContentType, Uuid } from '@standardnotes/common'
|
||||
import { EncryptionProvider } from '@standardnotes/encryption'
|
||||
import { PayloadEmitSource, FileItem, CreateEncryptedBackupFileContextPayload } from '@standardnotes/models'
|
||||
import { ClientDisplayableError } from '@standardnotes/responses'
|
||||
import { FileBackupMetadataFile, FileBackupsDevice, FileBackupsMapping } from '../Device/FileBackupsDevice'
|
||||
import { FilesApiInterface } from '@standardnotes/files'
|
||||
import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface'
|
||||
import { ItemManagerInterface } from '../Item/ItemManagerInterface'
|
||||
import { AbstractService } from '../Service/AbstractService'
|
||||
import { StatusServiceInterface } from '../Status/StatusServiceInterface'
|
||||
|
||||
export class FilesBackupService extends AbstractService {
|
||||
private itemsObserverDisposer: () => void
|
||||
private pendingFiles = new Set<Uuid>()
|
||||
|
||||
constructor(
|
||||
private items: ItemManagerInterface,
|
||||
private api: FilesApiInterface,
|
||||
private encryptor: EncryptionProvider,
|
||||
private device: FileBackupsDevice,
|
||||
private status: StatusServiceInterface,
|
||||
protected override internalEventBus: InternalEventBusInterface,
|
||||
) {
|
||||
super(internalEventBus)
|
||||
|
||||
this.itemsObserverDisposer = items.addObserver<FileItem>(ContentType.File, ({ changed, inserted, source }) => {
|
||||
const applicableSources = [
|
||||
PayloadEmitSource.LocalDatabaseLoaded,
|
||||
PayloadEmitSource.RemoteSaved,
|
||||
PayloadEmitSource.RemoteRetrieved,
|
||||
]
|
||||
|
||||
if (applicableSources.includes(source)) {
|
||||
void this.handleChangedFiles([...changed, ...inserted])
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override deinit() {
|
||||
this.itemsObserverDisposer()
|
||||
}
|
||||
|
||||
public isFilesBackupsEnabled(): Promise<boolean> {
|
||||
return this.device.isFilesBackupsEnabled()
|
||||
}
|
||||
|
||||
public async enableFilesBackups(): Promise<void> {
|
||||
await this.device.enableFilesBackups()
|
||||
|
||||
if (!(await this.isFilesBackupsEnabled())) {
|
||||
return
|
||||
}
|
||||
|
||||
this.backupAllFiles()
|
||||
}
|
||||
|
||||
private backupAllFiles(): void {
|
||||
const files = this.items.getItems<FileItem>(ContentType.File)
|
||||
|
||||
void this.handleChangedFiles(files)
|
||||
}
|
||||
|
||||
public disableFilesBackups(): Promise<void> {
|
||||
return this.device.disableFilesBackups()
|
||||
}
|
||||
|
||||
public changeFilesBackupsLocation(): Promise<string | undefined> {
|
||||
return this.device.changeFilesBackupsLocation()
|
||||
}
|
||||
|
||||
public getFilesBackupsLocation(): Promise<string> {
|
||||
return this.device.getFilesBackupsLocation()
|
||||
}
|
||||
|
||||
public openFilesBackupsLocation(): Promise<void> {
|
||||
return this.device.openFilesBackupsLocation()
|
||||
}
|
||||
|
||||
private async getBackupsMapping(): Promise<FileBackupsMapping['files']> {
|
||||
return (await this.device.getFilesBackupsMappingFile()).files
|
||||
}
|
||||
|
||||
private async handleChangedFiles(files: FileItem[]): Promise<void> {
|
||||
if (files.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!(await this.isFilesBackupsEnabled())) {
|
||||
return
|
||||
}
|
||||
|
||||
const mapping = await this.getBackupsMapping()
|
||||
|
||||
for (const file of files) {
|
||||
if (this.pendingFiles.has(file.uuid)) {
|
||||
continue
|
||||
}
|
||||
|
||||
const record = mapping[file.uuid]
|
||||
|
||||
if (record == undefined) {
|
||||
this.pendingFiles.add(file.uuid)
|
||||
|
||||
await this.performBackupOperation(file)
|
||||
|
||||
this.pendingFiles.delete(file.uuid)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async performBackupOperation(file: FileItem): Promise<'success' | 'failed' | 'aborted'> {
|
||||
const messageId = this.status.addMessage(`Backing up file ${file.name}...`)
|
||||
|
||||
const encryptedFile = await this.encryptor.encryptSplitSingle({
|
||||
usesItemsKeyWithKeyLookup: {
|
||||
items: [file.payload],
|
||||
},
|
||||
})
|
||||
|
||||
const itemsKey = this.items.getDisplayableItemsKeys().find((k) => k.uuid === encryptedFile.items_key_id)
|
||||
|
||||
if (!itemsKey) {
|
||||
return 'failed'
|
||||
}
|
||||
|
||||
const encryptedItemsKey = await this.encryptor.encryptSplitSingle({
|
||||
usesRootKeyWithKeyLookup: {
|
||||
items: [itemsKey.payload],
|
||||
},
|
||||
})
|
||||
|
||||
const token = await this.api.createFileValetToken(file.remoteIdentifier, 'read')
|
||||
|
||||
if (token instanceof ClientDisplayableError) {
|
||||
return 'failed'
|
||||
}
|
||||
|
||||
const metaFile: FileBackupMetadataFile = {
|
||||
info: {
|
||||
warning: 'Do not edit this file.',
|
||||
information: 'The file and key data below is encrypted with your account password.',
|
||||
instructions:
|
||||
'Drag and drop this metadata file into the File Backups preferences pane in the Standard Notes desktop or web application interface.',
|
||||
},
|
||||
file: CreateEncryptedBackupFileContextPayload(encryptedFile.ejected()),
|
||||
itemsKey: CreateEncryptedBackupFileContextPayload(encryptedItemsKey.ejected()),
|
||||
version: '1.0.0',
|
||||
}
|
||||
|
||||
const metaFileAsString = JSON.stringify(metaFile, null, 2)
|
||||
|
||||
const result = await this.device.saveFilesBackupsFile(file.uuid, metaFileAsString, {
|
||||
chunkSizes: file.encryptedChunkSizes,
|
||||
url: this.api.getFilesDownloadUrl(),
|
||||
valetToken: token,
|
||||
})
|
||||
|
||||
this.status.removeMessage(messageId)
|
||||
|
||||
if (result === 'failed') {
|
||||
const failMessageId = this.status.addMessage(`Failed to back up ${file.name}...`)
|
||||
setTimeout(() => {
|
||||
this.status.removeMessage(failMessageId)
|
||||
}, 2000)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -20,4 +20,5 @@ export interface ComponentManagerInterface {
|
||||
urlOverride?: string,
|
||||
): ComponentViewerInterface
|
||||
presentPermissionsDialog(_dialog: PermissionDialog): void
|
||||
getDefaultEditor(): SNComponent
|
||||
}
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
import { Uuid } from '@standardnotes/common'
|
||||
import { BackupFileEncryptedContextualPayload } from '@standardnotes/models'
|
||||
|
||||
/* istanbul ignore file */
|
||||
|
||||
export const FileBackupsConstantsV1 = {
|
||||
Version: '1.0.0',
|
||||
MetadataFileName: 'metadata.sn.json',
|
||||
BinaryFileName: 'file.encrypted',
|
||||
}
|
||||
|
||||
export interface FileBackupMetadataFile {
|
||||
info: Record<string, string>
|
||||
file: BackupFileEncryptedContextualPayload
|
||||
itemsKey: BackupFileEncryptedContextualPayload
|
||||
version: '1.0.0'
|
||||
}
|
||||
|
||||
export interface FileBackupsMapping {
|
||||
version: typeof FileBackupsConstantsV1.Version
|
||||
files: Record<
|
||||
Uuid,
|
||||
{
|
||||
backedUpOn: Date
|
||||
absolutePath: string
|
||||
relativePath: string
|
||||
metadataFileName: typeof FileBackupsConstantsV1.MetadataFileName
|
||||
binaryFileName: typeof FileBackupsConstantsV1.BinaryFileName
|
||||
version: typeof FileBackupsConstantsV1.Version
|
||||
}
|
||||
>
|
||||
}
|
||||
|
||||
export interface FileBackupsDevice {
|
||||
getFilesBackupsMappingFile(): Promise<FileBackupsMapping>
|
||||
saveFilesBackupsFile(
|
||||
uuid: Uuid,
|
||||
metaFile: string,
|
||||
downloadRequest: {
|
||||
chunkSizes: number[]
|
||||
valetToken: string
|
||||
url: string
|
||||
},
|
||||
): Promise<'success' | 'failed'>
|
||||
isFilesBackupsEnabled(): Promise<boolean>
|
||||
enableFilesBackups(): Promise<void>
|
||||
disableFilesBackups(): Promise<void>
|
||||
changeFilesBackupsLocation(): Promise<string | undefined>
|
||||
getFilesBackupsLocation(): Promise<string>
|
||||
openFilesBackupsLocation(): Promise<void>
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
export interface DirectoryHandle {
|
||||
nativeHandle: unknown
|
||||
}
|
||||
export interface FileHandleReadWrite {
|
||||
nativeHandle: unknown
|
||||
writableStream: unknown
|
||||
}
|
||||
export interface FileHandleRead {
|
||||
nativeHandle: unknown
|
||||
}
|
||||
|
||||
export type FileSystemResult = 'aborted' | 'success' | 'failed'
|
||||
export type FileSystemNoSelection = 'aborted' | 'failed'
|
||||
|
||||
export interface FileSystemApi {
|
||||
selectDirectory(): Promise<DirectoryHandle | FileSystemNoSelection>
|
||||
selectFile(): Promise<FileHandleRead | FileSystemNoSelection>
|
||||
readFile(
|
||||
file: FileHandleRead,
|
||||
onBytes: (bytes: Uint8Array, isLast: boolean) => Promise<void>,
|
||||
): Promise<FileSystemResult>
|
||||
createDirectory(parentDirectory: DirectoryHandle, name: string): Promise<DirectoryHandle | FileSystemNoSelection>
|
||||
createFile(directory: DirectoryHandle, name: string): Promise<FileHandleReadWrite | FileSystemNoSelection>
|
||||
saveBytes(file: FileHandleReadWrite, bytes: Uint8Array): Promise<'success' | 'failed'>
|
||||
saveString(file: FileHandleReadWrite, contents: string): Promise<'success' | 'failed'>
|
||||
closeFileWriteStream(file: FileHandleReadWrite): Promise<'success' | 'failed'>
|
||||
}
|
||||
121
packages/services/src/Domain/Files/FileService.spec.ts
Normal file
121
packages/services/src/Domain/Files/FileService.spec.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import {
|
||||
InternalEventBusInterface,
|
||||
SyncServiceInterface,
|
||||
ItemManagerInterface,
|
||||
AlertService,
|
||||
ApiServiceInterface,
|
||||
ChallengeServiceInterface,
|
||||
} from '@standardnotes/services'
|
||||
import { FileService } from './FileService'
|
||||
import { PureCryptoInterface, StreamEncryptor } from '@standardnotes/sncrypto-common'
|
||||
import { FileItem } from '@standardnotes/models'
|
||||
import { EncryptionProvider } from '@standardnotes/encryption'
|
||||
|
||||
describe('fileService', () => {
|
||||
let apiService: ApiServiceInterface
|
||||
let itemManager: ItemManagerInterface
|
||||
let syncService: SyncServiceInterface
|
||||
let alertService: AlertService
|
||||
let crypto: PureCryptoInterface
|
||||
let challengor: ChallengeServiceInterface
|
||||
let fileService: FileService
|
||||
let encryptor: EncryptionProvider
|
||||
let internalEventBus: InternalEventBusInterface
|
||||
|
||||
beforeEach(() => {
|
||||
apiService = {} as jest.Mocked<ApiServiceInterface>
|
||||
apiService.addEventObserver = jest.fn()
|
||||
apiService.createFileValetToken = jest.fn()
|
||||
apiService.downloadFile = jest.fn()
|
||||
apiService.deleteFile = jest.fn().mockReturnValue({})
|
||||
|
||||
itemManager = {} as jest.Mocked<ItemManagerInterface>
|
||||
itemManager.createItem = jest.fn()
|
||||
itemManager.createTemplateItem = jest.fn().mockReturnValue({})
|
||||
itemManager.setItemToBeDeleted = jest.fn()
|
||||
itemManager.addObserver = jest.fn()
|
||||
itemManager.changeItem = jest.fn()
|
||||
|
||||
challengor = {} as jest.Mocked<ChallengeServiceInterface>
|
||||
|
||||
syncService = {} as jest.Mocked<SyncServiceInterface>
|
||||
syncService.sync = jest.fn()
|
||||
|
||||
encryptor = {} as jest.Mocked<EncryptionProvider>
|
||||
|
||||
alertService = {} as jest.Mocked<AlertService>
|
||||
alertService.confirm = jest.fn().mockReturnValue(true)
|
||||
alertService.alert = jest.fn()
|
||||
|
||||
crypto = {} as jest.Mocked<PureCryptoInterface>
|
||||
crypto.base64Decode = jest.fn()
|
||||
internalEventBus = {} as jest.Mocked<InternalEventBusInterface>
|
||||
internalEventBus.publish = jest.fn()
|
||||
|
||||
fileService = new FileService(
|
||||
apiService,
|
||||
itemManager,
|
||||
syncService,
|
||||
encryptor,
|
||||
challengor,
|
||||
alertService,
|
||||
crypto,
|
||||
internalEventBus,
|
||||
)
|
||||
|
||||
crypto.xchacha20StreamInitDecryptor = jest.fn().mockReturnValue({
|
||||
state: {},
|
||||
} as StreamEncryptor)
|
||||
|
||||
crypto.xchacha20StreamDecryptorPush = jest.fn().mockReturnValue({ message: new Uint8Array([0xaa]), tag: 0 })
|
||||
|
||||
crypto.xchacha20StreamInitEncryptor = jest.fn().mockReturnValue({
|
||||
header: 'some-header',
|
||||
state: {},
|
||||
} as StreamEncryptor)
|
||||
|
||||
crypto.xchacha20StreamEncryptorPush = jest.fn().mockReturnValue(new Uint8Array())
|
||||
})
|
||||
|
||||
it.only('should cache file after download', async () => {
|
||||
const file = {
|
||||
uuid: '1',
|
||||
decryptedSize: 100_000,
|
||||
encryptedSize: 101_000,
|
||||
encryptedChunkSizes: [101_000],
|
||||
} as jest.Mocked<FileItem>
|
||||
|
||||
let downloadMock = apiService.downloadFile as jest.Mock
|
||||
|
||||
await fileService.downloadFile(file, async () => {
|
||||
return Promise.resolve()
|
||||
})
|
||||
|
||||
expect(downloadMock).toHaveBeenCalledTimes(1)
|
||||
|
||||
downloadMock = apiService.downloadFile = jest.fn()
|
||||
|
||||
await fileService.downloadFile(file, async () => {
|
||||
return Promise.resolve()
|
||||
})
|
||||
|
||||
expect(downloadMock).toHaveBeenCalledTimes(0)
|
||||
|
||||
expect(fileService['encryptedCache'].get(file.uuid)).toBeTruthy()
|
||||
})
|
||||
|
||||
it('deleting file should remove it from cache', async () => {
|
||||
const file = {
|
||||
uuid: '1',
|
||||
decryptedSize: 100_000,
|
||||
} as jest.Mocked<FileItem>
|
||||
|
||||
await fileService.downloadFile(file, async () => {
|
||||
return Promise.resolve()
|
||||
})
|
||||
|
||||
await fileService.deleteFile(file)
|
||||
|
||||
expect(fileService['encryptedCache'].get(file.uuid)).toBeFalsy()
|
||||
})
|
||||
})
|
||||
316
packages/services/src/Domain/Files/FileService.ts
Normal file
316
packages/services/src/Domain/Files/FileService.ts
Normal file
@@ -0,0 +1,316 @@
|
||||
import { DecryptedBytes, EncryptedBytes, FileMemoryCache, OrderedByteChunker } from '@standardnotes/filepicker'
|
||||
import { ClientDisplayableError } from '@standardnotes/responses'
|
||||
import { ContentType } from '@standardnotes/common'
|
||||
import {
|
||||
FileItem,
|
||||
FileProtocolV1Constants,
|
||||
FileMetadata,
|
||||
FileContentSpecialized,
|
||||
FillItemContentSpecialized,
|
||||
FileContent,
|
||||
EncryptedPayload,
|
||||
isEncryptedPayload,
|
||||
} from '@standardnotes/models'
|
||||
import { PureCryptoInterface } from '@standardnotes/sncrypto-common'
|
||||
import { UuidGenerator } from '@standardnotes/utils'
|
||||
import {
|
||||
AbstractService,
|
||||
InternalEventBusInterface,
|
||||
ItemManagerInterface,
|
||||
SyncServiceInterface,
|
||||
AlertService,
|
||||
FileSystemApi,
|
||||
FilesApiInterface,
|
||||
FileBackupMetadataFile,
|
||||
FileHandleRead,
|
||||
FileSystemNoSelection,
|
||||
ChallengeServiceInterface,
|
||||
FileBackupsConstantsV1,
|
||||
} from '@standardnotes/services'
|
||||
import { DecryptItemsKeyWithUserFallback, EncryptionProvider, SNItemsKey } from '@standardnotes/encryption'
|
||||
import {
|
||||
DownloadAndDecryptFileOperation,
|
||||
EncryptAndUploadFileOperation,
|
||||
FileDecryptor,
|
||||
FileDownloadProgress,
|
||||
FilesClientInterface,
|
||||
readAndDecryptBackupFile,
|
||||
} from '@standardnotes/files'
|
||||
|
||||
const OneHundredMb = 100 * 1_000_000
|
||||
|
||||
export class FileService extends AbstractService implements FilesClientInterface {
|
||||
private encryptedCache: FileMemoryCache = new FileMemoryCache(OneHundredMb)
|
||||
|
||||
constructor(
|
||||
private api: FilesApiInterface,
|
||||
private itemManager: ItemManagerInterface,
|
||||
private syncService: SyncServiceInterface,
|
||||
private encryptor: EncryptionProvider,
|
||||
private challengor: ChallengeServiceInterface,
|
||||
private alertService: AlertService,
|
||||
private crypto: PureCryptoInterface,
|
||||
protected override internalEventBus: InternalEventBusInterface,
|
||||
) {
|
||||
super(internalEventBus)
|
||||
}
|
||||
|
||||
override deinit(): void {
|
||||
super.deinit()
|
||||
|
||||
this.encryptedCache.clear()
|
||||
;(this.encryptedCache as unknown) = undefined
|
||||
;(this.api as unknown) = undefined
|
||||
;(this.itemManager as unknown) = undefined
|
||||
;(this.encryptor as unknown) = undefined
|
||||
;(this.syncService as unknown) = undefined
|
||||
;(this.alertService as unknown) = undefined
|
||||
;(this.challengor as unknown) = undefined
|
||||
;(this.crypto as unknown) = undefined
|
||||
}
|
||||
|
||||
public minimumChunkSize(): number {
|
||||
return 5_000_000
|
||||
}
|
||||
|
||||
public async beginNewFileUpload(
|
||||
sizeInBytes: number,
|
||||
): Promise<EncryptAndUploadFileOperation | ClientDisplayableError> {
|
||||
const remoteIdentifier = UuidGenerator.GenerateUuid()
|
||||
const tokenResult = await this.api.createFileValetToken(remoteIdentifier, 'write', sizeInBytes)
|
||||
|
||||
if (tokenResult instanceof ClientDisplayableError) {
|
||||
return tokenResult
|
||||
}
|
||||
|
||||
const key = this.crypto.generateRandomKey(FileProtocolV1Constants.KeySize)
|
||||
|
||||
const fileParams = {
|
||||
key,
|
||||
remoteIdentifier,
|
||||
decryptedSize: sizeInBytes,
|
||||
}
|
||||
|
||||
const uploadOperation = new EncryptAndUploadFileOperation(fileParams, tokenResult, this.crypto, this.api)
|
||||
|
||||
const uploadSessionStarted = await this.api.startUploadSession(tokenResult)
|
||||
|
||||
if (!uploadSessionStarted.uploadId) {
|
||||
return new ClientDisplayableError('Could not start upload session')
|
||||
}
|
||||
|
||||
return uploadOperation
|
||||
}
|
||||
|
||||
public async pushBytesForUpload(
|
||||
operation: EncryptAndUploadFileOperation,
|
||||
bytes: Uint8Array,
|
||||
chunkId: number,
|
||||
isFinalChunk: boolean,
|
||||
): Promise<ClientDisplayableError | undefined> {
|
||||
const success = await operation.pushBytes(bytes, chunkId, isFinalChunk)
|
||||
|
||||
if (!success) {
|
||||
return new ClientDisplayableError('Failed to push file bytes to server')
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
public async finishUpload(
|
||||
operation: EncryptAndUploadFileOperation,
|
||||
fileMetadata: FileMetadata,
|
||||
): Promise<FileItem | ClientDisplayableError> {
|
||||
const uploadSessionClosed = await this.api.closeUploadSession(operation.getApiToken())
|
||||
|
||||
if (!uploadSessionClosed) {
|
||||
return new ClientDisplayableError('Could not close upload session')
|
||||
}
|
||||
|
||||
const result = operation.getResult()
|
||||
|
||||
const fileContent: FileContentSpecialized = {
|
||||
decryptedSize: result.finalDecryptedSize,
|
||||
encryptedChunkSizes: operation.encryptedChunkSizes,
|
||||
encryptionHeader: result.encryptionHeader,
|
||||
key: result.key,
|
||||
mimeType: fileMetadata.mimeType,
|
||||
name: fileMetadata.name,
|
||||
remoteIdentifier: result.remoteIdentifier,
|
||||
}
|
||||
|
||||
const file = await this.itemManager.createItem<FileItem>(
|
||||
ContentType.File,
|
||||
FillItemContentSpecialized(fileContent),
|
||||
true,
|
||||
)
|
||||
|
||||
await this.syncService.sync()
|
||||
|
||||
return file
|
||||
}
|
||||
|
||||
private async decryptCachedEntry(file: FileItem, entry: EncryptedBytes): Promise<DecryptedBytes> {
|
||||
const decryptOperation = new FileDecryptor(file, this.crypto)
|
||||
|
||||
let decryptedAggregate = new Uint8Array()
|
||||
|
||||
const orderedChunker = new OrderedByteChunker(file.encryptedChunkSizes, async (encryptedBytes) => {
|
||||
const decryptedBytes = decryptOperation.decryptBytes(encryptedBytes)
|
||||
|
||||
if (decryptedBytes) {
|
||||
decryptedAggregate = new Uint8Array([...decryptedAggregate, ...decryptedBytes.decryptedBytes])
|
||||
}
|
||||
})
|
||||
|
||||
await orderedChunker.addBytes(entry.encryptedBytes)
|
||||
|
||||
return { decryptedBytes: decryptedAggregate }
|
||||
}
|
||||
|
||||
public async downloadFile(
|
||||
file: FileItem,
|
||||
onDecryptedBytes: (decryptedBytes: Uint8Array, progress?: FileDownloadProgress) => Promise<void>,
|
||||
): Promise<ClientDisplayableError | undefined> {
|
||||
const cachedBytes = this.encryptedCache.get(file.uuid)
|
||||
|
||||
if (cachedBytes) {
|
||||
const decryptedBytes = await this.decryptCachedEntry(file, cachedBytes)
|
||||
|
||||
await onDecryptedBytes(decryptedBytes.decryptedBytes, undefined)
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
const addToCache = file.encryptedSize < this.encryptedCache.maxSize
|
||||
|
||||
let cacheEntryAggregate = new Uint8Array()
|
||||
|
||||
const operation = new DownloadAndDecryptFileOperation(file, this.crypto, this.api)
|
||||
|
||||
const result = await operation.run(async ({ decrypted, encrypted, progress }): Promise<void> => {
|
||||
if (addToCache) {
|
||||
cacheEntryAggregate = new Uint8Array([...cacheEntryAggregate, ...encrypted.encryptedBytes])
|
||||
}
|
||||
return onDecryptedBytes(decrypted.decryptedBytes, progress)
|
||||
})
|
||||
|
||||
if (addToCache) {
|
||||
this.encryptedCache.add(file.uuid, { encryptedBytes: cacheEntryAggregate })
|
||||
}
|
||||
|
||||
return result.error
|
||||
}
|
||||
|
||||
public async deleteFile(file: FileItem): Promise<ClientDisplayableError | undefined> {
|
||||
this.encryptedCache.remove(file.uuid)
|
||||
|
||||
const tokenResult = await this.api.createFileValetToken(file.remoteIdentifier, 'delete')
|
||||
|
||||
if (tokenResult instanceof ClientDisplayableError) {
|
||||
return tokenResult
|
||||
}
|
||||
|
||||
const result = await this.api.deleteFile(tokenResult)
|
||||
|
||||
if (result.error) {
|
||||
return ClientDisplayableError.FromError(result.error)
|
||||
}
|
||||
|
||||
await this.itemManager.setItemToBeDeleted(file)
|
||||
await this.syncService.sync()
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
public isFileNameFileBackupRelated(name: string): 'metadata' | 'binary' | false {
|
||||
if (name === FileBackupsConstantsV1.MetadataFileName) {
|
||||
return 'metadata'
|
||||
} else if (name === FileBackupsConstantsV1.BinaryFileName) {
|
||||
return 'binary'
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
public async decryptBackupMetadataFile(metdataFile: FileBackupMetadataFile): Promise<FileItem | undefined> {
|
||||
const encryptedItemsKey = new EncryptedPayload({
|
||||
...metdataFile.itemsKey,
|
||||
waitingForKey: false,
|
||||
errorDecrypting: false,
|
||||
})
|
||||
|
||||
const decryptedItemsKeyResult = await DecryptItemsKeyWithUserFallback(
|
||||
encryptedItemsKey,
|
||||
this.encryptor,
|
||||
this.challengor,
|
||||
)
|
||||
|
||||
if (decryptedItemsKeyResult === 'failed' || decryptedItemsKeyResult === 'aborted') {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const encryptedFile = new EncryptedPayload({ ...metdataFile.file, waitingForKey: false, errorDecrypting: false })
|
||||
|
||||
const itemsKey = new SNItemsKey(decryptedItemsKeyResult)
|
||||
|
||||
const decryptedFile = await this.encryptor.decryptSplitSingle<FileContent>({
|
||||
usesItemsKey: {
|
||||
items: [encryptedFile],
|
||||
key: itemsKey,
|
||||
},
|
||||
})
|
||||
|
||||
if (isEncryptedPayload(decryptedFile)) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
return new FileItem(decryptedFile)
|
||||
}
|
||||
|
||||
public async selectFile(fileSystem: FileSystemApi): Promise<FileHandleRead | FileSystemNoSelection> {
|
||||
const result = await fileSystem.selectFile()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
public async readBackupFileAndSaveDecrypted(
|
||||
fileHandle: FileHandleRead,
|
||||
file: FileItem,
|
||||
fileSystem: FileSystemApi,
|
||||
): Promise<'success' | 'aborted' | 'failed'> {
|
||||
const destinationDirectoryHandle = await fileSystem.selectDirectory()
|
||||
|
||||
if (destinationDirectoryHandle === 'aborted' || destinationDirectoryHandle === 'failed') {
|
||||
return destinationDirectoryHandle
|
||||
}
|
||||
|
||||
const destinationFileHandle = await fileSystem.createFile(destinationDirectoryHandle, file.name)
|
||||
|
||||
if (destinationFileHandle === 'aborted' || destinationFileHandle === 'failed') {
|
||||
return destinationFileHandle
|
||||
}
|
||||
|
||||
const result = await readAndDecryptBackupFile(fileHandle, file, fileSystem, this.crypto, async (decryptedBytes) => {
|
||||
await fileSystem.saveBytes(destinationFileHandle, decryptedBytes)
|
||||
})
|
||||
|
||||
await fileSystem.closeFileWriteStream(destinationFileHandle)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
public async readBackupFileBytesDecrypted(
|
||||
fileHandle: FileHandleRead,
|
||||
file: FileItem,
|
||||
fileSystem: FileSystemApi,
|
||||
): Promise<Uint8Array> {
|
||||
let bytes = new Uint8Array()
|
||||
|
||||
await readAndDecryptBackupFile(fileHandle, file, fileSystem, this.crypto, async (decryptedBytes) => {
|
||||
bytes = new Uint8Array([...bytes, ...decryptedBytes])
|
||||
})
|
||||
|
||||
return bytes
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
import { StartUploadSessionResponse, MinimalHttpResponse, ClientDisplayableError } from '@standardnotes/responses'
|
||||
import { FileContent } from '@standardnotes/models'
|
||||
|
||||
export interface FilesApiInterface {
|
||||
startUploadSession(apiToken: string): Promise<StartUploadSessionResponse>
|
||||
|
||||
uploadFileBytes(apiToken: string, chunkId: number, encryptedBytes: Uint8Array): Promise<boolean>
|
||||
|
||||
closeUploadSession(apiToken: string): Promise<boolean>
|
||||
|
||||
downloadFile(
|
||||
file: { encryptedChunkSizes: FileContent['encryptedChunkSizes'] },
|
||||
chunkIndex: number,
|
||||
apiToken: string,
|
||||
contentRangeStart: number,
|
||||
onBytesReceived: (bytes: Uint8Array) => Promise<void>,
|
||||
): Promise<ClientDisplayableError | undefined>
|
||||
|
||||
deleteFile(apiToken: string): Promise<MinimalHttpResponse>
|
||||
|
||||
createFileValetToken(
|
||||
remoteIdentifier: string,
|
||||
operation: 'write' | 'read' | 'delete',
|
||||
unencryptedFileSize?: number,
|
||||
): Promise<string | ClientDisplayableError>
|
||||
|
||||
getFilesDownloadUrl(): string
|
||||
}
|
||||
@@ -8,6 +8,7 @@ export * from './Application/DeinitSource'
|
||||
export * from './Application/DeinitMode'
|
||||
export * from './Application/UserClientInterface'
|
||||
export * from './Application/WebApplicationInterface'
|
||||
export * from './Backups/BackupService'
|
||||
export * from './Challenge'
|
||||
export * from './Component/ComponentManagerInterface'
|
||||
export * from './Component/ComponentViewerError'
|
||||
@@ -33,8 +34,6 @@ export * from './Feature/FeaturesClientInterface'
|
||||
export * from './Feature/FeaturesEvent'
|
||||
export * from './Feature/OfflineSubscriptionEntitlements'
|
||||
export * from './Feature/SetOfflineFeaturesFunctionResponse'
|
||||
export * from './Files/FilesApiInterface'
|
||||
export * from './FileSystem/FileSystemApi'
|
||||
export * from './Integrity/IntegrityApiInterface'
|
||||
export * from './Integrity/IntegrityEvent'
|
||||
export * from './Integrity/IntegrityEventPayload'
|
||||
|
||||
Reference in New Issue
Block a user