|
| 1 | +import {Injectable, Logger} from "@nestjs/common"; |
| 2 | +import {BunFile, Glob, S3Client, S3File, S3ListObjectsResponse} from "bun"; |
| 3 | +import {MiscService} from "../misc/misc.service"; |
| 4 | + |
| 5 | +@Injectable() |
| 6 | +export class StorageService{ |
| 7 | + private readonly s3Client?: S3Client; |
| 8 | + private readonly logger: Logger = new Logger(StorageService.name); |
| 9 | + |
| 10 | + constructor( |
| 11 | + private readonly miscService: MiscService, |
| 12 | + ){ |
| 13 | + if(process.env.S3_ENDPOINT) |
| 14 | + this.s3Client = new S3Client({ |
| 15 | + endpoint: process.env.S3_ENDPOINT, |
| 16 | + bucket: process.env.S3_BUCKET_NAME, |
| 17 | + region: process.env.S3_REGION, |
| 18 | + accessKeyId: process.env.S3_ACCESS_KEY, |
| 19 | + secretAccessKey: process.env.S3_SECRET_KEY, |
| 20 | + }); |
| 21 | + } |
| 22 | + |
| 23 | + private getFileName(sum: string): string{ |
| 24 | + if(this.s3Client) |
| 25 | + return `${sum.substring(0, 2)}/${sum}.webp`; |
| 26 | + return `.storage/${sum.substring(0, 2)}/${sum}.webp`; |
| 27 | + } |
| 28 | + |
| 29 | + async uploadBuffer(data: Buffer): Promise<string>{ |
| 30 | + const sum: string = this.miscService.getSum(data); |
| 31 | + const fileName: string = this.getFileName(sum); |
| 32 | + this.logger.verbose(`Uploading file ${fileName}`); |
| 33 | + let file: BunFile | S3File; |
| 34 | + if(this.s3Client) |
| 35 | + file = this.s3Client.file(fileName); |
| 36 | + else |
| 37 | + file = Bun.file(fileName); |
| 38 | + await file.write(data); |
| 39 | + return sum; |
| 40 | + } |
| 41 | + |
| 42 | + async downloadBuffer(sum: string): Promise<Buffer>{ |
| 43 | + const fileName: string = this.getFileName(sum); |
| 44 | + this.logger.verbose(`Downloading file ${fileName}`); |
| 45 | + if(this.s3Client) |
| 46 | + return Buffer.from(await this.s3Client.file(fileName).arrayBuffer()); |
| 47 | + else |
| 48 | + return Buffer.from(await Bun.file(fileName).arrayBuffer()); |
| 49 | + } |
| 50 | + |
| 51 | + async deleteFile(sum: string): Promise<void>{ |
| 52 | + const fileName: string = this.getFileName(sum); |
| 53 | + this.logger.verbose(`Deleting file ${fileName}`); |
| 54 | + if(this.s3Client) |
| 55 | + await this.s3Client.delete(fileName); |
| 56 | + else |
| 57 | + await Bun.file(fileName).delete(); |
| 58 | + } |
| 59 | + |
| 60 | + async presign(sum: string, expiresIn: number = 60 * 60): Promise<string>{ |
| 61 | + const fileName: string = this.getFileName(sum); |
| 62 | + this.logger.verbose(`Presigning file ${fileName}`); |
| 63 | + if(!this.s3Client) |
| 64 | + throw new Error("S3 client not initialized"); |
| 65 | + return this.s3Client.presign(fileName, { |
| 66 | + expiresIn, |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + async listFiles(take: number = Infinity, skip: number = 0): Promise<(BunFile | S3File)[]>{ |
| 71 | + const files: (BunFile | S3File)[] = []; |
| 72 | + if(this.s3Client){ |
| 73 | + let continuationToken: string | undefined; |
| 74 | + let remainingItems: number = take; |
| 75 | + let totalSkipped: number = 0; |
| 76 | + do{ |
| 77 | + const s3Files: S3ListObjectsResponse = await this.s3Client.list({ |
| 78 | + maxKeys: Math.min(remainingItems + Math.max(0, skip - totalSkipped), 1000), |
| 79 | + continuationToken, |
| 80 | + }); |
| 81 | + let itemsToProcess = s3Files.contents || []; |
| 82 | + if(totalSkipped < skip){ |
| 83 | + const skipInThisBatch: number = Math.min(skip - totalSkipped, itemsToProcess.length); |
| 84 | + itemsToProcess = itemsToProcess.slice(skipInThisBatch); |
| 85 | + totalSkipped += skipInThisBatch; |
| 86 | + } |
| 87 | + const itemsToTake: number = Math.min(remainingItems, itemsToProcess.length); |
| 88 | + for(const file of itemsToProcess.slice(0, itemsToTake)) |
| 89 | + files.push(this.s3Client.file(file.key)); |
| 90 | + remainingItems -= itemsToTake; |
| 91 | + continuationToken = s3Files.nextContinuationToken; |
| 92 | + }while(remainingItems > 0 && continuationToken); |
| 93 | + return files; |
| 94 | + } |
| 95 | + const glob = new Glob("**/*"); |
| 96 | + for(const filePath of glob.scanSync("./.storage")) |
| 97 | + files.push(Bun.file(".storage/" + filePath)); |
| 98 | + return files.slice(skip, take === Infinity ? undefined : take + skip); |
| 99 | + } |
| 100 | +} |
0 commit comments