neet/refined-itsukara-link

View on GitHub
packages/@neet/vschedule-api/src/infra/services/storage-cloud-storage.ts

Summary

Maintainability
A
35 mins
Test Coverage
import { Storage as CloudStorage } from '@google-cloud/storage';
import { inject, injectable } from 'inversify';

import { File, IConfig, IStorage } from '../../app';
import { TYPES } from '../../types';

@injectable()
export class StorageCloudStorage implements IStorage {
  private readonly _bucket: string;

  constructor(
    @inject(TYPES.Config)
    config: IConfig,
  ) {
    this._bucket = config.storage.bucket;
  }

  private readonly _storage = new CloudStorage();

  async create(filename: string, source: Buffer): Promise<File> {
    const bucket = this._storage.bucket(this._bucket);
    const file = bucket.file(filename);
    await file.save(source);

    return {
      filename: file.name,
      bucket: file.bucket.name,
    };
  }
}