read-file.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import fs from 'node:fs'
  2. import path from 'node:path'
  3. import { getDocument } from 'pdfjs-dist/legacy/build/pdf.mjs'
  4. import AdmZip from 'adm-zip'
  5. import { setTimeout } from 'node:timers/promises'
  6. const MAX_ATTEMPTS = 15
  7. const POLL_INTERVAL = 500
  8. type ReadFileInZipArgs = {
  9. pathToZip: string
  10. fileToRead: string
  11. }
  12. export async function readFileInZip({
  13. pathToZip,
  14. fileToRead,
  15. }: ReadFileInZipArgs) {
  16. let attempt = 0
  17. while (attempt < MAX_ATTEMPTS) {
  18. if (fs.existsSync(pathToZip)) {
  19. const zip = new AdmZip(path.resolve(pathToZip))
  20. const entry = zip
  21. .getEntries()
  22. .find(entry => entry.entryName === fileToRead)
  23. if (entry) {
  24. return entry.getData().toString('utf8')
  25. } else {
  26. throw new Error(`${fileToRead} not found in ${pathToZip}`)
  27. }
  28. }
  29. await setTimeout(POLL_INTERVAL)
  30. attempt++
  31. }
  32. throw new Error(`${pathToZip} not found`)
  33. }
  34. export async function readPdf(file: string) {
  35. let attempt = 0
  36. while (attempt < MAX_ATTEMPTS) {
  37. if (fs.existsSync(file)) {
  38. const pdf = await getDocument(file).promise
  39. const text = []
  40. try {
  41. for (let i = 1; i <= pdf.numPages; i++) {
  42. const page = await pdf.getPage(i)
  43. const content = await page.getTextContent()
  44. for (const item of content.items) {
  45. if ('str' in item) text.push(item.str)
  46. }
  47. }
  48. return text.join('\n')
  49. } finally {
  50. await pdf.destroy()
  51. }
  52. }
  53. await setTimeout(POLL_INTERVAL)
  54. attempt++
  55. }
  56. throw new Error(`${file} not found`)
  57. }