@@ -21,25 +21,32 @@ export {
21
21
} from './project.controller/getProjectsForUser' ;
22
22
23
23
export async function updateProject ( req , res ) {
24
- const project = await Project . findById ( req . params . project_id ) . exec ( ) ;
25
- if ( ! project . user . equals ( req . user . _id ) ) {
26
- res . status ( 403 ) . send ( {
27
- success : false ,
28
- message : 'Session does not match owner of project.'
29
- } ) ;
30
- return ;
31
- }
32
- if (
33
- req . body . updatedAt &&
34
- isAfter ( new Date ( project . updatedAt ) , new Date ( req . body . updatedAt ) )
35
- ) {
36
- res . status ( 409 ) . send ( {
37
- success : false ,
38
- message : 'Attempted to save stale version of project.'
39
- } ) ;
40
- return ;
41
- }
42
24
try {
25
+ const project = await Project . findById ( req . params . project_id ) . exec ( ) ;
26
+ if ( ! project ) {
27
+ res . status ( 404 ) . send ( {
28
+ success : false ,
29
+ message : 'Project with that id does not exist.'
30
+ } ) ;
31
+ return ;
32
+ }
33
+ if ( ! project . user . equals ( req . user . _id ) ) {
34
+ res . status ( 403 ) . send ( {
35
+ success : false ,
36
+ message : 'Session does not match owner of project.'
37
+ } ) ;
38
+ return ;
39
+ }
40
+ if (
41
+ req . body . updatedAt &&
42
+ isAfter ( new Date ( project . updatedAt ) , new Date ( req . body . updatedAt ) )
43
+ ) {
44
+ res . status ( 409 ) . send ( {
45
+ success : false ,
46
+ message : 'Attempted to save stale version of project.'
47
+ } ) ;
48
+ return ;
49
+ }
43
50
const updatedProject = await Project . findByIdAndUpdate (
44
51
req . params . project_id ,
45
52
{
@@ -68,7 +75,8 @@ export async function updateProject(req, res) {
68
75
res . json ( updatedProject ) ;
69
76
}
70
77
} catch ( error ) {
71
- res . status ( 400 ) . json ( { success : false } ) ;
78
+ console . error ( error ) ;
79
+ res . status ( 500 ) . json ( { success : false } ) ;
72
80
}
73
81
}
74
82
@@ -152,7 +160,7 @@ export async function projectExists(projectId) {
152
160
153
161
/**
154
162
* @param {string } username
155
- * @param {string } projectId
163
+ * @param {string } projectId - the database id or the slug or the project
156
164
* @return {Promise<boolean> }
157
165
*/
158
166
export async function projectForUserExists ( username , projectId ) {
@@ -165,12 +173,18 @@ export async function projectForUserExists(username, projectId) {
165
173
return project != null ;
166
174
}
167
175
176
+ /**
177
+ * Adds URLs referenced in <script> tags to the `files` array of the project
178
+ * so that they can be downloaded along with other remote files from S3.
179
+ * @param {object } project
180
+ * @void - modifies the `project` parameter
181
+ */
168
182
function bundleExternalLibs ( project ) {
169
183
const indexHtml = project . files . find ( ( file ) => file . name === 'index.html' ) ;
170
184
const { window } = new JSDOM ( indexHtml . content ) ;
171
185
const scriptTags = window . document . getElementsByTagName ( 'script' ) ;
172
186
173
- Object . values ( scriptTags ) . forEach ( async ( { src } , i ) => {
187
+ Object . values ( scriptTags ) . forEach ( ( { src } ) => {
174
188
if ( ! isUrl ( src ) ) return ;
175
189
176
190
const path = src . split ( '/' ) ;
@@ -186,6 +200,13 @@ function bundleExternalLibs(project) {
186
200
} ) ;
187
201
}
188
202
203
+ /**
204
+ * Recursively adds a file and all of its children to the JSZip instance.
205
+ * @param {object } file
206
+ * @param {Array<object> } files
207
+ * @param {JSZip } zip
208
+ * @return {Promise<void> } - modifies the `zip` parameter
209
+ */
189
210
async function addFileToZip ( file , files , zip ) {
190
211
if ( file . fileType === 'folder' ) {
191
212
const folderZip = file . name === 'root' ? zip : zip . folder ( file . name ) ;
@@ -237,9 +258,10 @@ async function buildZip(project, req, res) {
237
258
}
238
259
239
260
export async function downloadProjectAsZip ( req , res ) {
240
- const project = await Project . findById ( req . params . project_id ) ;
261
+ const project = await Project . findById ( req . params . project_id ) . exec ( ) ;
241
262
if ( ! project ) {
242
263
res . status ( 404 ) . send ( { message : 'Project with that id does not exist' } ) ;
264
+ return ;
243
265
}
244
266
// save project to some path
245
267
buildZip ( project , req , res ) ;
0 commit comments