Permalink
Browse files

Support pass in expressjs file object as upload file attachment

  • Loading branch information...
1 parent be93877 commit a66d33af49f0a8bb5eb7113c99c4805b1ae234f1 @BillWang BillWang committed Oct 8, 2012
Showing with 15 additions and 7 deletions.
  1. +15 −7 lib/acs.js
View
@@ -88,13 +88,23 @@ Cocoafish.prototype.sendRequest = function(url, method, data, callback, useSecur
var fileObj = utils.getFileObject(data);
if(fileObj) {
//send request with file
- if(typeof fileObj != 'string') {
- callback(utils.fileTypeError);
- return;
+ var fileName = '';
+ var filePath = '';
+ if(typeof fileObj == 'string') {
+ filePath = fileObj;
+ fileName = path.basename(fileObj);
+ } else if(typeof fileObj == 'object') {
+ if(fileObj.path && fileObj.name) {
+ filePath = fileObj.path;
+ fileName = fileObj.name;
+ } else {
+ callback(utils.fileTypeError);
+ return;
+ }
}
try {
- var binary = fs.readFileSync(fileObj);
+ var binary = fs.readFileSync(filePath);
if(binary) {
var filePropName = 'file';
if(data['file']) {
@@ -104,13 +114,11 @@ Cocoafish.prototype.sendRequest = function(url, method, data, callback, useSecur
filePropName = 'photo';
}
- var mimeType = mime.lookup(fileObj);
+ var mimeType = mime.lookup(fileName);
if(!mimeType) {
mimeType = 'text/plain';
}
- var fileName = path.basename(fileObj);
-
var header = {};
if(authType == utils.oauth) {
var message = {

0 comments on commit a66d33a

Please sign in to comment.