Fix SVG detection (#4401)

* Fix SVG detection

* remove unnecessary import
This commit is contained in:
MeiMei 2019-03-03 08:48:02 +09:00 committed by syuilo
parent c99a64d167
commit c6055b3fd3
3 changed files with 16 additions and 4 deletions

12
src/misc/check-svg.ts Normal file
View file

@ -0,0 +1,12 @@
import * as fs from 'fs';
import * as isSvg from 'is-svg';
export default function(path: string) {
try {
const size = fs.statSync(path).size;
if (size > 1 * 1024 * 1024) return false;
return isSvg(fs.readFileSync(path));
} catch {
return false;
}
}

View file

@ -4,10 +4,10 @@ import * as tmp from 'tmp';
import * as Koa from 'koa'; import * as Koa from 'koa';
import * as request from 'request'; import * as request from 'request';
import * as fileType from 'file-type'; import * as fileType from 'file-type';
import * as isSvg from 'is-svg';
import { serverLogger } from '..'; import { serverLogger } from '..';
import config from '../../config'; import config from '../../config';
import { IImage, ConvertToPng } from '../../services/drive/image-processor'; import { IImage, ConvertToPng } from '../../services/drive/image-processor';
import checkSvg from '../../misc/check-svg';
export async function proxyMedia(ctx: Koa.BaseContext) { export async function proxyMedia(ctx: Koa.BaseContext) {
const url = 'url' in ctx.query ? ctx.query.url : 'https://' + ctx.params.url; const url = 'url' in ctx.query ? ctx.query.url : 'https://' + ctx.params.url;
@ -102,7 +102,7 @@ async function detectMine(path: string) {
const type = fileType(buffer); const type = fileType(buffer);
if (type) { if (type) {
res([type.mime, type.ext]); res([type.mime, type.ext]);
} else if (isSvg(buffer)) { } else if (checkSvg(path)) {
res(['image/svg+xml', 'svg']); res(['image/svg+xml', 'svg']);
} else { } else {
// 種類が同定できなかったら application/octet-stream にする // 種類が同定できなかったら application/octet-stream にする

View file

@ -7,7 +7,6 @@ import * as Minio from 'minio';
import * as uuid from 'uuid'; import * as uuid from 'uuid';
import * as sharp from 'sharp'; import * as sharp from 'sharp';
import * as fileType from 'file-type'; import * as fileType from 'file-type';
import * as isSvg from 'is-svg';
import DriveFile, { IMetadata, getDriveFileBucket, IDriveFile } from '../../models/drive-file'; import DriveFile, { IMetadata, getDriveFileBucket, IDriveFile } from '../../models/drive-file';
import DriveFolder from '../../models/drive-folder'; import DriveFolder from '../../models/drive-folder';
@ -26,6 +25,7 @@ import { GenerateVideoThumbnail } from './generate-video-thumbnail';
import { driveLogger } from './logger'; import { driveLogger } from './logger';
import { IImage, ConvertToJpeg, ConvertToWebp, ConvertToPng } from './image-processor'; import { IImage, ConvertToJpeg, ConvertToWebp, ConvertToPng } from './image-processor';
import Instance from '../../models/instance'; import Instance from '../../models/instance';
import checkSvg from '../../misc/check-svg';
const logger = driveLogger.createSubLogger('register', 'yellow'); const logger = driveLogger.createSubLogger('register', 'yellow');
@ -311,7 +311,7 @@ export default async function(
const type = fileType(buffer); const type = fileType(buffer);
if (type) { if (type) {
res([type.mime, type.ext]); res([type.mime, type.ext]);
} else if (isSvg(buffer)) { } else if (checkSvg(path)) {
res(['image/svg+xml', 'svg']); res(['image/svg+xml', 'svg']);
} else { } else {
// 種類が同定できなかったら application/octet-stream にする // 種類が同定できなかったら application/octet-stream にする