2023-07-27 14:31:52 +09:00
|
|
|
/*
|
2024-02-14 00:59:27 +09:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 14:31:52 +09:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
import * as fs from 'node:fs';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-07-20 17:00:54 +09:00
|
|
|
import { ZipReader } from 'slacc';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 14:28:29 +09:00
|
|
|
import type { EmojisRepository, DriveFilesRepository } from '@/models/_.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import type Logger from '@/logger.js';
|
|
|
|
import { CustomEmojiService } from '@/core/CustomEmojiService.js';
|
|
|
|
import { createTempDir } from '@/misc/create-temp.js';
|
|
|
|
import { DriveService } from '@/core/DriveService.js';
|
|
|
|
import { DownloadService } from '@/core/DownloadService.js';
|
2022-12-04 15:03:09 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { QueueLoggerService } from '../QueueLoggerService.js';
|
2023-05-29 11:54:49 +09:00
|
|
|
import type * as Bull from 'bullmq';
|
2022-09-18 03:27:08 +09:00
|
|
|
import type { DbUserImportJobData } from '../types.js';
|
|
|
|
|
|
|
|
// TODO: 名前衝突時の動作を選べるようにする
|
|
|
|
@Injectable()
|
|
|
|
export class ImportCustomEmojisProcessorService {
|
2022-09-19 03:11:50 +09:00
|
|
|
private logger: Logger;
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.emojisRepository)
|
|
|
|
private emojisRepository: EmojisRepository,
|
|
|
|
|
|
|
|
private customEmojiService: CustomEmojiService,
|
|
|
|
private driveService: DriveService,
|
|
|
|
private downloadService: DownloadService,
|
|
|
|
private queueLoggerService: QueueLoggerService,
|
|
|
|
) {
|
2022-09-19 03:11:50 +09:00
|
|
|
this.logger = this.queueLoggerService.logger.createSubLogger('import-custom-emojis');
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2023-05-29 11:54:49 +09:00
|
|
|
public async process(job: Bull.Job<DbUserImportJobData>): Promise<void> {
|
2022-09-19 03:11:50 +09:00
|
|
|
this.logger.info('Importing custom emojis ...');
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
const file = await this.driveFilesRepository.findOneBy({
|
|
|
|
id: job.data.fileId,
|
|
|
|
});
|
|
|
|
if (file == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [path, cleanup] = await createTempDir();
|
|
|
|
|
2022-09-19 03:11:50 +09:00
|
|
|
this.logger.info(`Temp dir is ${path}`);
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
const destPath = path + '/emojis.zip';
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(destPath, '', 'binary');
|
|
|
|
await this.downloadService.downloadUrl(file.url, destPath);
|
|
|
|
} catch (e) { // TODO: 何度か再試行
|
|
|
|
if (e instanceof Error || typeof e === 'string') {
|
2022-09-19 03:11:50 +09:00
|
|
|
this.logger.error(e);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
const outputPath = path + '/emojis';
|
2023-07-20 17:00:54 +09:00
|
|
|
try {
|
|
|
|
this.logger.succ(`Unzipping to ${outputPath}`);
|
|
|
|
ZipReader.withDestinationPath(outputPath).viaBuffer(await fs.promises.readFile(destPath));
|
2022-09-18 03:27:08 +09:00
|
|
|
const metaRaw = fs.readFileSync(outputPath + '/meta.json', 'utf-8');
|
|
|
|
const meta = JSON.parse(metaRaw);
|
|
|
|
|
|
|
|
for (const record of meta.emojis) {
|
|
|
|
if (!record.downloaded) continue;
|
2023-02-05 14:25:37 +09:00
|
|
|
if (!/^[a-zA-Z0-9_]+?([a-zA-Z0-9\.]+)?$/.test(record.fileName)) {
|
|
|
|
this.logger.error(`invalid filename: ${record.fileName}`);
|
|
|
|
continue;
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
const emojiInfo = record.emoji;
|
2023-04-06 08:37:18 +09:00
|
|
|
if (!/^[a-zA-Z0-9_]+$/.test(emojiInfo.name)) {
|
|
|
|
this.logger.error(`invalid emojiname: ${emojiInfo.name}`);
|
|
|
|
continue;
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
const emojiPath = outputPath + '/' + record.fileName;
|
|
|
|
await this.emojisRepository.delete({
|
|
|
|
name: emojiInfo.name,
|
|
|
|
});
|
|
|
|
const driveFile = await this.driveService.addFile({
|
|
|
|
user: null,
|
|
|
|
path: emojiPath,
|
|
|
|
name: record.fileName,
|
|
|
|
force: true,
|
|
|
|
});
|
|
|
|
await this.customEmojiService.add({
|
|
|
|
name: emojiInfo.name,
|
|
|
|
category: emojiInfo.category,
|
|
|
|
host: null,
|
|
|
|
aliases: emojiInfo.aliases,
|
|
|
|
driveFile,
|
2023-03-16 15:08:48 +09:00
|
|
|
license: emojiInfo.license,
|
2023-05-18 18:45:49 +09:00
|
|
|
isSensitive: emojiInfo.isSensitive,
|
|
|
|
localOnly: emojiInfo.localOnly,
|
|
|
|
roleIdsThatCanBeUsedThisEmojiAsReaction: [],
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup();
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-19 03:11:50 +09:00
|
|
|
this.logger.succ('Imported');
|
2023-07-20 17:00:54 +09:00
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof Error || typeof e === 'string') {
|
|
|
|
this.logger.error(e);
|
|
|
|
}
|
|
|
|
cleanup();
|
|
|
|
throw e;
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
}
|