Skip to content

Commit

Permalink
リファクタ
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Jan 2, 2020
1 parent e98327f commit 3ffc7e2
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions src/misc/detect-mine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import * as fileType from 'file-type';
import checkSvg from '../misc/check-svg';
const probeImageSize = require('probe-image-size');

const TYPE_OCTET_STREAM = {
mime: 'application/octet-stream',
ext: null as string
};

const TYPE_SVG = {
mime: 'image/svg+xml',
ext: 'svg'
};

export async function detectMine(path: string) {
let type = await detectType(path);

Expand All @@ -11,18 +21,12 @@ export async function detectMine(path: string) {

// うまく判定できない画像は octet-stream にする
if (!imageSize) {
type = {
mime: 'application/octet-stream',
ext: null
};
type = TYPE_OCTET_STREAM;
}

// 制限を超えている画像は octet-stream にする
if (imageSize.wUnits === 'px' && (imageSize.width > 16383 || imageSize.height > 16383)) {
type = {
mime: 'application/octet-stream',
ext: null
};
type = TYPE_OCTET_STREAM;
}
}

Expand All @@ -33,10 +37,7 @@ async function detectType(path: string) {
// Check 0 byte
const fileSize = await detectFileSize(path);
if (fileSize === 0) {
return {
mime: 'application/octet-stream',
ext: null
};
return TYPE_OCTET_STREAM;
}

const readable = fs.createReadStream(path);
Expand All @@ -46,10 +47,7 @@ async function detectType(path: string) {
if (type) {
// XMLはSVGかもしれない
if (type.mime === 'application/xml' && checkSvg(path)) {
return {
mime: 'image/svg+xml',
ext: 'svg'
};
return TYPE_SVG;
}

return {
Expand All @@ -60,17 +58,11 @@ async function detectType(path: string) {

// 種類が不明でもSVGかもしれない
if (checkSvg(path)) {
return {
mime: 'image/svg+xml',
ext: 'svg'
};
return TYPE_SVG;
}

// それでも種類が不明なら application/octet-stream にする
return {
mime: 'application/octet-stream',
ext: null
};
return TYPE_OCTET_STREAM;
}

async function detectFileSize(path: string) {
Expand Down

0 comments on commit 3ffc7e2

Please sign in to comment.