Merge pull request #3516 from overleaf/jpa-clsi-urls

[frontend] PdfController: cleanup the composing of download urls

GitOrigin-RevId: 599d9cb2fca1d75ca15cbb1bef7561215ee31aef
This commit is contained in:
Jakob Ackermann 2021-01-07 13:54:19 +00:00 committed by Copybot
parent 846f2379a8
commit de646d3160

View file

@ -315,16 +315,11 @@ App.controller('PdfController', function(
) )
} }
function buildPdfDownloadUrl(pdfDownloadDomain, path) { function buildPdfDownloadUrl(pdfDownloadDomain, url) {
// we only download builds from compiles server for security reasons if (pdfDownloadDomain) {
if ( return `${pdfDownloadDomain}${url}`
pdfDownloadDomain != null &&
path != null &&
path.indexOf('build') !== -1
) {
return `${pdfDownloadDomain}${path}`
} else { } else {
return path return url
} }
} }
@ -369,49 +364,30 @@ App.controller('PdfController', function(
ide.clsiServerId = qs.clsiserverid = response.clsiServerId ide.clsiServerId = qs.clsiserverid = response.clsiServerId
} }
// TODO(das7pad): drop this hack once 2747f0d40af8729304 has landed in clsi
if (response.status === 'success' && !fileByPath['output.pdf']) {
response.status = 'failure'
}
if (response.status === 'success') { if (response.status === 'success') {
$scope.pdf.view = 'pdf' $scope.pdf.view = 'pdf'
$scope.shouldShowLogs = false $scope.shouldShowLogs = false
$scope.pdf.lastCompileTimestamp = Date.now() $scope.pdf.lastCompileTimestamp = Date.now()
$scope.pdf.validation = {} $scope.pdf.validation = {}
// define the base url. if the pdf file has a build number, pass it to the clsi in the url
if (fileByPath['output.pdf'] && fileByPath['output.pdf'].url) {
$scope.pdf.url = buildPdfDownloadUrl( $scope.pdf.url = buildPdfDownloadUrl(
pdfDownloadDomain, pdfDownloadDomain,
fileByPath['output.pdf'].url fileByPath['output.pdf'].url
) )
} else if (fileByPath['output.pdf'] && fileByPath['output.pdf'].build) {
const { build } = fileByPath['output.pdf']
$scope.pdf.url = buildPdfDownloadUrl(
pdfDownloadDomain,
`/project/${$scope.project_id}/build/${build}/output/output.pdf`
)
} else {
$scope.pdf.url = buildPdfDownloadUrl(
pdfDownloadDomain,
`/project/${$scope.project_id}/output/output.pdf`
)
}
// check if we need to bust cache (build id is unique so don't need it in that case)
if (!(fileByPath['output.pdf'] && fileByPath['output.pdf'].build)) {
qs.cache_bust = `${Date.now()}`
}
// convert the qs hash into a query string and append it // convert the qs hash into a query string and append it
$scope.pdf.url += createQueryString(qs) $scope.pdf.url += createQueryString(qs)
// Save all downloads as files // Save all downloads as files
qs.popupDownload = true qs.popupDownload = true
// Pass build id to download if we have it const { build: buildId } = fileByPath['output.pdf']
let buildId = null
if (fileByPath['output.pdf'] && fileByPath['output.pdf'].build) {
buildId = fileByPath['output.pdf'].build
}
$scope.pdf.downloadUrl = $scope.pdf.downloadUrl =
`/download/project/${$scope.project_id}${ `/download/project/${$scope.project_id}/build/${buildId}/output/output.pdf` +
buildId ? '/build/' + buildId : '' createQueryString(qs)
}/output/output.pdf` + createQueryString(qs)
fetchLogs(fileByPath, { pdfDownloadDomain }) fetchLogs(fileByPath, { pdfDownloadDomain })
} else if (response.status === 'timedout') { } else if (response.status === 'timedout') {
$scope.pdf.view = 'errors' $scope.pdf.view = 'errors'
@ -435,7 +411,7 @@ App.controller('PdfController', function(
['validation-fail', 'validation-pass'].includes(response.status) ['validation-fail', 'validation-pass'].includes(response.status)
) { ) {
$scope.pdf.view = 'pdf' $scope.pdf.view = 'pdf'
$scope.pdf.url = buildPdfDownloadUrl(pdfDownloadDomain, lastPdfUrl) $scope.pdf.url = lastPdfUrl
$scope.shouldShowLogs = true $scope.shouldShowLogs = true
if (response.status === 'validation-fail') { if (response.status === 'validation-fail') {
$scope.pdf.failedCheck = true $scope.pdf.failedCheck = true
@ -445,7 +421,7 @@ App.controller('PdfController', function(
} else if (response.status === 'exited') { } else if (response.status === 'exited') {
$scope.pdf.view = 'pdf' $scope.pdf.view = 'pdf'
$scope.pdf.compileExited = true $scope.pdf.compileExited = true
$scope.pdf.url = buildPdfDownloadUrl(pdfDownloadDomain, lastPdfUrl) $scope.pdf.url = lastPdfUrl
$scope.shouldShowLogs = true $scope.shouldShowLogs = true
fetchLogs(fileByPath, { pdfDownloadDomain }) fetchLogs(fileByPath, { pdfDownloadDomain })
} else if (response.status === 'autocompile-backoff') { } else if (response.status === 'autocompile-backoff') {
@ -538,9 +514,7 @@ App.controller('PdfController', function(
name: isOutputFile name: isOutputFile
? `${file.path.replace(/^output\./, '')} file` ? `${file.path.replace(/^output\./, '')} file`
: file.path, : file.path,
url: url: file.url + createQueryString(qs),
`/project/${$scope.project_id}/output/${file.path}` +
createQueryString(qs),
main: !!isOutputFile, main: !!isOutputFile,
fileName: file.path, fileName: file.path,
type: file.type type: file.type
@ -568,24 +542,12 @@ App.controller('PdfController', function(
function getFile(name, file) { function getFile(name, file) {
const opts = { const opts = {
method: 'GET', method: 'GET',
url: buildPdfDownloadUrl(options.pdfDownloadDomain, file.url),
params: { params: {
compileGroup: ide.compileGroup, compileGroup: ide.compileGroup,
clsiserverid: ide.clsiServerId clsiserverid: ide.clsiServerId
} }
} }
if (file && file.url) {
// FIXME clean this up when we have file.urls out consistently
opts.url = file.url
} else if (file && file.build) {
opts.url = `/project/${$scope.project_id}/build/${file.build}/output/${name}`
} else {
opts.url = `/project/${$scope.project_id}/output/${name}`
}
// check if we need to bust cache (build id is unique so don't need it in that case)
if (!(file && file.build)) {
opts.params.cache_bust = `${Date.now()}`
}
opts.url = buildPdfDownloadUrl(options.pdfDownloadDomain, opts.url)
return $http(opts) return $http(opts)
} }