Skip to content

Commit 44cb10a

Browse files
committed
fix: scripts
1 parent 2c21101 commit 44cb10a

File tree

9 files changed

+42
-23
lines changed

9 files changed

+42
-23
lines changed

src/ctl/commands/export-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,6 @@ export async function exportConfig({ yml, showDefaults }: { yml?: boolean; showD
8686

8787
console.log(`${yml ? '- ' : ''}${envVar.variable}=${envValue}`);
8888
}
89+
90+
process.exit(0);
8991
}

src/ctl/commands/import-dir.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { bytes } from '@/lib/bytes';
2+
import { config, reloadSettings } from '@/lib/config';
13
import { guess } from '@/lib/mimes';
24
import { statSync } from 'fs';
3-
import { readFile, readdir } from 'fs/promises';
5+
import { mkdir, readdir } from 'fs/promises';
46
import { join, parse, resolve } from 'path';
5-
import { reloadSettings } from '@/lib/config';
6-
import { bytes } from '@/lib/bytes';
77

88
export async function importDir(
99
directory: string,
@@ -57,16 +57,18 @@ export async function importDir(
5757

5858
for (let i = 0; i !== files.length; ++i) {
5959
const info = parse(files[i]);
60+
if (info.base.startsWith('.thumbnail')) continue;
61+
6062
const mime = await guess(info.ext.replace('.', ''));
6163
const { size } = statSync(join(fullPath, files[i]));
6264

63-
data[i] = {
65+
data.push({
6466
name: info.base,
6567
type: mime,
6668
size,
6769
userId,
6870
...(folder ? { folderId: folder } : {}),
69-
};
71+
});
7072
}
7173

7274
if (!skipDb) {
@@ -78,16 +80,25 @@ export async function importDir(
7880

7981
const totalSize = data.reduce((acc, file) => acc + file.size, 0);
8082
let completed = 0;
83+
let imported = 0;
84+
85+
if (config.datasource.type === 'local')
86+
await mkdir(config.datasource.local!.directory, { recursive: true });
87+
88+
const { getDatasource } = await import('@/lib/datasource/index.js');
89+
const datasource = getDatasource(config);
90+
if (!datasource) return console.error('No datasource configured');
91+
92+
for (let i = 0; i !== data.length; ++i) {
93+
if (!data[i]) continue;
8194

82-
const { datasource } = await import('@/lib/datasource/index.js');
83-
for (let i = 0; i !== files.length; ++i) {
8495
console.log(`Uploading ${data[i].name} (${bytes(data[i].size)})...`);
8596

8697
const start = process.hrtime();
8798

88-
const buff = await readFile(join(fullPath, files[i]));
89-
await datasource.put(data[i].name, buff, {
99+
await datasource.put(data[i].name, join(fullPath, files[i]), {
90100
mimetype: data[i].type ?? 'application/octet-stream',
101+
noDelete: true,
91102
});
92103

93104
const diff = process.hrtime(start);
@@ -104,7 +115,11 @@ export async function importDir(
104115
console.log(
105116
`Uploaded ${data[i].name} in ${timeStr} (${bytes(data[i].size)}) ${i + 1}/${files.length} ${bytes(completed)}/${bytes(totalSize)} ${uploadSpeedStr}`,
106117
);
118+
119+
++imported;
107120
}
108121

109-
console.log('Done importing files.');
122+
console.log(`Done importing ${imported} files.`);
123+
124+
process.exit(0);
110125
}

src/ctl/commands/list-users.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ export async function listUsers({ extra, format, id }: { extra?: string[]; forma
2727
});
2828

2929
console.log(JSON.stringify(users, null, format ? 2 : 0));
30+
process.exit(0);
3031
}

src/ctl/commands/read-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export async function readConfig({ format }: { format: boolean }) {
66
const { config } = await import('@/lib/config/index.js');
77

88
console.log(JSON.stringify(config, null, format ? 2 : 0));
9+
process.exit(0);
910
}

src/ctl/commands/set-user.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ export async function setUser(property: string, value: string, { id }: { id: str
3434
if (property === 'password') parsed = '*********';
3535

3636
console.log(`updated user(${id}) -> ${property} = ${parsed || value}`);
37+
process.exit(0);
3738
}

src/lib/datasource/Datasource.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Readable } from 'stream';
22

3+
export type PutOptions = { mimetype?: string; noDelete?: boolean };
4+
35
export abstract class Datasource {
46
public name: string | undefined;
57

68
public abstract get(file: string): null | Readable | Promise<Readable | null>;
7-
public abstract put(file: string, data: Buffer | string, options?: { mimetype?: string }): Promise<void>;
9+
public abstract put(file: string, data: Buffer | string, options?: PutOptions): Promise<void>;
810
public abstract delete(file: string | string[]): Promise<void>;
911
public abstract size(file: string): Promise<number>;
1012
public abstract totalSize(): Promise<number>;

src/lib/datasource/Local.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createReadStream, existsSync } from 'fs';
22
import { access, constants, copyFile, readdir, rename, rm, stat, writeFile } from 'fs/promises';
33
import { join } from 'path';
44
import { Readable } from 'stream';
5-
import { Datasource } from './Datasource';
5+
import { Datasource, PutOptions } from './Datasource';
66

77
async function existsAndCanRW(path: string): Promise<boolean> {
88
try {
@@ -29,7 +29,7 @@ export class LocalDatasource extends Datasource {
2929
return readStream;
3030
}
3131

32-
public async put(file: string, data: Buffer | string): Promise<void> {
32+
public async put(file: string, data: Buffer | string, { noDelete }: PutOptions): Promise<void> {
3333
const path = join(this.dir, file);
3434

3535
// handles if given a path to a file, it will just move it instead of doing unecessary writes
@@ -41,7 +41,8 @@ export class LocalDatasource extends Datasource {
4141
);
4242

4343
await copyFile(data, path);
44-
await rm(data);
44+
45+
if (!noDelete) await rm(data);
4546

4647
return;
4748
}

src/lib/datasource/S3.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { Readable } from 'stream';
1515
import { ReadableStream } from 'stream/web';
1616
import Logger, { log } from '../logger';
1717
import { randomCharacters } from '../random';
18-
import { Datasource } from './Datasource';
18+
import { Datasource, PutOptions } from './Datasource';
1919

2020
function isOk(code: number) {
2121
return code >= 200 && code < 300;
@@ -160,13 +160,7 @@ export class S3Datasource extends Datasource {
160160
}
161161
}
162162

163-
public async put(
164-
file: string,
165-
data: Buffer | string,
166-
options: {
167-
mimetype?: string;
168-
} = {},
169-
): Promise<void> {
163+
public async put(file: string, data: Buffer | string, options: PutOptions = {}): Promise<void> {
170164
let command = new PutObjectCommand({
171165
Bucket: this.options.bucket,
172166
Key: this.key(file),

src/lib/datasource/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ declare global {
1111
var __datasource__: Datasource;
1212
}
1313

14-
function getDatasource(config?: Config): void {
14+
function getDatasource(config?: Config): Datasource | void {
1515
if (!config) return;
1616

1717
const logger = log('datasource');
@@ -35,6 +35,8 @@ function getDatasource(config?: Config): void {
3535
logger.error(`Datasource type ${config.datasource.type} is not supported`);
3636
process.exit(1);
3737
}
38+
39+
return datasource;
3840
}
3941

4042
datasource = global.__datasource__;

0 commit comments

Comments
 (0)