Skip to content

Commit

Permalink
fix: use system's tmp dir (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbrugger authored Sep 6, 2023
1 parent 0bd3f75 commit 9786771
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-deers-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@flatfile/plugin-zip-extractor': patch
---

Use system's tmp directory for unzipping
11 changes: 9 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion plugins/zip-extractor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
"@flatfile/api": "^1.4.9",
"@flatfile/hooks": "^1.3.0",
"@flatfile/listener": "^0.3.15",
"@flatfile/util-file-buffer": "0.0.4",
"@flatfile/util-common": "^0.1.1",
"@flatfile/util-file-buffer": "^0.0.4",
"adm-zip": "^0.5.10",
"os": "^0.1.2",
"remeda": "^1.14.0"
},
"devDependencies": {
Expand Down
28 changes: 19 additions & 9 deletions plugins/zip-extractor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import api from '@flatfile/api'
import * as fs from 'fs'
import path from 'path'
import AdmZip from 'adm-zip'
import { tmpdir } from 'os'
import { logInfo } from '@flatfile/util-common'

export const ZipExtractor = () => {
export interface PluginOptions {
readonly debug?: boolean
}

export const ZipExtractor = (options: PluginOptions = {}) => {
return (handler: FlatfileListener) => {
handler.use(
fileBuffer('.zip', async (file, buffer, event) => {
Expand All @@ -23,6 +29,9 @@ export const ZipExtractor = () => {
info: 'Unzipping file',
})
const zip = new AdmZip(buffer)
if (options.debug) {
logInfo('@flatfile/plugin-zip-extractor', `tmpdir ${tmpdir()}`)
}
const zipEntries = zip.getEntries()
await api.jobs.ack(job.data.id, {
progress: 50,
Expand All @@ -34,13 +43,14 @@ export const ZipExtractor = () => {
!zipEntry.entryName.startsWith('__MACOSX') &&
!zipEntry.isDirectory
) {
zip.extractEntryTo(
zipEntry,
path.join(__dirname, '../'),
false,
true
)
const filePath = path.join(__dirname, '../', zipEntry.name)
zip.extractEntryTo(zipEntry, tmpdir(), false, true)
const filePath = path.join(tmpdir(), zipEntry.name)
if (options.debug) {
logInfo(
'@flatfile/plugin-zip-extractor',
`filePath ${filePath}`
)
}
const stream = fs.createReadStream(filePath)
await api.files.upload(stream, {
spaceId,
Expand All @@ -54,7 +64,7 @@ export const ZipExtractor = () => {
info: 'Extraction complete',
})
} catch (e) {
console.log(`error ${e}`)
logInfo('@flatfile/plugin-zip-extractor', `error ${e}`)
await api.jobs.fail(job.data.id, {
info: `Extraction failed ${e.message}`,
})
Expand Down

0 comments on commit 9786771

Please sign in to comment.