file-list.ts 2.2 KB

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