-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
691b6f3
commit c3ff352
Showing
7 changed files
with
106 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters