Add blog thumbnail and banner upload endpoints with storage keys.

Store image filenames in DB, resolve paths for cleanup on replace/delete, and reuse product-thumb multer limits.
This commit is contained in:
2026-09-16 11:40:05 +02:00
parent d38def2302
commit abe6208e4e
7 changed files with 138 additions and 15 deletions
@@ -1,7 +1,9 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { resolveBlogImagePath } from '../../../config/uploadPaths';
import type { PaginatedResponse } from '../../../types/PaginatedResponse';
import { removeFileFromDisk } from '../../../utils/removeFileFromDisk';
import type { ListBlogPostsQueryDto } from '../dto/ListBlogPostsQueryDto';
import { UpdateBlogPostDto } from '../dto/UpdateBlogPostDto';
import { BlogPost } from '../entities/BlogPost';
@@ -61,13 +63,46 @@ export class BlogPostsService {
return this.findOne(id);
}
async remove(id: string): Promise<void> {
const exists = await this.blogPostRepo.exists({ where: { id } });
async uploadThumbnail(id: string, filename: string): Promise<BlogPost> {
const post = await this.findOne(id);
if (!exists) {
throw new NotFoundException();
}
const previousStorageKey = post.thumbnailStorageKey;
await this.blogPostRepo.update(id, { thumbnailStorageKey: filename });
await this.removeBlogImageFile(previousStorageKey);
return this.findOne(id);
}
async uploadBanner(id: string, filename: string): Promise<BlogPost> {
const post = await this.findOne(id);
const previousStorageKey = post.bannerStorageKey;
await this.blogPostRepo.update(id, { bannerStorageKey: filename });
await this.removeBlogImageFile(previousStorageKey);
return this.findOne(id);
}
async remove(id: string): Promise<void> {
const post = await this.findOne(id);
await this.blogPostRepo.delete(id);
await Promise.all([
this.removeBlogImageFile(post.thumbnailStorageKey),
this.removeBlogImageFile(post.bannerStorageKey)
]);
}
private async removeBlogImageFile(storageKey: string | null): Promise<void> {
if (!storageKey) {
return;
}
const path = resolveBlogImagePath(storageKey);
await removeFileFromDisk(path, BlogPostsService.name);
}
}