read-file.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import fs from 'fs'
  2. import path from 'path'
  3. // @ts-ignore broken package entrypoint
  4. import pdf from 'pdf-parse/lib/pdf-parse.js'
  5. import AdmZip from 'adm-zip'
  6. import { setTimeout } from 'timers/promises'
  7. const MAX_ATTEMPTS = 15
  8. const POLL_INTERVAL = 500
  9. type ReadFileInZipArgs = {
  10. pathToZip: string
  11. fileToRead: string
  12. }
  13. export async function readFileInZip({
  14. pathToZip,
  15. fileToRead,
  16. }: ReadFileInZipArgs) {
  17. let attempt = 0
  18. while (attempt < MAX_ATTEMPTS) {
  19. if (fs.existsSync(pathToZip)) {
  20. const zip = new AdmZip(path.resolve(pathToZip))
  21. const entry = zip
  22. .getEntries()
  23. .find(entry => entry.entryName == fileToRead)
  24. if (entry) {
  25. return entry.getData().toString('utf8')
  26. } else {
  27. throw new Error(`${fileToRead} not found in ${pathToZip}`)
  28. }
  29. }
  30. await setTimeout(POLL_INTERVAL)
  31. attempt++
  32. }
  33. throw new Error(`${pathToZip} not found`)
  34. }
  35. export async function readPdf(file: string) {
  36. let attempt = 0
  37. while (attempt < MAX_ATTEMPTS) {
  38. if (fs.existsSync(file)) {
  39. const dataBuffer = fs.readFileSync(path.resolve(file))
  40. const { text } = await pdf(dataBuffer)
  41. return text
  42. }
  43. await setTimeout(POLL_INTERVAL)
  44. attempt++
  45. }
  46. throw new Error(`${file} not found`)
  47. }