From 939b5c75cd8e11c733516c56aabc9fd6cf228692 Mon Sep 17 00:00:00 2001 From: Roman Bobrovskyi Date: Fri, 28 Feb 2025 09:25:29 +0100 Subject: [PATCH] feat: updating getting content-type of the file during upload to s3 --- package-lock.json | 8 ++++++++ package.json | 2 ++ src/common/aws.ts | 14 ++------------ 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index 857d79e..de82a5b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,6 +27,7 @@ "esbuild": "0.21.4", "express": "4.21.2", "lodash": "4.17.21", + "mime-types": "2.1.35", "yargs": "17.7.2" }, "bin": { @@ -40,6 +41,7 @@ "@types/express": "5.0.0", "@types/jest": "29.5.14", "@types/lodash": "4.17.13", + "@types/mime-types": "2.1.4", "@types/node": "20.12.11", "@types/yargs": "17.0.32", "@typescript-eslint/eslint-plugin": "7.1.0", @@ -5718,6 +5720,12 @@ "integrity": "sha512-/pyBZWSLD2n0dcHE3hq8s8ZvcETHtEuF+3E7XVt0Ig2nvsVQXdghHVcEkIWjy9A0wKfTn97a/PSDYohKIlnP/w==", "dev": true }, + "node_modules/@types/mime-types": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/@types/mime-types/-/mime-types-2.1.4.tgz", + "integrity": "sha512-lfU4b34HOri+kAY5UheuFMWPDOI+OPceBSHZKp69gEyTL/mmJ4cnU6Y/rlme3UL3GyOn6Y42hyIEw0/q8sWx5w==", + "dev": true + }, "node_modules/@types/node": { "version": "20.12.11", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.11.tgz", diff --git a/package.json b/package.json index 098f76f..3472438 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "@types/express": "5.0.0", "@types/jest": "29.5.14", "@types/lodash": "4.17.13", + "@types/mime-types": "2.1.4", "@types/node": "20.12.11", "@types/yargs": "17.0.32", "@typescript-eslint/eslint-plugin": "7.1.0", @@ -78,6 +79,7 @@ "esbuild": "0.21.4", "express": "4.21.2", "lodash": "4.17.21", + "mime-types": "2.1.35", "yargs": "17.7.2" }, "peerDependencies": { diff --git a/src/common/aws.ts b/src/common/aws.ts index 3ceffac..dc48654 100644 --- a/src/common/aws.ts +++ b/src/common/aws.ts @@ -6,6 +6,7 @@ import * as AWS from 'aws-sdk' import { partition } from '@aws-sdk/util-endpoints' import fs from 'node:fs' import path from 'node:path' +import mime from 'mime-types' type GetAWSBasicProps = | { @@ -46,18 +47,7 @@ export const getSTSIdentity = async (props: GetAWSBasicProps) => { export const getFileContentType = (filePath?: string) => { if (!filePath) return - const extension = path.extname(filePath) - - switch (extension) { - case '.css': - return 'text/css' - case '.js': - return 'application/javascript' - case '..html': - return 'text/html' - default: - return 'application/octet-stream' - } + return mime.lookup(filePath) || 'application/octet-stream' } export const uploadFileToS3 = async (s3Client: S3, options: PutObjectCommandInput) => {