Skip to content

Commit e815628

Browse files
committed
implement content-type detection
1 parent 873f1d8 commit e815628

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

lib/file-type-stream.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare module "file-type-stream" {
2+
import { Transform } from "stream";
3+
4+
class FileTypeStream extends Transform {
5+
constructor(callback: Function);
6+
_transform(chunk: any, encoding: string, cb: Function): void;
7+
}
8+
9+
export default function(cb: Function): FileTypeStream;
10+
}

lib/util.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
import { Readable } from "stream";
2+
import fileType = require("file-type");
3+
import fileTypeStream from "file-type-stream";
4+
15
export function toArray<T>(maybeArr: T | T[]): T[] {
26
return Array.isArray(maybeArr) ? maybeArr : [maybeArr];
37
}
8+
9+
export function detectContentType(data: Buffer | Readable): Promise<string> {
10+
return new Promise(resolve => {
11+
const handleFileTypeResult = (result: any) => {
12+
return resolve(result ? result.mime : null);
13+
};
14+
15+
if (data instanceof Buffer) {
16+
const result = fileType(data);
17+
return handleFileTypeResult(result);
18+
} else {
19+
data.pipe(fileTypeStream(handleFileTypeResult));
20+
}
21+
});
22+
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@
3838
],
3939
"dependencies": {
4040
"@types/body-parser": "^1.16.3",
41+
"@types/file-type": "^5.2.1",
4142
"@types/node": "^7.0.31",
4243
"axios": "^0.16.2",
43-
"body-parser": "^1.17.2"
44+
"body-parser": "^1.17.2",
45+
"file-type": "^7.2.0",
46+
"file-type-stream": "^1.0.0"
4447
},
4548
"devDependencies": {
4649
"@types/express": "^4.0.35",

test/helpers/LINE_Icon.png

15 KB
Loading

test/util.spec.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { deepEqual } from "assert";
2-
import { toArray } from "../lib/util";
1+
import { deepEqual, equal } from "assert";
2+
import { toArray, detectContentType } from "../lib/util";
3+
import * as fs from "fs";
4+
import { join } from "path";
35

46
describe("util", () => {
57
describe("toArray", () => {
@@ -11,4 +13,22 @@ describe("util", () => {
1113
deepEqual(toArray(1), [1]);
1214
});
1315
});
16+
17+
describe("detectContentType", () => {
18+
it("returns correct result for stream", () => {
19+
const filepath = join(__dirname, "/helpers/LINE_Icon.png");
20+
const readable = fs.createReadStream(filepath);
21+
return detectContentType(readable).then((type: string) => {
22+
equal(type, "image/png");
23+
});
24+
});
25+
26+
it("returns correct result for buffer", () => {
27+
const filepath = join(__dirname, "/helpers/LINE_Icon.png");
28+
const buffer = fs.readFileSync(filepath);
29+
return detectContentType(buffer).then((type: string) => {
30+
equal(type, "image/png");
31+
});
32+
});
33+
});
1434
});

0 commit comments

Comments
 (0)