Merge pull request #5382 from pitaj/assets-route
Move client-side assets to the `/assets` routev1.18.x
commit
107abe3197
@ -0,0 +1,46 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var fs = require('fs');
|
||||||
|
var path = require('path');
|
||||||
|
var mkdirp = require('mkdirp');
|
||||||
|
var winston = require('winston');
|
||||||
|
|
||||||
|
var filePath = path.join(__dirname, '../../build/cache-buster');
|
||||||
|
|
||||||
|
var cached;
|
||||||
|
|
||||||
|
// cache buster is an 11-character, lowercase, alphanumeric string
|
||||||
|
function generate() {
|
||||||
|
return (Math.random() * 1e18).toString(32).slice(0, 11);
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.write = function write(callback) {
|
||||||
|
mkdirp(path.dirname(filePath), function (err) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.writeFile(filePath, generate(), callback);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.read = function read(callback) {
|
||||||
|
if (cached) {
|
||||||
|
return callback(null, cached);
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.readFile(filePath, function (err, buffer) {
|
||||||
|
if (err) {
|
||||||
|
winston.warn('[cache-buster] could not read cache buster: ' + err.message);
|
||||||
|
return callback();
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = buffer.toString();
|
||||||
|
if (buffer) {
|
||||||
|
cached = buffer;
|
||||||
|
return callback(null, cached);
|
||||||
|
}
|
||||||
|
|
||||||
|
callback();
|
||||||
|
});
|
||||||
|
};
|
@ -1,40 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
|
|
||||||
var _ = require('underscore');
|
|
||||||
var path = require('path');
|
|
||||||
|
|
||||||
var plugins = require('../plugins');
|
|
||||||
|
|
||||||
module.exports = function (app, middleware, controllers) {
|
|
||||||
// Static Assets
|
|
||||||
app.get('/plugins/:id/*', middleware.addExpiresHeaders, function (req, res, next) {
|
|
||||||
|
|
||||||
var relPath = req._parsedUrl.pathname.replace('/plugins/', '');
|
|
||||||
|
|
||||||
var matches = _.map(plugins.staticDirs, function (realPath, mappedPath) {
|
|
||||||
if (relPath.match(mappedPath)) {
|
|
||||||
var pathToFile = path.join(plugins.staticDirs[mappedPath], decodeURIComponent(relPath.slice(mappedPath.length)));
|
|
||||||
if (pathToFile.startsWith(plugins.staticDirs[mappedPath])) {
|
|
||||||
return pathToFile;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}).filter(Boolean);
|
|
||||||
|
|
||||||
if (!matches || !matches.length) {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
|
|
||||||
res.sendFile(matches[0], {}, function (err) {
|
|
||||||
if (err) {
|
|
||||||
if (err.code === 'ENOENT') {
|
|
||||||
// File doesn't exist, this isn't an error, to send to 404 handler
|
|
||||||
return next();
|
|
||||||
} else {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
Loading…
Reference in New Issue