This repository has been archived by the owner on Nov 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathserver.js
229 lines (192 loc) · 6.7 KB
/
server.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* NodeJs Server-Side Example for Fine Uploader (traditional endpoints).
* Maintained by Widen Enterprises.
*
* This example:
* - handles non-CORS environments
* - handles delete file requests assuming the method is DELETE
* - Ensures the file size does not exceed the max
* - Handles chunked upload requests
* - Returns a public URL to allow Fine Uploader to display the image
* in the browser if the browser is not capable of generating previews
* pre-upload client-side.
*
* Requirements:
* - express (for handling requests)
* - rimraf (for "rm -rf" support)
* - mkdirp (for "mkdir -p" support)
*/
var PORT = 8000,
//dependencies
express = require("express"),
fs = require("fs"),
rimraf = require("rimraf"),
mkdirp = require("mkdirp"),
app = express(),
// paths/constants
fileInputName = "qqfile",
uploadedFilesPath = __dirname + "/assets/uploadedFiles/",
chunkDirName = "chunks";
app.use(express.bodyParser());
app.listen(PORT);
console.log("Express server started on port %s", PORT);
// routes
app.use(express.static(__dirname));
app.use("/fineuploader", express.static(__dirname + "/node_modules/fine-uploader/jquery.fine-uploader/"));
app.use("/uploads", express.static(uploadedFilesPath));
app.post("/uploads", onUpload);
app.delete("/uploads/:uuid", onDeleteFile);
function onUpload(req, res) {
var partIndex = req.body.qqpartindex;
// text/plain is required to ensure support for IE9 and older
res.set("Content-Type", "text/plain");
if (partIndex == null) {
onSimpleUpload(req, res);
}
else {
onChunkedUpload(req, res);
}
}
function onSimpleUpload(req, res) {
var file = req.files[fileInputName],
uuid = req.body.qquuid,
sendThumbnailUrl = req.body.sendThumbnailUrl == "true",
responseData = {
success: false
};
file.name = req.body.qqfilename;
moveUploadedFile(file, uuid, function() {
responseData.success = true;
if (sendThumbnailUrl) {
responseData.thumbnailUrl = "/uploads/" + uuid + "/" + file.name;
}
res.send(responseData);
},
function() {
responseData.error = "Problem copying the file!";
res.send(responseData);
});
}
function onChunkedUpload(req, res) {
var file = req.files[fileInputName],
size = parseInt(req.body.qqtotalfilesize),
uuid = req.body.qquuid,
index = req.body.qqpartindex,
totalParts = parseInt(req.body.qqtotalparts),
sendThumbnailUrl = req.body.sendThumbnailUrl == "true",
responseData = {
success: false
};
file.name = req.body.qqfilename;
storeChunk(file, uuid, index, totalParts, function() {
if (index < totalParts-1) {
responseData.success = true;
res.send(responseData);
}
else {
combineChunks(file, uuid, function() {
responseData.success = true;
if (sendThumbnailUrl) {
responseData.thumbnailUrl = "/uploads/" + uuid + "/" + file.name;
}
res.send(responseData);
},
function() {
responseData.error = "Problem conbining the chunks!";
res.send(responseData);
});
}
},
function(reset) {
responseData.error = "Problem storing the chunk!";
res.send(responseData);
});
}
function onDeleteFile(req, res) {
var uuid = req.params.uuid,
dirToDelete = uploadedFilesPath + uuid;
rimraf(dirToDelete, function(error) {
if (error) {
console.error("Problem deleting file! " + error);
res.status(500);
}
res.send();
});
}
function moveFile(destinationDir, sourceFile, destinationFile, success, failure) {
mkdirp(destinationDir, function(error) {
var sourceStream, destStream;
if (error) {
console.error("Problem creating directory " + destinationDir + ": " + error);
failure();
}
else {
sourceStream = fs.createReadStream(sourceFile);
destStream = fs.createWriteStream(destinationFile);
sourceStream
.on("error", function(error) {
console.error("Problem copying file: " + error.stack);
failure();
})
.on("end", success)
.pipe(destStream);
}
});
}
function moveUploadedFile(file, uuid, success, failure) {
var destinationDir = uploadedFilesPath + uuid + "/",
fileDestination = destinationDir + file.name;
moveFile(destinationDir, file.path, fileDestination, success, failure);
}
function storeChunk(file, uuid, index, numChunks, success, failure) {
var destinationDir = uploadedFilesPath + uuid + "/" + chunkDirName + "/",
chunkFilename = getChunkFilename(index, numChunks),
fileDestination = destinationDir + chunkFilename;
moveFile(destinationDir, file.path, fileDestination, success, failure);
}
function combineChunks(file, uuid, success, failure) {
var chunksDir = uploadedFilesPath + uuid + "/" + chunkDirName + "/",
destinationDir = uploadedFilesPath + uuid + "/",
fileDestination = destinationDir + file.name;
fs.readdir(chunksDir, function(err, fileNames) {
var destFileStream;
if (err) {
console.error("Problem listing chunks! " + err);
failure();
}
else {
fileNames.sort();
destFileStream = fs.createWriteStream(fileDestination, {flags: "a"});
appendToStream(destFileStream, chunksDir, fileNames, 0, function() {
rimraf(chunksDir, function(rimrafError) {
if (rimrafError) {
console.log("Problem deleting chunks dir! " + rimrafError);
}
});
success();
},
failure);
}
});
}
function appendToStream(destStream, srcDir, srcFilesnames, index, success, failure) {
if (index < srcFilesnames.length) {
fs.createReadStream(srcDir + srcFilesnames[index])
.on("end", function() {
appendToStream(destStream, srcDir, srcFilesnames, index+1, success, failure);
})
.on("error", function(error) {
console.error("Problem appending chunk! " + error);
failure();
})
.pipe(destStream, {end: false});
}
else {
success();
}
}
function getChunkFilename(index, count) {
var digits = new String(count).length,
zeros = new Array(digits + 1).join("0");
return (zeros + index).slice(-digits);
}