From 9164b200da0775b7c7db6e7c6b692dc1fadf58eb Mon Sep 17 00:00:00 2001 From: Wheat Carrier Date: Wed, 10 Jul 2024 00:58:00 +0800 Subject: [PATCH] refactor api --- src/api/client/directory-api.ts | 12 ++- src/api/client/file-desc-api.ts | 9 +- src/api/client/file-ref-api.ts | 41 ++++++-- src/api/client/index.ts | 95 +++---------------- .../client/repository/impl/metadata/tg-msg.ts | 2 +- src/api/ops/copy-dir.ts | 2 +- src/api/ops/copy-file.ts | 2 +- src/api/ops/create-dir.ts | 6 +- src/api/ops/create-empty-file.ts | 2 +- src/api/ops/list.ts | 32 +++---- src/api/ops/move-dir.ts | 2 +- src/api/ops/move-file.ts | 2 +- src/api/ops/navigate-to-dir.ts | 2 +- src/api/ops/remove-dir.ts | 4 +- src/api/ops/remove-file.ts | 2 +- src/api/ops/upload.ts | 6 +- src/commands/ls.ts | 2 +- src/server/webdav/tgfs-filesystem.ts | 9 +- test/api/model/operations.spec.ts | 94 +++++++++--------- 19 files changed, 140 insertions(+), 186 deletions(-) diff --git a/src/api/client/directory-api.ts b/src/api/client/directory-api.ts index 159850b..90881fc 100644 --- a/src/api/client/directory-api.ts +++ b/src/api/client/directory-api.ts @@ -10,9 +10,11 @@ import { MetaDataApi } from './metadata-api'; export class DirectoryApi { constructor(private metadataApi: MetaDataApi) {} + public root() { + return this.metadataApi.getRootDirectory(); + } - - public async createDirectory( + public async create( where: { name: string; under: TGFSDirectory }, dir?: TGFSDirectory, ) { @@ -38,14 +40,14 @@ export class DirectoryApi { return [...dir.findDirs(), ...dir.findFiles()]; } - public async deleteEmptyDirectory(directory: TGFSDirectory) { + public async rmEmpty(directory: TGFSDirectory) { if (directory.findDirs().length > 0 || directory.findFiles().length > 0) { throw new DirectoryIsNotEmptyError(); } - await this.dangerouslyDeleteDirectory(directory); + await this.rmDangerously(directory); } - public async dangerouslyDeleteDirectory(directory: TGFSDirectory) { + public async rmDangerously(directory: TGFSDirectory) { directory.delete(); await this.metadataApi.syncMetadata(); } diff --git a/src/api/client/file-desc-api.ts b/src/api/client/file-desc-api.ts index 9cb8a50..852ed11 100644 --- a/src/api/client/file-desc-api.ts +++ b/src/api/client/file-desc-api.ts @@ -1,6 +1,6 @@ import { FileDescAPIResponse } from 'src/api/client/model'; import { TGFSFileRef } from 'src/model/directory'; -import { TGFSFile } from 'src/model/file'; +import { TGFSFile, TGFSFileVersion } from 'src/model/file'; import { GeneralFileMessage, isFileMessageEmpty } from './model'; import { FileRepository } from './repository/impl/file'; @@ -31,6 +31,13 @@ export class FileDescApi { return await this.fdRepo.get(fr); } + public async *downloadFileAtVersion( + asName: string, + version: TGFSFileVersion, + ): AsyncGenerator { + return this.fileRepo.downloadFile(asName, version.messageId); + } + public async addFileVersion( fr: TGFSFileRef, fileMsg: GeneralFileMessage, diff --git a/src/api/client/file-ref-api.ts b/src/api/client/file-ref-api.ts index 19d9f6c..3f1bcf4 100644 --- a/src/api/client/file-ref-api.ts +++ b/src/api/client/file-ref-api.ts @@ -1,5 +1,5 @@ import { TGFSDirectory, TGFSFileRef } from 'src/model/directory'; -import { TGFSFile } from 'src/model/file'; +import { TGFSFile, TGFSFileVersion } from 'src/model/file'; import { validateName } from 'src/utils/validate-name'; import { FileDescApi } from './file-desc-api'; @@ -12,7 +12,7 @@ export class FileRefApi { private readonly fileDescApi: FileDescApi, ) {} - public async copyFile( + public async copy( where: TGFSDirectory, fr: TGFSFileRef, name?: string, @@ -22,7 +22,7 @@ export class FileRefApi { return copiedFR; } - private async createFile( + private async create( where: TGFSDirectory, fileMsg: GeneralFileMessage, ): Promise { @@ -45,7 +45,7 @@ export class FileRefApi { } } - private async updateFile( + private async update( fr: TGFSFileRef, fileMsg: GeneralFileMessage, versionId?: string, @@ -57,7 +57,7 @@ export class FileRefApi { return fd; } - public async deleteFile(fr: TGFSFileRef, version?: string): Promise { + public async rm(fr: TGFSFileRef, version?: string): Promise { if (!version) { fr.delete(); await this.metadataApi.syncMetadata(); @@ -70,7 +70,7 @@ export class FileRefApi { } } - public async uploadFile( + public async upload( where: { under: TGFSDirectory; versionId?: string; @@ -79,9 +79,34 @@ export class FileRefApi { ): Promise { const fr = where.under.findFiles([fileMsg.name])[0]; if (fr) { - return await this.updateFile(fr, fileMsg, where.versionId); + return await this.update(fr, fileMsg, where.versionId); } else { - return await this.createFile(where.under, fileMsg); + return await this.create(where.under, fileMsg); } } + + public async *retrieve( + fr: TGFSFileRef, + asName?: string, + ): AsyncGenerator { + const fd = await this.desc(fr); + + if (fd.isEmptyFile()) { + yield Buffer.from(''); + } else { + const version = fd.getLatest(); + yield* this.fileDescApi.downloadFileAtVersion(asName ?? fr.name, version); + } + } + + public async *retrieveVersion( + version: TGFSFileVersion, + asName: string, + ): AsyncGenerator { + yield* this.fileDescApi.downloadFileAtVersion(asName, version); + } + + public async desc(fr: TGFSFileRef): Promise { + return await this.fileDescApi.getFileDesc(fr); + } } diff --git a/src/api/client/index.ts b/src/api/client/index.ts index 51e683f..988282d 100644 --- a/src/api/client/index.ts +++ b/src/api/client/index.ts @@ -1,32 +1,27 @@ import { gramjs, telegraf } from 'src/api/impl'; import { config } from 'src/config'; -import { TGFSDirectory, TGFSFileRef } from 'src/model/directory'; -import { TGFSFile } from 'src/model/file'; import { DirectoryApi } from './directory-api'; import { FileDescApi } from './file-desc-api'; import { FileRefApi } from './file-ref-api'; import { MessageApi } from './message-api'; import { MetaDataApi } from './metadata-api'; -import { GeneralFileMessage } from './model'; import { TGMsgFDRepository } from './repository/impl/fd/tg-msg'; import { FileRepository } from './repository/impl/file'; -import { MetadataRepository } from './repository/impl/metadata/tg-msg'; +import { JSONMetadataRepository } from './repository/impl/metadata/tg-msg'; export const createClient = async () => { - const api = { - tdlib: { + const msgApi = new MessageApi( + { account: new gramjs.GramJSApi(await gramjs.loginAsAccount(config)), bot: new gramjs.GramJSApi(await gramjs.loginAsBot(config)), }, - bot: new telegraf.TelegrafApi(telegraf.createBot(config)), - }; - - const msgApi = new MessageApi(api.tdlib, api.bot); + new telegraf.TelegrafApi(telegraf.createBot(config)), + ); const fileRepo = new FileRepository(msgApi); const fdRepo = new TGMsgFDRepository(msgApi); - const metadataRepo = new MetadataRepository(msgApi, fileRepo); + const metadataRepo = new JSONMetadataRepository(msgApi, fileRepo); const fdApi = new FileDescApi(fdRepo, fileRepo); @@ -36,85 +31,19 @@ export const createClient = async () => { const frApi = new FileRefApi(metadataApi, fdApi); const dirApi = new DirectoryApi(metadataApi); - return new Client(metadataApi, frApi, fdApi, dirApi, fileRepo); + return new Client(metadataApi, frApi, dirApi); }; export class Client { + file: FileRefApi; + dir: DirectoryApi; + constructor( private readonly metadataApi: MetaDataApi, private readonly frApi: FileRefApi, - private readonly fdApi: FileDescApi, private readonly dirApi: DirectoryApi, - private readonly fileRepo: FileRepository, - ) {} - - public async *downloadLatestVersion( - fr: TGFSFileRef, - asName: string, - ): AsyncGenerator { - const fd = await this.fdApi.getFileDesc(fr); - - if (fd.isEmptyFile()) { - yield Buffer.from(''); - } else { - const version = fd.getLatest(); - yield* this.fileRepo.downloadFile(asName, version.messageId); - } - } - - public async getFileDesc(fr: TGFSFileRef) { - return this.fdApi.getFileDesc(fr); - } - - public getRootDirectory() { - return this.metadataApi.getRootDirectory(); - } - - public async createDirectory( - where: { name: string; under: TGFSDirectory }, - dir?: TGFSDirectory, ) { - return this.dirApi.createDirectory(where, dir); - } - - public findDirs(dir: TGFSDirectory) { - return dir.findDirs(); - } - - public async copyFile( - where: TGFSDirectory, - fr: TGFSFileRef, - name?: string, - ): Promise { - return this.frApi.copyFile(where, fr, name); - } - - public async uploadFile( - where: { - under: TGFSDirectory; - versionId?: string; - }, - fileMsg?: GeneralFileMessage, - ): Promise { - return this.frApi.uploadFile(where, fileMsg); - } - - public async ls( - dir: TGFSDirectory, - fileName?: string, - ): Promise> { - return this.dirApi.ls(dir, fileName); - } - - public async deleteEmptyDirectory(directory: TGFSDirectory) { - return this.dirApi.deleteEmptyDirectory(directory); - } - - public async dangerouslyDeleteDirectory(directory: TGFSDirectory) { - return this.dirApi.dangerouslyDeleteDirectory(directory); - } - - public async deleteFile(fr: TGFSFileRef, version?: string) { - return this.frApi.deleteFile(fr, version); + this.file = this.frApi; + this.dir = this.dirApi; } } diff --git a/src/api/client/repository/impl/metadata/tg-msg.ts b/src/api/client/repository/impl/metadata/tg-msg.ts index 89579db..5c3bbb9 100644 --- a/src/api/client/repository/impl/metadata/tg-msg.ts +++ b/src/api/client/repository/impl/metadata/tg-msg.ts @@ -5,7 +5,7 @@ import { TGFSMetadata } from 'src/model/metadata'; import { FileRepository } from '../file'; -export class MetadataRepository implements IMetaDataRepository { +export class JSONMetadataRepository implements IMetaDataRepository { constructor( private readonly msgApi: MessageApi, private readonly fileRepo: FileRepository, diff --git a/src/api/ops/copy-dir.ts b/src/api/ops/copy-dir.ts index 2d7aa6c..d6a94ec 100644 --- a/src/api/ops/copy-dir.ts +++ b/src/api/ops/copy-dir.ts @@ -32,7 +32,7 @@ export const copyDir = const [basePathTo, nameTo] = splitPath(pathTo); const dir2 = navigateToDir(client)(basePathTo); - const res = await client.createDirectory( + const res = await client.dir.create( { name: nameTo ?? nameFrom, under: dir2 }, dirToCopy, ); diff --git a/src/api/ops/copy-file.ts b/src/api/ops/copy-file.ts index 6630f2d..13f444f 100644 --- a/src/api/ops/copy-file.ts +++ b/src/api/ops/copy-file.ts @@ -28,6 +28,6 @@ export const copyFile = const [basePathTo, nameTo] = splitPath(pathTo); const dir2 = navigateToDir(client)(basePathTo); - const res = await client.copyFile(dir2, frToCopy, nameTo ?? nameFrom); + const res = await client.file.copy(dir2, frToCopy, nameTo ?? nameFrom); return { from: frToCopy, to: res }; }; diff --git a/src/api/ops/create-dir.ts b/src/api/ops/create-dir.ts index cd72431..c63042c 100644 --- a/src/api/ops/create-dir.ts +++ b/src/api/ops/create-dir.ts @@ -11,14 +11,14 @@ export const createDir = if (!parents) { const [basePath, name] = splitPath(path); const dir = navigateToDir(client)(basePath); - return await client.createDirectory({ name: name, under: dir }); + return await client.dir.create({ name: name, under: dir }); } else { if (!path.startsWith('/')) { throw new RelativePathError(path); } const paths = path.split('/').filter((p) => p); - let currentDir = client.getRootDirectory(); + let currentDir = client.dir.root(); for (const p of paths) { const children = currentDir.findDirs([p]); if (children.length > 0) { @@ -26,7 +26,7 @@ export const createDir = continue; } - const dir = await client.createDirectory({ + const dir = await client.dir.create({ name: p, under: currentDir, }); diff --git a/src/api/ops/create-empty-file.ts b/src/api/ops/create-empty-file.ts index 0d33b93..75b3ad1 100644 --- a/src/api/ops/create-empty-file.ts +++ b/src/api/ops/create-empty-file.ts @@ -10,6 +10,6 @@ export const createEmptyFile = (client: Client) => async (path: PathLike) => { const dir = navigateToDir(client)(basePath); if (!existsSync(path)) { - return await client.uploadFile({ under: dir }, { name, empty: true }); + return await client.file.upload({ under: dir }, { name, empty: true }); } }; diff --git a/src/api/ops/list.ts b/src/api/ops/list.ts index bc4d6fa..89dfce6 100644 --- a/src/api/ops/list.ts +++ b/src/api/ops/list.ts @@ -8,21 +8,21 @@ import { splitPath } from './utils'; export const list = (client: Client) => - async ( - path: PathLike, - ): Promise> => { - const [basePath, name] = splitPath(path); - const dir = navigateToDir(client)(basePath); + async ( + path: PathLike, + ): Promise> => { + const [basePath, name] = splitPath(path); + const dir = navigateToDir(client)(basePath); - let nextDir = dir; + let nextDir = dir; - if (name) { - nextDir = dir.findDir(name); - } - if (nextDir) { - return client.ls(nextDir); - } else { - // cannot find a sub-directory with the given name, so assume it's a file - return client.ls(dir, name); - } - }; + if (name) { + nextDir = dir.findDir(name); + } + if (nextDir) { + return client.dir.ls(nextDir); + } else { + // cannot find a sub-directory with the given name, so assume it's a file + return client.dir.ls(dir, name); + } + }; diff --git a/src/api/ops/move-dir.ts b/src/api/ops/move-dir.ts index 9b55cd3..a7b6331 100644 --- a/src/api/ops/move-dir.ts +++ b/src/api/ops/move-dir.ts @@ -6,5 +6,5 @@ export const moveDir = (client: Client) => async (pathFrom: string, pathTo: string) => { const { from, to } = await copyDir(client)(pathFrom, pathTo); - await client.dangerouslyDeleteDirectory(from); + await client.dir.rmDangerously(from); }; diff --git a/src/api/ops/move-file.ts b/src/api/ops/move-file.ts index 4176e3b..43b82d4 100644 --- a/src/api/ops/move-file.ts +++ b/src/api/ops/move-file.ts @@ -5,5 +5,5 @@ import { copyFile } from './copy-file'; export const moveFile = (client: Client) => async (pathFrom: string, pathTo: string) => { const { from, to } = await copyFile(client)(pathFrom, pathTo); - await client.deleteFile(from); + await client.file.rm(from); }; diff --git a/src/api/ops/navigate-to-dir.ts b/src/api/ops/navigate-to-dir.ts index 5041fc8..4811df0 100644 --- a/src/api/ops/navigate-to-dir.ts +++ b/src/api/ops/navigate-to-dir.ts @@ -7,7 +7,7 @@ export const navigateToDir = (client: Client) => (path: string) => { .split('/') .filter((part) => part !== ''); - let currentDirectory = client.getRootDirectory(); + let currentDirectory = client.dir.root(); for (const pathPart of pathParts) { const directory = currentDirectory.findDirs([pathPart])[0]; diff --git a/src/api/ops/remove-dir.ts b/src/api/ops/remove-dir.ts index fe31994..71dbb73 100644 --- a/src/api/ops/remove-dir.ts +++ b/src/api/ops/remove-dir.ts @@ -11,12 +11,12 @@ export const removeDir = if (!recursive) { const child = dir.findDirs([name])[0]; if (child) { - await client.deleteEmptyDirectory(child); + await client.dir.rmEmpty(child); } else { throw new FileOrDirectoryDoesNotExistError(path, `remove dir ${path}`); } } else { const nextDir = name ? dir.findDirs([name])[0] : dir; - await client.dangerouslyDeleteDirectory(nextDir); + await client.dir.rmDangerously(nextDir); } }; diff --git a/src/api/ops/remove-file.ts b/src/api/ops/remove-file.ts index 473e304..21538c1 100644 --- a/src/api/ops/remove-file.ts +++ b/src/api/ops/remove-file.ts @@ -9,7 +9,7 @@ export const removeFile = (client: Client) => async (path: string) => { const dir = navigateToDir(client)(basePath); const fileRef = dir.findFiles([name])[0]; if (fileRef) { - await client.deleteFile(fileRef); + await client.file.rm(fileRef); } else { throw new FileOrDirectoryDoesNotExistError(path, `remove file ${path}`); } diff --git a/src/api/ops/upload.ts b/src/api/ops/upload.ts index c84906e..9aca1ec 100644 --- a/src/api/ops/upload.ts +++ b/src/api/ops/upload.ts @@ -18,7 +18,7 @@ export const uploadFromLocal = throw new FileOrDirectoryDoesNotExistError(path, `upload from ${path}`); } - return await client.uploadFile( + return await client.file.upload( { under: dir }, { name, path: local.toString() }, ); @@ -30,7 +30,7 @@ export const uploadFromBytes = const dir = navigateToDir(client)(basePath); - return await client.uploadFile({ under: dir }, { name, buffer: bytes }); + return await client.file.upload({ under: dir }, { name, buffer: bytes }); }; export const uploadFromStream = @@ -40,5 +40,5 @@ export const uploadFromStream = const dir = navigateToDir(client)(basePath); - return await client.uploadFile({ under: dir }, { name, stream, size }); + return await client.file.upload({ under: dir }, { name, stream, size }); }; diff --git a/src/commands/ls.ts b/src/commands/ls.ts index eba016b..0067e4e 100644 --- a/src/commands/ls.ts +++ b/src/commands/ls.ts @@ -16,7 +16,7 @@ export const ls = (client: Client) => async (argv: { path: PathLike }) => { }) .join(' '); } else { - const fd = await client.getFileDesc(res); + const fd = await client.file.desc(res); return fileInfo(client, fd); } }; diff --git a/src/server/webdav/tgfs-filesystem.ts b/src/server/webdav/tgfs-filesystem.ts index a6ef4cf..367c211 100644 --- a/src/server/webdav/tgfs-filesystem.ts +++ b/src/server/webdav/tgfs-filesystem.ts @@ -152,7 +152,7 @@ export class TGFSFileSystem extends VirtualFileSystem { ): Promise { const res = await list(this.tgClient)(path.toString()); if (res instanceof TGFSFileRef) { - const fd = await this.tgClient.getFileDesc(res); + const fd = await this.tgClient.file.desc(res); this.resources[path.toString()] = TGFSFileResource.fromFileDesc(fd); } else { this.resources[path.toString()] = new TGFSDirResource(); @@ -169,7 +169,7 @@ export class TGFSFileSystem extends VirtualFileSystem { .filter((res) => res instanceof TGFSFileRef) .map((fr) => { return (async () => { - const fd = await this.tgClient.getFileDesc(fr as TGFSFileRef); + const fd = await this.tgClient.file.desc(fr as TGFSFileRef); if (path.toString() === '/') { this.resources[`/${fr.name}`] = TGFSFileResource.fromFileDesc(fd); } else { @@ -315,10 +315,7 @@ export class TGFSFileSystem extends VirtualFileSystem { const fileRef = (await list(this.tgClient)( path.toString(), )) as TGFSFileRef; - const chunks = this.tgClient.downloadLatestVersion( - fileRef, - fileRef.name, - ); + const chunks = this.tgClient.file.retrieve(fileRef, fileRef.name); callback(null, Readable.from(chunks)); } catch (err) { diff --git a/test/api/model/operations.spec.ts b/test/api/model/operations.spec.ts index 606aefa..3f49ec1 100644 --- a/test/api/model/operations.spec.ts +++ b/test/api/model/operations.spec.ts @@ -20,38 +20,38 @@ describe('file and directory operations', () => { }); it('should create a directory', async () => { - const root = client.getRootDirectory(); - const d1 = await client.createDirectory({ name: 'd1', under: root }); + const root = client.dir.root(); + const d1 = await client.dir.create({ name: 'd1', under: root }); expect(root.findDirs(['d1'])[0]).toEqual(d1); }); it('should throw an error if the directory name is illegal', async () => { - const root = client.getRootDirectory(); + const root = client.dir.root(); await expect( - client.createDirectory({ name: '-d1', under: root }), + client.dir.create({ name: '-d1', under: root }), ).rejects.toThrow(); await expect( - client.createDirectory({ name: 'd/1', under: root }), + client.dir.create({ name: 'd/1', under: root }), ).rejects.toThrow(); }); it('should remove a directory', async () => { - const root = client.getRootDirectory(); + const root = client.dir.root(); - const d1 = await client.createDirectory({ name: 'd1', under: root }); - await client.dangerouslyDeleteDirectory(d1); + const d1 = await client.dir.create({ name: 'd1', under: root }); + await client.dir.rmDangerously(d1); expect(root.findDirs(['d1'])[0]).toBeUndefined(); }); it('should remove all directories', async () => { - const d1 = await client.createDirectory({ + const d1 = await client.dir.create({ name: 'd1', - under: client.getRootDirectory(), + under: client.dir.root(), }); - await client.createDirectory({ name: 'd2', under: d1 }); - await client.dangerouslyDeleteDirectory(client.getRootDirectory()); - expect(client.getRootDirectory().findDirs(['d1'])[0]).toBeUndefined(); + await client.dir.create({ name: 'd2', under: d1 }); + await client.dir.rmDangerously(client.dir.root()); + expect(client.dir.root().findDirs(['d1'])[0]).toBeUndefined(); }); }); @@ -63,8 +63,8 @@ describe('file and directory operations', () => { }); it('should create a small file from buffer', async () => { - const root = client.getRootDirectory(); - const f1 = await client.uploadFile( + const root = client.dir.root(); + const f1 = await client.file.upload( { under: root }, { name: 'f1', buffer: Buffer.from('mock-file-content') }, ); @@ -75,8 +75,8 @@ describe('file and directory operations', () => { const fileName = `${Math.random()}.txt`; fs.writeFileSync(fileName, 'mock-file-content'); - const root = client.getRootDirectory(); - const f1 = await client.uploadFile( + const root = client.dir.root(); + const f1 = await client.file.upload( { under: root }, { name: 'f1', path: fileName }, ); @@ -88,9 +88,9 @@ describe('file and directory operations', () => { it('should create a big file from buffer', async () => { const content = Buffer.alloc(1024 * 1024 * 10, 'a'); - const root = client.getRootDirectory(); + const root = client.dir.root(); - const f1 = await client.uploadFile( + const f1 = await client.file.upload( { under: root }, { name: 'f1', buffer: content }, ); @@ -102,9 +102,9 @@ describe('file and directory operations', () => { const content = Buffer.alloc(1024 * 1024 * 10, 'a'); fs.writeFileSync(fileName, content); - const root = client.getRootDirectory(); + const root = client.dir.root(); - const f1 = await client.uploadFile( + const f1 = await client.file.upload( { under: root }, { name: 'f1', path: fileName }, ); @@ -114,47 +114,43 @@ describe('file and directory operations', () => { }); it('should add a file version', async () => { - const root = client.getRootDirectory(); - await client.uploadFile( + const root = client.dir.root(); + await client.file.upload( { under: root }, { name: 'f1', buffer: Buffer.from('mock-file-content') }, ); await sleep(300); // wait for the timestamp to change to ensure the order of versions const content2 = 'mock-file-content-edited'; - await client.uploadFile( + await client.file.upload( { under: root }, { name: 'f1', buffer: Buffer.from(content2) }, ); const fr = root.findFiles(['f1'])[0]; - const fd = await client.getFileDesc(fr); + const fd = await client.file.desc(fr); expect(Object.keys(fd.versions)).toHaveLength(2); - const content = await saveToBuffer( - client.downloadLatestVersion(fr, 'f1'), - ); + const content = await saveToBuffer(client.file.retrieve(fr, 'f1')); expect(content.toString()).toEqual(content2); }); it('should edit a file version', async () => { - const root = client.getRootDirectory(); + const root = client.dir.root(); - await client.uploadFile( + await client.file.upload( { under: root }, { name: 'f1', buffer: Buffer.from('mock-file-content') }, ); const content2 = 'mock-file-content-edited'; let fr = root.findFiles(['f1'])[0]; - const fd = await client.getFileDesc(fr); + const fd = await client.file.desc(fr); - await client.uploadFile( + await client.file.upload( { under: root, versionId: fd.latestVersionId }, { name: 'f1', buffer: Buffer.from(content2) }, ); - const content = await saveToBuffer( - client.downloadLatestVersion(fr, 'f1'), - ); + const content = await saveToBuffer(client.file.retrieve(fr, 'f1')); expect(content.toString()).toEqual(content2); }); @@ -176,48 +172,46 @@ describe('file and directory operations', () => { // }); it('should remove a file', async () => { - const root = client.getRootDirectory(); + const root = client.dir.root(); - const f1 = await client.uploadFile( + const f1 = await client.file.upload( { under: root }, { name: 'f1', buffer: Buffer.from('mock-file-content') }, ); const fr = root.findFiles(['f1'])[0]; - await client.deleteFile(fr); + await client.file.rm(fr); expect(root.findFiles(['f1'])[0]).toBeUndefined(); }); it('should remove a file version', async () => { - const root = client.getRootDirectory(); + const root = client.dir.root(); const content = 'mock-file-content'; - await client.uploadFile( + await client.file.upload( { under: root }, { name: 'f1', buffer: Buffer.from(content) }, ); await sleep(300); - await client.uploadFile( + await client.file.upload( { under: root }, { name: 'f1', buffer: Buffer.from('mock-file-content-edited') }, ); const fr = root.findFiles(['f1'])[0]; - let fd = await client.getFileDesc(fr); + let fd = await client.file.desc(fr); - await client.deleteFile(fr, fd.latestVersionId); + await client.file.rm(fr, fd.latestVersionId); - fd = await client.getFileDesc(fr); + fd = await client.file.desc(fr); expect(Object.keys(fd.versions)).toHaveLength(1); - const content2 = await saveToBuffer( - client.downloadLatestVersion(fr, 'f1'), - ); + const content2 = await saveToBuffer(client.file.retrieve(fr, 'f1')); expect(content2.toString()).toEqual(content); }); it('should download a file as a local file', async () => { - const root = client.getRootDirectory(); + const root = client.dir.root(); const content = 'mock-file-content'; - await client.uploadFile( + await client.file.upload( { under: root }, { name: 'f1', buffer: Buffer.from(content) }, ); @@ -225,7 +219,7 @@ describe('file and directory operations', () => { const fr = root.findFiles(['f1'])[0]; const localFileName = `${Math.random()}.txt`; - await saveToFile(client.downloadLatestVersion(fr, 'f1'), localFileName); + await saveToFile(client.file.retrieve(fr, 'f1'), localFileName); const contentRead = fs.readFileSync(localFileName); expect(contentRead.toString()).toEqual(content);