2022-02-27 11:07:39 +09:00
|
|
|
import * as fs from 'node:fs';
|
|
|
|
import * as crypto from 'node:crypto';
|
|
|
|
import * as stream from 'node:stream';
|
|
|
|
import * as util from 'node:util';
|
2022-03-07 23:51:34 +09:00
|
|
|
import { fileTypeFromFile } from 'file-type';
|
2020-01-12 16:40:58 +09:00
|
|
|
import isSvg from 'is-svg';
|
2022-02-27 11:07:39 +09:00
|
|
|
import probeImageSize from 'probe-image-size';
|
|
|
|
import sharp from 'sharp';
|
2020-07-19 00:24:07 +09:00
|
|
|
import { encode } from 'blurhash';
|
2020-01-12 16:40:58 +09:00
|
|
|
|
2020-04-11 18:28:40 +09:00
|
|
|
const pipeline = util.promisify(stream.pipeline);
|
|
|
|
|
2020-01-12 16:40:58 +09:00
|
|
|
export type FileInfo = {
|
|
|
|
size: number;
|
|
|
|
md5: string;
|
|
|
|
type: {
|
|
|
|
mime: string;
|
|
|
|
ext: string | null;
|
|
|
|
};
|
|
|
|
width?: number;
|
|
|
|
height?: number;
|
2021-12-03 11:19:28 +09:00
|
|
|
orientation?: number;
|
2020-07-19 00:24:07 +09:00
|
|
|
blurhash?: string;
|
2020-01-12 16:40:58 +09:00
|
|
|
warnings: string[];
|
|
|
|
};
|
|
|
|
|
|
|
|
const TYPE_OCTET_STREAM = {
|
|
|
|
mime: 'application/octet-stream',
|
2021-12-09 23:58:30 +09:00
|
|
|
ext: null,
|
2020-01-12 16:40:58 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
const TYPE_SVG = {
|
|
|
|
mime: 'image/svg+xml',
|
2021-12-09 23:58:30 +09:00
|
|
|
ext: 'svg',
|
2020-01-12 16:40:58 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get file information
|
|
|
|
*/
|
|
|
|
export async function getFileInfo(path: string): Promise<FileInfo> {
|
|
|
|
const warnings = [] as string[];
|
|
|
|
|
|
|
|
const size = await getFileSize(path);
|
|
|
|
const md5 = await calcHash(path);
|
|
|
|
|
|
|
|
let type = await detectType(path);
|
|
|
|
|
|
|
|
// image dimensions
|
|
|
|
let width: number | undefined;
|
|
|
|
let height: number | undefined;
|
2021-12-03 11:19:28 +09:00
|
|
|
let orientation: number | undefined;
|
2020-01-12 16:40:58 +09:00
|
|
|
|
|
|
|
if (['image/jpeg', 'image/gif', 'image/png', 'image/apng', 'image/webp', 'image/bmp', 'image/tiff', 'image/svg+xml', 'image/vnd.adobe.photoshop'].includes(type.mime)) {
|
|
|
|
const imageSize = await detectImageSize(path).catch(e => {
|
|
|
|
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;
|
2021-12-03 11:19:28 +09:00
|
|
|
orientation = imageSize.orientation;
|
2020-01-12 16:40:58 +09:00
|
|
|
|
|
|
|
// 制限を超えている画像は 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}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 00:24:07 +09:00
|
|
|
let blurhash: string | undefined;
|
2020-01-12 16:40:58 +09:00
|
|
|
|
|
|
|
if (['image/jpeg', 'image/gif', 'image/png', 'image/apng', 'image/webp', 'image/svg+xml'].includes(type.mime)) {
|
2020-07-19 00:24:07 +09:00
|
|
|
blurhash = await getBlurhash(path).catch(e => {
|
|
|
|
warnings.push(`getBlurhash failed: ${e}`);
|
2020-01-12 16:40:58 +09:00
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
size,
|
|
|
|
md5,
|
|
|
|
type,
|
|
|
|
width,
|
|
|
|
height,
|
2021-12-03 11:19:28 +09:00
|
|
|
orientation,
|
2020-07-19 00:24:07 +09:00
|
|
|
blurhash,
|
2020-01-12 16:40:58 +09:00
|
|
|
warnings,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detect MIME Type and extension
|
|
|
|
*/
|
2021-12-26 01:42:06 +09:00
|
|
|
export async function detectType(path: string): Promise<{
|
|
|
|
mime: string;
|
|
|
|
ext: string | null;
|
|
|
|
}> {
|
2020-01-12 16:40:58 +09:00
|
|
|
// Check 0 byte
|
|
|
|
const fileSize = await getFileSize(path);
|
|
|
|
if (fileSize === 0) {
|
|
|
|
return TYPE_OCTET_STREAM;
|
|
|
|
}
|
|
|
|
|
2022-03-07 23:51:34 +09:00
|
|
|
const type = await fileTypeFromFile(path);
|
2020-01-12 16:40:58 +09:00
|
|
|
|
|
|
|
if (type) {
|
|
|
|
// XMLはSVGかもしれない
|
|
|
|
if (type.mime === 'application/xml' && await checkSvg(path)) {
|
|
|
|
return TYPE_SVG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
mime: type.mime,
|
2021-12-09 23:58:30 +09:00
|
|
|
ext: type.ext,
|
2020-01-12 16:40:58 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// 種類が不明でもSVGかもしれない
|
|
|
|
if (await checkSvg(path)) {
|
|
|
|
return TYPE_SVG;
|
|
|
|
}
|
|
|
|
|
|
|
|
// それでも種類が不明なら application/octet-stream にする
|
|
|
|
return TYPE_OCTET_STREAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the file is SVG or not
|
|
|
|
*/
|
|
|
|
export async function checkSvg(path: string) {
|
|
|
|
try {
|
|
|
|
const size = await getFileSize(path);
|
|
|
|
if (size > 1 * 1024 * 1024) return false;
|
|
|
|
return isSvg(fs.readFileSync(path));
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get file size
|
|
|
|
*/
|
|
|
|
export async function getFileSize(path: string): Promise<number> {
|
2020-04-11 18:28:40 +09:00
|
|
|
const getStat = util.promisify(fs.stat);
|
|
|
|
return (await getStat(path)).size;
|
2020-01-12 16:40:58 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate MD5 hash
|
|
|
|
*/
|
|
|
|
async function calcHash(path: string): Promise<string> {
|
2020-04-11 18:28:40 +09:00
|
|
|
const hash = crypto.createHash('md5').setEncoding('hex');
|
|
|
|
await pipeline(fs.createReadStream(path), hash);
|
|
|
|
return hash.read();
|
2020-01-12 16:40:58 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detect dimensions of image
|
|
|
|
*/
|
|
|
|
async function detectImageSize(path: string): Promise<{
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
wUnits: string;
|
|
|
|
hUnits: string;
|
2021-12-03 11:19:28 +09:00
|
|
|
orientation?: number;
|
2020-01-12 16:40:58 +09:00
|
|
|
}> {
|
|
|
|
const readable = fs.createReadStream(path);
|
|
|
|
const imageSize = await probeImageSize(readable);
|
|
|
|
readable.destroy();
|
|
|
|
return imageSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate average color of image
|
|
|
|
*/
|
2020-07-19 00:24:07 +09:00
|
|
|
function getBlurhash(path: string): Promise<string> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
sharp(path)
|
|
|
|
.raw()
|
|
|
|
.ensureAlpha()
|
|
|
|
.resize(64, 64, { fit: 'inside' })
|
|
|
|
.toBuffer((err, buffer, { width, height }) => {
|
|
|
|
if (err) return reject(err);
|
2020-10-17 20:12:00 +09:00
|
|
|
|
|
|
|
let hash;
|
|
|
|
|
|
|
|
try {
|
|
|
|
hash = encode(new Uint8ClampedArray(buffer), width, height, 7, 7);
|
|
|
|
} catch (e) {
|
|
|
|
return reject(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(hash);
|
2020-07-19 00:24:07 +09:00
|
|
|
});
|
|
|
|
});
|
2020-01-12 16:40:58 +09:00
|
|
|
}
|