@@ -22,44 +22,46 @@ const mime = require('mime');
2222 async function listFiles ( req , res ) {
2323 let files = [ ] ;
2424 let site = await HAXCMS . loadSite ( req . query [ 'siteName' ] ) ;
25- let search = ( typeof req . query [ 'filename' ] !== 'undefined' ) ? req . query [ 'filename' ] : '' ;
26- // build files directory path
27- let siteFilePath = path . join ( site . siteDirectory , 'files' ) ;
28- let handle ;
29- if ( handle = fs . readdirSync ( siteFilePath ) ) {
30- handle . forEach ( file => {
31- if (
32- file != "." &&
33- file != ".." &&
34- file != '.gitkeep' &&
35- file != '.DS_Store'
36- ) {
37- // ensure this is a file
25+ if ( site && site . siteDirectory ) {
26+ let search = ( typeof req . query [ 'filename' ] !== 'undefined' ) ? req . query [ 'filename' ] : '' ;
27+ // build files directory path
28+ let siteFilePath = path . join ( site . siteDirectory , 'files' ) ;
29+ let handle ;
30+ if ( handle = fs . readdirSync ( siteFilePath ) ) {
31+ handle . forEach ( file => {
3832 if (
39- fs . lstatSync ( siteFilePath + '/' + file ) . isFile ( )
33+ file != "." &&
34+ file != ".." &&
35+ file != '.gitkeep' &&
36+ file != '.DS_Store'
4037 ) {
41- // ensure this is a file and if we are searching for results then return only exact ones
42- if ( search == "" || file . indexOf ( search ) !== - 1 ) {
43- let fullUrl = '/files/' + file ;
44- // multiple sites then append the base url to site management area
45- if ( HAXCMS . operatingContext == 'multisite' ) {
46- fullUrl = HAXCMS . basePath +
47- HAXCMS . sitesDirectory + '/' +
48- site . manifest . metadata . site . name + '/files/' + file
38+ // ensure this is a file
39+ if (
40+ fs . lstatSync ( siteFilePath + '/' + file ) . isFile ( )
41+ ) {
42+ // ensure this is a file and if we are searching for results then return only exact ones
43+ if ( search == "" || file . indexOf ( search ) !== - 1 ) {
44+ let fullUrl = '/files/' + file ;
45+ // multiple sites then append the base url to site management area
46+ if ( HAXCMS . operatingContext == 'multisite' ) {
47+ fullUrl = HAXCMS . basePath +
48+ HAXCMS . sitesDirectory + '/' +
49+ site . manifest . metadata . site . name + '/files/' + file
50+ }
51+ files . push ( {
52+ 'path' : 'files/' + file ,
53+ 'fullUrl' : fullUrl ,
54+ 'url' : 'files/' + file ,
55+ 'mimetype' : mime . getType ( siteFilePath + '/' + file ) ,
56+ 'name' : file
57+ } ) ;
4958 }
50- files . push ( {
51- 'path' : 'files/' + file ,
52- 'fullUrl' : fullUrl ,
53- 'url' : 'files/' + file ,
54- 'mimetype' : mime . getType ( siteFilePath + '/' + file ) ,
55- 'name' : file
56- } ) ;
59+ } else {
60+ // @todo maybe step into directories?
5761 }
58- } else {
59- // @todo maybe step into directories?
6062 }
61- }
62- } ) ;
63+ } ) ;
64+ }
6365 }
6466 res . send ( files ) ;
6567 }
0 commit comments