Skip to content

Commit c800421

Browse files
committed
Fallback to detecting mime type from file extension
1 parent 163baac commit c800421

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"lodash": "npm:lodash-es@^4.17.21",
8080
"lru-cache": "^7.18.3",
8181
"luxon": "^3.4.0",
82+
"mime": "beta",
8283
"nanoid": "^4.0.2",
8384
"neo4j-driver": "^5.12.0",
8485
"nestjs-console": "^9.0.0",

src/components/file/file.service.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import {
33
PutObjectCommand as PutObject,
44
} from '@aws-sdk/client-s3';
55
import { Injectable } from '@nestjs/common';
6-
import { bufferFromStream, cleanJoin } from '@seedcompany/common';
6+
import { bufferFromStream, cleanJoin, Nil } from '@seedcompany/common';
77
import { Connection } from 'cypher-query-builder';
88
import { fileTypeFromStream } from 'file-type';
99
import { intersection } from 'lodash';
1010
import { Duration } from 'luxon';
11+
import mime from 'mime';
1112
import sanitizeFilename from 'sanitize-filename';
1213
import { Readable } from 'stream';
1314
import {
@@ -278,6 +279,8 @@ export class FileService {
278279
'Only files and directories can be parents of a file version',
279280
);
280281

282+
const name = await this.resolveName(undefined, input);
283+
281284
if (uploadingFile) {
282285
const prevExists = uploadIdInput
283286
? await this.bucket.headObject(uploadId).catch((e) => {
@@ -293,15 +296,20 @@ export class FileService {
293296
);
294297
}
295298
const file = await uploadingFile;
296-
const type = await fileTypeFromStream(file.createReadStream());
299+
300+
let type: string | Nil = file.mimetype;
301+
type = type === 'application/octet-stream' ? null : type;
302+
type ??= (await fileTypeFromStream(file.createReadStream()))?.mime;
303+
type ??= mime.getType(name);
304+
type ??= 'application/octet-stream';
305+
297306
await this.bucket.putObject({
298307
Key: `temp/${uploadId}`,
299-
ContentType: type?.mime ?? file.mimetype,
308+
ContentType: type,
300309
ContentEncoding: file.encoding,
301310
Body: file.createReadStream(),
302311
});
303312
}
304-
const name = await this.resolveName(undefined, input);
305313

306314
const [tempUpload, existingUpload] = await Promise.allSettled([
307315
this.bucket.headObject(`temp/${uploadId}`),

yarn.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)