Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to typescript #49

Open
alepacheco opened this issue Mar 15, 2024 · 2 comments · May be fixed by #50
Open

Upgrade to typescript #49

alepacheco opened this issue Mar 15, 2024 · 2 comments · May be fixed by #50
Labels
bounty:$10 Bounty applies for fixing this issue (Parse Bounty Program) type:feature

Comments

@alepacheco
Copy link

I have written a TS version for myself, feel free to use the same code although it would need changes to be compiled

import { Storage, StorageOptions } from '@google-cloud/storage';


interface GCSAdapterOptions {
    projectId?: string;
    keyFilename?: string;
    bucket: string;
    bucketPrefix?: string;
    directAccess?: boolean;
    baseUrl?: string;
}

type GCSAdapterConstructorArgs = [GCSAdapterOptions];
function optionsFromArguments(args: GCSAdapterConstructorArgs): GCSAdapterOptions {
    const defaultOptions: Partial<GCSAdapterOptions> = {
        projectId: process.env['GCP_PROJECT_ID'],
        keyFilename: process.env['GCP_KEYFILE_PATH'],
        bucket: process.env['GCS_BUCKET'],
        bucketPrefix: process.env['GCS_BUCKET_PREFIX'] || '',
        directAccess: process.env['GCS_DIRECT_ACCESS'] === 'true' ? true : false
    };

    return {
        ...defaultOptions,
        ...args[0]
    };
}

class GCSAdapter {
    private _bucket: string;
    private _bucketPrefix: string;
    private _directAccess: boolean;
    private _gcsClient: Storage;
    private _baseUrl: string;

    constructor(...args: GCSAdapterConstructorArgs) {
        let options = optionsFromArguments(args);

        this._bucket = options.bucket;
        this._bucketPrefix = options.bucketPrefix || '';
        this._directAccess = options.directAccess || false;
        this._baseUrl = options.baseUrl || 'https://storage.googleapis.com/';
        this._gcsClient = new Storage(options as StorageOptions);
    }

    async createFile(filename: string, data: Buffer | string, contentType?: string): Promise<void> {
        const params = {
            metadata: {
                contentType: contentType || 'application/octet-stream',
            },
        };

        const file = this._gcsClient.bucket(this._bucket).file(this._bucketPrefix + filename);
        await new Promise((resolve, reject) => {
            const uploadStream = file.createWriteStream(params);
            uploadStream.on('error', reject);
            uploadStream.on('finish', resolve);
            uploadStream.end(data);
        });

        if (this._directAccess) {
            await file.makePublic().catch(err => {
                throw new Error(`Error making file public: ${err}`);
            });
        }
    }

    async deleteFile(filename: string): Promise<void> {
        try {
            const file = this._gcsClient.bucket(this._bucket).file(this._bucketPrefix + filename);
            await file.delete();
        } catch (err) {
            throw new Error(`Error deleting file ${filename}: ${err}`);
        }
    }

    async getFileData(filename: string): Promise<Buffer> {
        try {
            const file = this._gcsClient.bucket(this._bucket).file(this._bucketPrefix + filename);
            const [exists] = await file.exists();
            if (!exists) {
                throw new Error(`File ${filename} does not exist.`);
            }
            const [data] = await file.download();
            return data;
        } catch (err) {
            throw new Error(`Error downloading file ${filename}: ${err}`);
        }
    }


    getFileLocation(config: { mount: string; applicationId: string; }, filename: string): string {
        if (this._directAccess) {
            return `${this._baseUrl}${this._bucket}/${this._bucketPrefix + filename}`;
        }
        return `${config.mount}/files/${config.applicationId}/${encodeURIComponent(filename)}`;
    }
}


export default GCSAdapter;
@mtrezza
Copy link
Member

mtrezza commented Mar 15, 2024

Thanks for providing the code, would you want to make a PR?

@mtrezza mtrezza added the bounty:$10 Bounty applies for fixing this issue (Parse Bounty Program) label Mar 15, 2024
@alepacheco
Copy link
Author

Created PR: #50

@mtrezza mtrezza linked a pull request Apr 15, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bounty:$10 Bounty applies for fixing this issue (Parse Bounty Program) type:feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants