file-list.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. compileGroup,
  15. outputFilesArchive,
  16. fromCache = false,
  17. }: CompileResponseData
  18. ): PdfFileDataList {
  19. const files: PdfFileDataList = { top: [], other: [] }
  20. if (outputFiles) {
  21. const params = new URLSearchParams()
  22. if (fromCache) {
  23. params.set('clsiserverid', 'cache')
  24. } else if (clsiServerId) {
  25. params.set('clsiserverid', clsiServerId)
  26. }
  27. if (compileGroup) {
  28. params.set('compileGroup', compileGroup)
  29. }
  30. const queryString = params.toString()
  31. const allFiles = []
  32. // filter out ignored files and set some properties
  33. for (const file of outputFiles.values()) {
  34. if (!ignoreFiles.includes(file.path)) {
  35. file.main = file.path.startsWith('output.')
  36. if (queryString.length) {
  37. file.url += `?${queryString}`
  38. }
  39. allFiles.push(file)
  40. }
  41. }
  42. // sort main files first, then alphabetical
  43. allFiles.sort((a, b) => {
  44. if (a.main && !b.main) {
  45. return -1
  46. }
  47. if (b.main && !a.main) {
  48. return 1
  49. }
  50. return a.path.localeCompare(b.path, undefined, { numeric: true })
  51. })
  52. // group files into "top" and "other"
  53. for (const file of allFiles) {
  54. if (topFileTypes.includes(file.type)) {
  55. files.top.push(file)
  56. } else if (!(file.type === 'pdf' && file.main === true)) {
  57. files.other.push(file)
  58. }
  59. }
  60. const archivableFiles = [...files.top, ...files.other]
  61. if (outputFilesArchive && archivableFiles.length > 0) {
  62. archivableFiles.forEach(file => params.append('files', file.path))
  63. files.archive = {
  64. ...outputFilesArchive,
  65. fileCount: archivableFiles.length,
  66. url: `${outputFilesArchive.url}?${params.toString()}`,
  67. }
  68. }
  69. }
  70. return files
  71. }