1.0.9 • Published 3 years ago
jotive-firebase-storage-module v1.0.9
Jotive Nestjs Firebase Storage Module
Nesjs module for storage from Firebase / Google Cloud Storage.
Usage
Needs the following environment variables:
- process.env.FIREBASE_PROJECT_ID
- process.env.GOOGLE_APPLICATION_CREDENTIALS
- process.env.FIREBASE_BUCKET_ID
- (optional) process.env.REDIS_HOST && process.env.REDIS_PORT && process.env.REDIS_DB
- (optional) process.env.PRODUCT_NAME (for redis key prefix)
1. Initialize firebase admin:
import * as admin from 'firebase-admin';
admin.initializeApp({
credential: admin.credential.applicationDefault(),
});
2. Add to imports from the module
import { StorageModule } from '@jotive/jotive-firebase-storage-module';
//...
@Module({
imports: [
StorageModule,
//...
],
//...
})
3. For download of file use StorageService like below in Controller
import { StorageService } from '@jotive/jotive-firebase-storage-module';
constructor(
private readonly storageService: StorageService,
) { }
@Get('latest')
async getLatestFile(@Req() req, @Res() res): Promise<void> {
// path is a url encoded path saved in the database
const internalPath = decodeURIComponent(Path.basename(latest.path));
return this.storageService.downloadBinFile(internalPath, res);
}
@Get('bin/:path')
@UseGuards(ControllerAuthGuard)
async getFile(@CurrentUser() user: User, @Param('path') path: string, @Res() res): Promise<void> {
await this.storageService.downloadBinFile(path, res);
}
4. For download of image use StoreageService like below in Controller
options {width}x{height}{!}? = 0x0 is original and optional ! is fitted 200x200 is cropped 200x200! is fitted {width}w = 200w is only width constraint {height}h = 200h is only height constraint
import { StorageService } from '@jotive/jotive-firebase-storage-module';
constructor(
private readonly storageService: StorageService,
) { }
@Get('image/:options/:path')
@UseGuards(ControllerAuthGuard)
async getImage(@CurrentUser() user: User, @Param('options') options: string, @Param('path') path: string, @Res() res): Promise<void> {
// Some access checks if needed...
await this.storageService.downloadImageFile(options, path, res);
}
5. For upload of file use StorageService like below
import { StorageService, Upload } from '@jotive/jotive-firebase-storage-module';
constructor(
private readonly storageService: StorageService,
) { }
public projectExportPath(id: string, file: string): string {
return `/project/${id}/export/${file}`;
}
// ...
const zipfile = `${project.name}-${date}.zip`;
const exportPath = `${os.tmpdir()}/com.jotive.export/${zipfile}`;
const upload: Upload = {
filename: zipfile,
mimetype: 'application/zip',
createReadStream: () => fs.createReadStream(exportPath),
};
const uploadPath = await this.storageService.writeFile(this.exportService.projectExportPath(project.id, zipfile), upload);
6. For upload of image use StorageService like below
import { StorageService, Upload } from '@jotive/jotive-firebase-storage-module';
constructor(
private readonly storageService: StorageService,
) { }
public projectImagePath(id: string): string {
return `/project/${id}/image`;
}
// For client upload the resolver needs the following type:
// @Args({ name: 'image', type: () => GraphQLUpload, nullable: true }) image?: Upload)
async update(id: string, name: string, mainLocale: string, targetLocales: string[], image?: Upload): Promise<Project> {
// ...
if (image !== undefined) {
if (!image.mimetype.startsWith('image/')) {
throw new HttpException('The file does not contain a image format', HttpStatus.NOT_ACCEPTABLE);
}
project.image = await this.storageService.writeFile(this.projectImagePath(id), image);
}
// ...
}
7. For delete of file use StorageService like below
import { StorageService, Upload } from '@jotive/jotive-firebase-storage-module';
constructor(
private readonly storageService: StorageService,
) { }
// ...
await this.storageService.deleteFile(project.image);
// or if need to filter out
await this.storageService.deleteFile(Path.basename(exportObject.path));