file-list.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import {
  2. CompileOutputFile,
  3. CompileResponseData,
  4. PDFFile,
  5. } from '../../../../../types/compile'
  6. import { PdfFileDataList } from '@/features/pdf-preview/util/types'
  7. const topFileTypes = ['bbl', 'gls', 'ind']
  8. // NOTE: Updating this list requires a corresponding change in
  9. // * services/clsi/app/js/OutputFileArchiveManager.js
  10. const ignoreFiles = ['output.fls', 'output.fdb_latexmk']
  11. export function buildFileList(
  12. outputFiles: Map<string, CompileOutputFile>,
  13. {
  14. clsiServerId,
  15. clsiCacheShard,
  16. compileGroup,
  17. outputFilesArchive,
  18. fromCache = false,
  19. }: CompileResponseData
  20. ): PdfFileDataList {
  21. const files: PdfFileDataList = { top: [], other: [] }
  22. if (outputFiles) {
  23. const params = new URLSearchParams()
  24. if (fromCache) {
  25. params.set('clsiserverid', clsiCacheShard || 'cache')
  26. } else {
  27. if (clsiServerId) {
  28. params.set('clsiserverid', clsiServerId)
  29. }
  30. const pdf = outputFiles.get('output.pdf') as PDFFile
  31. if (pdf) {
  32. params.set('editorId', pdf.editorId)
  33. }
  34. }
  35. if (compileGroup) {
  36. params.set('compileGroup', compileGroup)
  37. }
  38. const queryString = params.toString()
  39. const allFiles = []
  40. // filter out ignored files and set some properties
  41. for (const file of outputFiles.values()) {
  42. if (!ignoreFiles.includes(file.path)) {
  43. file.main = file.path.startsWith('output.')
  44. if (queryString.length) {
  45. file.url += `?${queryString}`
  46. }
  47. allFiles.push(file)
  48. }
  49. }
  50. // sort main files first, then alphabetical
  51. allFiles.sort((a, b) => {
  52. if (a.main && !b.main) {
  53. return -1
  54. }
  55. if (b.main && !a.main) {
  56. return 1
  57. }
  58. return a.path.localeCompare(b.path, undefined, { numeric: true })
  59. })
  60. // group files into "top" and "other"
  61. for (const file of allFiles) {
  62. if (topFileTypes.includes(file.type)) {
  63. files.top.push(file)
  64. } else if (!(file.type === 'pdf' && file.main === true)) {
  65. files.other.push(file)
  66. }
  67. }
  68. const archivableFiles = [...files.top, ...files.other]
  69. if (outputFilesArchive && archivableFiles.length > 0) {
  70. archivableFiles.forEach(file => params.append('files', file.path))
  71. files.archive = {
  72. ...outputFilesArchive,
  73. fileCount: archivableFiles.length,
  74. url: `${outputFilesArchive.url}?${params.toString()}`,
  75. }
  76. }
  77. }
  78. return files
  79. }