"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GCSProvider = void 0;
const storage_1 = require("@google-cloud/storage");
const common_1 = require("@nestjs/common");
const path_1 = require("path");
const abstract_provider_1 = require("./abstract.provider");
class GCSProvider extends abstract_provider_1.AbstractProvider {
    constructor(setting) {
        super();
        this.setting = setting;
        this.initValidation();
    }
    async initValidation() {
        if (!this.setting['storage-gcs-keyfile']) {
            throw new common_1.BadRequestException(`You must set the storage-gcs-keyfile in the setting.`);
        }
        if (!this.setting['storage-gcs-bucket']) {
            throw new common_1.BadRequestException(`You must set the storage-gcs-bucket in the setting.`);
        }
        const storage = await this.getClient();
        const bucket = storage.bucket(this.setting['storage-gcs-bucket']);
        const exists = await bucket.exists();
        if (!exists[0]) {
            throw new common_1.BadRequestException(`The bucket "${this.setting['storage-gcs-bucket']}" does not exist.`);
        }
    }
    async getClient() {
        if (this.storage) {
            return this.storage;
        }
        return (this.storage = new storage_1.Storage({
            keyFilename: this.setting['storage-gcs-keyfile'],
        }));
    }
    async uploadFromString(destination, filename, fileContent, mimetype) {
        const storage = await this.getClient();
        const bucket = storage.bucket(this.setting['storage-gcs-bucket']);
        const file = bucket.file(destination + filename);
        return file.save(fileContent, {
            contentType: mimetype,
        });
    }
    async upload(destination, file) {
        const storage = await this.getClient();
        const bucket = storage.bucket(this.setting['storage-gcs-bucket']);
        const fileObject = bucket.file(destination + file.originalname);
        return fileObject.save(file.buffer, {
            contentType: file.mimetype,
        });
    }
    async uploadFromPath(destination, filePath, options) {
        var _a;
        const storage = await this.getClient();
        const bucket = storage.bucket(this.setting['storage-gcs-bucket']);
        const objectPath = [destination.replace(/\/$/, ''), this.getFilename((_a = options === null || options === void 0 ? void 0 : options.filename) !== null && _a !== void 0 ? _a : (0, path_1.basename)(filePath))]
            .filter(Boolean)
            .join('/');
        await bucket.upload(filePath, {
            destination: objectPath,
            contentType: options === null || options === void 0 ? void 0 : options.mimetype,
        });
        return objectPath;
    }
    async readStream(filepath) {
        const storage = await this.getClient();
        const bucket = storage.bucket(this.setting['storage-gcs-bucket']);
        const file = bucket.file(filepath);
        return file.createReadStream();
    }
    async delete(filepath) {
        const storage = await this.getClient();
        const bucket = storage.bucket(this.setting['storage-gcs-bucket']);
        const file = bucket.file(filepath);
        return file.delete();
    }
    async metaData(filepath) {
        const storage = await this.getClient();
        const bucket = storage.bucket(this.setting['storage-gcs-bucket']);
        const file = bucket.file(filepath);
        return file.getMetadata();
    }
    async buffer(filepath) {
        const storage = await this.getClient();
        const bucket = storage.bucket(this.setting['storage-gcs-bucket']);
        const file = bucket.file(filepath);
        return file.download();
    }
    async tempURL(filepath, expires) {
        const storage = await this.getClient();
        const bucket = storage.bucket(this.setting['storage-gcs-bucket']);
        const file = bucket.file(filepath);
        return file.getSignedUrl({
            action: 'read',
            expires: expires || 60 * 60,
        });
    }
}
exports.GCSProvider = GCSProvider;
//# sourceMappingURL=gcs.provider.js.map