@@ -9,36 +9,41 @@ import {
9
9
import { get404Sketch } from '../views/404Page' ;
10
10
11
11
export function serveProject ( req , res ) {
12
- Project . findById ( req . params . project_id ) . exec ( ( err , project ) => {
13
- if ( err || ! project ) {
14
- get404Sketch ( ( html ) => res . send ( html ) ) ;
15
- return ;
16
- }
17
- // TODO this does not parse html
18
- const { files } = project ;
19
- const htmlFile = files . find ( ( file ) => file . name . match ( / \. h t m l $ / i) ) . content ;
20
- const filesToInject = files . filter ( ( file ) =>
21
- file . name . match ( / \. ( j s | c s s ) $ / i)
22
- ) ;
23
- injectMediaUrls ( filesToInject , files , req . params . project_id ) ;
12
+ const projectId = req . params . project_id ;
13
+ Project . findOne (
14
+ { $or : [ { _id : projectId } , { slug : projectId } ] } ,
15
+ ( err , project ) => {
16
+ if ( err || ! project ) {
17
+ get404Sketch ( ( html ) => res . send ( html ) ) ;
18
+ return ;
19
+ }
20
+ // TODO this does not parse html
21
+ const { files } = project ;
22
+ const htmlFile = files . find ( ( file ) => file . name . match ( / \. h t m l $ / i) )
23
+ . content ;
24
+ const filesToInject = files . filter ( ( file ) =>
25
+ file . name . match ( / \. ( j s | c s s ) $ / i)
26
+ ) ;
27
+ injectMediaUrls ( filesToInject , files , req . params . project_id ) ;
24
28
25
- jsdom . env ( htmlFile , ( innerErr , window ) => {
26
- const sketchDoc = window . document ;
29
+ jsdom . env ( htmlFile , ( innerErr , window ) => {
30
+ const sketchDoc = window . document ;
27
31
28
- const base = sketchDoc . createElement ( 'base' ) ;
29
- const fullUrl = `//${ req . get ( 'host' ) } ${ req . originalUrl } ` ;
30
- base . href = `${ fullUrl } /` ;
31
- sketchDoc . head . appendChild ( base ) ;
32
+ const base = sketchDoc . createElement ( 'base' ) ;
33
+ const fullUrl = `//${ req . get ( 'host' ) } ${ req . originalUrl } ` ;
34
+ base . href = `${ fullUrl } /` ;
35
+ sketchDoc . head . appendChild ( base ) ;
32
36
33
- resolvePathsForElementsWithAttribute ( 'src' , sketchDoc , files ) ;
34
- resolvePathsForElementsWithAttribute ( 'href' , sketchDoc , files ) ;
35
- resolveScripts ( sketchDoc , files ) ;
36
- resolveStyles ( sketchDoc , files ) ;
37
+ resolvePathsForElementsWithAttribute ( 'src' , sketchDoc , files ) ;
38
+ resolvePathsForElementsWithAttribute ( 'href' , sketchDoc , files ) ;
39
+ resolveScripts ( sketchDoc , files ) ;
40
+ resolveStyles ( sketchDoc , files ) ;
37
41
38
- res . setHeader ( 'Cache-Control' , 'public, max-age=0' ) ;
39
- res . send ( serializeDocument ( sketchDoc ) ) ;
40
- } ) ;
41
- } ) ;
42
+ res . setHeader ( 'Cache-Control' , 'public, max-age=0' ) ;
43
+ res . send ( serializeDocument ( sketchDoc ) ) ;
44
+ } ) ;
45
+ }
46
+ ) ;
42
47
}
43
48
44
49
export default serveProject ;
0 commit comments