read-file.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import fs from 'fs'
  2. import path from 'path'
  3. import pdf from 'pdf-parse'
  4. import AdmZip from 'adm-zip'
  5. import { promisify } from 'util'
  6. const sleep = promisify(setTimeout)
  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 sleep(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 sleep(POLL_INTERVAL)
  44. attempt++
  45. }
  46. throw new Error(`${file} not found`)
  47. }