diff options
Diffstat (limited to 'frontend/lib/drive_server.ts')
| -rw-r--r-- | frontend/lib/drive_server.ts | 83 |
1 files changed, 21 insertions, 62 deletions
diff --git a/frontend/lib/drive_server.ts b/frontend/lib/drive_server.ts index e7c88e8..1462f6e 100644 --- a/frontend/lib/drive_server.ts +++ b/frontend/lib/drive_server.ts | |||
| @@ -1,11 +1,21 @@ | |||
| 1 | import { spawnSync } from 'child_process' | 1 | import { spawnSync } from 'child_process' |
| 2 | import { DriveLsEntry, DriveLogEntry } from './drive_types' | 2 | import { DriveLsEntry, DriveLogEntry } from './drive_types' |
| 3 | import { Drive_split_path, Drive_basename } from './drive_shared' | ||
| 4 | 3 | ||
| 5 | /** | 4 | /** |
| 6 | * Server-only drive functions that use Node.js APIs | 5 | * Server-only drive functions that use Node.js APIs |
| 7 | */ | 6 | */ |
| 8 | 7 | ||
| 8 | function executeFctDriveCommand(args: string[]): string { | ||
| 9 | const result = spawnSync('fctdrive', args, { encoding: 'utf-8', maxBuffer: 50 * 1024 * 1024 }) | ||
| 10 | if (result.error) { | ||
| 11 | throw new Error(`Failed to execute fctdrive: ${result.error.message}`) | ||
| 12 | } | ||
| 13 | if (result.status !== 0) { | ||
| 14 | throw new Error(`fctdrive exited with code ${result.status}: ${result.stderr}`) | ||
| 15 | } | ||
| 16 | return result.stdout | ||
| 17 | } | ||
| 18 | |||
| 9 | /// lists the given path on the drive | 19 | /// lists the given path on the drive |
| 10 | export async function Drive_ls(path: string, recursive: boolean): Promise<DriveLsEntry[]> { | 20 | export async function Drive_ls(path: string, recursive: boolean): Promise<DriveLsEntry[]> { |
| 11 | const args = ['ls'] | 21 | const args = ['ls'] |
| @@ -16,14 +26,7 @@ export async function Drive_ls(path: string, recursive: boolean): Promise<DriveL | |||
| 16 | args.push(path) | 26 | args.push(path) |
| 17 | } | 27 | } |
| 18 | 28 | ||
| 19 | const result = spawnSync('fctdrive', args, { encoding: 'utf-8', maxBuffer: 50 * 1024 * 1024 }) | 29 | const stdout = executeFctDriveCommand(args) |
| 20 | if (result.error) { | ||
| 21 | throw new Error(`Failed to execute fctdrive: ${result.error.message}`) | ||
| 22 | } | ||
| 23 | if (result.status !== 0) { | ||
| 24 | throw new Error(`fctdrive exited with code ${result.status}: ${result.stderr}`) | ||
| 25 | } | ||
| 26 | const stdout = result.stdout | ||
| 27 | const entries = [] | 30 | const entries = [] |
| 28 | for (const line of stdout.split('\n')) { | 31 | for (const line of stdout.split('\n')) { |
| 29 | if (line.trim() == "") | 32 | if (line.trim() == "") |
| @@ -54,70 +57,34 @@ export async function Drive_ls(path: string, recursive: boolean): Promise<DriveL | |||
| 54 | 57 | ||
| 55 | /// import the file at local_path by moving it to drive_path | 58 | /// import the file at local_path by moving it to drive_path |
| 56 | export async function Drive_import(local_path: string, drive_path: string, email: string) { | 59 | export async function Drive_import(local_path: string, drive_path: string, email: string) { |
| 57 | const result = spawnSync('fctdrive', ['import', local_path, "--mode", "move", "--destination", drive_path, "--email", email], { encoding: 'utf-8' }); | 60 | executeFctDriveCommand(['import', local_path, "--mode", "move", "--destination", drive_path, "--email", email]); |
| 58 | if (result.error) { | ||
| 59 | throw new Error(`Failed to execute fctdrive: ${result.error.message}`); | ||
| 60 | } | ||
| 61 | if (result.status !== 0) { | ||
| 62 | throw new Error(`fctdrive exited with code ${result.status}: ${result.stderr}`); | ||
| 63 | } | ||
| 64 | } | 61 | } |
| 65 | 62 | ||
| 66 | /// returns the full filesystem path of the blob given its id | 63 | /// returns the full filesystem path of the blob given its id |
| 67 | export async function Drive_blob_path(blob: string): Promise<string> { | 64 | export async function Drive_blob_path(blob: string): Promise<string> { |
| 68 | const result = spawnSync('fctdrive', ['blob', blob], { encoding: 'utf-8' }) | 65 | const stdout = executeFctDriveCommand(['blob', blob]) |
| 69 | if (result.error) { | 66 | return stdout.trim(); |
| 70 | throw new Error(`Failed to execute fctdrive: ${result.error.message}`) | ||
| 71 | } | ||
| 72 | if (result.status !== 0) { | ||
| 73 | throw new Error(`fctdrive exited with code ${result.status}: ${result.stderr}`) | ||
| 74 | } | ||
| 75 | return result.stdout.trim(); | ||
| 76 | } | 67 | } |
| 77 | 68 | ||
| 78 | /// removes the file or directory at the given path | 69 | /// removes the file or directory at the given path |
| 79 | export async function Drive_remove(path: string, email: string) { | 70 | export async function Drive_remove(path: string, email: string) { |
| 80 | const result = spawnSync('fctdrive', ['remove', '--email', email, path], { encoding: 'utf-8' }); | 71 | executeFctDriveCommand(['remove', '--email', email, path]); |
| 81 | if (result.error) { | ||
| 82 | throw new Error(`Failed to execute fctdrive: ${result.error.message}`); | ||
| 83 | } | ||
| 84 | if (result.status !== 0) { | ||
| 85 | throw new Error(`fctdrive exited with code ${result.status}: ${result.stderr}`); | ||
| 86 | } | ||
| 87 | } | 72 | } |
| 88 | 73 | ||
| 89 | /// creates a directory at the given path | 74 | /// creates a directory at the given path |
| 90 | export async function Drive_mkdir(path: string, email: string) { | 75 | export async function Drive_mkdir(path: string, email: string) { |
| 91 | const result = spawnSync('fctdrive', ['mkdir', '--email', email, '--path', path], { encoding: 'utf-8' }); | 76 | executeFctDriveCommand(['mkdir', '--email', email, '--path', path]); |
| 92 | if (result.error) { | ||
| 93 | throw new Error(`Failed to execute fctdrive: ${result.error.message}`); | ||
| 94 | } | ||
| 95 | if (result.status !== 0) { | ||
| 96 | throw new Error(`fctdrive exited with code ${result.status}: ${result.stderr}`); | ||
| 97 | } | ||
| 98 | } | 77 | } |
| 99 | 78 | ||
| 100 | /// renames a file or directory from old path to new path | 79 | /// renames a file or directory from old path to new path |
| 101 | export async function Drive_rename(oldPath: string, newPath: string, email: string) { | 80 | export async function Drive_rename(oldPath: string, newPath: string, email: string) { |
| 102 | const result = spawnSync('fctdrive', ['rename', '--email', email, '--old', oldPath, '--new', newPath], { encoding: 'utf-8' }); | 81 | executeFctDriveCommand(['rename', '--email', email, '--old', oldPath, '--new', newPath]); |
| 103 | if (result.error) { | ||
| 104 | throw new Error(`Failed to execute fctdrive: ${result.error.message}`); | ||
| 105 | } | ||
| 106 | if (result.status !== 0) { | ||
| 107 | throw new Error(`fctdrive exited with code ${result.status}: ${result.stderr}`); | ||
| 108 | } | ||
| 109 | } | 82 | } |
| 110 | 83 | ||
| 111 | /// gets the blob ID for a given path | 84 | /// gets the blob ID for a given path |
| 112 | export async function Drive_stat(path: string): Promise<string> { | 85 | export async function Drive_stat(path: string): Promise<string> { |
| 113 | const result = spawnSync('fctdrive', ['stat', path], { encoding: 'utf-8' }); | 86 | const stdout = executeFctDriveCommand(['stat', path]); |
| 114 | if (result.error) { | 87 | return stdout.trim(); |
| 115 | throw new Error(`Failed to execute fctdrive: ${result.error.message}`); | ||
| 116 | } | ||
| 117 | if (result.status !== 0) { | ||
| 118 | throw new Error(`fctdrive exited with code ${result.status}: ${result.stderr}`); | ||
| 119 | } | ||
| 120 | return result.stdout.trim(); | ||
| 121 | } | 88 | } |
| 122 | 89 | ||
| 123 | 90 | ||
| @@ -130,15 +97,7 @@ export async function Drive_ls_directories(path: string = '/'): Promise<DriveLsE | |||
| 130 | 97 | ||
| 131 | /// returns the log entries from the drive | 98 | /// returns the log entries from the drive |
| 132 | export async function Drive_log(): Promise<DriveLogEntry[]> { | 99 | export async function Drive_log(): Promise<DriveLogEntry[]> { |
| 133 | const result = spawnSync('fctdrive', ['log'], { encoding: 'utf-8', maxBuffer: 50 * 1024 * 1024 }) | 100 | const stdout = executeFctDriveCommand(['log']) |
| 134 | if (result.error) { | ||
| 135 | throw new Error(`Failed to execute fctdrive: ${result.error.message}`) | ||
| 136 | } | ||
| 137 | if (result.status !== 0) { | ||
| 138 | throw new Error(`fctdrive exited with code ${result.status}: ${result.stderr}`) | ||
| 139 | } | ||
| 140 | |||
| 141 | const stdout = result.stdout | ||
| 142 | const entries: DriveLogEntry[] = [] | 101 | const entries: DriveLogEntry[] = [] |
| 143 | for (const line of stdout.split('\n')) { | 102 | for (const line of stdout.split('\n')) { |
| 144 | if (line.trim() === "") | 103 | if (line.trim() === "") |
