import { MediaType } from '../enum/medias.enum'; import { createId } from '@paralleldrive/cuid2'; import { BadRequestException } from '@nestjs/common'; import { MediasModel } from '../entities/medias.entity'; import { IBaseMediasAttr } from '../interfaces/base.medias-attr.interface'; import { MediasRepository } from '../medias.repository'; import { ObjectBase } from '@tomei/general'; export abstract class BaseMedias extends ObjectBase { MediaId: string; ObjectId: string; ObjectType: string; Title: string; Description: string; Type: MediaType; IsExternalYN: string; ExternalSource: string; IsEncryptedYN: string; FileName: string; FileExtension: string; FilePath: string; URL: string; CreatedById: string; CreatedAt: Date; UpdatedById: string; UpdatedAt: Date; container: string; private static mediasRepository = new MediasRepository(); protected constructor(media?: IBaseMediasAttr) { super(); if (media) { this.init(media); } } init(media: IBaseMediasAttr) { this.MediaId = media.MediaId ? media.MediaId : createId(); this.ObjectId = media.ObjectId; this.ObjectType = media.ObjectType; this.Title = media.Title; this.Description = media.Description; this.Type = media.Type; this.IsExternalYN = media.IsExternalYN; this.ExternalSource = media.ExternalSource; this.IsEncryptedYN = media.IsEncryptedYN; this.FileName = media.FileName; this.FileExtension = media.FileExtension; this.URL = media.URL && media.URL.includes('media') ? media.URL : this.createSaveLocation(); this.FilePath = media.FilePath && media.IsExternalYN === 'Y' ? media.FilePath : this.createFilePath(); this.CreatedById = media.CreatedById; this.CreatedAt = media.CreatedAt; this.UpdatedById = media.UpdatedById; this.UpdatedAt = media.UpdatedAt; } createSaveLocation(): string { try { const mediaFileStorageType = process.env.MEDIA_STORAGE_TYPE; let basePath: string; if (mediaFileStorageType === 'local') { basePath = process.env.MEDIA_LOCAL_STORAGE_PATH; } else { basePath = process.env.MEDIA_AZUREBLOB_CONTAINER_NAME; } if (!basePath) { throw new BadRequestException( 'MEDIA_LOCAL_STORAGE_PATH and or MEDIA_AZUREBLOB_CONTAINER_NAME not found.', ); } return basePath + `/${this.ObjectType}/${this.ObjectId}`; } catch (error) { throw error; } } createFilePath(): string { try { const fileLocation = this.createSaveLocation(); return `${fileLocation}/${this.FileName}.${this.FileExtension}`; } catch (error) { throw error; } } async create(options?: any): Promise { try { return BaseMedias.mediasRepository.create( { MediaId: this.MediaId, ObjectId: this.ObjectId, ObjectType: this.ObjectType, Title: this.Title, Description: this.Description, Type: this.Type, IsExternalYN: this.IsExternalYN, ExternalSource: this.ExternalSource, IsEncryptedYN: this.IsEncryptedYN, FileName: this.FileName, FileExtension: this.FileExtension, FilePath: this.FilePath, URL: this.URL, CreatedById: this.CreatedById, CreatedAt: this.CreatedAt, UpdatedById: this.UpdatedById, UpdatedAt: this.UpdatedAt, }, options, ); } catch (error) { throw error; } } async update(options?: any): Promise { try { const media = await BaseMedias.mediasRepository.findOne({ where: { MediaId: this.MediaId, }, options, }); if (!media) { throw new BadRequestException('Media not found.'); } return await media.update( { MediaId: this.MediaId, ObjectId: this.ObjectId, ObjectType: this.ObjectType, Title: this.Title, Description: this.Description, Type: this.Type, IsExternalYN: this.IsExternalYN, ExternalSource: this.ExternalSource, IsEncryptedYN: this.IsEncryptedYN, FileName: this.FileName, FileExtension: this.FileExtension, FilePath: this.FilePath, URL: this.URL, CreatedById: this.CreatedById, CreatedAt: this.CreatedAt, UpdatedById: this.UpdatedById, UpdatedAt: this.UpdatedAt, }, options, ); } catch (error) { throw error; } } async delete(options?: any): Promise { try { const media = await BaseMedias.mediasRepository.findOne({ where: { MediaId: this.MediaId, }, options, }); const data = { ...media.get({ plain: true }), }; if (!media) { throw new BadRequestException('Media not found.'); } await media.destroy(options); return data; } catch (error) { throw error; } } async findAll(options?: any): Promise { try { return BaseMedias.mediasRepository.findAll(options); } catch (error) { throw error; } } async findAllWithPagination( options?: any, ): Promise<{ count: number; rows: MediasModel[] }> { try { return BaseMedias.mediasRepository.findAndCountAll(options); } catch (error) { throw error; } } async findOne(options?: any): Promise { try { return BaseMedias.mediasRepository.findOne(options); } catch (error) { throw error; } } }