Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

fixed a bug not allowing users to use their custom URL set in the con… #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 63 additions & 53 deletions src/S3Client.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<UploadResponse> {
throwError(this.config, file);
private config: IConfig;
constructor(config: IConfig) {
this.config = config;
}
public async uploadFile(
file: File,
newFileName?: string
): Promise<UploadResponse> {
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<DeleteResponse> {
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<DeleteResponse> {
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;