2015-07-15 19:01:23 +03:00
|
|
|
var crypto = require('crypto'),
|
|
|
|
fs = require('fs'),
|
|
|
|
path = require('path'),
|
2016-09-09 13:23:47 +03:00
|
|
|
config = require('../config'),
|
|
|
|
utils = require('../utils');
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2017-04-07 15:21:41 +03:00
|
|
|
// ### servePublicFile Middleware
|
2015-07-15 19:01:23 +03:00
|
|
|
// Handles requests to robots.txt and favicon.ico (and caches them)
|
2017-04-07 15:21:41 +03:00
|
|
|
function servePublicFile(file, type, maxAge) {
|
2015-07-15 19:01:23 +03:00
|
|
|
var content,
|
2017-04-07 15:21:41 +03:00
|
|
|
publicFilePath = config.get('paths').publicFilePath,
|
2015-12-15 13:41:53 +03:00
|
|
|
filePath,
|
|
|
|
blogRegex = /(\{\{blog-url\}\})/g,
|
|
|
|
apiRegex = /(\{\{api-url\}\})/g;
|
|
|
|
|
2017-04-07 15:21:41 +03:00
|
|
|
filePath = file.match(/^public/) ? path.join(publicFilePath, file.replace(/^public/, '')) : path.join(publicFilePath, file);
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2017-04-07 15:21:41 +03:00
|
|
|
return function servePublicFile(req, res, next) {
|
2015-12-15 13:41:53 +03:00
|
|
|
if (req.path === '/' + file) {
|
2015-07-15 19:01:23 +03:00
|
|
|
if (content) {
|
|
|
|
res.writeHead(200, content.headers);
|
|
|
|
res.end(content.body);
|
|
|
|
} else {
|
|
|
|
fs.readFile(filePath, function readFile(err, buf) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2015-12-15 13:41:53 +03:00
|
|
|
|
|
|
|
if (type === 'text/xsl' || type === 'text/plain' || type === 'application/javascript') {
|
2017-01-23 11:22:37 +03:00
|
|
|
buf = buf.toString().replace(blogRegex, utils.url.urlFor('home', true).replace(/\/$/, ''));
|
🔥✨ remove forceAdminSSL and urlSSL, add admin url (#7937)
* 🔥 kill apiUrl helper, use urlFor helper instead
More consistency of creating urls.
Creates an easier ability to add config changes.
Attention: urlFor function is getting a little nesty, BUT that is for now wanted to make easier and centralised changes to the configs.
The url util need's refactoring anyway.
* 🔥 urlSSL
Remove all urlSSL usages.
Add TODO's for the next commit to re-add logic for deleted logic.
e.g.
- cors helper generated an array of url's to allow requests from the defined config url's -> will be replaced by the admin url if available
- theme handler prefered the urlSSL in case it was defined -> will be replaced by using the urlFor helper to get the blog url (based on the request secure flag)
The changes in this commit doesn't have to be right, but it helped going step by step.
The next commit is the more interesting one.
* 🔥 ✨ remove forceAdminSSL, add new admin url and adapt logic
I wanted to remove the forceAdminSSL as separate commit, but was hard to realise.
That's why both changes are in one commit:
1. remove forceAdminSSL
2. add admin.url option
- fix TODO's from last commits
- rewrite the ssl middleware!
- create some private helper functions in the url helper to realise the changes
- rename some wordings and functions e.g. base === blog (we have so much different wordings)
- i would like to do more, but this would end in a non readable PR
- this commit contains the most important changes to offer admin.url option
* 🤖 adapt tests
IMPORTANT
- all changes in the routing tests were needed, because each routing test did not start the ghost server
- they just required the ghost application, which resulted in a random server port
- having a random server port results in a redirect, caused by the ssl/redirect middleware
* 😎 rename check-ssl middleware
* 🎨 fix theme-handler because of master rebase
2017-02-03 21:13:22 +03:00
|
|
|
buf = buf.toString().replace(apiRegex, utils.url.urlFor('api', {cors: true}, true));
|
2015-07-15 19:01:23 +03:00
|
|
|
}
|
|
|
|
content = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': type,
|
|
|
|
'Content-Length': buf.length,
|
|
|
|
ETag: '"' + crypto.createHash('md5').update(buf, 'utf8').digest('hex') + '"',
|
|
|
|
'Cache-Control': 'public, max-age=' + maxAge
|
|
|
|
},
|
|
|
|
body: buf
|
|
|
|
};
|
|
|
|
res.writeHead(200, content.headers);
|
|
|
|
res.end(content.body);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2017-11-01 16:44:54 +03:00
|
|
|
return next();
|
2015-07-15 19:01:23 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-04-07 15:21:41 +03:00
|
|
|
module.exports = servePublicFile;
|