Adding more type handling around the upload object store function.
This commit is contained in:
parent
913eb96ab0
commit
2f01caa88d
|
@ -22,7 +22,19 @@ type ListParams = {
|
||||||
ContinuationToken?: string
|
ContinuationToken?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UploadParams = {
|
||||||
|
bucket: string
|
||||||
|
filename: string
|
||||||
|
path: string
|
||||||
|
type: string
|
||||||
|
// can be undefined, we will remove it
|
||||||
|
metadata: {
|
||||||
|
[key: string]: string | undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const CONTENT_TYPE_MAP: any = {
|
const CONTENT_TYPE_MAP: any = {
|
||||||
|
txt: "text/plain",
|
||||||
html: "text/html",
|
html: "text/html",
|
||||||
css: "text/css",
|
css: "text/css",
|
||||||
js: "application/javascript",
|
js: "application/javascript",
|
||||||
|
@ -149,20 +161,26 @@ export const upload = async ({
|
||||||
path,
|
path,
|
||||||
type,
|
type,
|
||||||
metadata,
|
metadata,
|
||||||
}: any) => {
|
}: UploadParams) => {
|
||||||
const extension = filename.split(".").pop()
|
const extension = filename.split(".").pop()
|
||||||
const fileBytes = fs.readFileSync(path)
|
const fileBytes = fs.readFileSync(path)
|
||||||
|
|
||||||
const objectStore = ObjectStore(bucketName)
|
const objectStore = ObjectStore(bucketName)
|
||||||
await makeSureBucketExists(objectStore, bucketName)
|
await makeSureBucketExists(objectStore, bucketName)
|
||||||
|
|
||||||
|
let contentType = type
|
||||||
|
if (!contentType) {
|
||||||
|
contentType = extension
|
||||||
|
? CONTENT_TYPE_MAP[extension.toLowerCase()]
|
||||||
|
: CONTENT_TYPE_MAP.txt
|
||||||
|
}
|
||||||
const config: any = {
|
const config: any = {
|
||||||
// windows file paths need to be converted to forward slashes for s3
|
// windows file paths need to be converted to forward slashes for s3
|
||||||
Key: sanitizeKey(filename),
|
Key: sanitizeKey(filename),
|
||||||
Body: fileBytes,
|
Body: fileBytes,
|
||||||
ContentType: type || CONTENT_TYPE_MAP[extension.toLowerCase()],
|
ContentType: contentType,
|
||||||
}
|
}
|
||||||
if (metadata) {
|
if (metadata && typeof metadata === "object") {
|
||||||
// remove any nullish keys from the metadata object, as these may be considered invalid
|
// remove any nullish keys from the metadata object, as these may be considered invalid
|
||||||
for (let key of Object.keys(metadata)) {
|
for (let key of Object.keys(metadata)) {
|
||||||
if (!metadata[key] || typeof metadata[key] !== "string") {
|
if (!metadata[key] || typeof metadata[key] !== "string") {
|
||||||
|
|
Loading…
Reference in New Issue