8000 avoid exception when delete file without thumbnail by fangj · Pull Request #81 · aguidrevitch/jquery-file-upload-middleware · GitHub
Skip to content

avoid exception when delete file without thumbnail #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions .gitignore

This file was deleted.

64 changes: 44 additions & 20 deletions lib/uploadhandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ var EventEmitter = require('events').EventEmitter,
mkdirp = require('mkdirp'),
_ = require('lodash'),
async = require('async');
crypto = require('crypto');
var doNothing=function(){};

module.exports = function (options) {

var FileInfo = require('./fileinfo')(
_.extend({
baseDir: options.uploadDir
Expand Down Expand Up @@ -63,6 +64,10 @@ module.exports = function (options) {
}, this));
};

var isFileExist = function (filePath) {
return fs.existsSync(filePath);
}

UploadHandler.prototype.post = function () {
var self = this,
form = new formidable.IncomingForm(),
Expand All @@ -88,6 +93,7 @@ module.exports = function (options) {
this.noCache();

form.uploadDir = options.tmpDir;
form.hash = "sha1";
form
.on('fileBegin', function (name, file) {
tmpFiles.push(file.path);
Expand Down Expand Up @@ -127,7 +133,7 @@ module.exports = function (options) {
imageMagick.resize({
width: opts.width,
height: opts.height,
srcPath: options.uploadDir() + '/' + fileInfo.name,
srcPath: options.uploadDir() + '/' + fileInfo.fileDir + '/' + fileInfo.name,
dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name,
customArgs: opts.imageArgs || ['-auto-orient']
}, finish);
Expand All @@ -137,22 +143,40 @@ module.exports = function (options) {
}

mkdirp(options.uploadDir() + '/', function(err, made) {
fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) {
if (!err) {
generatePreviews();
finish();
} else {
var is = fs.createReadStream(file.path);
var os = fs.createWriteStream(options.uploadDir() + '/' + fileInfo.name);
is.on('end', function (err) {
if (!err) {
fs.unlink(file.path);
generatePreviews();
}
//文件后缀
const fileSuffix = fileInfo.name.substring(fileInfo.name.lastIndexOf('.'));
const fileName = file.hash + fileSuffix;
//子目录
const fileDir = fileName.substring(0,2);
const filePath = path.join(fileDir+fileName);
fileInfo.fileDir = fileDir;
fileInfo.name = fileName;
const isExist = isFileExist(filePath);

if(isExist) {
finish();
return;
}

mkdirp(path.join(options.uploadDir(),fileDir), function(err, made_1) {
const new_path = path.join(options.uploadDir(),fileDir,fileName);
fs.rename(file.path, new_path, function (err) {
if (!err) {
generatePreviews();
finish();
});
is.pipe(os);
}
} else {
var is = fs.createReadStream(file.path);
var os = fs.createWriteStream(new_path);
is.on('end', function (err) {
if (!err) {
fs.unlink(file.path);
generatePreviews();
}
finish();
});
is.pipe(os);
}
});
});
});
}
Expand Down Expand Up @@ -189,7 +213,7 @@ module.exports = function (options) {
}
fs.unlink(filepath, function (ex) {
_.each(options.imageVersions, function (value, version) {
fs.unlink(path.join(options.uploadDir(), version, fileName));
fs.unlink(path.join(options.uploadDir(), version, fileName), doNothing);
});
self.emit('delete', fileName);
self.callback({success: !ex});
Expand All @@ -198,8 +222,8 @@ module.exports = function (options) {

UploadHandler.prototype.initUrls = function (fileInfo, cb) {
var baseUrl = (options.ssl ? 'https:' : 'http:') + '//' + (options.hostname || this.req.get('Host'));
fileInfo.setUrl(null, baseUrl + options.uploadUrl());
fileInfo.setUrl('delete', baseUrl + this.req.originalUrl);
fileInfo.setUrl(null, baseUrl + options.uploadUrl() + '/' + fileInfo.fileDir);
fileInfo.setUrl('delete', baseUrl + this.req.originalUrl + '/' + fileInfo.fileDir);
async.each(Object.keys(options.imageVersions), function(version, cb) {
fs.exists(options.uploadDir() + '/' + version + '/' + fileInfo.name, function(exists) {
if (exists) fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version);
Expand Down
60 changes: 44 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,62 @@
{
"name": "jquery-file-upload-middleware",
"author": "Aleksandr Guidrevitch <aguidrevitch@gmail.com>",
"description": "jQuery-File-Upload Express.js Middleware",
"keywords": [
"jquery",
"upload",
"express",
"middleware"
"_from": "github:fangj/jquery-file-upload-middleware",
"_id": "jquery-file-upload-middleware@0.1.7",
"_inBundle": false,
"_location": "/jquery-file-upload-middleware",
"_phantomChildren": {},
"_requested": {
"type": "git",
"raw": "jquery-file-upload-middleware@github:fangj/jquery-file-upload-middleware",
"name": "jquery-file-upload-middleware",
"escapedName": "jquery-file-upload-middleware",
"rawSpec": "github:fangj/jquery-file-upload-middleware",
"saveSpec": "github:fangj/jquery-file-upload-middleware",
"fetchSpec": null,
"gitCommittish": null
},
"_requiredBy": [
"/"
],
"version": "0.1.7",
"_resolved": "github:fangj/jquery-file-upload-middleware#999e430e5f207bc2ae8ded31b1b54d538cb95dab",
"_spec": "jquery-file-upload-middleware@github:fangj/jquery-file-upload-middleware",
"_where": "D:\\project\\lims_next_x1\\extra\\file_service",
"author": {
"name": "Aleksandr Guidrevitch",
"email": "aguidrevitch@gmail.com"
},
"bugs": {
"url": "https://github.com/aguidrevitch/jquery-file-upload-middleware/issues"
},
"bundleDependencies": false,
"dependencies": {
"async": "*",
"formidable": ">=1.0.11",
"imagemagick": ">=0.1.2",
"lodash": ">= 0.9.2",
"mkdirp": ">= 0.3.4",
"async": "*"
"mkdirp": ">= 0.3.4"
},
"deprecated": false,
"description": "jQuery-File-Upload Express.js Middleware",
"devDependencies": {},
"engines": {
"node": ">= 0.8.8"
},
"homepage": "https://github.com/aguidrevitch/jquery-file-upload-middleware#readme",
"keywords": [
"jquery",
"upload",
"express",
"middleware"
],
"license": "MIT",
"main": "./index.js",
"name": "jquery-file-upload-middleware",
"repository": {
"type": "git",
"url": "git://github.com/aguidrevitch/jquery-file-upload-middleware.git"
},
"main": "./index.js",
"readmeFilename": "README.md",
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"_id": "jquery-file-upload-middleware@0.1.7",
"license": "MIT"
"version": "0.1.7"
}