Last active
August 29, 2015 14:08
-
-
Save thiagozs/e4d886140ccb33c0f2b2 to your computer and use it in GitHub Desktop.
Upload Sails
This file contains hidden or 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
/** | |
* FileController | |
* | |
* @description :: Server-side logic for managing files | |
* @help :: See http://links.sailsjs.org/docs/controllers | |
*/ | |
var async = require('async'); | |
module.exports = { | |
upload: function (req, res) { | |
res.setTimeout(0); | |
req.file('avatar') | |
.upload({ maxBytes: 1000000 }, | |
function whenDone(err, uploadedFiles) { | |
if (err) return res.serverError(err); | |
else { | |
var tasks = []; | |
_.map(uploadedFiles, function fileOne(file){ | |
tasks.push(function(callback) { | |
uploadService.processImage(file, function(result){ | |
callback(null, result); | |
}); | |
}); | |
}); | |
async.parallel(tasks, function wellDone(err, result) { | |
if (err) return console.log(err); | |
return res.json({ files: result }); | |
}); | |
} | |
} | |
); | |
}, | |
s3upload: function (req, res) { | |
res.setTimeout(0); | |
req.file('avatar').upload({ | |
adapter: require('skipper-s3'), | |
bucket: process.env.BUCKET, | |
key: process.env.KEY, | |
secret: process.env.SECRET | |
}, function whenDone(err, uploadedFiles) { | |
if (err) return res.serverError(err); | |
else return res.json({ | |
files: uploadedFiles, | |
textParams: req.params.all() | |
}); | |
}); | |
}, | |
download: function (req, res) { | |
require('fs').createReadStream(req.param('path')) | |
.on('error', function (err) { | |
return res.serverError(err); | |
}) | |
.pipe(res); | |
} | |
}; | |
This file contains hidden or 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
<form enctype="multipart/form-data" id="my-form"> | |
<label>Please upload your avatar:</label> | |
<input type="file" name="avatar" id="avatar[]" /> | |
<input type="file" name="avatar" id="avatar[]" /> | |
<input type="file" name="avatar" id="avatar[]" /> | |
<input type="file" name="avatar" id="avatar[]" /> | |
<input type="hidden" name="_csrf" value="<%= _csrf %>"> | |
<br/> | |
<br/> | |
<input type="submit"/> | |
<div id="rr"></div> | |
</form> | |
<script type="text/javascript"> | |
$("form#my-form").submit(function(event){ | |
event.preventDefault(); | |
var formData = new FormData($(this)[0]); | |
$.ajax({ | |
url: '/file/upload', | |
type: 'POST', | |
data: formData, | |
async: false, | |
cache: false, | |
contentType: false, | |
processData: false, | |
success: function (rdata) { | |
$.each(rdata.files, function(i, r){ | |
var uri = 'http://localhost:1337' + '/' + r.path + '/' + r.name; | |
console.log(uri); | |
$("#rr").append("<div id='"+r.id+"' style='width:"+r.data.width+"px; height:"+r.data.height+"px;'><img id='"+r.id+"IMG'></img></div>"); | |
setTimeout(function(){ | |
$("#"+r.id+"IMG").attr("src", uri).one("load",function(){ | |
$("#"+r.id).attr("onclick", "javascript:window.open('"+uri+"');"); | |
console.log("img carregada!"); | |
}); | |
}, 2000); | |
}); | |
} | |
}); | |
return false; | |
}); | |
</script> |
This file contains hidden or 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
/**Uploads**/ | |
'post /file/upload': 'FileController.upload', | |
'post /file/s3upload': 'FileController.s3upload' | |
/***/ |
This file contains hidden or 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
var S = require('string'); | |
var fs = require('fs'); | |
var uuid = require('node-uuid'); | |
var mkdirp = require('mkdirp'); | |
var im = require('imagemagick'); | |
var query = require('querystring'); | |
var moment = require('moment'); | |
var UPLOAD_PATH_ASSETS = 'assets/images'; | |
function safeFilename(fileName) { | |
fileName = S(fileName).replaceAll(' ', '-').latinise().s; | |
fileName = S(fileName).replaceAll('_', '-').latinise().s; | |
fileName = S(fileName).replaceAll(',', '-').latinise().s; | |
fileName = S(fileName).replaceAll('$', '-').latinise().s; | |
fileName = S(fileName).replaceAll('%', '-').latinise().s; | |
fileName = S(fileName).replaceAll('@', '-').latinise().s; | |
return fileName; | |
} | |
function fileExtension(fileName) { | |
return S(fileName).latinise().s.split('.').slice(-1); | |
} | |
exports.processImage = function(file, callback) { | |
var fd = file.fd, | |
id = uuid.v1(), | |
fileName = id + "." + safeFilename(file.filename), | |
dirPathAssets = UPLOAD_PATH_ASSETS + '/' + moment(new Date()).format('YYYY-MM-DD'), | |
filePath = dirPathAssets + '/' + fileName; | |
try { | |
mkdirp.sync(dirPathAssets, 0755); | |
} catch (e) { | |
console.log(e); | |
} | |
return fs.readFile(fd, function dataDone(err, data) { | |
if (err) return {'error': '(fs) nao consegui LER o arquivo.'}; | |
return fs.writeFile(filePath, data, function whenError(err) { | |
if (err) return {'error': '(fs) nao consegui ESCREVER o arquivo'}; | |
return im.identify(filePath, function featureDone(err, features){ | |
if (err) return {'error': '(imagemagick) nao consegui IDENTIFICAR o arquivo'}; | |
else { | |
var dfeature = { | |
"geometry": features.geometry, | |
"resolution": features.resolution, | |
"type": features.type, | |
"colorspace": features.colorspace, | |
"depth": features.depth, | |
"compression": features.compression, | |
"width": features.width, | |
"height": features.height | |
}; | |
var result = { | |
'result': 'success', | |
'id': id, | |
'name': fileName, | |
'path': S(dirPathAssets).replaceAll('assets/','').s, | |
'size': file.size, | |
'type': file.type, | |
'data' : dfeature | |
}; | |
return callback(result); | |
} | |
}); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment