2022-02-27 03:07:39 +01:00
|
|
|
import Bull from 'bull';
|
|
|
|
import * as fs from 'node:fs';
|
|
|
|
import unzipper from 'unzipper';
|
2022-01-12 16:47:40 +01:00
|
|
|
|
2022-02-27 03:07:39 +01:00
|
|
|
import { queueLogger } from '../../logger.js';
|
2022-05-25 09:50:22 +02:00
|
|
|
import { createTempDir } from '@/misc/create-temp.js';
|
2022-02-27 03:07:39 +01:00
|
|
|
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';
|
2022-03-26 07:34:00 +01:00
|
|
|
import { db } from '@/db/postgre.js';
|
2022-01-12 16:47:40 +01:00
|
|
|
|
|
|
|
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 ...`);
|
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
const file = await DriveFiles.findOneBy({
|
2022-01-12 16:47:40 +01:00
|
|
|
id: job.data.fileId,
|
|
|
|
});
|
|
|
|
if (file == null) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-25 09:50:22 +02:00
|
|
|
const [path, cleanup] = await createTempDir();
|
2022-01-12 16:47:40 +01:00
|
|
|
|
|
|
|
logger.info(`Temp dir is ${path}`);
|
|
|
|
|
|
|
|
const destPath = path + '/emojis.zip';
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(destPath, '', 'binary');
|
|
|
|
await downloadUrl(file.url, destPath);
|
|
|
|
} catch (e) { // TODO: 何度か再試行
|
2022-02-04 03:10:53 +01:00
|
|
|
if (e instanceof Error || typeof e === 'string') {
|
|
|
|
logger.error(e);
|
|
|
|
}
|
2022-01-12 16:47:40 +01:00
|
|
|
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,
|
|
|
|
});
|
2022-01-23 14:52:35 +01:00
|
|
|
const driveFile = await addFile({ user: null, path: emojiPath, name: record.fileName, force: true });
|
2022-01-12 16:47:40 +01:00
|
|
|
const emoji = await Emojis.insert({
|
|
|
|
id: genId(),
|
|
|
|
updatedAt: new Date(),
|
|
|
|
name: emojiInfo.name,
|
|
|
|
category: emojiInfo.category,
|
|
|
|
host: null,
|
|
|
|
aliases: emojiInfo.aliases,
|
2022-01-21 10:47:02 +01:00
|
|
|
originalUrl: driveFile.url,
|
|
|
|
publicUrl: driveFile.webpublicUrl ?? driveFile.url,
|
|
|
|
type: driveFile.webpublicType ?? driveFile.type,
|
2022-03-26 07:34:00 +01:00
|
|
|
}).then(x => Emojis.findOneByOrFail(x.identifiers[0]));
|
2022-01-12 16:47:40 +01:00
|
|
|
}
|
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
await db.queryResultCache!.remove(['meta_emojis']);
|
2022-01-12 16:47:40 +01:00
|
|
|
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
logger.succ('Imported');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
unzipStream.pipe(extractor);
|
|
|
|
logger.succ(`Unzipping to ${outputPath}`);
|
|
|
|
}
|