e27c6abaea
* 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`
82 lines
2.4 KiB
TypeScript
82 lines
2.4 KiB
TypeScript
import Bull from 'bull';
|
|
import * as fs from 'node:fs';
|
|
import unzipper from 'unzipper';
|
|
|
|
import { queueLogger } from '../../logger.js';
|
|
import { createTempDir } from '@/misc/create-temp.js';
|
|
import { downloadUrl } from '@/misc/download-url.js';
|
|
import { DriveFiles, Emojis } from '@/models/index.js';
|
|
import { DbUserImportJobData } from '@/queue/types.js';
|
|
import { addFile } from '@/services/drive/add-file.js';
|
|
import { genId } from '@/misc/gen-id.js';
|
|
import { db } from '@/db/postgre.js';
|
|
|
|
const logger = queueLogger.createSubLogger('import-custom-emojis');
|
|
|
|
// TODO: 名前衝突時の動作を選べるようにする
|
|
export async function importCustomEmojis(job: Bull.Job<DbUserImportJobData>, done: any): Promise<void> {
|
|
logger.info(`Importing custom emojis ...`);
|
|
|
|
const file = await DriveFiles.findOneBy({
|
|
id: job.data.fileId,
|
|
});
|
|
if (file == null) {
|
|
done();
|
|
return;
|
|
}
|
|
|
|
const [path, cleanup] = await createTempDir();
|
|
|
|
logger.info(`Temp dir is ${path}`);
|
|
|
|
const destPath = path + '/emojis.zip';
|
|
|
|
try {
|
|
fs.writeFileSync(destPath, '', 'binary');
|
|
await downloadUrl(file.url, destPath);
|
|
} catch (e) { // TODO: 何度か再試行
|
|
if (e instanceof Error || typeof e === 'string') {
|
|
logger.error(e);
|
|
}
|
|
throw e;
|
|
}
|
|
|
|
const outputPath = path + '/emojis';
|
|
const unzipStream = fs.createReadStream(destPath);
|
|
const extractor = unzipper.Extract({ path: outputPath });
|
|
extractor.on('close', async () => {
|
|
const metaRaw = fs.readFileSync(outputPath + '/meta.json', 'utf-8');
|
|
const meta = JSON.parse(metaRaw);
|
|
|
|
for (const record of meta.emojis) {
|
|
if (!record.downloaded) continue;
|
|
const emojiInfo = record.emoji;
|
|
const emojiPath = outputPath + '/' + record.fileName;
|
|
await Emojis.delete({
|
|
name: emojiInfo.name,
|
|
});
|
|
const driveFile = await addFile({ user: null, path: emojiPath, name: record.fileName, force: true });
|
|
const emoji = await Emojis.insert({
|
|
id: genId(),
|
|
updatedAt: new Date(),
|
|
name: emojiInfo.name,
|
|
category: emojiInfo.category,
|
|
host: null,
|
|
aliases: emojiInfo.aliases,
|
|
originalUrl: driveFile.url,
|
|
publicUrl: driveFile.webpublicUrl ?? driveFile.url,
|
|
type: driveFile.webpublicType ?? driveFile.type,
|
|
}).then(x => Emojis.findOneByOrFail(x.identifiers[0]));
|
|
}
|
|
|
|
await db.queryResultCache!.remove(['meta_emojis']);
|
|
|
|
cleanup();
|
|
|
|
logger.succ('Imported');
|
|
done();
|
|
});
|
|
unzipStream.pipe(extractor);
|
|
logger.succ(`Unzipping to ${outputPath}`);
|
|
}
|