2022-02-27 03:07:39 +01:00
|
|
|
import Bull from 'bull';
|
|
|
|
import * as fs from 'node:fs';
|
2021-12-10 10:24:26 +01:00
|
|
|
|
|
|
|
import { ulid } from 'ulid';
|
2022-02-27 03:07:39 +01:00
|
|
|
import mime from 'mime-types';
|
|
|
|
import archiver from 'archiver';
|
|
|
|
import { queueLogger } from '../../logger.js';
|
|
|
|
import { addFile } from '@/services/drive/add-file.js';
|
2022-02-03 17:56:33 +01:00
|
|
|
import { format as dateFormat } from 'date-fns';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { Users, Emojis } from '@/models/index.js';
|
|
|
|
import { } from '@/queue/types.js';
|
2022-05-29 14:32:52 +02:00
|
|
|
import { createTemp, createTempDir } from '@/misc/create-temp.js';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { downloadUrl } from '@/misc/download-url.js';
|
|
|
|
import config from '@/config/index.js';
|
2022-03-26 07:34:00 +01:00
|
|
|
import { IsNull } from 'typeorm';
|
2021-12-10 10:24:26 +01:00
|
|
|
|
|
|
|
const logger = queueLogger.createSubLogger('export-custom-emojis');
|
|
|
|
|
|
|
|
export async function exportCustomEmojis(job: Bull.Job, done: () => void): Promise<void> {
|
|
|
|
logger.info(`Exporting custom emojis ...`);
|
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
const user = await Users.findOneBy({ id: job.data.user.id });
|
2021-12-10 10:24:26 +01:00
|
|
|
if (user == null) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-25 09:50:22 +02:00
|
|
|
const [path, cleanup] = await createTempDir();
|
2021-12-10 10:24:26 +01:00
|
|
|
|
|
|
|
logger.info(`Temp dir is ${path}`);
|
|
|
|
|
|
|
|
const metaPath = path + '/meta.json';
|
|
|
|
|
|
|
|
fs.writeFileSync(metaPath, '', 'utf-8');
|
|
|
|
|
|
|
|
const metaStream = fs.createWriteStream(metaPath, { flags: 'a' });
|
|
|
|
|
2021-12-11 22:21:48 +01:00
|
|
|
const writeMeta = (text: string): Promise<void> => {
|
|
|
|
return new Promise<void>((res, rej) => {
|
|
|
|
metaStream.write(text, err => {
|
|
|
|
if (err) {
|
|
|
|
logger.error(err);
|
|
|
|
rej(err);
|
|
|
|
} else {
|
|
|
|
res();
|
|
|
|
}
|
|
|
|
});
|
2021-12-10 10:24:26 +01:00
|
|
|
});
|
2021-12-11 22:21:48 +01:00
|
|
|
};
|
|
|
|
|
2022-01-20 18:56:08 +01:00
|
|
|
await writeMeta(`{"metaVersion":2,"host":"${config.host}","exportedAt":"${new Date().toString()}","emojis":[`);
|
2021-12-10 10:24:26 +01:00
|
|
|
|
|
|
|
const customEmojis = await Emojis.find({
|
|
|
|
where: {
|
2022-03-26 07:34:00 +01:00
|
|
|
host: IsNull(),
|
2021-12-10 10:24:26 +01:00
|
|
|
},
|
|
|
|
order: {
|
|
|
|
id: 'ASC',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const emoji of customEmojis) {
|
2021-12-29 09:04:13 +01:00
|
|
|
const ext = mime.extension(emoji.type);
|
2022-01-12 16:47:40 +01:00
|
|
|
const fileName = emoji.name + (ext ? '.' + ext : '');
|
|
|
|
const emojiPath = path + '/' + fileName;
|
2021-12-10 10:24:26 +01:00
|
|
|
fs.writeFileSync(emojiPath, '', 'binary');
|
|
|
|
let downloaded = false;
|
|
|
|
|
|
|
|
try {
|
2022-01-21 10:47:02 +01:00
|
|
|
await downloadUrl(emoji.originalUrl, emojiPath);
|
2021-12-10 10:24:26 +01:00
|
|
|
downloaded = true;
|
|
|
|
} catch (e) { // TODO: 何度か再試行
|
2022-02-19 06:53:33 +01:00
|
|
|
logger.error(e instanceof Error ? e : new Error(e as string));
|
2021-12-10 10:24:26 +01:00
|
|
|
}
|
|
|
|
|
2022-01-12 16:47:40 +01:00
|
|
|
if (!downloaded) {
|
|
|
|
fs.unlinkSync(emojiPath);
|
|
|
|
}
|
|
|
|
|
2021-12-11 22:21:48 +01:00
|
|
|
const content = JSON.stringify({
|
2022-01-12 16:47:40 +01:00
|
|
|
fileName: fileName,
|
2021-12-11 22:21:48 +01:00
|
|
|
downloaded: downloaded,
|
|
|
|
emoji: emoji,
|
2021-12-10 10:24:26 +01:00
|
|
|
});
|
2021-12-11 22:21:48 +01:00
|
|
|
const isFirst = customEmojis.indexOf(emoji) === 0;
|
|
|
|
|
|
|
|
await writeMeta(isFirst ? content : ',\n' + content);
|
2021-12-10 10:24:26 +01:00
|
|
|
}
|
|
|
|
|
2021-12-11 22:24:13 +01:00
|
|
|
await writeMeta(']}');
|
2021-12-10 10:24:26 +01:00
|
|
|
|
|
|
|
metaStream.end();
|
|
|
|
|
|
|
|
// Create archive
|
2022-05-25 09:50:22 +02:00
|
|
|
const [archivePath, archiveCleanup] = await createTemp();
|
2021-12-10 10:24:26 +01:00
|
|
|
const archiveStream = fs.createWriteStream(archivePath);
|
|
|
|
const archive = archiver('zip', {
|
|
|
|
zlib: { level: 0 },
|
|
|
|
});
|
|
|
|
archiveStream.on('close', async () => {
|
|
|
|
logger.succ(`Exported to: ${archivePath}`);
|
|
|
|
|
2022-02-03 17:56:33 +01:00
|
|
|
const fileName = 'custom-emojis-' + dateFormat(new Date(), 'yyyy-MM-dd-HH-mm-ss') + '.zip';
|
2022-01-23 14:52:35 +01:00
|
|
|
const driveFile = await addFile({ user, path: archivePath, name: fileName, force: true });
|
2021-12-10 10:24:26 +01:00
|
|
|
|
|
|
|
logger.succ(`Exported to: ${driveFile.id}`);
|
|
|
|
cleanup();
|
|
|
|
archiveCleanup();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
archive.pipe(archiveStream);
|
|
|
|
archive.directory(path, false);
|
|
|
|
archive.finalize();
|
|
|
|
}
|