Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import { Injectable, NotFoundException } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { MediaAsset } from './schemas/media.schema'; import { StorageHubService } from '../storage/storage-hub.service'; import * as path from 'path'; @Injectable() export class MediaService { constructor( @InjectModel(MediaAsset.name) private mediaModel: Model<MediaAsset>, private readonly storageService: StorageHubService, ) {} async uploadMedia( tenantId: string, file: { originalname: string; mimetype: string; size: number; buffer: Buffer; }, tags: string[] = [], uploadedBy?: string, ): Promise<MediaAsset> { const filename = `${Date.now()}_${path.basename(file.originalname)}`; const storageKey = `tenants/${tenantId}/media/${filename}`; const uploaded = await this.storageService.uploadFile(tenantId, { key: storageKey, filename: file.originalname, mimeType: file.mimetype, buffer: file.buffer, }); const publicUrl = await this.storageService.getDownloadUrl( tenantId, storageKey, ); return this.mediaModel.create({ tenantId, filename: file.originalname, mimeType: file.mimetype, sizeBytes: file.size, storageKey: uploaded.key, publicUrl, tags, uploadedBy, }); } async getMedia(tenantId: string, mediaId: string): Promise<MediaAsset> { const doc = await this.mediaModel.findOne({ _id: mediaId, tenantId }); if (!doc) throw new NotFoundException('Media asset not found'); return doc; } async listMedia(tenantId: string, tags?: string[]): Promise<MediaAsset[]> { const query: any = { tenantId }; if (tags && tags.length > 0) { query.tags = { $in: tags }; } return this.mediaModel.find(query).sort({ createdAt: -1 }).exec(); } async deleteMedia(tenantId: string, mediaId: string): Promise<void> { const doc = await this.getMedia(tenantId, mediaId); await this.storageService.deleteFile(tenantId, doc.storageKey); await this.mediaModel.deleteOne({ _id: mediaId }); } } |