34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
|
|
import { UploadedFileTypes } from '@/common/enums/enums'
|
||
|
|
import { Injectable } from '@nestjs/common'
|
||
|
|
import { StorageService } from '../storage/storage.service'
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class UploaderService {
|
||
|
|
constructor(private readonly storageService: StorageService) {}
|
||
|
|
|
||
|
|
async uploadFile(file: Express.Multer.File, type: UploadedFileTypes) {
|
||
|
|
const uploaded = await this.storageService.uploadFile(file, type)
|
||
|
|
return uploaded
|
||
|
|
|
||
|
|
// await fs.mkdir(dirPath, { recursive: true })
|
||
|
|
// await fs.writeFile(filePath, buffer)
|
||
|
|
|
||
|
|
// Return a URL path, e.g. /uploads/{accountId}/{type}/{filename}
|
||
|
|
// return `/uploads/${type}/${filename}`
|
||
|
|
}
|
||
|
|
|
||
|
|
async deleteFile(url: string) {
|
||
|
|
const key = url?.split('/').pop()
|
||
|
|
if (key) {
|
||
|
|
return await this.storageService.deleteFile(key)
|
||
|
|
}
|
||
|
|
return {}
|
||
|
|
|
||
|
|
// await fs.mkdir(dirPath, { recursive: true })
|
||
|
|
// await fs.writeFile(filePath, buffer)
|
||
|
|
|
||
|
|
// Return a URL path, e.g. /uploads/{accountId}/{type}/{filename}
|
||
|
|
// return `/uploads/${type}/${filename}`
|
||
|
|
}
|
||
|
|
}
|