first commit

This commit is contained in:
Oscar
2026-06-02 15:52:22 +03:00
commit dc44cdd639
105 changed files with 14674 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { eq } from 'drizzle-orm';
import { DrizzleService } from '../../database/drizzle.service';
import { media } from '../../database/schema';
import { StorageService } from '../../storage/storage.service';
@Injectable()
export class MediaService {
constructor(
private readonly drizzleService: DrizzleService,
private readonly storageService: StorageService,
) {}
async uploadMedia(
userId: string,
file: { buffer: Buffer; originalname: string; mimetype: string },
type: 'photo' | 'video',
) {
const folder = type === 'photo' ? 'photos' : 'videos';
const objectName = await this.storageService.uploadFile(
file.buffer,
file.originalname,
file.mimetype,
folder,
);
const publicUrl = this.storageService.getPublicUrl(objectName);
const [newMedia] = await this.drizzleService.db
.insert(media)
.values({ userId, path: publicUrl, type })
.returning();
return newMedia;
}
async getByUserId(userId: string) {
return this.drizzleService.db
.select()
.from(media)
.where(eq(media.userId, userId));
}
async deleteMedia(userId: string, mediaId: string) {
const [found] = await this.drizzleService.db
.select()
.from(media)
.where(eq(media.id, mediaId))
.limit(1);
if (!found || found.userId !== userId) {
throw new NotFoundException('Media not found');
}
const objectName = found.path.split('/').slice(-2).join('/');
await this.storageService.deleteFile(objectName).catch(() => {});
await this.drizzleService.db.delete(media).where(eq(media.id, mediaId));
return { message: 'Media deleted' };
}
}