-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileio.js
139 lines (120 loc) · 4.13 KB
/
fileio.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
139
const multer = require('multer')
const unzip = require('unzip')
const S3FS = require('s3fs')
const AWS = require('aws-sdk')
const multerS3 = require('multer-s3')
var path = require('path');
var appDir = path.dirname(require.main.filename);
console.log("app dir",appDir);
var fs = require('fs-extra');
var s3 = require('s3')
var AWSs3 = new AWS.S3( {
endpoint: 's3.us-east-2.amazonaws.com',
signatureVersion: 'v4',
region: 'us-east-2'
} );
var options = {
s3Client: AWSs3,
maxAsyncS3: 20, // this is the default
s3RetryCount: 4, // this is the default
s3RetryDelay: 2000, // this is the default
multipartUploadThreshold: 20971520, // this is the default (20 MB)
multipartUploadSize: 15728640, // this is the default (15 MB)
};
var client = s3.createClient(options);
var skysphereUpload = multer({storage: multerS3({
s3: AWSs3,
bucket: 'odinvr',
metadata: function (req, file, cb) {
cb(null, {fieldName: file.fieldname});
},
key: function (req, file, cb) {
var newFileName = Date.now() + "-" + file.originalname;
var fullPath = 'public/skyspheres/'+ newFileName;
cb(null, fullPath);
}
}),
limits: { fileSize: 10485760, files: 1 },
});
var modelUpload = multer({dest: 'temp/models', limits: { fileSize: 25000000 }});
function extractModelTempAndUpload(file,callback) {
const stream = fs.createReadStream(file.path).pipe(unzip.Extract({path:file.path}));
stream.on('finish', function () {
console.log("finished")
setTimeout(function() {
if (!fs.existsSync(file.path + "/model.dae")) {
callback(null,'ZIP contents invalid');
fs.remove(file.path);
return
}
console.log(file.path)
var params = {
localDir: appDir + '/' + file.path,
deleteRemoved: false, // default false, whether to remove s3 objects
// that have no corresponding local file.
s3Params: {
Bucket: "odinvr",
Prefix: "public/models/" + file.filename,
// other options supported by putObject, except Body and ContentLength.
// See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
},
};
var uploader = client.uploadDir(params);
uploader.on('error', function(err) {
console.error("unable to sync:", err.stack);
});
uploader.on('progress', function() {
console.log("progress", uploader.progressAmount, uploader.progressTotal);
});
uploader.on('end', function() {
console.log("done uploading");
fs.remove(file.path);
callback('http://odinvr.s3.us-east-2.amazonaws.com/public/models/' + file.filename,null);
});
},500);
});
stream.on('error', function(e) {
console.log("error",e);
callback(null,'Invalid model: not a valid model ZIP file');
});
}
function deleteModelFile(model,callback) {
if(!model.filename || model.filename.length == 0) {
callback({error: "Invalid model file name."})
return
}
var params = {
Bucket: 'odinvr',
Prefix: 'public/models/' + model.filename + '/'
};
AWSs3.listObjects(params, function(err, data) {
if (err) return callback({error: err});
if (data.Contents.length == 0) callback({error: "No content to delete"});
params = {Bucket: 'odinvr'};
params.Delete = {Objects:[]};
data.Contents.forEach(function(content) {
params.Delete.Objects.push({Key: content.Key});
});
AWSs3.deleteObjects(params, function(err, data) {
if (err) return callback({error: err});
callback(model);
});
});
}
function deleteSkysphere(key,callback) {
var params = {
Bucket: 'odinvr',
Prefix: 'public/skyspheres/',
};
params = {Bucket: 'odinvr'};
params.Delete = {Objects:[{Key: key}]};
AWSs3.deleteObjects(params, function(err, data) {
if (err) return callback({error: err});
callback(data);
});
}
module.exports.extractModelTempAndUpload = extractModelTempAndUpload;
module.exports.modelUpload = modelUpload;
module.exports.skysphereUpload = skysphereUpload;
module.exports.deleteModelFile = deleteModelFile;
module.exports.deleteSkysphere = deleteSkysphere;