2022-09-17 21:27:08 +03:00
|
|
|
import * as fs from 'node:fs';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-02-16 16:09:41 +02:00
|
|
|
import { IsNull } from 'typeorm';
|
2022-09-17 21:27:08 +03:00
|
|
|
import { format as dateFormat } from 'date-fns';
|
|
|
|
import mime from 'mime-types';
|
|
|
|
import archiver from 'archiver';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 23:33:11 +03:00
|
|
|
import type { EmojisRepository, UsersRepository } from '@/models/index.js';
|
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import type Logger from '@/logger.js';
|
|
|
|
import { DriveService } from '@/core/DriveService.js';
|
|
|
|
import { createTemp, createTempDir } from '@/misc/create-temp.js';
|
|
|
|
import { DownloadService } from '@/core/DownloadService.js';
|
2023-02-05 07:25:37 +02:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import { QueueLoggerService } from '../QueueLoggerService.js';
|
|
|
|
import type Bull from 'bull';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ExportCustomEmojisProcessorService {
|
2022-09-18 21:11:50 +03:00
|
|
|
private logger: Logger;
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.emojisRepository)
|
|
|
|
private emojisRepository: EmojisRepository,
|
|
|
|
|
|
|
|
private driveService: DriveService,
|
|
|
|
private downloadService: DownloadService,
|
|
|
|
private queueLoggerService: QueueLoggerService,
|
|
|
|
) {
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger = this.queueLoggerService.logger.createSubLogger('export-custom-emojis');
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2022-09-17 21:27:08 +03:00
|
|
|
public async process(job: Bull.Job, done: () => void): Promise<void> {
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.info('Exporting custom emojis ...');
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
const user = await this.usersRepository.findOneBy({ id: job.data.user.id });
|
|
|
|
if (user == null) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [path, cleanup] = await createTempDir();
|
|
|
|
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.info(`Temp dir is ${path}`);
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
const metaPath = path + '/meta.json';
|
|
|
|
|
|
|
|
fs.writeFileSync(metaPath, '', 'utf-8');
|
|
|
|
|
|
|
|
const metaStream = fs.createWriteStream(metaPath, { flags: 'a' });
|
|
|
|
|
|
|
|
const writeMeta = (text: string): Promise<void> => {
|
|
|
|
return new Promise<void>((res, rej) => {
|
|
|
|
metaStream.write(text, err => {
|
|
|
|
if (err) {
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.error(err);
|
2022-09-17 21:27:08 +03:00
|
|
|
rej(err);
|
|
|
|
} else {
|
|
|
|
res();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
await writeMeta(`{"metaVersion":2,"host":"${this.config.host}","exportedAt":"${new Date().toString()}","emojis":[`);
|
|
|
|
|
|
|
|
const customEmojis = await this.emojisRepository.find({
|
|
|
|
where: {
|
|
|
|
host: IsNull(),
|
|
|
|
},
|
|
|
|
order: {
|
|
|
|
id: 'ASC',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const emoji of customEmojis) {
|
2023-02-05 07:25:37 +02:00
|
|
|
if (!/^[a-zA-Z0-9_]+$/.test(emoji.name)) {
|
|
|
|
this.logger.error(`invalid emoji name: ${emoji.name}`);
|
|
|
|
continue;
|
|
|
|
}
|
2022-09-24 11:07:56 +03:00
|
|
|
const ext = mime.extension(emoji.type ?? 'image/png');
|
2022-09-17 21:27:08 +03:00
|
|
|
const fileName = emoji.name + (ext ? '.' + ext : '');
|
|
|
|
const emojiPath = path + '/' + fileName;
|
|
|
|
fs.writeFileSync(emojiPath, '', 'binary');
|
|
|
|
let downloaded = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.downloadService.downloadUrl(emoji.originalUrl, emojiPath);
|
|
|
|
downloaded = true;
|
|
|
|
} catch (e) { // TODO: 何度か再試行
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.error(e instanceof Error ? e : new Error(e as string));
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!downloaded) {
|
|
|
|
fs.unlinkSync(emojiPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
const content = JSON.stringify({
|
|
|
|
fileName: fileName,
|
|
|
|
downloaded: downloaded,
|
|
|
|
emoji: emoji,
|
|
|
|
});
|
|
|
|
const isFirst = customEmojis.indexOf(emoji) === 0;
|
|
|
|
|
|
|
|
await writeMeta(isFirst ? content : ',\n' + content);
|
|
|
|
}
|
|
|
|
|
|
|
|
await writeMeta(']}');
|
|
|
|
|
|
|
|
metaStream.end();
|
|
|
|
|
|
|
|
// Create archive
|
|
|
|
const [archivePath, archiveCleanup] = await createTemp();
|
|
|
|
const archiveStream = fs.createWriteStream(archivePath);
|
|
|
|
const archive = archiver('zip', {
|
|
|
|
zlib: { level: 0 },
|
|
|
|
});
|
|
|
|
archiveStream.on('close', async () => {
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.succ(`Exported to: ${archivePath}`);
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
const fileName = 'custom-emojis-' + dateFormat(new Date(), 'yyyy-MM-dd-HH-mm-ss') + '.zip';
|
|
|
|
const driveFile = await this.driveService.addFile({ user, path: archivePath, name: fileName, force: true });
|
|
|
|
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.succ(`Exported to: ${driveFile.id}`);
|
2022-09-17 21:27:08 +03:00
|
|
|
cleanup();
|
|
|
|
archiveCleanup();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
archive.pipe(archiveStream);
|
|
|
|
archive.directory(path, false);
|
|
|
|
archive.finalize();
|
|
|
|
}
|
|
|
|
}
|