forked from akapuya/s3-unzip-plus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
138 lines (118 loc) · 5.28 KB
/
util.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Copyright (c) 2017 Steve Yardumian
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use strict'
var AWS = require('aws-sdk')
var s3 = new AWS.S3()
var AdmZip = require('adm-zip')
var fs = require('fs')
var dateTime = require('date-time')
var md5 = require('md5')
var mime = require('mime-types')
var decompress = function (/* String */command, /* Function */ cb) {
const targetBucket = Object.prototype.hasOwnProperty.call(command, 'targetBucket') ? command.targetBucket : command.bucket
let targetFolder = Object.prototype.hasOwnProperty.call(command, 'targetFolder') ? command.targetFolder : ''
if (targetFolder.length > 0) targetFolder += '/'
if (!command.bucket || !command.file) { // bucket and file are required
if (cb) cb(new Error('Error: missing either bucket name, full filename, targetBucket or targetKey!'))
else console.error('Error: missing either bucket name, full filename, targetBucket or targetKey!')
return
}
s3.getObject({
Bucket: command.bucket,
Key: command.file
}, function (err, data) {
if (err) {
if (cb) cb(new Error('File Error: ' + err.message))
else console.error('File Error: ' + err.message)
} else {
if (command.verbose) console.log('Zip file \'' + command.file + '\' found in S3 bucket!')
var metadata = {}
if (command.copyMetadata) {
metadata = data.Metadata
console.log('zip metadata', metadata)
}
// write the zip file locally in a tmp dir
var tmpZipFilename = md5(dateTime({ showMilliseconds: true }))
fs.writeFileSync('/tmp/' + tmpZipFilename + '.zip', data.Body)
// check that file in that location is a zip content type, otherwise throw error and exit
if (mime.lookup('/tmp/' + tmpZipFilename + '.zip') !== 'application/zip') {
if (cb) cb(new Error('Error: file is not of type zip. Please select a valid file (filename.zip).'))
else console.error('Error: file is not of type zip. Please select a valid file (filename.zip).')
fs.unlinkSync('/tmp/' + tmpZipFilename + '.zip')
return
}
var zip, zipEntries, zipEntryCount
// find all files in the zip and the count of them
try {
zip = new AdmZip('/tmp/' + tmpZipFilename + '.zip')
zipEntries = zip.getEntries()
zipEntryCount = Object.keys(zipEntries).length
} catch (err) {
cb(err)
}
// if no files found in the zip
if (zipEntryCount === 0) {
if (cb) cb(new Error('Error: the zip file was empty!'))
else console.error('Error: the zip file was empty!')
fs.unlinkSync('/tmp/' + tmpZipFilename + '.zip')
return
}
// for each file in the zip, decompress and upload it to S3; once all are uploaded, delete the tmp zip and zip on S3
var counter = 0
zipEntries.forEach(function (zipEntry) {
s3.upload({ Bucket: targetBucket, Key: targetFolder + zipEntry.entryName, Body: zipEntry.getData(), Metadata: metadata }, function (err, data) {
counter++
if (err) {
if (cb) cb(new Error('Upload Error: ' + err.message))
else console.error('Upload Error: ' + err.message)
fs.unlinkSync('/tmp/' + tmpZipFilename + '.zip')
return
}
if (command.verbose) console.log('File decompressed to S3: ' + data.Location)
// if all files are unzipped...
if (zipEntryCount === counter) {
// delete the tmp (local) zip file
fs.unlinkSync('/tmp/' + tmpZipFilename + '.zip')
if (command.verbose) console.log('Local temp zip file deleted.')
// delete the zip file up on S3
if (command.deleteOnSuccess) {
s3.deleteObject({ Bucket: command.bucket, Key: command.file }, function (err) {
if (err) {
if (cb) cb(new Error('Delete Error: ' + err.message))
else console.error('Delete Error: ' + err.message)
return
}
if (command.verbose) console.log('S3 file \'' + command.file + '\' deleted.')
// WE GOT TO THE END
cb(null, 'Success!')
})
} else {
// WE GOT TO THE END
cb(null, 'Success!')
}
}
})
})
}
}
)
}
module.exports = {
decompress: decompress
}