-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conflicts: views/listview.html
- Loading branch information
Showing
11 changed files
with
117 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ results | |
|
||
npm-debug.log | ||
node_modules | ||
public/upload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
|
||
var fs = require('fs'), | ||
path = require('path'); | ||
|
||
|
||
// prevent overriding of existing files | ||
function getName (target, cb) { | ||
var ext = path.extname(target), | ||
fname = path.basename(target, ext), | ||
dpath = path.dirname(target); | ||
fs.exists(target, function (exists) { | ||
return exists ? loop(1) : cb(target, fname+ext); | ||
}); | ||
function loop (i) { | ||
var name = fname+'-'+i, | ||
fpath = path.join(dpath, name+ext); | ||
fs.exists(fpath, function (exists) { | ||
return exists ? loop(++i) : cb(fpath, name+ext); | ||
}); | ||
} | ||
} | ||
|
||
function processFile (file, dpath, cb) { | ||
if (!file.name) return cb(null, file.name); | ||
// file.name; // file-name.jpg | ||
// file.path; // /tmp/9c9b10b72fe71be752bd3895f1185bc8 | ||
|
||
var source = file.path, | ||
target = path.join(dpath, file.name); | ||
|
||
fs.readFile(source, function (err, data) { | ||
if (err) return cb(err); | ||
getName(target, function (target, fname) { | ||
fs.writeFile(target, data, function (err) { | ||
if (err) return cb(err); | ||
cb(null, fname); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
function setName (req, file, fname) { | ||
var m = file.fieldName.match(/(.+)\[(.+)\]\[records\]\[(\d+)\]\[columns\]\[(.+)\]/); | ||
req.body[m[1]][m[2]].records[parseInt(m[3])].columns[m[4]] = fname; | ||
} | ||
|
||
function getFiles (req) { | ||
var files = []; | ||
for (var key in req.files) { | ||
var view = req.files[key]; | ||
for (var key in view) { | ||
var table = view[key]; | ||
if (!table.records) continue; | ||
for (var i=0; i < table.records.length; i++) { | ||
var record = table.records[i]; | ||
for (var key in record.columns) { | ||
files.push(record.columns[key]); | ||
} | ||
} | ||
} | ||
} | ||
return files; | ||
} | ||
|
||
exports.files = function (req, res, next) { | ||
var files = getFiles(req), | ||
dpath = res.locals._admin.config.app.upload; | ||
|
||
(function loop (index) { | ||
if (index == files.length) return next(); | ||
var file = files[index]; | ||
processFile(file, dpath, function (err, fname) { | ||
if (fname != '') setName(req, file, fname); | ||
loop(++index); | ||
}); | ||
}(0)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters