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
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
2020-01-12 16:40:58 +09:00
|
|
|
import * as assert from 'assert';
|
2022-05-14 16:09:47 +09:00
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
import { dirname } from 'node:path';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { ModuleMocker } from 'jest-mock';
|
|
|
|
import { Test } from '@nestjs/testing';
|
2024-01-08 17:43:52 +09:00
|
|
|
import { afterAll, beforeAll, describe, test } from '@jest/globals';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { GlobalModule } from '@/GlobalModule.js';
|
|
|
|
import { FileInfoService } from '@/core/FileInfoService.js';
|
2023-04-26 02:17:58 +09:00
|
|
|
//import { DI } from '@/di-symbols.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { AiService } from '@/core/AiService.js';
|
2024-03-31 12:43:39 +09:00
|
|
|
import { LoggerService } from '@/core/LoggerService.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import type { TestingModule } from '@nestjs/testing';
|
|
|
|
import type { MockFunctionMetadata } from 'jest-mock';
|
2022-05-14 16:09:47 +09:00
|
|
|
|
|
|
|
const _filename = fileURLToPath(import.meta.url);
|
|
|
|
const _dirname = dirname(_filename);
|
2022-09-18 03:27:08 +09:00
|
|
|
const resources = `${_dirname}/../resources`;
|
|
|
|
|
|
|
|
const moduleMocker = new ModuleMocker(global);
|
|
|
|
|
|
|
|
describe('FileInfoService', () => {
|
|
|
|
let app: TestingModule;
|
|
|
|
let fileInfoService: FileInfoService;
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
app = await Test.createTestingModule({
|
|
|
|
imports: [
|
|
|
|
GlobalModule,
|
|
|
|
],
|
|
|
|
providers: [
|
|
|
|
AiService,
|
2024-03-31 12:43:39 +09:00
|
|
|
LoggerService,
|
2022-09-18 03:27:08 +09:00
|
|
|
FileInfoService,
|
|
|
|
],
|
|
|
|
})
|
|
|
|
.useMocker((token) => {
|
|
|
|
//if (token === AiService) {
|
|
|
|
// return { };
|
|
|
|
//}
|
|
|
|
if (typeof token === 'function') {
|
|
|
|
const mockMetadata = moduleMocker.getMetadata(token) as MockFunctionMetadata<any, any>;
|
|
|
|
const Mock = moduleMocker.generateFromMetadata(mockMetadata);
|
|
|
|
return new Mock();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.compile();
|
|
|
|
|
|
|
|
app.enableShutdownHooks();
|
|
|
|
|
|
|
|
fileInfoService = app.get<FileInfoService>(FileInfoService);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await app.close();
|
|
|
|
});
|
2020-01-12 16:40:58 +09:00
|
|
|
|
2023-02-02 18:18:25 +09:00
|
|
|
test('Empty file', async () => {
|
2022-09-18 03:27:08 +09:00
|
|
|
const path = `${resources}/emptyfile`;
|
|
|
|
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
|
2020-01-12 16:40:58 +09:00
|
|
|
delete info.warnings;
|
2020-10-18 16:03:51 +09:00
|
|
|
delete info.blurhash;
|
2022-07-07 21:06:37 +09:00
|
|
|
delete info.sensitive;
|
2022-07-08 11:21:11 +09:00
|
|
|
delete info.porn;
|
2020-01-12 16:40:58 +09:00
|
|
|
assert.deepStrictEqual(info, {
|
|
|
|
size: 0,
|
|
|
|
md5: 'd41d8cd98f00b204e9800998ecf8427e',
|
|
|
|
type: {
|
|
|
|
mime: 'application/octet-stream',
|
2022-05-21 22:21:41 +09:00
|
|
|
ext: null,
|
2020-01-12 16:40:58 +09:00
|
|
|
},
|
|
|
|
width: undefined,
|
|
|
|
height: undefined,
|
2021-12-03 11:19:28 +09:00
|
|
|
orientation: undefined,
|
2020-01-12 16:40:58 +09:00
|
|
|
});
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
2020-01-12 16:40:58 +09:00
|
|
|
|
2023-04-26 02:17:58 +09:00
|
|
|
describe('IMAGE', () => {
|
|
|
|
test('Generic JPEG', async () => {
|
2024-07-02 14:29:44 +09:00
|
|
|
const path = `${resources}/192.jpg`;
|
2023-04-26 02:17:58 +09:00
|
|
|
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
|
|
|
|
delete info.warnings;
|
|
|
|
delete info.blurhash;
|
|
|
|
delete info.sensitive;
|
|
|
|
delete info.porn;
|
|
|
|
assert.deepStrictEqual(info, {
|
2024-07-02 14:29:44 +09:00
|
|
|
size: 5131,
|
|
|
|
md5: '8c9ed0677dd2b8f9f7472c3af247e5e3',
|
2023-04-26 02:17:58 +09:00
|
|
|
type: {
|
|
|
|
mime: 'image/jpeg',
|
|
|
|
ext: 'jpg',
|
|
|
|
},
|
2024-07-02 14:29:44 +09:00
|
|
|
width: 192,
|
|
|
|
height: 192,
|
2023-04-26 02:17:58 +09:00
|
|
|
orientation: undefined,
|
|
|
|
});
|
2020-01-12 16:40:58 +09:00
|
|
|
});
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2023-04-26 02:17:58 +09:00
|
|
|
test('Generic APNG', async () => {
|
|
|
|
const path = `${resources}/anime.png`;
|
|
|
|
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
|
|
|
|
delete info.warnings;
|
|
|
|
delete info.blurhash;
|
|
|
|
delete info.sensitive;
|
|
|
|
delete info.porn;
|
|
|
|
assert.deepStrictEqual(info, {
|
|
|
|
size: 1868,
|
|
|
|
md5: '08189c607bea3b952704676bb3c979e0',
|
|
|
|
type: {
|
|
|
|
mime: 'image/apng',
|
|
|
|
ext: 'apng',
|
|
|
|
},
|
|
|
|
width: 256,
|
|
|
|
height: 256,
|
|
|
|
orientation: undefined,
|
|
|
|
});
|
2020-01-12 16:40:58 +09:00
|
|
|
});
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2023-04-26 02:17:58 +09:00
|
|
|
test('Generic AGIF', async () => {
|
|
|
|
const path = `${resources}/anime.gif`;
|
|
|
|
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
|
|
|
|
delete info.warnings;
|
|
|
|
delete info.blurhash;
|
|
|
|
delete info.sensitive;
|
|
|
|
delete info.porn;
|
|
|
|
assert.deepStrictEqual(info, {
|
|
|
|
size: 2248,
|
|
|
|
md5: '32c47a11555675d9267aee1a86571e7e',
|
|
|
|
type: {
|
|
|
|
mime: 'image/gif',
|
|
|
|
ext: 'gif',
|
|
|
|
},
|
|
|
|
width: 256,
|
|
|
|
height: 256,
|
|
|
|
orientation: undefined,
|
|
|
|
});
|
2020-01-12 16:40:58 +09:00
|
|
|
});
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2023-04-26 02:17:58 +09:00
|
|
|
test('PNG with alpha', async () => {
|
|
|
|
const path = `${resources}/with-alpha.png`;
|
|
|
|
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
|
|
|
|
delete info.warnings;
|
|
|
|
delete info.blurhash;
|
|
|
|
delete info.sensitive;
|
|
|
|
delete info.porn;
|
|
|
|
assert.deepStrictEqual(info, {
|
|
|
|
size: 3772,
|
|
|
|
md5: 'f73535c3e1e27508885b69b10cf6e991',
|
|
|
|
type: {
|
|
|
|
mime: 'image/png',
|
|
|
|
ext: 'png',
|
|
|
|
},
|
|
|
|
width: 256,
|
|
|
|
height: 256,
|
|
|
|
orientation: undefined,
|
|
|
|
});
|
2020-01-12 16:40:58 +09:00
|
|
|
});
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2023-04-26 02:17:58 +09:00
|
|
|
test('Generic SVG', async () => {
|
|
|
|
const path = `${resources}/image.svg`;
|
|
|
|
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
|
|
|
|
delete info.warnings;
|
|
|
|
delete info.blurhash;
|
|
|
|
delete info.sensitive;
|
|
|
|
delete info.porn;
|
|
|
|
assert.deepStrictEqual(info, {
|
|
|
|
size: 505,
|
|
|
|
md5: 'b6f52b4b021e7b92cdd04509c7267965',
|
|
|
|
type: {
|
|
|
|
mime: 'image/svg+xml',
|
|
|
|
ext: 'svg',
|
|
|
|
},
|
|
|
|
width: 256,
|
|
|
|
height: 256,
|
|
|
|
orientation: undefined,
|
|
|
|
});
|
2020-01-12 16:40:58 +09:00
|
|
|
});
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2023-04-26 02:17:58 +09:00
|
|
|
test('SVG with XML definition', async () => {
|
|
|
|
// https://github.com/misskey-dev/misskey/issues/4413
|
|
|
|
const path = `${resources}/with-xml-def.svg`;
|
|
|
|
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
|
|
|
|
delete info.warnings;
|
|
|
|
delete info.blurhash;
|
|
|
|
delete info.sensitive;
|
|
|
|
delete info.porn;
|
|
|
|
assert.deepStrictEqual(info, {
|
|
|
|
size: 544,
|
|
|
|
md5: '4b7a346cde9ccbeb267e812567e33397',
|
|
|
|
type: {
|
|
|
|
mime: 'image/svg+xml',
|
|
|
|
ext: 'svg',
|
|
|
|
},
|
|
|
|
width: 256,
|
|
|
|
height: 256,
|
|
|
|
orientation: undefined,
|
|
|
|
});
|
2020-01-12 16:40:58 +09:00
|
|
|
});
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2023-04-26 02:17:58 +09:00
|
|
|
test('Dimension limit', async () => {
|
|
|
|
const path = `${resources}/25000x25000.png`;
|
|
|
|
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
|
|
|
|
delete info.warnings;
|
|
|
|
delete info.blurhash;
|
|
|
|
delete info.sensitive;
|
|
|
|
delete info.porn;
|
|
|
|
assert.deepStrictEqual(info, {
|
|
|
|
size: 75933,
|
|
|
|
md5: '268c5dde99e17cf8fe09f1ab3f97df56',
|
|
|
|
type: {
|
|
|
|
mime: 'application/octet-stream', // do not treat as image
|
|
|
|
ext: null,
|
|
|
|
},
|
|
|
|
width: 25000,
|
|
|
|
height: 25000,
|
|
|
|
orientation: undefined,
|
|
|
|
});
|
|
|
|
});
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2023-04-26 02:17:58 +09:00
|
|
|
test('Rotate JPEG', async () => {
|
|
|
|
const path = `${resources}/rotate.jpg`;
|
|
|
|
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
|
|
|
|
delete info.warnings;
|
|
|
|
delete info.blurhash;
|
|
|
|
delete info.sensitive;
|
|
|
|
delete info.porn;
|
|
|
|
assert.deepStrictEqual(info, {
|
|
|
|
size: 12624,
|
|
|
|
md5: '68d5b2d8d1d1acbbce99203e3ec3857e',
|
|
|
|
type: {
|
|
|
|
mime: 'image/jpeg',
|
|
|
|
ext: 'jpg',
|
|
|
|
},
|
|
|
|
width: 512,
|
|
|
|
height: 256,
|
|
|
|
orientation: 8,
|
|
|
|
});
|
2021-12-03 11:19:28 +09:00
|
|
|
});
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
2021-12-03 11:19:28 +09:00
|
|
|
|
2023-04-26 02:17:58 +09:00
|
|
|
describe('AUDIO', () => {
|
|
|
|
test('MP3', async () => {
|
|
|
|
const path = `${resources}/kick_gaba7.mp3`;
|
|
|
|
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
|
|
|
|
delete info.warnings;
|
|
|
|
delete info.blurhash;
|
|
|
|
delete info.sensitive;
|
|
|
|
delete info.porn;
|
|
|
|
delete info.width;
|
|
|
|
delete info.height;
|
|
|
|
delete info.orientation;
|
|
|
|
assert.deepStrictEqual(info, {
|
|
|
|
size: 19853,
|
|
|
|
md5: '4f557df8548bc3cecc794c652f690446',
|
|
|
|
type: {
|
|
|
|
mime: 'audio/mpeg',
|
|
|
|
ext: 'mp3',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2023-04-26 02:17:58 +09:00
|
|
|
test('WAV', async () => {
|
|
|
|
const path = `${resources}/kick_gaba7.wav`;
|
|
|
|
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
|
|
|
|
delete info.warnings;
|
|
|
|
delete info.blurhash;
|
|
|
|
delete info.sensitive;
|
|
|
|
delete info.porn;
|
|
|
|
delete info.width;
|
|
|
|
delete info.height;
|
|
|
|
delete info.orientation;
|
|
|
|
assert.deepStrictEqual(info, {
|
|
|
|
size: 87630,
|
|
|
|
md5: '8bc9bb4fe5e77bb1871448209be635c1',
|
|
|
|
type: {
|
|
|
|
mime: 'audio/wav',
|
|
|
|
ext: 'wav',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2023-04-26 02:17:58 +09:00
|
|
|
test('AAC', async () => {
|
|
|
|
const path = `${resources}/kick_gaba7.aac`;
|
|
|
|
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
|
|
|
|
delete info.warnings;
|
|
|
|
delete info.blurhash;
|
|
|
|
delete info.sensitive;
|
|
|
|
delete info.porn;
|
|
|
|
delete info.width;
|
|
|
|
delete info.height;
|
|
|
|
delete info.orientation;
|
|
|
|
assert.deepStrictEqual(info, {
|
|
|
|
size: 7291,
|
|
|
|
md5: '2789323f05e3392b648066f50be6a2a6',
|
|
|
|
type: {
|
|
|
|
mime: 'audio/aac',
|
|
|
|
ext: 'aac',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2023-04-26 02:17:58 +09:00
|
|
|
test('FLAC', async () => {
|
|
|
|
const path = `${resources}/kick_gaba7.flac`;
|
|
|
|
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
|
|
|
|
delete info.warnings;
|
|
|
|
delete info.blurhash;
|
|
|
|
delete info.sensitive;
|
|
|
|
delete info.porn;
|
|
|
|
delete info.width;
|
|
|
|
delete info.height;
|
|
|
|
delete info.orientation;
|
|
|
|
assert.deepStrictEqual(info, {
|
|
|
|
size: 108793,
|
|
|
|
md5: 'bc0f3adfe0e1ca99ae6c7528c46b3173',
|
|
|
|
type: {
|
|
|
|
mime: 'audio/flac',
|
|
|
|
ext: 'flac',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2024-03-31 12:43:39 +09:00
|
|
|
test('MPEG-4 AUDIO (M4A)', async () => {
|
|
|
|
const path = `${resources}/kick_gaba7.m4a`;
|
|
|
|
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
|
|
|
|
delete info.warnings;
|
|
|
|
delete info.blurhash;
|
|
|
|
delete info.sensitive;
|
|
|
|
delete info.porn;
|
|
|
|
delete info.width;
|
|
|
|
delete info.height;
|
|
|
|
delete info.orientation;
|
|
|
|
assert.deepStrictEqual(info, {
|
|
|
|
size: 9817,
|
|
|
|
md5: '74c9279a4abe98789565f1dc1a541a42',
|
|
|
|
type: {
|
|
|
|
mime: 'audio/mp4',
|
|
|
|
ext: 'm4a',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-04-26 02:17:58 +09:00
|
|
|
test('WEBM AUDIO', async () => {
|
|
|
|
const path = `${resources}/kick_gaba7.webm`;
|
|
|
|
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
|
|
|
|
delete info.warnings;
|
|
|
|
delete info.blurhash;
|
|
|
|
delete info.sensitive;
|
|
|
|
delete info.porn;
|
|
|
|
delete info.width;
|
|
|
|
delete info.height;
|
|
|
|
delete info.orientation;
|
|
|
|
assert.deepStrictEqual(info, {
|
|
|
|
size: 8879,
|
2024-03-31 12:43:39 +09:00
|
|
|
md5: '53bc1adcb6acbbda67ff9bd484896438',
|
2023-04-26 02:17:58 +09:00
|
|
|
type: {
|
|
|
|
mime: 'audio/webm',
|
|
|
|
ext: 'webm',
|
|
|
|
},
|
|
|
|
});
|
2020-01-12 16:40:58 +09:00
|
|
|
});
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
2020-01-12 16:40:58 +09:00
|
|
|
});
|