Skip to content

Commit

Permalink
adding some tests and examples for uploading newfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed Aug 14, 2013
1 parent 3433bf6 commit ebd718e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
Binary file added examples/unhappy-cat.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions examples/uploadImage.sh
@@ -0,0 +1,8 @@
#!/bin/sh

# Shell script written by Aaron Stannard - designed to do a multi-part FORM post with an image and upload it to IFS

FILE="unhappy-cat.jpg"
TYPE="image/jpeg"

curl -F "filename=$FILE;$TYPE;" -F "name=$FILE" -F "image=@$FILE" http://localhost:1337
12 changes: 7 additions & 5 deletions lib/filesys/fileSysAgent.js
Expand Up @@ -93,13 +93,13 @@ FileSysAgent.prototype.writeFileResponse = function(absolutePath, file, fn){
var self = this;
self.logger.verbose('creating file {0}'.format(absolutePath));

fs.rename(file.upload.path, absolutePath, function(err){
fs.rename(file.path, absolutePath, function(err){
if(err){
var serverErr = {
code: 500,
message: err
};
self.logger.verbose('Exception! Unable to move uploaded file from {0} to {1}'.format(file.upload.path, absolutePath));
self.logger.verbose('Exception! Unable to move uploaded file from {0} to {1}'.format(file.path, absolutePath));
return fn(serverErr);
}

Expand Down Expand Up @@ -138,7 +138,9 @@ FileSysAgent.prototype.readDirectoryResponse = function(absolutePath, relativePa

response += "<ul>";
files.forEach(function(file){
response += '<li><a href="{0}">{0}</li>'.format(path.join(relativePath, file));
var fileName = path.join(relativePath, file);
var filePath = fileName.replace(/[\ ]/gi, '%20');
response += '<li><a href="{0}">{1}</li>'.format(filePath,fileName);
});
response += "</ul></body></html>";

Expand Down Expand Up @@ -258,8 +260,8 @@ FileSysAgent.prototype.write = function(relPath, req, fileData, fn){
if(exists == true){
self.isDirectory(absolutePath, function(isDir){
if(isDir){
self.logger.verbose('directory {0} found on disk - using uploaded filename ({1}) for absolute file path'.format(absolutePath, fileData.upload.name));
absolutePath = path.join(absolutePath, fileData.upload.name); //use the name of the upload if none specified in path
self.logger.verbose('directory {0} found on disk - using uploaded filename ({1}) for absolute file path'.format(absolutePath, fileData.name));
absolutePath = path.join(absolutePath, fileData.name); //use the name of the upload if none specified in path
}

return self.writeFileResponse(absolutePath, fileData, fn);
Expand Down
29 changes: 22 additions & 7 deletions lib/server/router.js
Expand Up @@ -3,7 +3,7 @@
* description: used to route HTTP commands to specific files
* author: Aaron Stannard
* created: 8/12/2013
* last-modified: 8/12/2013
* last-modified: 8/13/2013
*/

var url = require('url');
Expand All @@ -24,7 +24,7 @@ Router = function (options, fs){
* Path method - parses the relative file path from the request
*/
Router.prototype.parsePath = function(req){
return url.parse(req.url).pathname;
return decodeURI(url.parse(req.url).pathname);
}

/*
Expand Down Expand Up @@ -56,15 +56,26 @@ Router.prototype.parseFile = function(req, fn){
self.logger.verbose('parsing request for file content...');

form.parse(req, function(error, fields, files){
if(error) return fn(error);
if(error){
self.logger.verbose('failed to parse file upload. Error: {0}'.format(JSON.stringify(error)));
return fn(error);
}

var file = {};

for(var prop in files) {
if(files.hasOwnProperty(prop))
file = files[prop];
}

self.logger.verbose(
'successfully parsed upload for file {0}; saved to temporary location {1}'.format(
files.upload.name,
files.upload.path
file.name,
file.path
)
);

return fn(null, files);
return fn(null, file);
});
}

Expand Down Expand Up @@ -100,7 +111,11 @@ Router.prototype.listen = function(req, res){

if(req.method == "POST"){
return self.parseFile(req, function(err, file){
if(err) return self.output(err, null, res);
var serverErr = {
code: 500,
message: JSON.stringify(err)
};
if(err) return self.output(serverErr, null, res);
self.fs.write(path, req, file, function(err, data){
self.output(err, data, res);
});
Expand Down
8 changes: 0 additions & 8 deletions test/writeFile.js
Expand Up @@ -97,10 +97,8 @@ describe('filesys agent', function(){
var targetPath = path.join(directory, 'newFile.txt');
var finalPath = path.join(absoluteDirPath, 'newFile.txt');
var fileData = {
upload: {
name: fileName,
path: absoluteFilePath
}
};

fsAgent.write(targetPath, req, fileData, function(err, data){
Expand All @@ -118,10 +116,8 @@ describe('filesys agent', function(){
var targetPath = path.join('newDirectory', 'newFile.txt');
var finalPath = path.join(process.cwd(), 'newDirectory', 'newFile.txt');
var fileData = {
upload: {
name: fileName,
path: absoluteFilePath
}
};

fsAgent.write(targetPath, req, fileData, function(err, data){
Expand All @@ -141,10 +137,8 @@ describe('create file to path used in URL and filename in upload', function(){
var targetPath = directory;
var finalPath = path.join(absoluteDirPath, fileName);
var fileData = {
upload: {
name: fileName,
path: absoluteFilePath
}
};

fsAgent.write(targetPath, req, fileData, function(err, data){
Expand All @@ -162,10 +156,8 @@ describe('create file to path used in URL and filename in upload', function(){
var targetPath = 'newDirectory';
var finalPath = path.join(process.cwd(), 'newDirectory', fileName);
var fileData = {
upload: {
name: fileName,
path: absoluteFilePath
}
};

fsAgent.write(targetPath, req, fileData, function(err, data){
Expand Down

0 comments on commit ebd718e

Please sign in to comment.