Skip to content

Commit fb44371

Browse files
authored
Merge pull request #1 from zhaoyu69/master
文件上传以hash值命名,不重复保存
2 parents 999e430 + 9e9bcf6 commit fb44371

File tree

3 files changed

+86
-60
lines changed

3 files changed

+86
-60
lines changed

.gitignore

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

lib/uploadhandler.js

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ 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) {
12-
1313
var FileInfo = require('./fileinfo')(
1414
_.extend({
1515
baseDir: options.uploadDir
@@ -64,6 +64,10 @@ module.exports = function (options) {
6464
}, this));
6565
};
6666

67+
var isFileExist = function (filePath) {
68+
return fs.existsSync(filePath);
69+
}
70+
6771
UploadHandler.prototype.post = function () {
6872
var self = this,
6973
form = new formidable.IncomingForm(),
@@ -89,6 +93,7 @@ module.exports = function (options) {
8993
this.noCache();
9094

9195
form.uploadDir = options.tmpDir;
96+
form.hash = "sha1";
9297
form
9398
.on('fileBegin', function (name, file) {
9499
tmpFiles.push(file.path);
@@ -128,7 +133,7 @@ module.exports = function (options) {
128133
imageMagick.resize({
129134
width: opts.width,
130135
height: opts.height,
131-
srcPath: options.uploadDir() + '/' + fileInfo.name,
136+
srcPath: options.uploadDir() + '/' + fileInfo.fileDir + '/' + fileInfo.name,
132137
dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name,
133138
customArgs: opts.imageArgs || ['-auto-orient']
134139
}, finish);
@@ -138,22 +143,40 @@ module.exports = function (options) {
138143
}
139144

140145
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-
}
146+
//文件后缀
147+
const fileSuffix = fileInfo.name.substring(fileInfo.name.lastIndexOf('.'));
148+
const fileName = file.hash + fileSuffix;
149+
//子目录
150+
const fileDir = fileName.substring(0,2);
151+
const filePath = path.join(fileDir+fileName);
152+
fileInfo.fileDir = fileDir;
153+
fileInfo.name = fileName;
154+
const isExist = isFileExist(filePath);
155+
156+
if(isExist) {
157+
finish();
158+
return;
159+
}
160+
161+
mkdirp(path.join(options.uploadDir(),fileDir), function(err, made_1) {
162+
const new_path = path.join(options.uploadDir(),fileDir,fileName);
163+
fs.rename(file.path, new_path, function (err) {
164+
if (!err) {
165+
generatePreviews();
153166
finish();
154-
});
155-
is.pipe(os);
156-
}
167+
} else {
168+
var is = fs.createReadStream(file.path);
169+
var os = fs.createWriteStream(new_path);
170+
is.on('end', function (err) {
171+
if (!err) {
172+
fs.unlink(file.path);
173+
generatePreviews();
174+
}
175+
finish();
176+
});
177+
is.pipe(os);
178+
}
179+
});
157180
});
158181
});
159182
}
@@ -199,8 +222,8 @@ module.exports = function (options) {
199222

200223
UploadHandler.prototype.initUrls = function (fileInfo, cb) {
201224
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);
225+
fileInfo.setUrl(null, baseUrl + options.uploadUrl() + '/' + fileInfo.fileDir);
226+
fileInfo.setUrl('delete', baseUrl + this.req.originalUrl + '/' + fileInfo.fileDir);
204227
async.each(Object.keys(options.imageVersions), function(version, cb) {
205228
fs.exists(options.uploadDir() + '/' + version + '/' + fileInfo.name, function(exists) {
206229
if (exists) fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version);

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)