@lonelyplanet/dotcom-core
Version:
This package is meant to house some of our more common UI and shared libs across dotcom applications.
41 lines (34 loc) • 1.12 kB
text/typescript
import * as AWS from "aws-sdk";
import * as path from "path";
import { IS3UploadOptions, IUploadResult, IUploader } from "../interfaces/uploader";
import { mimeTypes } from "./mimeTypes";
const s3 = new AWS.S3();
export class S3Uploader implements IUploader {
private bucket: string = "static-asset";
private acl: string = "public-read";
private cacheControl: string = "public, max-age=0, s-maxage=86400";
public async upload(options: IS3UploadOptions) {
const ext = this.extension(options.key);
return new Promise<IUploadResult>((resolve, reject) => {
s3.putObject({
Bucket: options.bucket || this.bucket,
Key: options.key,
ACL: options.acl || this.acl,
Body: options.body,
ContentType: mimeTypes[ext],
CacheControl: options.cacheControl || this.cacheControl,
}, (err, result) => {
if (err) {
return reject(err);
}
return resolve({
key: options.key,
etag: result.ETag,
});
});
});
}
public extension(key) {
return path.extname(key).replace(/^./, "");
}
}