Skip to content

Commit 3b8fe1c

Browse files
committed
文件上传以hash值命名,不重复保存
1 parent 999e430 commit 3b8fe1c

File tree

3 files changed

+141
-59
lines changed

3 files changed

+141
-59
lines changed

.gitignore

Lines changed: 0 additions & 25 deletions
This file was deleted.

lib/uploadhandler.js

Lines changed: 97 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,67 @@ var EventEmitter = require('events').EventEmitter,
66
mkdirp = require('mkdirp'),
77
_ = require('lodash'),
88
async = require('async');
9+
crypto = require('crypto');
910
var doNothing=function(){};
1011

1112
module.exports = function (options) {
13+
var getFiles = function (callback) {
14+
var files = {};
15+
var counter = 1;
16+
var finish = function () {
17+
if (!--counter)
18+
callback(files);
19+
};
20+
21+
fs.readdir(options.uploadDir(), _.bind(function (err, list) {
22+
// console.log(list);//子目录
23+
_.each(list, function (dir) {
24+
const subDir = options.uploadDir() + '/' + dir;
25+
fs.readdir(subDir, _.bind(function (err, _list) {
26+
_.each(_list, function (name) {
27+
var stats = fs.statSync(subDir + '/' + name);
28+
if (stats.isFile()) {
29+
files[name] = {
30+
path: subDir + '/' + name
31+
};
32+
_.each(options.imageVersions, function (value, version) {
33+
counter++;
34+
fs.exists(options.uploadDir() + '/' + version + '/' + name, function (exists) {
35+
if (exists)
36+
files[name][version] = options.uploadDir() + '/' + version + '/' + name;
37+
finish();
38+
});
39+
});
40+
}
41+
});
42+
finish();
43+
}))
44+
}, this);
45+
// finish();
46+
}, this));
47+
return files;
48+
};
49+
50+
var getHash = function (filePath, callback) {
51+
var is = fs.createReadStream(filePath);
52+
const hash = crypto.createHash('sha1');
53+
is.on('data', function (data) {
54+
hash.update(data);
55+
});
56+
is.on('end', function (err) {
57+
if (!err) {
58+
const sha1 = hash.digest('hex');
59+
callback(sha1);
60+
}
61+
});
62+
}
63+
64+
var isFileExist = function (fileName, callback) {
65+
getFiles(function (files) {
66+
const fileNames = Object.keys(files);
67+
callback(_.includes(fileNames, fileName));
68+
})
69+
}
1270

1371
var FileInfo = require('./fileinfo')(
1472
_.extend({
@@ -128,7 +186,7 @@ module.exports = function (options) {
128186
imageMagick.resize({
129187
width: opts.width,
130188
height: opts.height,
131-
srcPath: options.uploadDir() + '/' + fileInfo.name,
189+
srcPath: options.uploadDir() + '/' + fileInfo.fileDir + '/' + fileInfo.name,
132190
dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name,
133191
customArgs: opts.imageArgs || ['-auto-orient']
134192
}, finish);
@@ -138,22 +196,43 @@ module.exports = function (options) {
138196
}
139197

140198
mkdirp(options.uploadDir() + '/', function(err, made) {
141-
fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) {
142-
if (!err) {
143-
generatePreviews();
144-
finish();
145-
} else {
146-
var is = fs.createReadStream(file.path);
147-
var os = fs.createWriteStream(options.uploadDir() + '/' + fileInfo.name);
148-
is.on('end', function (err) {
149-
if (!err) {
150-
fs.unlink(file.path);
151-
generatePreviews();
152-
}
199+
//文件后缀
200+
const fileSuffix = fileInfo.name.substring(fileInfo.name.lastIndexOf('.'));
201+
202+
getHash(file.path, function (hash) {
203+
const fileName = hash + fileSuffix;
204+
//子目录
205+
const fileDir = fileName.substring(0,2);
206+
fileInfo.fileDir = fileDir;
207+
fileInfo.name = fileName;
208+
209+
isFileExist(fileName, function (isExist) {
210+
if(isExist) {
153211
finish();
212+
return;
213+
}
214+
215+
mkdirp(options.uploadDir() + '/' + fileDir + '/', function(err, made_1) {
216+
const new_path = options.uploadDir() + '/' + fileDir + '/' + fileName;
217+
fs.rename(file.path, new_path, function (err) {
218+
if (!err) {
219+
generatePreviews();
220+
finish();
221+
} else {
222+
var is = fs.createReadStream(file.path);
223+
var os = fs.createWriteStream(new_path);
224+
is.on('end', function (err) {
225+
if (!err) {
226+
fs.unlink(file.path);
227+
generatePreviews();
228+
}
229+
finish();
230+
});
231+
is.pipe(os);
232+
}
233+
});
154234
});
155-
is.pipe(os);
156-
}
235+
});
157236
});
158237
});
159238
}
@@ -199,11 +278,11 @@ module.exports = function (options) {
199278

200279
UploadHandler.prototype.initUrls = function (fileInfo, cb) {
201280
var baseUrl = (options.ssl ? 'https:' : 'http:') + '//' + (options.hostname || this.req.get('Host'));
202-
fileInfo.setUrl(null, baseUrl + options.uploadUrl());
203-
fileInfo.setUrl('delete', baseUrl + this.req.originalUrl);
281+
fileInfo.setUrl(null, baseUrl + options.uploadUrl() + '/' + fileInfo.fileDir);
282+
fileInfo.setUrl('delete', baseUrl + this.req.originalUrl + '/' + fileInfo.fileDir);
204283
async.each(Object.keys(options.imageVersions), function(version, cb) {
205284
fs.exists(options.uploadDir() + '/' + version + '/' + fileInfo.name, function(exists) {
206-
if (exists) fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version);
285+
if (exists) fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version + '/');
207286
cb(null);
208287
})
209288
},

package.json

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,62 @@
11
{
2-
"name": "jquery-file-upload-middleware",
3-
"author": "Aleksandr Guidrevitch <aguidrevitch@gmail.com>",
4-
"description": "jQuery-File-Upload Express.js Middleware",
5-
"keywords": [
6-
"jquery",
7-
"upload",
8-
"express",
9-
"middleware"
2+
"_from": "github:fangj/jquery-file-upload-middleware",
3+
"_id": "jquery-file-upload-middleware@0.1.7",
4+
"_inBundle": false,
5+
"_location": "/jquery-file-upload-middleware",
6+
"_phantomChildren": {},
7+
"_requested": {
8+
"type": "git",
9+
"raw": "jquery-file-upload-middleware@github:fangj/jquery-file-upload-middleware",
10+
"name": "jquery-file-upload-middleware",
11+
"escapedName": "jquery-file-upload-middleware",
12+
"rawSpec": "github:fangj/jquery-file-upload-middleware",
13+
"saveSpec": "github:fangj/jquery-file-upload-middleware",
14+
"fetchSpec": null,
15+
"gitCommittish": null
16+
},
17+
"_requiredBy": [
18+
"/"
1019
],
11-
"version": "0.1.7",
20+
"_resolved": "github:fangj/jquery-file-upload-middleware#999e430e5f207bc2ae8ded31b1b54d538cb95dab",
21+
"_spec": "jquery-file-upload-middleware@github:fangj/jquery-file-upload-middleware",
22+
"_where": "D:\\project\\lims_next_x1\\extra\\file_service",
23+
"author": {
24+
"name": "Aleksandr Guidrevitch",
25+
"email": "aguidrevitch@gmail.com"
26+
},
27+
"bugs": {
28+
"url": "https://github.com/aguidrevitch/jquery-file-upload-middleware/issues"
29+
},
30+
"bundleDependencies": false,
1231
"dependencies": {
32+
"async": "*",
1333
"formidable": ">=1.0.11",
1434
"imagemagick": ">=0.1.2",
1535
"lodash": ">= 0.9.2",
16-
"mkdirp": ">= 0.3.4",
17-
"async": "*"
36+
"mkdirp": ">= 0.3.4"
1837
},
38+
"deprecated": false,
39+
"description": "jQuery-File-Upload Express.js Middleware",
40+
"devDependencies": {},
1941
"engines": {
2042
"node": ">= 0.8.8"
2143
},
44+
"homepage": "https://github.com/aguidrevitch/jquery-file-upload-middleware#readme",
45+
"keywords": [
46+
"jquery",
47+
"upload",
48+
"express",
49+
"middleware"
50+
],
51+
"license": "MIT",
52+
"main": "./index.js",
53+
"name": "jquery-file-upload-middleware",
2254
"repository": {
2355
"type": "git",
2456
"url": "git://github.com/aguidrevitch/jquery-file-upload-middleware.git"
2557
},
26-
"main": "./index.js",
27-
"readmeFilename": "README.md",
28-
"devDependencies": {},
2958
"scripts": {
3059
"test": "echo \"Error: no test specified\" && exit 1"
3160
},
32-
"_id": "jquery-file-upload-middleware@0.1.7",
33-
"license": "MIT"
61+
"version": "0.1.7"
3462
}

0 commit comments

Comments
 (0)