Skip to content

Commit 2ad9768

Browse files
committed
1 parent 150773e commit 2ad9768

File tree

2 files changed

+61
-52
lines changed

2 files changed

+61
-52
lines changed

src/routes/listFiles.js

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/routes/saveFile.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,34 @@ const HAXCMSFile = require('../lib/HAXCMSFile.js');
5050
* )
5151
*/
5252
async function saveFile(req, res, next) {
53-
if (req.file.fieldname == 'file-upload') {
53+
let sendResult = 500;
54+
if (
55+
req.file &&
56+
req.file.fieldname == 'file-upload' &&
57+
req.query &&
58+
req.query['siteName'] &&
59+
req.query['nodeId']
60+
) {
5461
let site = await HAXCMS.loadSite(req.query['siteName']);
55-
// update the page's content, using manifest to find it
56-
// this ensures that writing is always to what the file system
57-
// determines to be the correct page
58-
let page = site.loadNode(req.query['nodeId']);
59-
let upload = req.file;
60-
upload.name = upload.originalname;
61-
upload.tmp_name = path.join("./", upload.path);
62-
let file = new HAXCMSFile();
63-
let fileResult = await file.save(upload, site, page);
64-
if (!fileResult || fileResult['status'] == 500) {
65-
res.send(500);
62+
if (site) {
63+
// update the page's content, using manifest to find it
64+
// this ensures that writing is always to what the file system
65+
// determines to be the correct page
66+
let page = site.loadNode(req.query['nodeId']);
67+
let upload = req.file;
68+
upload.name = upload.originalname;
69+
upload.tmp_name = path.join("./", upload.path);
70+
let file = new HAXCMSFile();
71+
let fileResult = await file.save(upload, site, page);
72+
if (!fileResult || fileResult['status'] == 500) {
73+
// do nothing so we can 500
74+
}
75+
else {
76+
await site.gitCommit('File added: ' + upload['name']);
77+
sendResult = fileResult;
78+
}
6679
}
67-
else {
68-
await site.gitCommit('File added: ' + upload['name']);
69-
res.send(fileResult);
70-
}
71-
}
72-
else {
73-
res.send(500);
7480
}
81+
res.send(sendResult);
7582
}
7683
module.exports = saveFile;

0 commit comments

Comments
 (0)