From 379026bd873b30468d07d3db6766c49a36090293 Mon Sep 17 00:00:00 2001 From: aelassas Date: Tue, 20 Feb 2024 14:03:20 +0100 Subject: [PATCH] Update app.ts --- api/coverage/cobertura-coverage.xml | 1210 ++++++++++----------- api/src/app.ts | 7 + api/src/common/Helper.ts | 13 + api/src/controllers/propertyController.ts | 12 - api/src/controllers/userController.ts | 11 - 5 files changed, 624 insertions(+), 629 deletions(-) diff --git a/api/coverage/cobertura-coverage.xml b/api/coverage/cobertura-coverage.xml index f95e128e..29f9a81e 100644 --- a/api/coverage/cobertura-coverage.xml +++ b/api/coverage/cobertura-coverage.xml @@ -1,6 +1,6 @@ - + C:\dev\movinin\src\api @@ -11,8 +11,7 @@ - - + @@ -26,26 +25,31 @@ - + - + - + - + + + + + + - + @@ -75,51 +79,56 @@ - + - + + + + + + - + - + - + - + - + - + - + - + - + - + @@ -127,29 +136,30 @@ - - - + + + - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + @@ -279,7 +289,7 @@ - + @@ -308,9 +318,9 @@ - + - + @@ -396,8 +406,8 @@ - - + + @@ -981,7 +991,7 @@ - + @@ -990,72 +1000,72 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -1069,43 +1079,45 @@ - - - - + + + + + - - - - - - - - - - - + + + + + + + + + + + - + - - - - - - - - - - - - + + + + + + + + + + + + - - - - + + + + + @@ -1122,186 +1134,178 @@ - - + - - - - + + + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -1335,137 +1339,137 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -1519,379 +1523,373 @@ - + - + - - - - - + + + + - - - - - - + + + + + + + - - - - - + + + + - - - - - - - + + + + + + + + - - - + + - - - + + + + - - - - - + + + + - + - + - - - - + + + + - - - + + + + - - - - - + + + + + + - - + - - - - - + + + + + - - - - - + + + + + - - - - - - - + + + + + + - - - - - - - + + + + + + + + - - - - + + + + + - - - + + - - + + - - - - - + + + + - - - + + + + - - - - - + + + + + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - - - - + + + + - - - - - - - - + + + + + + + - - - - - - + + + + + + + + - - - - - - + + + + + - + - - + + + - - + - - + - - - - + + + + + - - - - - - - + + + + + + + - - - - - + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - + + + + + + + + diff --git a/api/src/app.ts b/api/src/app.ts index c5f7d2b7..1f5d7805 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -13,6 +13,7 @@ import locationRoutes from './routes/locationRoutes' import notificationRoutes from './routes/notificationRoutes' import propertyRoutes from './routes/propertyRoutes' import userRoutes from './routes/userRoutes' +import * as Helper from './common/Helper' const app: Express = express() @@ -49,4 +50,10 @@ app.use('/', propertyRoutes) app.use('/', userRoutes) strings.setLanguage(env.DEFAULT_LANGUAGE) + +Helper.mkdir(env.CDN_USERS) +Helper.mkdir(env.CDN_TEMP_USERS) +Helper.mkdir(env.CDN_PROPERTIES) +Helper.mkdir(env.CDN_TEMP_PROPERTIES) + export default app diff --git a/api/src/common/Helper.ts b/api/src/common/Helper.ts index a766e0a0..80c5a525 100644 --- a/api/src/common/Helper.ts +++ b/api/src/common/Helper.ts @@ -35,6 +35,19 @@ export async function exists(filePath: string): Promise { } } +/** + * Create a folder recursively. + * + * @export + * @async + * @param {string} folder + * @param {boolean} recursive + * @returns {Promise} + */ +export async function mkdir(folder: string) { + await fs.mkdir(folder, { recursive: true }) +} + /** * Removes a leading and trailing line terminator character from a string. * diff --git a/api/src/controllers/propertyController.ts b/api/src/controllers/propertyController.ts index e4c0c08d..38c7f883 100644 --- a/api/src/controllers/propertyController.ts +++ b/api/src/controllers/propertyController.ts @@ -79,10 +79,6 @@ export async function create(req: Request, res: Response) { await property.save() // image - if (!await Helper.exists(env.CDN_PROPERTIES)) { - await fs.mkdir(env.CDN_PROPERTIES, { recursive: true }) - } - const _image = path.join(env.CDN_TEMP_PROPERTIES, imageFile) if (await Helper.exists(_image)) { const filename = `${property._id}_${Date.now()}${path.extname(imageFile)}` @@ -192,10 +188,6 @@ export async function update(req: Request, res: Response) { property.aircon = aircon property.rentalTerm = rentalTerm as movininTypes.RentalTerm - if (!await Helper.exists(env.CDN_PROPERTIES)) { - await fs.mkdir(env.CDN_PROPERTIES, { recursive: true }) - } - if (image && image !== property.image) { const oldImage = path.join(env.CDN_PROPERTIES, property.image) if (await Helper.exists(oldImage)) { @@ -347,10 +339,6 @@ export async function uploadImage(req: Request, res: Response) { return res.status(400).send(msg) } - if (!await Helper.exists(env.CDN_TEMP_PROPERTIES)) { - await fs.mkdir(env.CDN_TEMP_PROPERTIES, { recursive: true }) - } - const filename = `${Helper.getFilenameWithoutExtension(req.file.originalname)}_${uuid()}_${Date.now()}${path.extname(req.file.originalname)}` const filepath = path.join(env.CDN_TEMP_PROPERTIES, filename) diff --git a/api/src/controllers/userController.ts b/api/src/controllers/userController.ts index c1301b1e..727dcaf9 100644 --- a/api/src/controllers/userController.ts +++ b/api/src/controllers/userController.ts @@ -159,9 +159,6 @@ export async function create(req: Request, res: Response) { const newPath = path.join(env.CDN_USERS, filename) try { - if (!await Helper.exists(env.CDN_USERS)) { - await fs.mkdir(env.CDN_USERS, { recursive: true }) - } await fs.rename(avatar, newPath) user.avatar = filename await user.save() @@ -899,10 +896,6 @@ export async function createAvatar(req: Request, res: Response) { return res.status(204).send(msg) } - if (!(await Helper.exists(env.CDN_TEMP_USERS))) { - await fs.mkdir(env.CDN_TEMP_USERS, { recursive: true }) - } - const filename = `${Helper.getFilenameWithoutExtension(req.file.originalname)}_${uuid()}_${Date.now()}${path.extname(req.file.originalname)}` const filepath = path.join(env.CDN_TEMP_USERS, filename) @@ -936,10 +929,6 @@ export async function updateAvatar(req: Request, res: Response) { const user = await User.findById(userId) if (user) { - if (!(await Helper.exists(env.CDN_USERS))) { - await fs.mkdir(env.CDN_USERS, { recursive: true }) - } - if (user.avatar && !user.avatar.startsWith('http')) { const avatar = path.join(env.CDN_USERS, user.avatar)