Skip to content
This repository was archived by the owner on Sep 28, 2021. It is now read-only.

Commit feea5e1

Browse files
committed
fix: fix content-type by doing a fall-back using extensions
The JS IPFS gateway was only responding with the correct content-type for some mime-types, see https://github.com/sindresorhus/file-type#supported-file-types. This commit now fall-backs to detecting based on the extension as well. Note that SVGs aren't supported by the `file-type` module.
1 parent ca3f64f commit feea5e1

File tree

10 files changed

+371
-27
lines changed

10 files changed

+371
-27
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"debug": "^3.1.0",
3636
"file-type": "^8.0.0",
3737
"filesize": "^3.6.1",
38+
"get-stream": "^3.0.0",
3839
"ipfs-unixfs": "^0.1.14",
3940
"mime-types": "^2.1.18",
4041
"multihashes": "^0.4.13",

src/index.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
'use strict'
44

5-
const fileType = require('file-type')
6-
const mimeTypes = require('mime-types')
75
const stream = require('stream')
86
const toBlob = require('stream-to-blob')
97

108
const resolver = require('./resolver')
119
const pathUtils = require('./utils/path')
10+
const detectContenType = require('./utils/content-type')
1211

1312
const header = (status = 200, statusText = 'OK', headers = {}) => ({
1413
status,
@@ -73,21 +72,20 @@ const response = (ipfsNode, ipfsPath) => {
7372
})
7473

7574
// return only after first chunk being checked
76-
let filetypeChecked = false
75+
let contentTypeDetected = false
7776
readableStream.on('data', (chunk) => {
7877
// check mime on first chunk
79-
if (filetypeChecked) {
78+
if (contentTypeDetected) {
8079
return
8180
}
8281

83-
filetypeChecked = true
82+
contentTypeDetected = true
8483
// return Response with mime type
85-
const fileSignature = fileType(chunk)
86-
const mimeType = mimeTypes.lookup(fileSignature ? fileSignature.ext : null)
84+
const contentType = detectContenType(ipfsPath, chunk)
8785

8886
if (typeof Blob === 'undefined') {
89-
if (mimeType) {
90-
resolve(new Response(responseStream, header(200, 'OK', { 'Content-Type': mimeTypes.contentType(mimeType) })))
87+
if (contentType) {
88+
resolve(new Response(responseStream, header(200, 'OK', { 'Content-Type': contentType })))
9189
} else {
9290
resolve(new Response(responseStream, header()))
9391
}
@@ -97,8 +95,8 @@ const response = (ipfsNode, ipfsPath) => {
9795
resolve(new Response(err.toString(), header(500, 'Error fetching the file')))
9896
}
9997

100-
if (mimeType) {
101-
resolve(new Response(blob, header(200, 'OK', { 'Content-Type': mimeTypes.contentType(mimeType) })))
98+
if (contentType) {
99+
resolve(new Response(blob, header(200, 'OK', { 'Content-Type': contentType })))
102100
} else {
103101
resolve(new Response(blob, header()))
104102
}

src/utils/content-type.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
const fileType = require('file-type')
4+
const mime = require('mime-types')
5+
6+
const detectContenType = (path, chunk) => {
7+
let fileSignature
8+
9+
// try to guess the filetype based on the first bytes
10+
// note that `file-type` doesn't support svgs, thereore we assume it's a svg if path looks like it
11+
if (!path.endsWith('.svg')) {
12+
fileSignature = fileType(chunk)
13+
}
14+
15+
// if we were unable to, fallback to the `path` which might contain the extension
16+
const mimeType = mime.lookup(fileSignature ? fileSignature.ext : path)
17+
18+
return mime.contentType(mimeType)
19+
}
20+
21+
module.exports = detectContenType

test/fixtures/test-mime-types/cat.jpg

433 KB
Loading
Loading
+55
Loading
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
Website
4+
</body>
5+
</html>

test/fixtures/test-mime-types/pp.txt

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
PRIDE AND PREJUDICE
2+
3+
By Jane Austen
4+
5+
6+
7+
Chapter 1
8+
9+
10+
It is a truth universally acknowledged, that a single man in possession
11+
of a good fortune, must be in want of a wife.
12+
13+
However little known the feelings or views of such a man may be on his
14+
first entering a neighbourhood, this truth is so well fixed in the minds
15+
of the surrounding families, that he is considered the rightful property
16+
of some one or other of their daughters.
17+
18+
"My dear Mr. Bennet," said his lady to him one day, "have you heard that
19+
Netherfield Park is let at last?"
20+
21+
Mr. Bennet replied that he had not.
22+
23+
"But it is," returned she; "for Mrs. Long has just been here, and she
24+
told me all about it."
25+
26+
Mr. Bennet made no answer.
27+
28+
"Do you not want to know who has taken it?" cried his wife impatiently.
29+
30+
"_You_ want to tell me, and I have no objection to hearing it."
31+
32+
This was invitation enough.
33+
34+
"Why, my dear, you must know, Mrs. Long says that Netherfield is taken
35+
by a young man of large fortune from the north of England; that he came
36+
down on Monday in a chaise and four to see the place, and was so much
37+
delighted with it, that he agreed with Mr. Morris immediately; that he
38+
is to take possession before Michaelmas, and some of his servants are to
39+
be in the house by the end of next week."
40+
41+
"What is his name?"
42+
43+
"Bingley."
44+
45+
"Is he married or single?"
46+
47+
"Oh! Single, my dear, to be sure! A single man of large fortune; four or
48+
five thousand a year. What a fine thing for our girls!"
49+
50+
"How so? How can it affect them?"
51+
52+
"My dear Mr. Bennet," replied his wife, "how can you be so tiresome! You
53+
must know that I am thinking of his marrying one of them."
54+
55+
"Is that his design in settling here?"
56+
57+
"Design! Nonsense, how can you talk so! But it is very likely that he
58+
_may_ fall in love with one of them, and therefore you must visit him as
59+
soon as he comes."
60+
61+
"I see no occasion for that. You and the girls may go, or you may send
62+
them by themselves, which perhaps will be still better, for as you are
63+
as handsome as any of them, Mr. Bingley may like you the best of the
64+
party."
65+
66+
"My dear, you flatter me. I certainly _have_ had my share of beauty, but
67+
I do not pretend to be anything extraordinary now. When a woman has five
68+
grown-up daughters, she ought to give over thinking of her own beauty."
69+
70+
"In such cases, a woman has not often much beauty to think of."
71+
72+
"But, my dear, you must indeed go and see Mr. Bingley when he comes into
73+
the neighbourhood."
74+
75+
"It is more than I engage for, I assure you."
76+
77+
"But consider your daughters. Only think what an establishment it would
78+
be for one of them. Sir William and Lady Lucas are determined to
79+
go, merely on that account, for in general, you know, they visit no
80+
newcomers. Indeed you must go, for it will be impossible for _us_ to
81+
visit him if you do not."
82+
83+
"You are over-scrupulous, surely. I dare say Mr. Bingley will be very
84+
glad to see you; and I will send a few lines by you to assure him of my
85+
hearty consent to his marrying whichever he chooses of the girls; though
86+
I must throw in a good word for my little Lizzy."
87+
88+
"I desire you will do no such thing. Lizzy is not a bit better than the
89+
others; and I am sure she is not half so handsome as Jane, nor half so
90+
good-humoured as Lydia. But you are always giving _her_ the preference."
91+
92+
"They have none of them much to recommend them," replied he; "they are
93+
all silly and ignorant like other girls; but Lizzy has something more of
94+
quickness than her sisters."
95+
96+
"Mr. Bennet, how _can_ you abuse your own children in such a way? You
97+
take delight in vexing me. You have no compassion for my poor nerves."
98+
99+
"You mistake me, my dear. I have a high respect for your nerves. They
100+
are my old friends. I have heard you mention them with consideration
101+
these last twenty years at least."
102+
103+
"Ah, you do not know what I suffer."
104+
105+
"But I hope you will get over it, and live to see many young men of four
106+
thousand a year come into the neighbourhood."
107+
108+
"It will be no use to us, if twenty such should come, since you will not
109+
visit them."
110+
111+
"Depend upon it, my dear, that when there are twenty, I will visit them
112+
all."
113+
114+
Mr. Bennet was so odd a mixture of quick parts, sarcastic humour,
115+
reserve, and caprice, that the experience of three-and-twenty years had
116+
been insufficient to make his wife understand his character. _Her_ mind
117+
was less difficult to develop. She was a woman of mean understanding,
118+
little information, and uncertain temper. When she was discontented,
119+
she fancied herself nervous. The business of her life was to get her
120+
daughters married; its solace was visiting and news.

0 commit comments

Comments
 (0)