imagePath changed
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
import 'dotenv/config';
|
||||
import { drizzle } from 'drizzle-orm/node-postgres';
|
||||
import { existsSync, readFileSync, readdirSync, statSync, unlinkSync } from 'fs';
|
||||
import fs from 'fs-extra';
|
||||
import { join } from 'path';
|
||||
import pkg from 'pg';
|
||||
import { rimraf } from 'rimraf';
|
||||
import sharp from 'sharp';
|
||||
import { BusinessListing, CommercialPropertyListing, User, UserData } from 'src/models/db.model.js';
|
||||
import { emailToDirName } from 'src/models/main.model.js';
|
||||
import * as schema from './schema.js';
|
||||
const { Pool } = pkg;
|
||||
|
||||
@@ -32,6 +34,11 @@ const targetPathProfile = `./pictures/profile`;
|
||||
deleteFilesOfDir(targetPathProfile);
|
||||
const targetPathLogo = `./pictures/logo`;
|
||||
deleteFilesOfDir(targetPathLogo);
|
||||
const targetPathProperty = `./pictures/property`;
|
||||
deleteFilesOfDir(targetPathProperty);
|
||||
fs.ensureDirSync(`./pictures/logo`);
|
||||
fs.ensureDirSync(`./pictures/profile`);
|
||||
fs.ensureDirSync(`./pictures/property`);
|
||||
for (const userData of usersData) {
|
||||
const user: User = { firstname: '', lastname: '', email: '' };
|
||||
user.licensedIn = [];
|
||||
@@ -58,20 +65,21 @@ for (const userData of usersData) {
|
||||
user.gender = userData.gender;
|
||||
user.created = new Date();
|
||||
user.updated = new Date();
|
||||
const u = await db.insert(schema.users).values(user).returning({ insertedId: schema.users.id, gender: schema.users.gender });
|
||||
generatedUserData.push(u[0].insertedId);
|
||||
const u = await db.insert(schema.users).values(user).returning({ insertedId: schema.users.id, gender: schema.users.gender, email: schema.users.email });
|
||||
generatedUserData.push(u[0]);
|
||||
i++;
|
||||
|
||||
if (u[0].gender === 'male') {
|
||||
male++;
|
||||
const data = readFileSync(`./pictures/profile_base/Mann_${male}.jpg`);
|
||||
await storeProfilePicture(data, u[0].insertedId);
|
||||
const data = readFileSync(`./pictures_base/profile/Mann_${male}.jpg`);
|
||||
await storeProfilePicture(data, emailToDirName(u[0].email));
|
||||
} else {
|
||||
female++;
|
||||
const data = readFileSync(`./pictures/profile_base/Frau_${female}.jpg`);
|
||||
await storeProfilePicture(data, u[0].insertedId);
|
||||
const data = readFileSync(`./pictures_base/profile/Frau_${female}.jpg`);
|
||||
await storeProfilePicture(data, emailToDirName(u[0].email));
|
||||
}
|
||||
const data = readFileSync(`./pictures/logos_base/${i}.jpg`);
|
||||
await storeCompanyLogo(data, u[0].insertedId);
|
||||
const data = readFileSync(`./pictures_base/logo/${i}.jpg`);
|
||||
await storeCompanyLogo(data, emailToDirName(u[0].email));
|
||||
}
|
||||
//Business Listings
|
||||
filePath = `./data/businesses.json`;
|
||||
@@ -82,7 +90,9 @@ for (const business of businessJsonData) {
|
||||
delete business.id;
|
||||
business.created = new Date(business.created);
|
||||
business.updated = new Date(business.created);
|
||||
business.userId = getRandomItem(generatedUserData);
|
||||
const user = getRandomItem(generatedUserData);
|
||||
business.userId = user.insertedId;
|
||||
business.imageName = emailToDirName(user.email);
|
||||
await db.insert(schema.businesses).values(business);
|
||||
}
|
||||
//Corporate Listings
|
||||
@@ -92,14 +102,20 @@ const commercialJsonData = JSON.parse(data) as CommercialPropertyListing[]; // E
|
||||
for (const commercial of commercialJsonData) {
|
||||
const id = commercial.id;
|
||||
delete commercial.id;
|
||||
|
||||
const user = getRandomItem(generatedUserData);
|
||||
commercial.imageOrder = getFilenames(id);
|
||||
commercial.imagePath = id;
|
||||
commercial.imagePath = emailToDirName(user.email);
|
||||
const insertionDate = getRandomDateWithinLastYear();
|
||||
commercial.created = insertionDate;
|
||||
commercial.updated = insertionDate;
|
||||
commercial.userId = getRandomItem(generatedUserData);
|
||||
await db.insert(schema.commercials).values(commercial);
|
||||
commercial.userId = user.insertedId;
|
||||
const result = await db.insert(schema.commercials).values(commercial).returning();
|
||||
//fs.ensureDirSync(`./pictures/property/${result[0].imagePath}/${result[0].serialId}`);
|
||||
try {
|
||||
fs.copySync(`./pictures_base/property/${id}`, `./pictures/property/${result[0].imagePath}/${result[0].serialId}`);
|
||||
} catch (err) {
|
||||
console.log(`----- No pictures available for ${id} ------`);
|
||||
}
|
||||
}
|
||||
|
||||
//End
|
||||
@@ -115,7 +131,7 @@ function getRandomItem<T>(arr: T[]): T {
|
||||
}
|
||||
function getFilenames(id: string): string[] {
|
||||
try {
|
||||
let filePath = `./pictures/property/${id}`;
|
||||
let filePath = `./pictures_base/property/${id}`;
|
||||
return readdirSync(filePath);
|
||||
} catch (e) {
|
||||
return null;
|
||||
@@ -141,14 +157,14 @@ async function storeProfilePicture(buffer: Buffer, userId: string) {
|
||||
await sharp(output).toFile(`./pictures/profile/${userId}.avif`);
|
||||
}
|
||||
|
||||
async function storeCompanyLogo(buffer: Buffer, userId: string) {
|
||||
async function storeCompanyLogo(buffer: Buffer, adjustedEmail: string) {
|
||||
let quality = 50;
|
||||
const output = await sharp(buffer)
|
||||
.resize({ width: 300 })
|
||||
.avif({ quality }) // Verwende AVIF
|
||||
//.webp({ quality }) // Verwende Webp
|
||||
.toBuffer();
|
||||
await sharp(output).toFile(`./pictures/logo/${userId}.avif`); // Ersetze Dateierweiterung
|
||||
await sharp(output).toFile(`./pictures/logo/${adjustedEmail}.avif`); // Ersetze Dateierweiterung
|
||||
// await fs.outputFile(`./pictures/logo/${userId}`, file.buffer);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ CREATE TABLE IF NOT EXISTS "businesses" (
|
||||
"reasonForSale" varchar(255),
|
||||
"brokerLicencing" varchar(255),
|
||||
"internals" text,
|
||||
"imagePath" varchar(200),
|
||||
"created" timestamp,
|
||||
"updated" timestamp,
|
||||
"visits" integer,
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"id": "a27bba95-3910-4b41-b241-ce91f2201311",
|
||||
"id": "fc58c59b-ac5c-406e-8fdb-b05de40aed17",
|
||||
"prevId": "00000000-0000-0000-0000-000000000000",
|
||||
"version": "5",
|
||||
"dialect": "pg",
|
||||
@@ -147,6 +147,12 @@
|
||||
"primaryKey": false,
|
||||
"notNull": false
|
||||
},
|
||||
"imagePath": {
|
||||
"name": "imagePath",
|
||||
"type": "varchar(200)",
|
||||
"primaryKey": false,
|
||||
"notNull": false
|
||||
},
|
||||
"created": {
|
||||
"name": "created",
|
||||
"type": "timestamp",
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
{
|
||||
"idx": 0,
|
||||
"version": "5",
|
||||
"when": 1716417232952,
|
||||
"tag": "0000_melted_doomsday",
|
||||
"when": 1716495198537,
|
||||
"tag": "0000_burly_bruce_banner",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
|
||||
@@ -49,6 +49,7 @@ export const businesses = pgTable('businesses', {
|
||||
reasonForSale: varchar('reasonForSale', { length: 255 }),
|
||||
brokerLicencing: varchar('brokerLicencing', { length: 255 }),
|
||||
internals: text('internals'),
|
||||
imageName: varchar('imagePath', { length: 200 }),
|
||||
created: timestamp('created'),
|
||||
updated: timestamp('updated'),
|
||||
visits: integer('visits'),
|
||||
|
||||
@@ -20,6 +20,9 @@ export class FileService {
|
||||
fs.ensureDirSync(`./pictures/logo`);
|
||||
fs.ensureDirSync(`./pictures/property`);
|
||||
}
|
||||
// ############
|
||||
// Subscriptions
|
||||
// ############
|
||||
private loadSubscriptions(): void {
|
||||
const filePath = join(__dirname, '../..', 'assets', 'subscriptions.json');
|
||||
const rawData = readFileSync(filePath, 'utf8');
|
||||
@@ -28,36 +31,43 @@ export class FileService {
|
||||
getSubscriptions(): Subscription[] {
|
||||
return this.subscriptions;
|
||||
}
|
||||
async storeProfilePicture(file: Express.Multer.File, userId: string) {
|
||||
// ############
|
||||
// Profile
|
||||
// ############
|
||||
async storeProfilePicture(file: Express.Multer.File, adjustedEmail: string) {
|
||||
let quality = 50;
|
||||
const output = await sharp(file.buffer)
|
||||
.resize({ width: 300 })
|
||||
.avif({ quality }) // Verwende AVIF
|
||||
//.webp({ quality }) // Verwende Webp
|
||||
.toBuffer();
|
||||
await sharp(output).toFile(`./pictures/profile/${userId}.avif`);
|
||||
await sharp(output).toFile(`./pictures/profile/${adjustedEmail}.avif`);
|
||||
}
|
||||
hasProfile(userId: string) {
|
||||
return fs.existsSync(`./pictures/profile/${userId}.avif`);
|
||||
hasProfile(adjustedEmail: string) {
|
||||
return fs.existsSync(`./pictures/profile/${adjustedEmail}.avif`);
|
||||
}
|
||||
|
||||
async storeCompanyLogo(file: Express.Multer.File, userId: string) {
|
||||
// ############
|
||||
// Logo
|
||||
// ############
|
||||
async storeCompanyLogo(file: Express.Multer.File, adjustedEmail: string) {
|
||||
let quality = 50;
|
||||
const output = await sharp(file.buffer)
|
||||
.resize({ width: 300 })
|
||||
.avif({ quality }) // Verwende AVIF
|
||||
//.webp({ quality }) // Verwende Webp
|
||||
.toBuffer();
|
||||
await sharp(output).toFile(`./pictures/logo/${userId}.avif`); // Ersetze Dateierweiterung
|
||||
await sharp(output).toFile(`./pictures/logo/${adjustedEmail}.avif`); // Ersetze Dateierweiterung
|
||||
// await fs.outputFile(`./pictures/logo/${userId}`, file.buffer);
|
||||
}
|
||||
hasCompanyLogo(userId: string) {
|
||||
return fs.existsSync(`./pictures/logo/${userId}.avif`) ? true : false;
|
||||
hasCompanyLogo(adjustedEmail: string) {
|
||||
return fs.existsSync(`./pictures/logo/${adjustedEmail}.avif`) ? true : false;
|
||||
}
|
||||
|
||||
async getPropertyImages(listingId: string): Promise<string[]> {
|
||||
// ############
|
||||
// Property
|
||||
// ############
|
||||
async getPropertyImages(imagePath: string, serial: string): Promise<string[]> {
|
||||
const result: string[] = [];
|
||||
const directory = `./pictures/property/${listingId}`;
|
||||
const directory = `./pictures/property/${imagePath}/${serial}`;
|
||||
if (fs.existsSync(directory)) {
|
||||
const files = await fs.readdir(directory);
|
||||
files.forEach(f => {
|
||||
@@ -68,9 +78,9 @@ export class FileService {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
async hasPropertyImages(listingId: string): Promise<boolean> {
|
||||
async hasPropertyImages(imagePath: string, serial: string): Promise<boolean> {
|
||||
const result: ImageProperty[] = [];
|
||||
const directory = `./pictures/property/${listingId}`;
|
||||
const directory = `./pictures/property/${imagePath}/${serial}`;
|
||||
if (fs.existsSync(directory)) {
|
||||
const files = await fs.readdir(directory);
|
||||
return files.length > 0;
|
||||
@@ -78,15 +88,18 @@ export class FileService {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
async storePropertyPicture(file: Express.Multer.File, listingId: string): Promise<string> {
|
||||
async storePropertyPicture(file: Express.Multer.File, imagePath: string, serial: string): Promise<string> {
|
||||
const suffix = file.mimetype.includes('png') ? 'png' : 'jpg';
|
||||
const directory = `./pictures/property/${listingId}`;
|
||||
const directory = `./pictures/property/${imagePath}/${serial}`;
|
||||
fs.ensureDirSync(`${directory}`);
|
||||
const imageName = await this.getNextImageName(directory);
|
||||
//await fs.outputFile(`${directory}/${imageName}`, file.buffer);
|
||||
await this.resizeImageToAVIF(file.buffer, 150 * 1024, imageName, directory);
|
||||
return `${imageName}.avif`;
|
||||
}
|
||||
// ############
|
||||
// utils
|
||||
// ############
|
||||
async getNextImageName(directory) {
|
||||
try {
|
||||
const files = await fs.readdir(directory);
|
||||
@@ -115,22 +128,22 @@ export class FileService {
|
||||
let timeTaken = Date.now() - start;
|
||||
this.logger.info(`Quality: ${quality} - Time: ${timeTaken} milliseconds`);
|
||||
}
|
||||
getProfileImagesForUsers(userids: string) {
|
||||
const ids = userids.split(',');
|
||||
let result = {};
|
||||
for (const id of ids) {
|
||||
result = { ...result, [id]: fs.existsSync(`./pictures/profile/${id}.avif`) };
|
||||
}
|
||||
return result;
|
||||
}
|
||||
getCompanyLogosForUsers(userids: string) {
|
||||
const ids = userids.split(',');
|
||||
let result = {};
|
||||
for (const id of ids) {
|
||||
result = { ...result, [id]: fs.existsSync(`./pictures/logo/${id}.avif`) };
|
||||
}
|
||||
return result;
|
||||
}
|
||||
// getProfileImagesForUsers(userids: string) {
|
||||
// const ids = userids.split(',');
|
||||
// let result = {};
|
||||
// for (const id of ids) {
|
||||
// result = { ...result, [id]: fs.existsSync(`./pictures/profile/${id}.avif`) };
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
// getCompanyLogosForUsers(userids: string) {
|
||||
// const ids = userids.split(',');
|
||||
// let result = {};
|
||||
// for (const id of ids) {
|
||||
// result = { ...result, [id]: fs.existsSync(`./pictures/logo/${id}.avif`) };
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
deleteImage(path: string) {
|
||||
fs.unlinkSync(path);
|
||||
}
|
||||
|
||||
@@ -17,58 +17,54 @@ export class ImageController {
|
||||
@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
|
||||
private selectOptions: SelectOptionsService,
|
||||
) {}
|
||||
|
||||
@Post('uploadPropertyPicture/:imagePath')
|
||||
// ############
|
||||
// Property
|
||||
// ############
|
||||
@Post('uploadPropertyPicture/:imagePath/:serial')
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
async uploadPropertyPicture(@UploadedFile() file: Express.Multer.File, @Param('imagePath') imagePath: string) {
|
||||
const imagename = await this.fileService.storePropertyPicture(file, imagePath);
|
||||
async uploadPropertyPicture(@UploadedFile() file: Express.Multer.File, @Param('imagePath') imagePath: string, @Param('serial') serial: string) {
|
||||
const imagename = await this.fileService.storePropertyPicture(file, imagePath, serial);
|
||||
await this.listingService.addImage(imagePath, imagename);
|
||||
}
|
||||
|
||||
@Post('uploadProfile/:id')
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
async uploadProfile(@UploadedFile() file: Express.Multer.File, @Param('id') id: string) {
|
||||
await this.fileService.storeProfilePicture(file, id);
|
||||
}
|
||||
|
||||
@Post('uploadCompanyLogo/:id')
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
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> {
|
||||
const result = await this.listingService.findById(id, commercials);
|
||||
@Get(':email/:serial')
|
||||
async getPropertyImagesById(@Param('email') adjustedEmail: string, @Param('serial') serial: string): Promise<any> {
|
||||
const result = await this.listingService.findByImagePath(adjustedEmail);
|
||||
const listing = result as CommercialPropertyListing;
|
||||
if (listing.imageOrder) {
|
||||
return listing.imageOrder;
|
||||
} else {
|
||||
const imageOrder = await this.fileService.getPropertyImages(id);
|
||||
const imageOrder = await this.fileService.getPropertyImages(adjustedEmail, serial);
|
||||
listing.imageOrder = imageOrder;
|
||||
this.listingService.updateListing(listing.id, listing, commercials);
|
||||
return imageOrder;
|
||||
}
|
||||
}
|
||||
@Get('profileImages/:userids')
|
||||
async getProfileImagesForUsers(@Param('userids') userids: string): Promise<any> {
|
||||
return await this.fileService.getProfileImagesForUsers(userids);
|
||||
@Delete('propertyPicture/:imagePath/:serial/:imagename')
|
||||
async deletePropertyImagesById(@Param('imagePath') imagePath: string, @Param('serial') serial: string, @Param('imagename') imagename: string): Promise<any> {
|
||||
this.fileService.deleteImage(`pictures/property/${imagePath}/${serial}/${imagename}`);
|
||||
}
|
||||
@Get('companyLogos/:userids')
|
||||
async getCompanyLogosForUsers(@Param('userids') userids: string): Promise<any> {
|
||||
return await this.fileService.getCompanyLogosForUsers(userids);
|
||||
// ############
|
||||
// Profile
|
||||
// ############
|
||||
@Post('uploadProfile/:email')
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
async uploadProfile(@UploadedFile() file: Express.Multer.File, @Param('email') adjustedEmail: string) {
|
||||
await this.fileService.storeProfilePicture(file, adjustedEmail);
|
||||
}
|
||||
|
||||
@Delete('propertyPicture/:imagePath/:imagename')
|
||||
async deletePropertyImagesById(@Param('imagePath') imagePath: string, @Param('imagename') imagename: string): Promise<any> {
|
||||
this.fileService.deleteImage(`pictures/property/${imagePath}/${imagename}`);
|
||||
@Delete('profile/:email/')
|
||||
async deleteProfileImagesById(@Param('email') email: string): Promise<any> {
|
||||
this.fileService.deleteImage(`pictures/profile/${email}.avif`);
|
||||
}
|
||||
@Delete('logo/:userid/')
|
||||
async deleteLogoImagesById(@Param('userid') userid: string): Promise<any> {
|
||||
this.fileService.deleteImage(`pictures/logo/${userid}.avif`);
|
||||
// ############
|
||||
// Logo
|
||||
// ############
|
||||
@Post('uploadCompanyLogo/:email')
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
async uploadCompanyLogo(@UploadedFile() file: Express.Multer.File, @Param('email') adjustedEmail: string) {
|
||||
await this.fileService.storeCompanyLogo(file, adjustedEmail);
|
||||
}
|
||||
@Delete('profile/:userid/')
|
||||
async deleteProfileImagesById(@Param('userid') userid: string): Promise<any> {
|
||||
this.fileService.deleteImage(`pictures/profile/${userid}.avif`);
|
||||
@Delete('logo/:email/')
|
||||
async deleteLogoImagesById(@Param('email') adjustedEmail: string): Promise<any> {
|
||||
this.fileService.deleteImage(`pictures/logo/${adjustedEmail}.avif`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,6 +61,7 @@ export interface BusinessListing {
|
||||
reasonForSale?: string;
|
||||
brokerLicencing?: string;
|
||||
internals?: string;
|
||||
imageName?: string;
|
||||
created?: Date;
|
||||
updated?: Date;
|
||||
visits?: number;
|
||||
|
||||
@@ -7,7 +7,7 @@ import * as schema from '../drizzle/schema.js';
|
||||
import { PG_CONNECTION } from '../drizzle/schema.js';
|
||||
import { FileService } from '../file/file.service.js';
|
||||
import { User } from '../models/db.model.js';
|
||||
import { ListingCriteria } from '../models/main.model.js';
|
||||
import { ListingCriteria, emailToDirName } from '../models/main.model.js';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
@@ -33,8 +33,8 @@ export class UserService {
|
||||
.from(schema.users)
|
||||
.where(sql`email = ${email}`)) as User[];
|
||||
const user = users[0];
|
||||
user.hasCompanyLogo = this.fileService.hasCompanyLogo(user.id);
|
||||
user.hasProfile = this.fileService.hasProfile(user.id);
|
||||
user.hasCompanyLogo = this.fileService.hasCompanyLogo(emailToDirName(user.email));
|
||||
user.hasProfile = this.fileService.hasProfile(emailToDirName(user.email));
|
||||
return user;
|
||||
}
|
||||
async getUserById(id: string) {
|
||||
@@ -43,8 +43,8 @@ export class UserService {
|
||||
.from(schema.users)
|
||||
.where(sql`id = ${id}`)) as User[];
|
||||
const user = users[0];
|
||||
user.hasCompanyLogo = this.fileService.hasCompanyLogo(id);
|
||||
user.hasProfile = this.fileService.hasProfile(id);
|
||||
user.hasCompanyLogo = this.fileService.hasCompanyLogo(emailToDirName(user.email));
|
||||
user.hasProfile = this.fileService.hasProfile(emailToDirName(user.email));
|
||||
return user;
|
||||
}
|
||||
async saveUser(user: any): Promise<User> {
|
||||
|
||||
Reference in New Issue
Block a user