UNPKG

localbackup

Version:

Utility to make local backups easily and without the hassle.

79 lines 4.07 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.s3StorageProvider = exports.S3StorageProvider = void 0; const node_path_1 = __importDefault(require("node:path")); const aws_sdk_1 = __importDefault(require("aws-sdk")); const fs_extra_1 = __importDefault(require("fs-extra")); const mime_types_1 = __importDefault(require("mime-types")); const env_1 = require("../env"); class S3StorageProvider { constructor() { this.bucketName = String(env_1.env.S3_BUCKET); } getS3Client() { if (this.s3 === undefined) { const endpoint = env_1.env.S3_ENDPOINT !== undefined ? new aws_sdk_1.default.Endpoint(env_1.env.S3_ENDPOINT) : undefined; this.s3 = new aws_sdk_1.default.S3(Object.assign(Object.assign({}, (endpoint !== undefined && { endpoint: endpoint })), { accessKeyId: env_1.env.S3_ACCESS_KEY, secretAccessKey: env_1.env.S3_SECRET_KEY })); } return this.s3; } removeFirstSlash(path) { return path.startsWith('/') ? path.slice(1) : path; } isAvailable() { return __awaiter(this, void 0, void 0, function* () { const hasS3Config = env_1.env.S3_BUCKET !== undefined && env_1.env.S3_ACCESS_KEY !== undefined && env_1.env.S3_SECRET_KEY !== undefined; return hasS3Config; }); } uploadFile(localFilePath, destinationFilePath) { return __awaiter(this, void 0, void 0, function* () { const s3Client = this.getS3Client(); const file = fs_extra_1.default.readFileSync(localFilePath); const mimeType = mime_types_1.default.lookup(node_path_1.default.extname(destinationFilePath)); const parameters = Object.assign({ Bucket: this.bucketName, Key: this.removeFirstSlash(destinationFilePath), Body: file, ACL: 'public-read' }, (mimeType !== false && { ContentType: mimeType })); yield s3Client.upload(parameters).promise(); }); } deleteFile(fileFullPath) { return __awaiter(this, void 0, void 0, function* () { const s3Client = this.getS3Client(); const deleteParams = { Bucket: this.bucketName, Key: this.removeFirstSlash(fileFullPath) }; yield s3Client.deleteObject(deleteParams).promise(); }); } listFiles(directoryPath) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const s3Client = this.getS3Client(); const listParams = { Bucket: this.bucketName, Prefix: this.removeFirstSlash(directoryPath) }; const objects = yield s3Client.listObjectsV2(listParams).promise(); const files = (_b = ((_a = objects === null || objects === void 0 ? void 0 : objects.Contents) !== null && _a !== void 0 ? _a : [])) === null || _b === void 0 ? void 0 : _b.map(content => String(content.Key)); return files !== null && files !== void 0 ? files : []; }); } } exports.S3StorageProvider = S3StorageProvider; S3StorageProvider.providerName = 's3'; exports.s3StorageProvider = new S3StorageProvider(); //# sourceMappingURL=s3Provider.js.map