accept build id parameter when serving static files
This commit is contained in:
24
app.coffee
24
app.coffee
@@ -36,7 +36,24 @@ app.delete "/project/:project_id", CompileController.clearCache
|
|||||||
app.get "/project/:project_id/sync/code", CompileController.syncFromCode
|
app.get "/project/:project_id/sync/code", CompileController.syncFromCode
|
||||||
app.get "/project/:project_id/sync/pdf", CompileController.syncFromPdf
|
app.get "/project/:project_id/sync/pdf", CompileController.syncFromPdf
|
||||||
|
|
||||||
staticServer = express.static Settings.path.compilesDir, setHeaders: (res, path, stat) ->
|
url = require "url"
|
||||||
|
|
||||||
|
staticForbidSymLinks = (root, options) ->
|
||||||
|
expressStatic = express.static root, options
|
||||||
|
basePath = Path.resolve(root)
|
||||||
|
return (req, res, next) ->
|
||||||
|
path = url.parse(req.url).pathname
|
||||||
|
requestedFsPath = Path.normalize("#{basePath}/#{path}")
|
||||||
|
fs.realpath requestedFsPath, (err, realFsPath)->
|
||||||
|
if err?
|
||||||
|
return res.send(500)
|
||||||
|
else if requestedFsPath != realFsPath
|
||||||
|
logger.warn requestedFsPath:requestedFsPath, realFsPath:realFsPath, path: req.params[0], project_id: req.params.project_id, "trying to access a different file (symlink), aborting"
|
||||||
|
return res.send(404)
|
||||||
|
else
|
||||||
|
expressStatic(req, res, next)
|
||||||
|
|
||||||
|
staticServer = staticForbidSymLinks Settings.path.compilesDir, setHeaders: (res, path, stat) ->
|
||||||
if Path.basename(path) == "output.pdf"
|
if Path.basename(path) == "output.pdf"
|
||||||
res.set("Content-Type", "application/pdf")
|
res.set("Content-Type", "application/pdf")
|
||||||
# Calculate an etag in the same way as nginx
|
# Calculate an etag in the same way as nginx
|
||||||
@@ -51,7 +68,10 @@ staticServer = express.static Settings.path.compilesDir, setHeaders: (res, path,
|
|||||||
res.set("Content-Type", "text/plain")
|
res.set("Content-Type", "text/plain")
|
||||||
|
|
||||||
app.get "/project/:project_id/output/*", require("./app/js/SymlinkCheckerMiddlewear"), (req, res, next) ->
|
app.get "/project/:project_id/output/*", require("./app/js/SymlinkCheckerMiddlewear"), (req, res, next) ->
|
||||||
req.url = "/#{req.params.project_id}/#{req.params[0]}"
|
if req.query?.build? && req.query.build.match(/^[0-9]+$/)
|
||||||
|
req.url = "/#{req.params.project_id}/.cache/clsi/#{req.query.build}/#{req.params[0]}"
|
||||||
|
else
|
||||||
|
req.url = "/#{req.params.project_id}/#{req.params[0]}"
|
||||||
staticServer(req, res, next)
|
staticServer(req, res, next)
|
||||||
|
|
||||||
app.get "/status", (req, res, next) ->
|
app.get "/status", (req, res, next) ->
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ module.exports = CompileController =
|
|||||||
outputFiles: outputFiles.map (file) ->
|
outputFiles: outputFiles.map (file) ->
|
||||||
url: "#{Settings.apis.clsi.url}/project/#{request.project_id}/output/#{file.path}"
|
url: "#{Settings.apis.clsi.url}/project/#{request.project_id}/output/#{file.path}"
|
||||||
type: file.type
|
type: file.type
|
||||||
|
build: file.build
|
||||||
}
|
}
|
||||||
|
|
||||||
clearCache: (req, res, next = (error) ->) ->
|
clearCache: (req, res, next = (error) ->) ->
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ module.exports = CompileManager =
|
|||||||
|
|
||||||
OutputFileFinder.findOutputFiles request.resources, compileDir, (error, outputFiles) ->
|
OutputFileFinder.findOutputFiles request.resources, compileDir, (error, outputFiles) ->
|
||||||
return callback(error) if error?
|
return callback(error) if error?
|
||||||
OutputCacheManager.saveOutputFiles outputFiles, compileDir, (error, newOutputFiles) ->
|
OutputCacheManager.saveOutputFiles outputFiles, compileDir, (error, newOutputFiles) ->
|
||||||
callback null, newOutputFiles
|
callback null, newOutputFiles
|
||||||
|
|
||||||
clearProject: (project_id, _callback = (error) ->) ->
|
clearProject: (project_id, _callback = (error) ->) ->
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ module.exports = OutputCacheManager =
|
|||||||
# copy all the output files into it
|
# copy all the output files into it
|
||||||
#
|
#
|
||||||
# TODO: use Path module
|
# TODO: use Path module
|
||||||
buildId = 'build-' + Date.now()
|
buildId = Date.now()
|
||||||
relDir = OutputCacheManager.CACHE_DIR + '/' + buildId
|
relDir = OutputCacheManager.CACHE_DIR + '/' + buildId
|
||||||
newDir = target + '/' + relDir
|
newDir = target + '/' + relDir
|
||||||
OutputCacheManager.expireOutputFiles target
|
OutputCacheManager.expireOutputFiles target
|
||||||
@@ -25,9 +25,8 @@ module.exports = OutputCacheManager =
|
|||||||
else
|
else
|
||||||
async.mapSeries outputFiles, (file, cb) ->
|
async.mapSeries outputFiles, (file, cb) ->
|
||||||
newFile = _.clone(file)
|
newFile = _.clone(file)
|
||||||
newFile.path = relDir + '/' + file.path
|
|
||||||
src = target + '/' + file.path
|
src = target + '/' + file.path
|
||||||
dst = target + '/' + newFile.path
|
dst = target + '/' + relDir + '/' + file.path
|
||||||
#console.log 'src', src, 'dst', dst
|
#console.log 'src', src, 'dst', dst
|
||||||
fs.stat src, (err, stats) ->
|
fs.stat src, (err, stats) ->
|
||||||
if err?
|
if err?
|
||||||
@@ -36,6 +35,8 @@ module.exports = OutputCacheManager =
|
|||||||
#console.log 'isFile: copying'
|
#console.log 'isFile: copying'
|
||||||
fse.copy src, dst, (err) ->
|
fse.copy src, dst, (err) ->
|
||||||
OutputFileOptimiser.optimiseFile src, dst, (err, result) ->
|
OutputFileOptimiser.optimiseFile src, dst, (err, result) ->
|
||||||
|
console.log 'setting buildId on', newFile, 'to', buildId
|
||||||
|
newFile.build = buildId
|
||||||
cb(err, newFile)
|
cb(err, newFile)
|
||||||
else
|
else
|
||||||
# other filetype - shouldn't happen
|
# other filetype - shouldn't happen
|
||||||
|
|||||||
Reference in New Issue
Block a user