use async IO for InternalStorageService
This commit is contained in:
parent
ba17776b19
commit
2deb64486b
2 changed files with 26 additions and 23 deletions
|
@ -227,25 +227,27 @@ export class DriveService {
|
||||||
const thumbnailAccessKey = 'thumbnail-' + randomUUID();
|
const thumbnailAccessKey = 'thumbnail-' + randomUUID();
|
||||||
const webpublicAccessKey = 'webpublic-' + randomUUID();
|
const webpublicAccessKey = 'webpublic-' + randomUUID();
|
||||||
|
|
||||||
const url = this.internalStorageService.saveFromPath(accessKey, path);
|
// Ugly type is just to help TS figure out that 2nd / 3rd promises are optional.
|
||||||
|
const promises: [Promise<string>, ...(Promise<string> | undefined)[]] = [
|
||||||
let thumbnailUrl: string | null = null;
|
this.internalStorageService.saveFromPath(accessKey, path),
|
||||||
let webpublicUrl: string | null = null;
|
];
|
||||||
|
|
||||||
if (alts.thumbnail) {
|
if (alts.thumbnail) {
|
||||||
thumbnailUrl = this.internalStorageService.saveFromBuffer(thumbnailAccessKey, alts.thumbnail.data);
|
promises.push(this.internalStorageService.saveFromBuffer(thumbnailAccessKey, alts.thumbnail.data));
|
||||||
this.registerLogger.info(`thumbnail stored: ${thumbnailAccessKey}`);
|
this.registerLogger.info(`thumbnail stored: ${thumbnailAccessKey}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (alts.webpublic) {
|
if (alts.webpublic) {
|
||||||
webpublicUrl = this.internalStorageService.saveFromBuffer(webpublicAccessKey, alts.webpublic.data);
|
promises.push(this.internalStorageService.saveFromBuffer(webpublicAccessKey, alts.webpublic.data));
|
||||||
this.registerLogger.info(`web stored: ${webpublicAccessKey}`);
|
this.registerLogger.info(`web stored: ${webpublicAccessKey}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const [url, thumbnailUrl, webpublicUrl] = await Promise.all(promises);
|
||||||
|
|
||||||
file.storedInternal = true;
|
file.storedInternal = true;
|
||||||
file.url = url;
|
file.url = url;
|
||||||
file.thumbnailUrl = thumbnailUrl;
|
file.thumbnailUrl = thumbnailUrl ?? null;
|
||||||
file.webpublicUrl = webpublicUrl;
|
file.webpublicUrl = webpublicUrl ?? null;
|
||||||
file.accessKey = accessKey;
|
file.accessKey = accessKey;
|
||||||
file.thumbnailAccessKey = thumbnailAccessKey;
|
file.thumbnailAccessKey = thumbnailAccessKey;
|
||||||
file.webpublicAccessKey = webpublicAccessKey;
|
file.webpublicAccessKey = webpublicAccessKey;
|
||||||
|
@ -720,19 +722,19 @@ export class DriveService {
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public async deleteFileSync(file: MiDriveFile, isExpired = false, deleter?: MiUser) {
|
public async deleteFileSync(file: MiDriveFile, isExpired = false, deleter?: MiUser) {
|
||||||
|
const promises = [];
|
||||||
|
|
||||||
if (file.storedInternal) {
|
if (file.storedInternal) {
|
||||||
this.internalStorageService.del(file.accessKey!);
|
promises.push(this.internalStorageService.del(file.accessKey!));
|
||||||
|
|
||||||
if (file.thumbnailUrl) {
|
if (file.thumbnailUrl) {
|
||||||
this.internalStorageService.del(file.thumbnailAccessKey!);
|
promises.push(this.internalStorageService.del(file.thumbnailAccessKey!));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (file.webpublicUrl) {
|
if (file.webpublicUrl) {
|
||||||
this.internalStorageService.del(file.webpublicAccessKey!);
|
promises.push(this.internalStorageService.del(file.webpublicAccessKey!));
|
||||||
}
|
}
|
||||||
} else if (!file.isLink) {
|
} else if (!file.isLink) {
|
||||||
const promises = [];
|
|
||||||
|
|
||||||
promises.push(this.deleteObjectStorageFile(file.accessKey!));
|
promises.push(this.deleteObjectStorageFile(file.accessKey!));
|
||||||
|
|
||||||
if (file.thumbnailUrl) {
|
if (file.thumbnailUrl) {
|
||||||
|
@ -742,9 +744,9 @@ export class DriveService {
|
||||||
if (file.webpublicUrl) {
|
if (file.webpublicUrl) {
|
||||||
promises.push(this.deleteObjectStorageFile(file.webpublicAccessKey!));
|
promises.push(this.deleteObjectStorageFile(file.webpublicAccessKey!));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
}
|
|
||||||
|
|
||||||
this.deletePostProcess(file, isExpired, deleter);
|
this.deletePostProcess(file, isExpired, deleter);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as fs from 'node:fs';
|
import * as fs from 'node:fs';
|
||||||
|
import { copyFile, mkdir, unlink, writeFile } from 'node:fs/promises';
|
||||||
import * as Path from 'node:path';
|
import * as Path from 'node:path';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import { dirname } from 'node:path';
|
import { dirname } from 'node:path';
|
||||||
|
@ -36,21 +37,21 @@ export class InternalStorageService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public saveFromPath(key: string, srcPath: string) {
|
public async saveFromPath(key: string, srcPath: string): Promise<string> {
|
||||||
fs.mkdirSync(path, { recursive: true });
|
await mkdir(path, { recursive: true });
|
||||||
fs.copyFileSync(srcPath, this.resolvePath(key));
|
await copyFile(srcPath, this.resolvePath(key));
|
||||||
return `${this.config.url}/files/${key}`;
|
return `${this.config.url}/files/${key}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public saveFromBuffer(key: string, data: Buffer) {
|
public async saveFromBuffer(key: string, data: Buffer): Promise<string> {
|
||||||
fs.mkdirSync(path, { recursive: true });
|
await mkdir(path, { recursive: true });
|
||||||
fs.writeFileSync(this.resolvePath(key), data);
|
await writeFile(this.resolvePath(key), data);
|
||||||
return `${this.config.url}/files/${key}`;
|
return `${this.config.url}/files/${key}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public del(key: string) {
|
public async del(key: string): Promise<void> {
|
||||||
fs.unlink(this.resolvePath(key), () => {});
|
await unlink(this.resolvePath(key));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue