From ae0747a5bfb7e8daa47f7680152178d76ac8d9b8 Mon Sep 17 00:00:00 2001 From: Seth Carrino Date: Fri, 24 May 2019 15:11:15 -0400 Subject: [PATCH] fixed a bug not allowing users to use their custom URL set in the config for the S3Client --- src/S3Client.ts | 116 ++++++++++++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 53 deletions(-) diff --git a/src/S3Client.ts b/src/S3Client.ts index 4c7a9e6..e59f32b 100644 --- a/src/S3Client.ts +++ b/src/S3Client.ts @@ -1,4 +1,4 @@ -import shortId from 'short-uuid'; +import shortId from "short-uuid"; import { dateYMD, xAmzDate } from "./Date"; import { IConfig, DeleteResponse, UploadResponse } from "./types"; import { throwError } from "./ErrorThrower"; @@ -7,62 +7,72 @@ import Policy from "./Policy"; import Signature from "./Signature"; class S3Client { - private config: IConfig; - constructor(config: IConfig) { - this.config = config; - } - public async uploadFile(file: File, newFileName?: string): Promise { - throwError(this.config, file); + private config: IConfig; + constructor(config: IConfig) { + this.config = config; + } + public async uploadFile( + file: File, + newFileName?: string + ): Promise { + throwError(this.config, file); - const fd = new FormData(); - const fileExtension: string = file.type.split('/')[1]; - const fileName: string = `${newFileName || shortId.generate()}.${fileExtension}`; - const key: string = `${this.config.dirName ? this.config.dirName + "/" : ""}${fileName}`; - const url: string = GetUrl(this.config); - fd.append("key", key); - fd.append("acl", "public-read"); - fd.append("Content-Type", file.type); - fd.append("x-amz-meta-uuid", "14365123651274"); - fd.append("x-amz-server-side-encryption", "AES256"); - fd.append( - "X-Amz-Credential", - `${this.config.accessKeyId}/${dateYMD}/${this.config.region}/s3/aws4_request` - ); - fd.append("X-Amz-Algorithm", "AWS4-HMAC-SHA256"); - fd.append("X-Amz-Date", xAmzDate); - fd.append("x-amz-meta-tag", ""); - fd.append("Policy", Policy.getPolicy(this.config)); - fd.append( - "X-Amz-Signature", - Signature.getSignature(this.config, dateYMD, Policy.getPolicy(this.config)) - ); - fd.append("file", file); + const fd = new FormData(); + const fileExtension: string = file.type.split("/")[1]; + const fileName: string = `${newFileName || + shortId.generate()}.${fileExtension}`; + const key: string = `${ + this.config.dirName ? this.config.dirName + "/" : "" + }${fileName}`; + const url: string = GetUrl(this.config); + fd.append("key", key); + fd.append("acl", "public-read"); + fd.append("Content-Type", file.type); + fd.append("x-amz-meta-uuid", "14365123651274"); + fd.append("x-amz-server-side-encryption", "AES256"); + fd.append( + "X-Amz-Credential", + `${this.config.accessKeyId}/${dateYMD}/${ + this.config.region + }/s3/aws4_request` + ); + fd.append("X-Amz-Algorithm", "AWS4-HMAC-SHA256"); + fd.append("X-Amz-Date", xAmzDate); + fd.append("x-amz-meta-tag", ""); + fd.append("Policy", Policy.getPolicy(this.config)); + fd.append( + "X-Amz-Signature", + Signature.getSignature( + this.config, + dateYMD, + Policy.getPolicy(this.config) + ) + ); + fd.append("file", file); - const data = await fetch(url, { method: "post", body: fd }); - if (!data.ok) return Promise.reject(data); - return Promise.resolve({ - bucket: this.config.bucketName, - key: `${this.config.dirName ? this.config.dirName + "/" : ""}${fileName}`, - location: `${url}/${this.config.dirName ? this.config.dirName + "/" : ""}${fileName}`, - status: data.status - }); - } - public async deleteFile(fileName: string): Promise { - const url: string = `https://${this.config.bucketName}.s3${ - this.config.region ? "-" + this.config.region : "" - }.amazonaws.com/${ + const data = await fetch(url, { method: "post", body: fd }); + if (!data.ok) return Promise.reject(data); + return Promise.resolve({ + bucket: this.config.bucketName, + key: `${this.config.dirName ? this.config.dirName + "/" : ""}${fileName}`, + location: `${url}/${ this.config.dirName ? this.config.dirName + "/" : "" - }${fileName}`; + }${fileName}`, + status: data.status + }); + } + public async deleteFile(fileName: string): Promise { + const url: string = GetUrl(this.config); - const deleteResult = await fetch(url, { method: "delete" }); - if (!deleteResult.ok) return Promise.reject(deleteResult); - return Promise.resolve({ - ok: deleteResult.ok, - status: deleteResult.status, - message: "File Deleted", - fileName: fileName - }); - } + const deleteResult = await fetch(url, { method: "delete" }); + if (!deleteResult.ok) return Promise.reject(deleteResult); + return Promise.resolve({ + ok: deleteResult.ok, + status: deleteResult.status, + message: "File Deleted", + fileName: fileName + }); + } } export default S3Client;