Skip to content
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

added renaming of files and deletion of existing file name #43

Open
wants to merge 1 commit 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
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ function uploadService(opts) {

fileUploader.config = options;

fileUploader.ReapplyOptions = function(__opts){
var __options = configs.apply(__opts);
fileUploader.config = __options;
};

function setNoCacheHeaders(res) {
res.setHeader('Pragma', 'no-cache');
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate');
Expand Down
1 change: 1 addition & 0 deletions lib/checkFolder.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';
var fs = require('fs');
var mkdirp = require('mkdirp');
/**
Expand Down
3 changes: 3 additions & 0 deletions lib/configs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*jslint node: true */
'use strict';
var checkExists = require('./checkFolder.js');

var options = {
Expand Down Expand Up @@ -37,6 +38,8 @@ var options = {
acl: 'public-read'
}
},
renameFile:'',
deleteExisting: true,
/**
* apply custom options to the default options
*/
Expand Down
20 changes: 19 additions & 1 deletion lib/fileinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,17 @@ function FileInfo(file, opts, fields) {
this.width = udf;
this.height = udf;
this.fields = fields;
if(this.options.renameFile.length > 0 ){
this.name = this.name.substr(0, this.name.lastIndexOf('.')) + this.options.renameFile + this.name.substr(this.name.lastIndexOf('.')+1);
}
if (opts.saveFile) {
this.safeName();
if(opts.deleteExisting){
this.deleteExist();
}
else
{
this.safeName();
}
}
}
FileInfo.prototype.update = function(file) {
Expand All @@ -49,6 +58,15 @@ FileInfo.prototype.safeName = function() {
}
};

FileInfo.prototype.deleteExist = function() {
var path = this.options.uploadDir + '/' + this.name;
fs.exists(path, function(exists) {
if (exists) {
fs.unlinkSync(path);
}
});
};

FileInfo.prototype.initUrls = function() {

var that = this;
Expand Down