Image Upload, spinner aktivirt, listings & details überarbeitet
This commit is contained in:
@@ -1,18 +1,20 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { fstat, readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import { ImageProperty } from 'src/models/main.model.js';
|
||||
|
||||
import { ImageProperty } from '../models/main.model.js';
|
||||
import sharp from 'sharp';
|
||||
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
|
||||
import { Logger } from 'winston';
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
@Injectable()
|
||||
export class FileService {
|
||||
private subscriptions: any;
|
||||
constructor() {
|
||||
constructor(@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger) {
|
||||
this.loadSubscriptions();
|
||||
fs.ensureDirSync(`./pictures`);
|
||||
fs.ensureDirSync(`./pictures/profile`);
|
||||
@@ -20,42 +22,60 @@ export class FileService {
|
||||
fs.ensureDirSync(`./pictures/property`);
|
||||
}
|
||||
private loadSubscriptions(): void {
|
||||
const filePath = join(__dirname,'..', 'assets', 'subscriptions.json');
|
||||
const filePath = join(__dirname, '..', 'assets', 'subscriptions.json');
|
||||
const rawData = readFileSync(filePath, 'utf8');
|
||||
this.subscriptions = JSON.parse(rawData);
|
||||
}
|
||||
getSubscriptions() {
|
||||
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);
|
||||
async storeProfilePicture(file: Express.Multer.File, userId: string) {
|
||||
const suffix = file.mimetype.includes('png') ? 'png' : 'jpg'
|
||||
await fs.outputFile(`./pictures/profile/${userId}`, file.buffer);
|
||||
}
|
||||
async storeCompanyLogo(file: Express.Multer.File,userId: string){
|
||||
const suffix = file.mimetype.includes('png')?'png':'jpg'
|
||||
await fs.outputFile(`./pictures/logo/${userId}`,file.buffer);
|
||||
hasProfile(userId: string){
|
||||
return fs.existsSync(`./pictures/profile/${userId}`)
|
||||
}
|
||||
async getPropertyImages(listingId: string):Promise<ImageProperty[]>{
|
||||
const result:ImageProperty[]=[]
|
||||
|
||||
async storeCompanyLogo(file: Express.Multer.File, userId: string) {
|
||||
const suffix = file.mimetype.includes('png') ? 'png' : 'jpg'
|
||||
await fs.outputFile(`./pictures/logo/${userId}`, file.buffer);
|
||||
}
|
||||
hasCompanyLogo(userId: string){
|
||||
return fs.existsSync(`./pictures/logo/${userId}`)
|
||||
}
|
||||
|
||||
async getPropertyImages(listingId: string): Promise<ImageProperty[]> {
|
||||
const result: ImageProperty[] = []
|
||||
const directory = `./pictures/property/${listingId}`
|
||||
if (fs.existsSync(directory)){
|
||||
if (fs.existsSync(directory)) {
|
||||
const files = await fs.readdir(directory);
|
||||
files.forEach(f=>{
|
||||
const image:ImageProperty={name:f,id:'',code:''};
|
||||
files.forEach(f => {
|
||||
const image: ImageProperty = { name: f, id: '', code: '' };
|
||||
result.push(image)
|
||||
})
|
||||
return result;
|
||||
} else {
|
||||
return []
|
||||
}
|
||||
|
||||
}
|
||||
async storePropertyPicture(file: Express.Multer.File,listingId: string){
|
||||
const suffix = file.mimetype.includes('png')?'png':'jpg'
|
||||
async hasPropertyImages(listingId: string): Promise<boolean> {
|
||||
const result: ImageProperty[] = []
|
||||
const directory = `./pictures/property/${listingId}`
|
||||
if (fs.existsSync(directory)) {
|
||||
const files = await fs.readdir(directory);
|
||||
return files.length>0
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
async storePropertyPicture(file: Express.Multer.File, listingId: string) {
|
||||
const suffix = file.mimetype.includes('png') ? 'png' : 'jpg'
|
||||
const directory = `./pictures/property/${listingId}`
|
||||
fs.ensureDirSync(`${directory}`);
|
||||
const imageName = await this.getNextImageName(directory);
|
||||
await fs.outputFile(`${directory}/${imageName}`,file.buffer);
|
||||
//await fs.outputFile(`${directory}/${imageName}`, file.buffer);
|
||||
await this.resizeImageToAVIF(file.buffer,150 * 1024,imageName,directory);
|
||||
}
|
||||
async getNextImageName(directory) {
|
||||
try {
|
||||
@@ -64,7 +84,7 @@ export class FileService {
|
||||
.map(file => parseInt(file, 10)) // Dateinamen direkt in Zahlen umwandeln
|
||||
.filter(number => !isNaN(number)) // Sicherstellen, dass die Konvertierung gültig ist
|
||||
.sort((a, b) => a - b); // Aufsteigend sortieren
|
||||
|
||||
|
||||
const nextNumber = imageNumbers.length > 0 ? Math.max(...imageNumbers) + 1 : 1;
|
||||
return `${nextNumber}`; // Keine Endung für den Dateinamen
|
||||
} catch (error) {
|
||||
@@ -72,4 +92,24 @@ export class FileService {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
async resizeImageToAVIF(buffer: Buffer, maxSize: number,imageName:string,directory:string) {
|
||||
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);
|
||||
let timeTaken = Date.now() - start;
|
||||
this.logger.info(`Quality: ${quality} - Time: ${timeTaken} milliseconds`)
|
||||
await sharp(output).toFile(`${directory}/${imageName}.avif`); // Ersetze Dateierweiterung
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user