image-cropper component, drag & drop bilder

This commit is contained in:
2024-03-29 20:59:34 +01:00
parent 840d7a63b1
commit 89bb85a512
20 changed files with 478 additions and 261 deletions

View File

@@ -30,8 +30,6 @@ export class FileService {
return this.subscriptions
}
async storeProfilePicture(file: Express.Multer.File, userId: string) {
// const suffix = file.mimetype.includes('png') ? 'png' : 'jpg'
// await fs.outputFile(`./pictures/profile/${userId}`, file.buffer);
let quality = 50;
const output = await sharp(file.buffer)
.resize({ width: 300 })
@@ -45,7 +43,6 @@ export class FileService {
}
async storeCompanyLogo(file: Express.Multer.File, userId: string) {
// const suffix = file.mimetype.includes('png') ? 'png' : 'jpg'
let quality = 50;
const output = await sharp(file.buffer)
.resize({ width: 300 })
@@ -110,17 +107,11 @@ export class FileService {
let quality = 50; // AVIF kann mit niedrigeren Qualitätsstufen gute Ergebnisse erzielen
let output;
let start = Date.now();
// do {
output = await sharp(buffer)
.resize({ width: 1500 })
.avif({ quality }) // Verwende AVIF
//.webp({ quality }) // Verwende Webp
.toBuffer();
// if (output.byteLength > maxSize) {
// quality -= 5; // Justiere Qualität in feineren Schritten
// }
// } while (output.byteLength > maxSize && quality > 0);
await sharp(output).toFile(`${directory}/${imageName}.avif`); // Ersetze Dateierweiterung
let timeTaken = Date.now() - start;
this.logger.info(`Quality: ${quality} - Time: ${timeTaken} milliseconds`)
@@ -141,4 +132,8 @@ export class FileService {
}
return result;
}
deleteImage(path:string){
fs.unlinkSync(path);
}
}

View File

@@ -3,24 +3,22 @@ import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';
import { FileInterceptor } from '@nestjs/platform-express';
import { FileService } from '../file/file.service.js';
import { SelectOptionsService } from '../select-options/select-options.service.js';
@Controller('image')
export class ImageController {
constructor(private fileService:FileService,@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger) {
constructor(private fileService:FileService,
@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
private selectOptions:SelectOptionsService) {
}
@Post('uploadPropertyPicture/:id')
@UseInterceptors(FileInterceptor('file'),)
async uploadFile(@UploadedFile() file: Express.Multer.File,@Param('id') id:string) {
async uploadPropertyPicture(@UploadedFile() file: Express.Multer.File,@Param('id') id:string) {
await this.fileService.storePropertyPicture(file,id);
}
@Get(':id')
async getPropertyImagesById(@Param('id') id:string): Promise<any> {
return await this.fileService.getPropertyImages(id);
}
@Post('uploadProfile/:id')
@UseInterceptors(FileInterceptor('file'),)
async uploadProfile(@UploadedFile() file: Express.Multer.File,@Param('id') id:string) {
@@ -32,7 +30,11 @@ export class ImageController {
async uploadCompanyLogo(@UploadedFile() file: Express.Multer.File,@Param('id') id:string) {
await this.fileService.storeCompanyLogo(file,id);
}
@Get(':id')
async getPropertyImagesById(@Param('id') id:string): Promise<any> {
return await this.fileService.getPropertyImages(id);
}
@Get('profileImages/:userids')
async getProfileImagesForUsers(@Param('userids') userids:string): Promise<any> {
return await this.fileService.getProfileImagesForUsers(userids);
@@ -41,4 +43,16 @@ export class ImageController {
async getCompanyLogosForUsers(@Param('userids') userids:string): Promise<any> {
return await this.fileService.getCompanyLogosForUsers(userids);
}
@Delete('propertyPicture/:listingid/:imagename')
async deletePropertyImagesById(@Param('listingid') listingid:string,@Param('imagename') imagename:string): Promise<any> {
this.fileService.deleteImage(`pictures/property/${listingid}/${imagename}`)
}
@Delete('logo/:userid/')
async deleteLogoImagesById(@Param('id') id:string): Promise<any> {
this.fileService.deleteImage(`pictures/property//${id}`)
}
@Delete('profile/:userid/')
async deleteProfileImagesById(@Param('id') id:string): Promise<any> {
this.fileService.deleteImage(`pictures/property//${id}`)
}
}

View File

@@ -2,9 +2,10 @@ import { Module } from '@nestjs/common';
import { ImageController } from './image.controller.js';
import { ImageService } from './image.service.js';
import { FileService } from '../file/file.service.js';
import { SelectOptionsService } from '../select-options/select-options.service.js';
@Module({
controllers: [ImageController],
providers: [ImageService,FileService]
providers: [ImageService,FileService,SelectOptionsService]
})
export class ImageModule {}

View File

@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { KeyValue, KeyValueStyle } from '../models/main.model.js';
import { ImageType, KeyValue, KeyValueStyle } from '../models/main.model.js';
@Injectable()
export class SelectOptionsService {
@@ -45,6 +45,11 @@ export class SelectOptionsService {
{ name: 'Broker', value: 'broker', icon:'pi-image',bgColorClass:'bg-green-100',textColorClass:'text-green-600' },
{ name: 'Professional', value: 'professional', icon:'pi-globe',bgColorClass:'bg-yellow-100',textColorClass:'text-yellow-600' },
]
public imageTypes:ImageType[] = [
{name:'propertyPicture',upload:'uploadPropertyPicture',delete:'propertyPicture'},
{name:'companyLogo',upload:'uploadCompanyLogo',delete:'logo'},
{name:'profile',upload:'uploadProfile',delete:'profile'},
]
private usStates = [
{ name: 'ALABAMA', abbreviation: 'AL'},
{ name: 'ALASKA', abbreviation: 'AK'},