Skip to content

Commit

Permalink
support move file
Browse files Browse the repository at this point in the history
  • Loading branch information
TheodoreKrypton committed May 12, 2024
1 parent 0fb1922 commit 691b6f3
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/api/client/directory-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class DirectoryApi extends MetaDataApi {
public async createDirectory(where: { name: string; under: TGFSDirectory }) {
validateName(where.name);

const newDirectory = where.under.createChild(where.name);
const newDirectory = where.under.createDir(where.name);
await this.syncMetadata();

return newDirectory;
Expand Down
8 changes: 4 additions & 4 deletions src/api/client/file-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ export class FileApi extends DirectoryApi {
under: TGFSDirectory;
versionId?: string;
},
file?: GeneralFileMessage,
fileMsg?: GeneralFileMessage,
) {
const fr = where.under.findFiles([file.name])[0];
const fr = where.under.findFiles([fileMsg.name])[0];
if (fr) {
return await this.updateFile(fr, file, where.versionId);
return await this.updateFile(fr, fileMsg, where.versionId);
} else {
return await this.createFile(where.under, file);
return await this.createFile(where.under, fileMsg);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/api/ops/create-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const createDir =
Logger.info(`Creating directory ${path}`);
if (!parents) {
const [basePath, name] = splitPath(path);
const dir = await navigateToDir(client)(basePath);
const dir = navigateToDir(client)(basePath);
return await client.createDirectory({ name: name, under: dir });
} else {
if (!path.startsWith('/')) {
Expand Down
2 changes: 1 addition & 1 deletion src/api/ops/create-empty-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { splitPath } from './utils';
export const createEmptyFile = (client: Client) => async (path: PathLike) => {
let [basePath, name] = splitPath(path);

const dir = await navigateToDir(client)(basePath);
const dir = navigateToDir(client)(basePath);

if (!existsSync(path)) {
return await client.uploadFile({ under: dir }, { name, empty: true });
Expand Down
3 changes: 2 additions & 1 deletion src/api/ops/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { createDir } from './create-dir';
export { list } from './list';
export { moveFile } from './move-file';
export { removeDir } from './remove-dir';
export { removeFile } from './remove-file';
export { uploadFromLocal, uploadFromBytes } from './upload';
export { uploadFromBytes, uploadFromLocal } from './upload';
2 changes: 1 addition & 1 deletion src/api/ops/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const list =
path: PathLike,
): Promise<TGFSFileRef | Array<TGFSFileRef | TGFSDirectory>> => {
const [basePath, name] = splitPath(path);
const dir = await navigateToDir(client)(basePath);
const dir = navigateToDir(client)(basePath);

let nextDir = dir;

Expand Down
32 changes: 32 additions & 0 deletions src/api/ops/move-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Client } from 'src/api';
import { FileOrDirectoryDoesNotExistError } from 'src/errors/path';

import { navigateToDir } from './navigate-to-dir';
import { splitPath } from './utils';

export const moveFile =
(client: Client) => async (pathFrom: string, pathTo: string) => {
const [dirFrom, nameFrom] = splitPath(pathFrom);
const dir = navigateToDir(client)(dirFrom);
const fileRef = dir.findFiles([nameFrom])[0];

if (!fileRef) {
throw new FileOrDirectoryDoesNotExistError(
pathFrom,
`move file from ${pathFrom} to ${pathTo}`,
);
}

const [dirTo, nameTo] = splitPath(pathTo);

try {
const dir2 = navigateToDir(client)(dirTo);
dir2.createFileRef(nameTo, fileRef.getMessageId());
await client.deleteFile(fileRef);
} catch (err) {
throw new FileOrDirectoryDoesNotExistError(
dirTo,
`move file from ${pathFrom} to ${pathTo}`,
);
}
};
2 changes: 1 addition & 1 deletion src/api/ops/navigate-to-dir.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Client } from 'src/api';
import { FileOrDirectoryDoesNotExistError } from 'src/errors/path';

export const navigateToDir = (client: Client) => async (path: string) => {
export const navigateToDir = (client: Client) => (path: string) => {
const pathParts = path
.toString()
.split('/')
Expand Down
2 changes: 1 addition & 1 deletion src/api/ops/remove-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { splitPath } from './utils';
export const removeDir =
(client: Client) => async (path: string, recursive: boolean) => {
const [basePath, name] = splitPath(path);
const dir = await navigateToDir(client)(basePath);
const dir = navigateToDir(client)(basePath);
if (!recursive) {
const child = dir.findChildren([name])[0];
if (child) {
Expand Down
2 changes: 1 addition & 1 deletion src/api/ops/remove-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { splitPath } from './utils';

export const removeFile = (client: Client) => async (path: string) => {
const [basePath, name] = splitPath(path);
const dir = await navigateToDir(client)(basePath);
const dir = navigateToDir(client)(basePath);
const fileRef = dir.findFiles([name])[0];
if (fileRef) {
await client.deleteFile(fileRef);
Expand Down
6 changes: 3 additions & 3 deletions src/api/ops/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const uploadFromLocal =
(client: Client) => async (local: fs.PathLike, remote: fs.PathLike) => {
let [basePath, name] = splitPath(remote);

const dir = await navigateToDir(client)(basePath);
const dir = navigateToDir(client)(basePath);

if (!fs.existsSync(local.toString())) {
const path = local.toString();
Expand All @@ -28,7 +28,7 @@ export const uploadFromBytes =
(client: Client) => async (bytes: Buffer, remote: fs.PathLike) => {
let [basePath, name] = splitPath(remote);

const dir = await navigateToDir(client)(basePath);
const dir = navigateToDir(client)(basePath);

return await client.uploadFile({ under: dir }, { name, buffer: bytes });
};
Expand All @@ -38,7 +38,7 @@ export const uploadFromStream =
async (stream: Readable, size: number, remote: fs.PathLike) => {
let [basePath, name] = splitPath(remote);

const dir = await navigateToDir(client)(basePath);
const dir = navigateToDir(client)(basePath);

return await client.uploadFile({ under: dir }, { name, stream, size });
};
2 changes: 1 addition & 1 deletion src/model/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class TGFSDirectory {
return dir;
}

public createChild(name: string) {
public createDir(name: string) {
if (this.findChildren([name]).length) {
throw new FileOrDirectoryAlreadyExistsError(name);
}
Expand Down
20 changes: 19 additions & 1 deletion src/server/webdav/tgfs-filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
LocalLockManager,
LocalPropertyManager,
LockManagerInfo,
MoveInfo,
OpenReadStreamInfo,
OpenWriteStreamInfo,
Path,
Expand All @@ -26,7 +27,7 @@ import {
} from 'webdav-server/lib/index.v2';

import { Client, createClient } from 'src/api';
import { createDir, list, removeDir, removeFile } from 'src/api/ops';
import { createDir, list, moveFile, removeDir, removeFile } from 'src/api/ops';
import { createEmptyFile } from 'src/api/ops/create-empty-file';
import { uploadFromStream } from 'src/api/ops/upload';
import { BusinessError } from 'src/errors/base';
Expand Down Expand Up @@ -95,6 +96,23 @@ export class TGFSFileSystem extends FileSystem {
}
}

protected _move(
pathFrom: Path,
pathTo: Path,
ctx: MoveInfo,
callback: ReturnCallback<boolean>,
): void {
(async () => {
try {
await moveFile(this.tgClient)(pathFrom.toString(), pathTo.toString());
callback(null, true);
} catch (err) {
handleError(callback)(err);
Logger.error(err);
}
})();
}

protected _delete(
path: Path,
ctx: DeleteInfo,
Expand Down
4 changes: 2 additions & 2 deletions test/api/model/serialization-and-deseriealization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TGFSDirectory } from 'src/model/directory';
describe('serialization and deserialization', () => {
it('should equal to the original directory', () => {
const d1 = new TGFSDirectory('d1', null);
const d2 = d1.createChild('d2');
const d2 = d1.createDir('d2');
d2.createFileRef('f1', 1);

expect(TGFSDirectory.fromObject(d1.toObject()).toObject()).toEqual(
Expand All @@ -13,7 +13,7 @@ describe('serialization and deserialization', () => {

it('should equal to the expected structure', () => {
const d1 = new TGFSDirectory('d1', null);
const d2 = d1.createChild('d2');
const d2 = d1.createDir('d2');
d2.createFileRef('f1', 1);

expect(d1.toObject()).toEqual({
Expand Down

0 comments on commit 691b6f3

Please sign in to comment.