2021-09-30 07:29:25 -04:00
|
|
|
import getMeta from '../../../utils/meta'
|
|
|
|
import HumanReadableLogs from '../../../ide/human-readable-logs/HumanReadableLogs'
|
|
|
|
import BibLogParser from '../../../ide/log-parser/bib-log-parser'
|
2021-12-07 04:18:48 -05:00
|
|
|
import { v4 as uuid } from 'uuid'
|
2021-09-30 07:29:25 -04:00
|
|
|
|
|
|
|
const searchParams = new URLSearchParams(window.location.search)
|
|
|
|
|
2022-05-18 06:37:25 -04:00
|
|
|
export function handleOutputFiles(outputFiles, projectId, data) {
|
2021-09-30 07:29:25 -04:00
|
|
|
const result = {}
|
|
|
|
|
|
|
|
const outputFile = outputFiles.get('output.pdf')
|
|
|
|
|
|
|
|
if (outputFile) {
|
|
|
|
// build the URL for viewing the PDF in the preview UI
|
|
|
|
const params = new URLSearchParams({
|
|
|
|
compileGroup: data.compileGroup,
|
|
|
|
})
|
|
|
|
|
|
|
|
if (data.clsiServerId) {
|
|
|
|
params.set('clsiserverid', data.clsiServerId)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (searchParams.get('verify_chunks') === 'true') {
|
|
|
|
// Instruct the serviceWorker to verify composed ranges.
|
|
|
|
params.set('verify_chunks', 'true')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getMeta('ol-enablePdfCaching')) {
|
|
|
|
// Tag traffic that uses the pdf caching logic.
|
|
|
|
params.set('enable_pdf_caching', 'true')
|
|
|
|
}
|
|
|
|
|
2022-06-01 09:36:50 -04:00
|
|
|
result.pdfUrl = `${buildURL(outputFile, data.pdfDownloadDomain)}?${params}`
|
2021-09-30 07:29:25 -04:00
|
|
|
|
|
|
|
// build the URL for downloading the PDF
|
|
|
|
params.set('popupDownload', 'true') // save PDF download as file
|
|
|
|
|
|
|
|
result.pdfDownloadUrl = `/download/project/${projectId}/build/${outputFile.build}/output/output.pdf?${params}`
|
|
|
|
}
|
|
|
|
|
2022-03-25 05:42:10 -04:00
|
|
|
return result
|
|
|
|
}
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-03-25 05:42:10 -04:00
|
|
|
export const handleLogFiles = async (outputFiles, data, signal) => {
|
|
|
|
const result = {
|
|
|
|
log: null,
|
|
|
|
logEntries: {
|
|
|
|
errors: [],
|
|
|
|
warnings: [],
|
|
|
|
typesetting: [],
|
|
|
|
},
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function accumulateResults(newEntries, type) {
|
|
|
|
for (const key in result.logEntries) {
|
|
|
|
if (newEntries[key]) {
|
|
|
|
for (const entry of newEntries[key]) {
|
|
|
|
if (type) {
|
|
|
|
entry.type = newEntries.type
|
|
|
|
}
|
|
|
|
if (entry.file) {
|
|
|
|
entry.file = normalizeFilePath(entry.file)
|
|
|
|
}
|
2021-12-07 04:18:48 -05:00
|
|
|
entry.key = uuid()
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
|
|
|
result.logEntries[key].push(...newEntries[key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const logFile = outputFiles.get('output.log')
|
|
|
|
|
|
|
|
if (logFile) {
|
2022-03-25 05:42:10 -04:00
|
|
|
try {
|
2022-06-01 09:36:50 -04:00
|
|
|
const response = await fetch(buildURL(logFile, data.pdfDownloadDomain), {
|
2022-03-25 05:42:10 -04:00
|
|
|
signal,
|
|
|
|
})
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-03-25 05:42:10 -04:00
|
|
|
result.log = await response.text()
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-03-25 05:42:10 -04:00
|
|
|
const { errors, warnings, typesetting } = HumanReadableLogs.parse(
|
|
|
|
result.log,
|
|
|
|
{
|
|
|
|
ignoreDuplicates: true,
|
|
|
|
}
|
|
|
|
)
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-03-25 05:42:10 -04:00
|
|
|
accumulateResults({ errors, warnings, typesetting })
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(e) // ignore failure to fetch/parse the log file, but log a warning
|
|
|
|
}
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const blgFile = outputFiles.get('output.blg')
|
|
|
|
|
|
|
|
if (blgFile) {
|
2021-11-25 05:23:16 -05:00
|
|
|
try {
|
2022-06-01 09:36:50 -04:00
|
|
|
const response = await fetch(buildURL(blgFile, data.pdfDownloadDomain), {
|
2022-03-25 05:42:10 -04:00
|
|
|
signal,
|
|
|
|
})
|
|
|
|
|
|
|
|
const log = await response.text()
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { errors, warnings } = new BibLogParser(log, {
|
|
|
|
maxErrors: 100,
|
|
|
|
}).parse()
|
|
|
|
accumulateResults({ errors, warnings }, 'BibTeX:')
|
|
|
|
} catch (e) {
|
|
|
|
// BibLog parsing errors are ignored
|
|
|
|
}
|
2021-11-25 05:23:16 -05:00
|
|
|
} catch (e) {
|
2022-03-25 05:42:10 -04:00
|
|
|
console.warn(e) // ignore failure to fetch/parse the log file, but log a warning
|
2021-11-25 05:23:16 -05:00
|
|
|
}
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
|
|
|
|
2021-11-25 05:24:25 -05:00
|
|
|
result.logEntries.all = [
|
|
|
|
...result.logEntries.errors,
|
|
|
|
...result.logEntries.warnings,
|
|
|
|
...result.logEntries.typesetting,
|
|
|
|
]
|
|
|
|
|
2021-09-30 07:29:25 -04:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildLogEntryAnnotations(entries, fileTreeManager) {
|
|
|
|
const rootDocDirname = fileTreeManager.getRootDocDirname()
|
|
|
|
|
|
|
|
const logEntryAnnotations = {}
|
|
|
|
|
|
|
|
for (const entry of entries) {
|
|
|
|
if (entry.file) {
|
|
|
|
entry.file = normalizeFilePath(entry.file, rootDocDirname)
|
|
|
|
|
|
|
|
const entity = fileTreeManager.findEntityByPath(entry.file)
|
|
|
|
|
|
|
|
if (entity) {
|
|
|
|
if (!(entity.id in logEntryAnnotations)) {
|
|
|
|
logEntryAnnotations[entity.id] = []
|
|
|
|
}
|
|
|
|
|
|
|
|
logEntryAnnotations[entity.id].push({
|
|
|
|
row: entry.line - 1,
|
|
|
|
type: entry.level === 'error' ? 'error' : 'warning',
|
|
|
|
text: entry.message,
|
|
|
|
source: 'compile',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return logEntryAnnotations
|
|
|
|
}
|
|
|
|
|
2022-06-01 09:36:50 -04:00
|
|
|
function buildURL(file, pdfDownloadDomain) {
|
|
|
|
if (file.build && pdfDownloadDomain) {
|
|
|
|
// Downloads from the compiles domain must include a build id.
|
|
|
|
// The build id is used implicitly for access control.
|
|
|
|
return `${pdfDownloadDomain}${file.url}`
|
|
|
|
}
|
|
|
|
// Go through web instead, which uses mongo for checking project access.
|
|
|
|
return file.url
|
|
|
|
}
|
|
|
|
|
2021-09-30 07:29:25 -04:00
|
|
|
function normalizeFilePath(path, rootDocDirname) {
|
|
|
|
path = path.replace(
|
|
|
|
/^.*\/compiles\/[0-9a-f]{24}(-[0-9a-f]{24})?\/(\.\/)?/,
|
|
|
|
''
|
|
|
|
)
|
|
|
|
|
|
|
|
path = path.replace(/^\/compile\//, '')
|
|
|
|
|
|
|
|
if (rootDocDirname) {
|
|
|
|
path = path.replace(/^\.\//, rootDocDirname + '/')
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|