forked from orangewise/s3-zip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3-zip.js
100 lines (84 loc) · 2.75 KB
/
s3-zip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const s3Files = require('@sustainhawaii/s3-files')
const archiver = require('archiver')
const { PassThrough } = require('stream')
const fs = require('fs')
const s3Zip = {}
module.exports = s3Zip
s3Zip.archive = function (opts, folder, filesS3, filesZip) {
const self = this
let connectionConfig
this.folder = folder
self.debug = opts.debug || false
if ('s3' in opts) {
connectionConfig = {
s3: opts.s3
}
} else {
connectionConfig = {
region: opts.region
}
}
connectionConfig.bucket = opts.bucket
connectionConfig.extraGetObjectParams = opts.extraGetObjectParams
self.client = s3Files.connect(connectionConfig)
const keyStream = self.client.createKeyStream(folder, filesS3)
const preserveFolderStructure = opts.preserveFolderStructure === true || filesZip
const fileStream = s3Files.createFileStream(keyStream, preserveFolderStructure)
const archive = self.archiveStream(fileStream, filesS3, filesZip)
return archive
}
s3Zip.archiveStream = function (stream, filesS3, filesZip) {
const self = this
const folder = this.folder || ''
if (this.registerFormat) {
archiver.registerFormat(this.registerFormat, this.formatModule)
}
const archive = archiver(this.format || 'zip', this.archiverOpts || {})
archive.on('error', function (err) {
self.debug && console.log('archive error', err)
})
stream.on('data', function (file) {
if (file.path[file.path.length - 1] === '/') {
self.debug && console.log('don\'t append to zip', file.path)
return
}
let fname
if (filesZip) {
// Place files_s3[i] into the archive as files_zip[i]
const i = filesS3.indexOf(file.path.startsWith(folder) ? file.path.substr(folder.length) : file.path)
fname = (i >= 0 && i < filesZip.length) ? filesZip[i] : file.path
filesS3[i] = ''
} else {
// Just use the S3 file name
fname = file.path
}
const entryData = typeof fname === 'object' ? fname : { name: fname }
self.debug && console.log('append to zip', fname)
if (file.data.length === 0) {
archive.append('', entryData)
} else {
// archive.append(file.data, entryData)
fs.writeFileSync('myfile', file.data)
archive.file('myfile', entryData)
}
}).on('end', function () {
self.debug && console.log('end -> finalize')
archive.finalize()
}).on('error', function (err) {
archive.emit('error', err)
})
return archive
}
s3Zip.setFormat = function (format) {
this.format = format
return this
}
s3Zip.setArchiverOptions = function (archiverOpts) {
this.archiverOpts = archiverOpts
return this
}
s3Zip.setRegisterFormatOptions = function (registerFormat, formatModule) {
this.registerFormat = registerFormat
this.formatModule = formatModule
return this
}