added filestoreDomainOveride

This commit is contained in:
Henry Oswald 2018-07-03 16:41:34 +01:00
parent 2384eb83d9
commit eb89ca1d3b
3 changed files with 40 additions and 2 deletions

View file

@ -1,6 +1,8 @@
request = require("request").defaults(jar: false) request = require("request").defaults(jar: false)
fs = require("fs") fs = require("fs")
logger = require "logger-sharelatex" logger = require "logger-sharelatex"
settings = require("settings-sharelatex")
URL = require('url');
oneMinute = 60 * 1000 oneMinute = 60 * 1000
@ -11,6 +13,9 @@ module.exports = UrlFetcher =
_callback(error) _callback(error)
_callback = () -> _callback = () ->
if settings.filestoreDomainOveride?
p = URL.parse(url).path
url = "#{settings.filestoreDomainOveride}#{p}"
timeoutHandler = setTimeout () -> timeoutHandler = setTimeout () ->
timeoutHandler = null timeoutHandler = null
logger.error url:url, filePath: filePath, "Timed out downloading file to cache" logger.error url:url, filePath: filePath, "Timed out downloading file to cache"
@ -31,6 +36,7 @@ module.exports = UrlFetcher =
logger.log url:url, filePath: filePath, "finished downloading file into cache" logger.log url:url, filePath: filePath, "finished downloading file into cache"
urlStream.on "response", (res) -> urlStream.on "response", (res) ->
console.log
if res.statusCode >= 200 and res.statusCode < 300 if res.statusCode >= 200 and res.statusCode < 300
fileStream = fs.createWriteStream(filePath) fileStream = fs.createWriteStream(filePath)

View file

@ -32,6 +32,7 @@ module.exports =
smokeTest: process.env["SMOKE_TEST"] or false smokeTest: process.env["SMOKE_TEST"] or false
project_cache_length_ms: 1000 * 60 * 60 * 24 project_cache_length_ms: 1000 * 60 * 60 * 24
parallelFileDownloads:1 parallelFileDownloads:1
filestoreDomainOveride: process.env["FILESTORE_DOMAIN_OVERRIDE"]
if process.env["DOCKER_RUNNER"] if process.env["DOCKER_RUNNER"]

View file

@ -7,16 +7,47 @@ EventEmitter = require("events").EventEmitter
describe "UrlFetcher", -> describe "UrlFetcher", ->
beforeEach -> beforeEach ->
@callback = sinon.stub() @callback = sinon.stub()
@url = "www.example.com/file" @url = "https://www.example.com/file/here?query=string"
@UrlFetcher = SandboxedModule.require modulePath, requires: @UrlFetcher = SandboxedModule.require modulePath, requires:
request: defaults: @defaults = sinon.stub().returns(@request = {}) request: defaults: @defaults = sinon.stub().returns(@request = {})
fs: @fs = {} fs: @fs = {}
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() } "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
"settings-sharelatex": @settings = {}
it "should turn off the cookie jar in request", -> it "should turn off the cookie jar in request", ->
@defaults.calledWith(jar: false) @defaults.calledWith(jar: false)
.should.equal true .should.equal true
describe "rewrite url domain if filestoreDomainOveride is set", ->
beforeEach ->
@path = "/path/to/file/on/disk"
@request.get = sinon.stub().returns(@urlStream = new EventEmitter)
@urlStream.pipe = sinon.stub()
@urlStream.pause = sinon.stub()
@urlStream.resume = sinon.stub()
@fs.createWriteStream = sinon.stub().returns(@fileStream = new EventEmitter)
@fs.unlink = (file, callback) -> callback()
it "should use the normal domain when override not set", (done)->
@UrlFetcher.pipeUrlToFile @url, @path, =>
@request.get.args[0][0].url.should.equal @url
done()
@res = statusCode: 200
@urlStream.emit "response", @res
@urlStream.emit "end"
@fileStream.emit "finish"
it "should use override domain when filestoreDomainOveride is set", (done)->
@settings.filestoreDomainOveride = "192.11.11.11"
@UrlFetcher.pipeUrlToFile @url, @path, =>
@request.get.args[0][0].url.should.equal "192.11.11.11/file/here?query=string"
done()
@res = statusCode: 200
@urlStream.emit "response", @res
@urlStream.emit "end"
@fileStream.emit "finish"
describe "pipeUrlToFile", -> describe "pipeUrlToFile", ->
beforeEach (done)-> beforeEach (done)->
@path = "/path/to/file/on/disk" @path = "/path/to/file/on/disk"