11import express from 'express'
2- import fs from 'fs'
3- import path from 'path'
42import authenticator from '../../lib/middleware/authentication.js'
53import rateLimiter from '../../lib/middleware/rate-limiter.js'
64import {
7- dist ,
8- imagesDirectory ,
95 imageSizeBig ,
106 imageSizeSmall ,
11- uploadDirectory ,
127} from '../../lib/constants/constants.js'
138import House from '../../models/house-model.js'
149import Meta from '../../models/meta-model.js'
10+ import TemporaryPhoto from '../../models/temporary-photo-model.js'
1511import sharp from 'sharp'
1612import uploader from '../../lib/middleware/uploader.js'
1713
@@ -40,43 +36,40 @@ router.post(
4036 return res . status ( 400 ) . redirect ( '/' )
4137 }
4238
43- let photoCounter = 1
44-
4539 for ( const photo of files ) {
46- await sharp ( photo . buffer )
40+ const buffer = await sharp ( photo . buffer )
4741 . resize ( {
4842 width : imageSizeBig ,
4943 height : imageSizeSmall ,
5044 } )
5145 . rotate ( 90 )
5246 . webp ( { quality : 80 } )
53- . toFile (
54- path . join ( dist , uploadDirectory , `/photo${ photoCounter ++ } .webp` )
55- )
47+ . toBuffer ( )
48+
49+ const temporaryPhoto = new TemporaryPhoto ( {
50+ data : buffer ,
51+ contentType : 'image/webp' ,
52+ } )
53+ await temporaryPhoto . save ( )
5654 }
5755
5856 res . status ( 201 ) . redirect ( '/' )
5957 }
6058)
6159
6260router . post ( '/clearPhotos' , rateLimiter , authenticator , async ( _req , res ) => {
63- fs . readdir ( path . join ( dist , uploadDirectory ) , ( _err , files ) => {
64- for ( const file of files ) {
65- fs . unlinkSync ( path . join ( dist , uploadDirectory , file ) )
66- }
67-
68- res . status ( 200 ) . redirect ( '/' )
69- } )
61+ await TemporaryPhoto . deleteMany ( { } ) . exec ( )
62+ res . status ( 200 ) . redirect ( '/' )
7063} )
7164
7265router . post ( '/addHouse' , rateLimiter , authenticator , async ( req , res ) => {
73- let num_photos = fs . readdirSync ( path . join ( dist , uploadDirectory ) ) . length
66+ const temporaryPhotos = await TemporaryPhoto . find ( ) . exec ( )
7467
75- if ( num_photos >= 1 ) {
68+ if ( temporaryPhotos . length >= 1 ) {
7669 let id = req . body . id
7770
7871 if ( ! id ) {
79- id = Date . now ( )
72+ id = Date . now ( ) . toString ( )
8073 }
8174
8275 const house = new House ( {
@@ -94,19 +87,15 @@ router.post('/addHouse', rateLimiter, authenticator, async (req, res) => {
9487 dinningroom : ! ! req . body . dinningroom ,
9588 balcony : ! ! req . body . balcony ,
9689 gardin : ! ! req . body . gardin ,
97- photos : num_photos ,
90+ photos : temporaryPhotos . length ,
91+ images : temporaryPhotos . map ( ( p ) => ( {
92+ data : p . data ,
93+ contentType : p . contentType ,
94+ } ) ) ,
9895 } )
9996
100- fs . mkdirSync ( path . join ( dist , imagesDirectory , id ) )
101- const files = fs . readdirSync ( path . join ( dist , uploadDirectory ) )
102- for ( const file of files ) {
103- fs . renameSync (
104- path . join ( dist , uploadDirectory , file ) ,
105- path . join ( dist , imagesDirectory , id , file )
106- )
107- }
108-
10997 await house . save ( )
98+ await TemporaryPhoto . deleteMany ( { } ) . exec ( )
11099
111100 res . redirect ( '/' )
112101 } else {
@@ -136,18 +125,7 @@ router.post('/toggleHouse', rateLimiter, authenticator, async (req, res) => {
136125} )
137126
138127router . post ( '/removeHouse' , rateLimiter , authenticator , async ( req , res ) => {
139- const directory = path
140- . join ( dist , imagesDirectory , req . body . id )
141- . replace ( / ^ ( \. \. ( \/ | \\ | $ ) ) + / , '' )
142-
143128 await House . deleteOne ( { id : req . body . id } ) . exec ( )
144-
145- const files = fs . readdirSync ( directory )
146- for ( const file of files ) {
147- fs . unlinkSync ( path . join ( directory , file ) )
148- }
149- fs . rmdirSync ( directory )
150-
151129 res . redirect ( '/' )
152130} )
153131
0 commit comments