2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
import * as fs from 'node:fs';
|
|
|
|
import * as crypto from 'node:crypto';
|
2023-07-27 02:04:19 +02:00
|
|
|
import * as stream from 'node:stream/promises';
|
2023-02-16 15:09:41 +01:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2023-04-25 19:17:58 +02:00
|
|
|
import * as fileType from 'file-type';
|
2022-09-17 20:27:08 +02:00
|
|
|
import isSvg from 'is-svg';
|
|
|
|
import probeImageSize from 'probe-image-size';
|
|
|
|
import sharp from 'sharp';
|
2024-02-21 06:42:37 +01:00
|
|
|
import { sharpBmp } from '@misskey-dev/sharp-read-bmp';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { encode } from 'blurhash';
|
2022-12-04 07:03:09 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
export type FileInfo = {
|
|
|
|
size: number;
|
|
|
|
md5: string;
|
|
|
|
type: {
|
|
|
|
mime: string;
|
|
|
|
ext: string | null;
|
|
|
|
};
|
|
|
|
width?: number;
|
|
|
|
height?: number;
|
|
|
|
orientation?: number;
|
|
|
|
blurhash?: string;
|
|
|
|
sensitive: boolean;
|
|
|
|
porn: boolean;
|
|
|
|
warnings: string[];
|
|
|
|
};
|
|
|
|
|
|
|
|
const TYPE_OCTET_STREAM = {
|
|
|
|
mime: 'application/octet-stream',
|
|
|
|
ext: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
const TYPE_SVG = {
|
|
|
|
mime: 'image/svg+xml',
|
|
|
|
ext: 'svg',
|
|
|
|
};
|
2022-12-04 07:03:09 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
|
|
|
export class FileInfoService {
|
|
|
|
constructor(
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get file information
|
|
|
|
*/
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-11-05 13:43:16 +01:00
|
|
|
public async getFileInfo(path: string): Promise<FileInfo> {
|
2022-09-17 20:27:08 +02:00
|
|
|
const warnings = [] as string[];
|
|
|
|
|
|
|
|
const size = await this.getFileSize(path);
|
2022-09-18 20:11:50 +02:00
|
|
|
const md5 = await this.calcHash(path);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
let type = await this.detectType(path);
|
|
|
|
|
|
|
|
// image dimensions
|
|
|
|
let width: number | undefined;
|
|
|
|
let height: number | undefined;
|
|
|
|
let orientation: number | undefined;
|
|
|
|
|
2022-12-08 06:49:49 +01:00
|
|
|
if ([
|
|
|
|
'image/png',
|
|
|
|
'image/gif',
|
|
|
|
'image/jpeg',
|
|
|
|
'image/webp',
|
|
|
|
'image/avif',
|
|
|
|
'image/apng',
|
|
|
|
'image/bmp',
|
|
|
|
'image/tiff',
|
|
|
|
'image/svg+xml',
|
|
|
|
'image/vnd.adobe.photoshop',
|
|
|
|
].includes(type.mime)) {
|
2022-09-18 20:11:50 +02:00
|
|
|
const imageSize = await this.detectImageSize(path).catch(e => {
|
2022-09-17 20:27:08 +02:00
|
|
|
warnings.push(`detectImageSize failed: ${e}`);
|
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
// うまく判定できない画像は octet-stream にする
|
|
|
|
if (!imageSize) {
|
|
|
|
warnings.push('cannot detect image dimensions');
|
|
|
|
type = TYPE_OCTET_STREAM;
|
|
|
|
} else if (imageSize.wUnits === 'px') {
|
|
|
|
width = imageSize.width;
|
|
|
|
height = imageSize.height;
|
|
|
|
orientation = imageSize.orientation;
|
|
|
|
|
|
|
|
// 制限を超えている画像は octet-stream にする
|
|
|
|
if (imageSize.width > 16383 || imageSize.height > 16383) {
|
|
|
|
warnings.push('image dimensions exceeds limits');
|
|
|
|
type = TYPE_OCTET_STREAM;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
warnings.push(`unsupported unit type: ${imageSize.wUnits}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let blurhash: string | undefined;
|
|
|
|
|
2022-12-08 06:49:49 +01:00
|
|
|
if ([
|
|
|
|
'image/jpeg',
|
|
|
|
'image/gif',
|
|
|
|
'image/png',
|
|
|
|
'image/apng',
|
|
|
|
'image/webp',
|
|
|
|
'image/avif',
|
|
|
|
'image/svg+xml',
|
|
|
|
].includes(type.mime)) {
|
2024-02-21 06:42:37 +01:00
|
|
|
blurhash = await this.getBlurhash(path, type.mime).catch(e => {
|
2022-09-17 20:27:08 +02:00
|
|
|
warnings.push(`getBlurhash failed: ${e}`);
|
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-05 09:54:52 +01:00
|
|
|
const sensitive = false;
|
|
|
|
const porn = false;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
size,
|
|
|
|
md5,
|
|
|
|
type,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
orientation,
|
|
|
|
blurhash,
|
|
|
|
sensitive,
|
|
|
|
porn,
|
|
|
|
warnings,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-25 19:17:58 +02:00
|
|
|
@bindThis
|
|
|
|
public fixMime(mime: string | fileType.MimeType): string {
|
|
|
|
// see https://github.com/misskey-dev/misskey/pull/10686
|
2023-07-07 03:53:06 +02:00
|
|
|
if (mime === 'audio/x-flac') {
|
|
|
|
return 'audio/flac';
|
2023-04-25 19:17:58 +02:00
|
|
|
}
|
2023-07-07 03:53:06 +02:00
|
|
|
if (mime === 'audio/vnd.wave') {
|
|
|
|
return 'audio/wav';
|
2023-04-25 19:17:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return mime;
|
|
|
|
}
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
/**
|
|
|
|
* Detect MIME Type and extension
|
|
|
|
*/
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async detectType(path: string): Promise<{
|
2023-04-25 19:17:58 +02:00
|
|
|
mime: string;
|
|
|
|
ext: string | null;
|
|
|
|
}> {
|
2022-09-17 20:27:08 +02:00
|
|
|
// Check 0 byte
|
|
|
|
const fileSize = await this.getFileSize(path);
|
|
|
|
if (fileSize === 0) {
|
|
|
|
return TYPE_OCTET_STREAM;
|
|
|
|
}
|
|
|
|
|
2023-04-25 19:17:58 +02:00
|
|
|
const type = await fileType.fileTypeFromFile(path);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
if (type) {
|
|
|
|
// XMLはSVGかもしれない
|
|
|
|
if (type.mime === 'application/xml' && await this.checkSvg(path)) {
|
|
|
|
return TYPE_SVG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2023-04-25 19:17:58 +02:00
|
|
|
mime: this.fixMime(type.mime),
|
2022-09-17 20:27:08 +02:00
|
|
|
ext: type.ext,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// 種類が不明でもSVGかもしれない
|
|
|
|
if (await this.checkSvg(path)) {
|
|
|
|
return TYPE_SVG;
|
|
|
|
}
|
|
|
|
|
|
|
|
// それでも種類が不明なら application/octet-stream にする
|
|
|
|
return TYPE_OCTET_STREAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the file is SVG or not
|
|
|
|
*/
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-07-07 03:53:06 +02:00
|
|
|
public async checkSvg(path: string): Promise<boolean> {
|
2022-09-17 20:27:08 +02:00
|
|
|
try {
|
|
|
|
const size = await this.getFileSize(path);
|
|
|
|
if (size > 1 * 1024 * 1024) return false;
|
2023-07-07 03:53:06 +02:00
|
|
|
const buffer = await fs.promises.readFile(path);
|
|
|
|
return isSvg(buffer.toString());
|
2022-09-17 20:27:08 +02:00
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get file size
|
|
|
|
*/
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async getFileSize(path: string): Promise<number> {
|
2023-07-27 02:04:19 +02:00
|
|
|
return (await fs.promises.stat(path)).size;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate MD5 hash
|
|
|
|
*/
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-18 20:11:50 +02:00
|
|
|
private async calcHash(path: string): Promise<string> {
|
2022-09-17 20:27:08 +02:00
|
|
|
const hash = crypto.createHash('md5').setEncoding('hex');
|
2023-07-27 02:04:19 +02:00
|
|
|
await stream.pipeline(fs.createReadStream(path), hash);
|
2022-09-17 20:27:08 +02:00
|
|
|
return hash.read();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detect dimensions of image
|
|
|
|
*/
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-18 20:11:50 +02:00
|
|
|
private async detectImageSize(path: string): Promise<{
|
2022-09-17 20:27:08 +02:00
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
wUnits: string;
|
|
|
|
hUnits: string;
|
|
|
|
orientation?: number;
|
|
|
|
}> {
|
|
|
|
const readable = fs.createReadStream(path);
|
|
|
|
const imageSize = await probeImageSize(readable);
|
|
|
|
readable.destroy();
|
|
|
|
return imageSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate average color of image
|
|
|
|
*/
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2024-02-21 06:42:37 +01:00
|
|
|
private getBlurhash(path: string, type: string): Promise<string> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
(await sharpBmp(path, type))
|
2022-09-17 20:27:08 +02:00
|
|
|
.raw()
|
|
|
|
.ensureAlpha()
|
|
|
|
.resize(64, 64, { fit: 'inside' })
|
2023-01-09 06:03:22 +01:00
|
|
|
.toBuffer((err, buffer, info) => {
|
2022-09-17 20:27:08 +02:00
|
|
|
if (err) return reject(err);
|
|
|
|
|
|
|
|
let hash;
|
|
|
|
|
|
|
|
try {
|
2023-01-09 06:03:22 +01:00
|
|
|
hash = encode(new Uint8ClampedArray(buffer), info.width, info.height, 5, 5);
|
2022-09-17 20:27:08 +02:00
|
|
|
} catch (e) {
|
|
|
|
return reject(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(hash);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|