UNPKG

do-spaces

Version:

Unofficial package for simple managing file operations hosted on "Digital Ocean Spaces" written in Typescript

3 lines (2 loc) 2.69 kB
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=e(require("aws-sdk")),s=e(require("mime-types"));class a{constructor({endpoint:e,accessKey:s,secret:a,bucket:r}){const i=new t.Endpoint(e),n=new t.S3({endpoint:i,accessKeyId:s,secretAccessKey:a});this.endpoint=e,this.bucket=r,this.s3=n}_removeLeadingSlash(e){return"/"===e[0]?e.slice(1):e}_getContentTypeFromExtension(e){const t=e.split(".");return s.lookup(t[t.length-1])||"application/octet-stream"}async createFolder({path:e,awsParams:t}){if("/"!==e[e.length-1])throw new Error("do-spaces ~ createFolder - path must end with '/'");const s={Bucket:this.bucket,Key:this._removeLeadingSlash(e),...t};return await this.s3.putObject(s).promise()}async deleteFolder({path:e,awsListParams:t,awsDeleteParams:s}){if("/"!==e[e.length-1])throw new Error("do-spaces ~ deleteFolder - path must end with '/'");const a=this;return await async function e({path:r,nextMarker:i}){const{Contents:n,NextMarker:o}=await a.listFiles({maxFiles:1e3,path:r,nextMarker:i,...t});if(n&&n.length){const t=n.map(e=>({Key:e.Key})),c={Bucket:a.bucket,Delete:{Objects:t},...s};return o?(await a.s3.deleteObjects(c).promise(),await e({path:r,nextMarker:i})):await a.s3.deleteObjects(c).promise()}return console.info("do-spaces ~ removeFolder - nothing to remove for path "+r),null}({path:e})}async downloadFile({pathname:e,awsParams:t}){let s=e;const a=`https://${this.bucket}.${this.endpoint}`;0===s.indexOf(a)&&(s=s.replace(a,""));const r={Bucket:this.bucket,Key:this._removeLeadingSlash(s),...t};return this.s3.getObject(r).promise()}async listFiles({maxFiles:e=1e3,path:t,nextMarker:s,awsParams:a}){if("/"!==t[t.length-1])throw new Error("do-spaces ~ listFiles - path must end with '/'");const r={Bucket:this.bucket,MaxKeys:e,Prefix:this._removeLeadingSlash(t),Marker:s,...a};return await this.s3.listObjects(r).promise()}async uploadFile({pathname:e,privacy:t,file:s,awsParams:a}){const r={Bucket:this.bucket,Key:this._removeLeadingSlash(e),Body:s,ACL:t,ContentType:this._getContentTypeFromExtension(e),...a};return await this.s3.putObject(r).promise()}async copyFile({pathname:e,copiedPathname:t,privacy:s,fromBucket:a,awsParams:r}){const i=`/${a||this.bucket}/${this._removeLeadingSlash(t)}`,n={Bucket:this.bucket,Key:this._removeLeadingSlash(e),CopySource:i,ACL:s,...r};return await this.s3.copyObject(n).promise()}async deleteFile({pathname:e,awsParams:t}){const s={Bucket:this.bucket,Key:this._removeLeadingSlash(e),...t};return await this.s3.deleteObject(s).promise()}}exports.Spaces=a,exports.default=a; //# sourceMappingURL=do-spaces.cjs.production.min.js.map