Skip to content

Commit

Permalink
fsでSyncをなるべく使用しないように
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Nov 22, 2022
1 parent 43f46e7 commit 2151361
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/misc/get-file-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export async function checkSvg(path: string) {
try {
const size = await getFileSize(path);
if (size > 1 * 1024 * 1024) return false;
return isSvg(fs.readFileSync(path));
return isSvg(await fs.promises.readFile(path));
} catch {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/proxy/proxy-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function proxyMedia(ctx: Koa.Context) {
image = await convertToJpeg(path, 200, 200);
} else {
image = {
data: fs.readFileSync(path),
data: await fs.promises.readFile(path),
ext,
type: mime,
};
Expand Down
4 changes: 2 additions & 2 deletions src/services/drive/add-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ async function save(file: DriveFile, path: string, name: string, type: string, h
const accessKey = uuid();
const thumbnailAccessKey = 'thumbnail-' + uuid();

const url = InternalStorage.saveFromPath(accessKey, path);
const url = await InternalStorage.saveFromPathAsync(accessKey, path);

let thumbnailUrl: string | null = null;

if (thumb) {
thumbnailUrl = InternalStorage.saveFromBuffer(thumbnailAccessKey, thumb.data);
thumbnailUrl = await InternalStorage.saveFromBufferAsync(thumbnailAccessKey, thumb.data);
logger.info(`thumbnail stored: ${thumbnailAccessKey}`);
}

Expand Down
20 changes: 11 additions & 9 deletions src/services/drive/internal-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@ import * as Path from 'path';
import config from '../../config';

export class InternalStorage {
private static readonly path = Path.resolve(`${__dirname}/../../../files`);
private static readonly path = Path.resolve(__dirname, '../../../files');

public static resolvePath = (key: string) => Path.resolve(InternalStorage.path, key);

public static read(key: string) {
return fs.createReadStream(`${InternalStorage.path}/${key}`);
return fs.createReadStream(InternalStorage.resolvePath(key));
}

public static saveFromPath(key: string, srcPath: string) {
fs.mkdirSync(InternalStorage.path, { recursive: true });
fs.copyFileSync(srcPath, `${InternalStorage.path}/${key}`);
public static async saveFromPathAsync(key: string, srcPath: string) {
await fs.promises.mkdir(InternalStorage.path, { recursive: true });
await fs.promises.copyFile(srcPath, InternalStorage.resolvePath(key));
return `${config.url}/files/${key}`;
}

public static saveFromBuffer(key: string, data: Buffer) {
fs.mkdirSync(InternalStorage.path, { recursive: true });
fs.writeFileSync(`${InternalStorage.path}/${key}`, data);
public static async saveFromBufferAsync(key: string, data: Buffer) {
await fs.promises.mkdir(InternalStorage.path, { recursive: true });
await fs.promises.writeFile(InternalStorage.resolvePath(key), data);
return `${config.url}/files/${key}`;
}

public static del(key: string) {
fs.unlink(`${InternalStorage.path}/${key}`, () => {});
fs.unlink(InternalStorage.resolvePath(key), () => {});
}
}

0 comments on commit 2151361

Please sign in to comment.