Skip to content

Commit

Permalink
Add support for multiple files with same name.
Browse files Browse the repository at this point in the history
  • Loading branch information
fpereiro committed Jun 5, 2017
1 parent a639c2d commit 55c6080
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
18 changes: 11 additions & 7 deletions cicek.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
çiçek - v3.1.0
çiçek - v3.1.1
Written by Federico Pereiro (fpereiro@gmail.com) and released into the public domain.
Expand Down Expand Up @@ -511,10 +511,12 @@ Please refer to readme.md to read the annotated source (but not yet!).

cicek.Apres = function (response) {
dale.do (response.request.data.files, function (v) {
fs.stat (v, function (error, stat) {
if (error && error.code !== 'ENOENT') return cicek.log (['error', 'temp file deletion error 1', v]);
if (! error) fs.unlink (v, function (error) {
if (error) cicek.log (['error', 'temp file deletion error 2', v]);
dale.do (v, function (v2) {
fs.stat (v2, function (error, stat) {
if (error && error.code !== 'ENOENT') return cicek.log (['error', 'temp file deletion error 1', v2]);
if (! error) fs.unlink (v2, function (error) {
if (error) cicek.log (['error', 'temp file deletion error 2', v2]);
});
});
});
});
Expand Down Expand Up @@ -606,9 +608,11 @@ Please refer to readme.md to read the annotated source (but not yet!).
busboy.on ('file', function (field, file, value, encoding, mimetype) {

files++;
request.data.files [field] = path.join ((os.tmpdir || os.tmpDir) (), cicek.pseudorandom (24) + '_' + value);
var filename = path.join ((os.tmpdir || os.tmpDir) (), cicek.pseudorandom (24) + '_' + value);
if (request.data.files [field]) request.data.files [field] = [request.data.files [field], filename];
else request.data.files [field] = filename;

var save = fs.createWriteStream (request.data.files [field]);
var save = fs.createWriteStream (filename);

save.on ('finish', function () {
if (--files === 0 && ! Error && Finish) route [2].apply (route [2], [request, response].concat (route.slice (3)));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cicek",
"version": "3.1.0",
"version": "3.1.1",
"description": "HTTP/HTTPS server.",
"dependencies": {
"busboy": "0.2.14",
Expand Down
8 changes: 7 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

## Current status of the project

The current version of çiçek, v3.1.0, is considered to be *unstable* and *somewhat complete*. [Suggestions](https://github.com/fpereiro/cicek/issues) and [patches](https://github.com/fpereiro/cicek/pulls) are welcome. Future changes planned are:
The current version of çiçek, v3.1.1, is considered to be *unstable* and *somewhat complete*. [Suggestions](https://github.com/fpereiro/cicek/issues) and [patches](https://github.com/fpereiro/cicek/pulls) are welcome. Future changes planned are:

- Fix bug when exceptions are thrown in cluster mode.
- Add an API reference.
Expand All @@ -45,6 +45,12 @@ The current version of çiçek, v3.1.0, is considered to be *unstable* and *some

To use çiçek, you need node.js v0.8.0 or newer.

## Source code

The complete source code is contained in `cicek.js`. It is about 790 lines long.

Annotated source code will be forthcoming when the library stabilizes.

## License

çiçek is written by Federico Pereiro (fpereiro@gmail.com) and released into the public domain.
17 changes: 15 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
çiçek - v3.1.0
çiçek - v3.1.1
Written by Federico Pereiro (fpereiro@gmail.com) and released into the public domain.
Expand Down Expand Up @@ -33,7 +33,9 @@ To run the test first run `node test` at the command prompt and then open the ur
request.data.body = request.body;

if (dale.keys (request.data.files).length > 0) {
fs.readFile (request.data.files [dale.keys (request.data.files) [0]], 'utf8', function (error, data) {
var onefile = request.data.files [dale.keys (request.data.files) [0]];
if (type (onefile) === 'array') onefile = onefile [0];
fs.readFile (onefile, 'utf8', function (error, data) {
if (error) return reply (response, 500, error);
request.data.fileContent = data;
reply (response, 200, JSON.stringify (request.data, null, ' '), {'content-encoding': request.data.query && request.data.query.compression ? false : undefined}, 'application/json');
Expand Down Expand Up @@ -120,6 +122,12 @@ To run the test first run `node test` at the command prompt and then open the ur
var blob2 = new Blob ([content], {type: 'application/json'});
formHack.append ('file', blob2, '/..');

var formMultiple = new FormData ();
var blob3 = new Blob ([content], {type: 'application/json'});
var blob4 = new Blob ([content], {type: 'application/json'});
formMultiple.append ('file', blob3);
formMultiple.append ('file', blob4);

var tests = [
// XXX Make this work
//['Check no crash on error', 'get', 'error', 0],
Expand Down Expand Up @@ -191,6 +199,11 @@ To run the test first run `node test` at the command prompt and then open the ur
['Upload file to root', 'post', 'upload', formHack, function (data) {
return data.files.file && data.fileContent === content;
}],
['Upload multiple files', 'post', 'upload', formMultiple, function (data) {
if (data.fileContent !== content) return false;
if (! data.files || type (data.files.file) !== 'array' || data.files.file.length !== 2) return false;
return true;
}],
]

var doTest = function () {
Expand Down

0 comments on commit 55c6080

Please sign in to comment.