Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheodoreKrypton committed May 12, 2024
1 parent 691b6f3 commit c3ff352
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 30 deletions.
7 changes: 5 additions & 2 deletions src/api/client/directory-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ export class DirectoryApi extends MetaDataApi {
return this.getRootDirectory();
}

public async createDirectory(where: { name: string; under: TGFSDirectory }) {
public async createDirectory(
where: { name: string; under: TGFSDirectory },
dir?: TGFSDirectory,
) {
validateName(where.name);

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

return newDirectory;
Expand Down
10 changes: 10 additions & 0 deletions src/api/client/file-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import { DirectoryApi } from './directory-api';
import { GeneralFileMessage } from './message-api/types';

export class FileApi extends DirectoryApi {
public async copyFile(
where: TGFSDirectory,
fr: TGFSFileRef,
name?: string,
): Promise<TGFSFileRef> {
const copiedFR = where.createFileRef(name ?? fr.name, fr.getMessageId());
await this.syncMetadata();
return copiedFR;
}

private async createFile(where: TGFSDirectory, fileMsg: GeneralFileMessage) {
validateName(fileMsg.name);

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

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

export const copyDir =
(client: Client) =>
async (
pathFrom: string,
pathTo: string,
): Promise<{
from: TGFSDirectory;
to: TGFSDirectory;
}> => {
const [basePathFrom, nameFrom] = splitPath(pathFrom);
if (nameFrom === '') {
return;
}

const dir = navigateToDir(client)(basePathFrom);
const dirToCopy = dir.findChildren([nameFrom])[0];

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

const [basePathTo, nameTo] = splitPath(pathTo);
const dir2 = navigateToDir(client)(basePathTo);

const res = await client.createDirectory(
{ name: nameTo ?? nameFrom, under: dir2 },
dirToCopy,
);

return { from: dirToCopy, to: res };
};
33 changes: 33 additions & 0 deletions src/api/ops/copy-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Client } from 'src/api';
import { FileOrDirectoryDoesNotExistError } from 'src/errors/path';
import { TGFSFileRef } from 'src/model/directory';

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

export const copyFile =
(client: Client) =>
async (
pathFrom: string,
pathTo: string,
): Promise<{
from: TGFSFileRef;
to: TGFSFileRef;
}> => {
const [basePathFrom, nameFrom] = splitPath(pathFrom);
const dir = navigateToDir(client)(basePathFrom);
const frToCopy = dir.findFiles([nameFrom])[0];

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

const [basePathTo, nameTo] = splitPath(pathTo);
const dir2 = navigateToDir(client)(basePathTo);

const res = await client.copyFile(dir2, frToCopy, nameTo ?? nameFrom);
return { from: frToCopy, to: res };
};
10 changes: 10 additions & 0 deletions src/api/ops/move-dir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Client } from 'src/api';

import { copyDir } from './copy-dir';

export const moveDir =
(client: Client) => async (pathFrom: string, pathTo: string) => {
const { from, to } = await copyDir(client)(pathFrom, pathTo);

await client.dangerouslyDeleteDirectory(from);
};
29 changes: 3 additions & 26 deletions src/api/ops/move-file.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
import { Client } from 'src/api';
import { FileOrDirectoryDoesNotExistError } from 'src/errors/path';

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

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}`,
);
}
const { from, to } = await copyFile(client)(pathFrom, pathTo);
await client.deleteFile(from);
};
6 changes: 4 additions & 2 deletions src/model/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ export class TGFSDirectory {
return dir;
}

public createDir(name: string) {
public createDir(name: string, dir?: TGFSDirectory) {
if (this.findChildren([name]).length) {
throw new FileOrDirectoryAlreadyExistsError(name);
}
const child = new TGFSDirectory(name, this);
const child = dir
? new TGFSDirectory(name, this, dir.children, dir.files)
: new TGFSDirectory(name, this);
this.children.push(child);
return child;
}
Expand Down

0 comments on commit c3ff352

Please sign in to comment.