UNPKG

do-spaces

Version:

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

183 lines (180 loc) 4.37 kB
import AWS from 'aws-sdk'; import mime from 'mime-types'; class Spaces { constructor({ endpoint, accessKey, secret, bucket }) { const spacesEndpoint = new AWS.Endpoint(endpoint); const s3 = new AWS.S3({ endpoint: spacesEndpoint, accessKeyId: accessKey, secretAccessKey: secret }); this.endpoint = endpoint; this.bucket = bucket; this.s3 = s3; } _removeLeadingSlash(pathname) { return pathname[0] === '/' ? pathname.slice(1) : pathname; } _getContentTypeFromExtension(pathname) { const _split = pathname.split('.'); const extension = _split[_split.length - 1]; return mime.lookup(extension) || 'application/octet-stream'; } async createFolder({ path, awsParams }) { if (path[path.length - 1] !== '/') { throw new Error("do-spaces ~ createFolder - path must end with '/'"); } const params = { Bucket: this.bucket, Key: this._removeLeadingSlash(path), ...awsParams }; return await this.s3.putObject(params).promise(); } /** * Removed all files inside folder */ async deleteFolder({ path, awsListParams, awsDeleteParams }) { if (path[path.length - 1] !== '/') { throw new Error("do-spaces ~ deleteFolder - path must end with '/'"); } const that = this; // @ts-ignore async function _delete({ path, nextMarker }) { const { Contents, NextMarker } = await that.listFiles({ maxFiles: 1000, path, nextMarker, ...awsListParams }); if (Contents && Contents.length) { const _filesToDelete = Contents.map(item => { return { Key: item.Key }; }); const params = { Bucket: that.bucket, Delete: { Objects: _filesToDelete }, ...awsDeleteParams }; if (NextMarker) { await that.s3.deleteObjects(params).promise(); return await _delete({ path, nextMarker }); } else { return await that.s3.deleteObjects(params).promise(); } } else { console.info(`do-spaces ~ removeFolder - nothing to remove for path ${path}`); return null; } } return await _delete({ path }); } async downloadFile({ pathname, awsParams }) { let _pathname = pathname; const _fullUrl = `https://${this.bucket}.${this.endpoint}`; if (_pathname.indexOf(_fullUrl) === 0) { _pathname = _pathname.replace(_fullUrl, ''); } const params = { Bucket: this.bucket, Key: this._removeLeadingSlash(_pathname), ...awsParams }; return this.s3.getObject(params).promise(); } async listFiles({ maxFiles = 1000, path, nextMarker, awsParams }) { if (path[path.length - 1] !== '/') { throw new Error("do-spaces ~ listFiles - path must end with '/'"); } const params = { Bucket: this.bucket, MaxKeys: maxFiles, Prefix: this._removeLeadingSlash(path), Marker: nextMarker, ...awsParams }; return await this.s3.listObjects(params).promise(); } async uploadFile({ pathname, privacy, file, awsParams }) { const params = { Bucket: this.bucket, Key: this._removeLeadingSlash(pathname), Body: file, ACL: privacy, ContentType: this._getContentTypeFromExtension(pathname), ...awsParams }; return await this.s3.putObject(params).promise(); } async copyFile({ pathname, copiedPathname, privacy, fromBucket, awsParams }) { const _copyPathname = `/${fromBucket || this.bucket}/${this._removeLeadingSlash(copiedPathname)}`; const params = { Bucket: this.bucket, Key: this._removeLeadingSlash(pathname), CopySource: _copyPathname, ACL: privacy, ...awsParams }; return await this.s3.copyObject(params).promise(); } async deleteFile({ pathname, awsParams }) { const params = { Bucket: this.bucket, Key: this._removeLeadingSlash(pathname), ...awsParams }; return await this.s3.deleteObject(params).promise(); } } export default Spaces; export { Spaces }; //# sourceMappingURL=do-spaces.esm.js.map