refactor: temporary files (#8713)
* simplify temporary files for thumbnails Because only a single file will be written to the directory, creating a separate directory seems unnecessary. If only a temporary file is created, the code from `createTemp` can be reused here as well. * refactor: deduplicate code for temporary files/directories To follow the DRY principle, the same code should not be duplicated across different files. Instead an already existing function is used. Because temporary directories are also create in multiple locations, a function for this is also newly added to reduce duplication. * fix: clean up identicon temp files The temporary files for identicons are not reused and can be deleted after they are fully read. This condition is met when the stream is closed and so the file can be cleaned up using the events API of the stream. * fix: ensure cleanup is called when download fails * fix: ensure cleanup is called in error conditions This covers import/export queue jobs and is mostly just wrapping all code in a try...finally statement where the finally runs the cleanup. * fix: use correct type instead of `any`
This commit is contained in:
@ -1,38 +1,31 @@
|
||||
import * as fs from 'node:fs';
|
||||
import * as tmp from 'tmp';
|
||||
import * as path from 'node:path';
|
||||
import { createTemp } from '@/misc/create-temp.js';
|
||||
import { IImage, convertToJpeg } from './image-processor.js';
|
||||
import FFmpeg from 'fluent-ffmpeg';
|
||||
|
||||
export async function GenerateVideoThumbnail(path: string): Promise<IImage> {
|
||||
const [outDir, cleanup] = await new Promise<[string, any]>((res, rej) => {
|
||||
tmp.dir((e, path, cleanup) => {
|
||||
if (e) return rej(e);
|
||||
res([path, cleanup]);
|
||||
export async function GenerateVideoThumbnail(source: string): Promise<IImage> {
|
||||
const [file, cleanup] = await createTemp();
|
||||
const parsed = path.parse(file);
|
||||
|
||||
try {
|
||||
await new Promise((res, rej) => {
|
||||
FFmpeg({
|
||||
source,
|
||||
})
|
||||
.on('end', res)
|
||||
.on('error', rej)
|
||||
.screenshot({
|
||||
folder: parsed.dir,
|
||||
filename: parsed.base,
|
||||
count: 1,
|
||||
timestamps: ['5%'],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
await new Promise((res, rej) => {
|
||||
FFmpeg({
|
||||
source: path,
|
||||
})
|
||||
.on('end', res)
|
||||
.on('error', rej)
|
||||
.screenshot({
|
||||
folder: outDir,
|
||||
filename: 'output.png',
|
||||
count: 1,
|
||||
timestamps: ['5%'],
|
||||
});
|
||||
});
|
||||
|
||||
const outPath = `${outDir}/output.png`;
|
||||
|
||||
// JPEGに変換 (Webpでもいいが、MastodonはWebpをサポートせず表示できなくなる)
|
||||
const thumbnail = await convertToJpeg(outPath, 498, 280);
|
||||
|
||||
// cleanup
|
||||
await fs.promises.unlink(outPath);
|
||||
cleanup();
|
||||
|
||||
return thumbnail;
|
||||
// JPEGに変換 (Webpでもいいが、MastodonはWebpをサポートせず表示できなくなる)
|
||||
return await convertToJpeg(498, 280);
|
||||
} finally {
|
||||
cleanup();
|
||||
}
|
||||
}
|
||||
|
@ -45,29 +45,20 @@ export async function uploadFromUrl({
|
||||
// Create temp file
|
||||
const [path, cleanup] = await createTemp();
|
||||
|
||||
// write content at URL to temp file
|
||||
await downloadUrl(url, path);
|
||||
|
||||
let driveFile: DriveFile;
|
||||
let error;
|
||||
|
||||
try {
|
||||
driveFile = await addFile({ user, path, name, comment, folderId, force, isLink, url, uri, sensitive });
|
||||
// write content at URL to temp file
|
||||
await downloadUrl(url, path);
|
||||
|
||||
const driveFile = await addFile({ user, path, name, comment, folderId, force, isLink, url, uri, sensitive });
|
||||
logger.succ(`Got: ${driveFile.id}`);
|
||||
return driveFile!;
|
||||
} catch (e) {
|
||||
error = e;
|
||||
logger.error(`Failed to create drive file: ${e}`, {
|
||||
url: url,
|
||||
e: e,
|
||||
});
|
||||
}
|
||||
|
||||
// clean-up
|
||||
cleanup();
|
||||
|
||||
if (error) {
|
||||
throw error;
|
||||
} else {
|
||||
return driveFile!;
|
||||
throw e;
|
||||
} finally {
|
||||
cleanup();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user