Added File Upload Method

This commit is contained in:
Sagi Dayan 2016-07-08 23:06:28 +03:00
parent aa20f92f88
commit 033d807071
11 changed files with 81 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

View file

@ -14,6 +14,7 @@
"body-parser": "^1.15.2",
"express": "^4.14.0",
"gcloud": "^0.36.0",
"geolib": "^2.0.21"
"geolib": "^2.0.21",
"node-uuid": "^1.4.7"
}
}

View file

@ -4,6 +4,7 @@ var utils = require('./utils');
var db = require('./DBManager');
var noteRouter = require('./noteRouter');
var userRouter = require('./userRouter');
var fileRouter = require('./fileRouter');
router.get('/status', (req, res) => {
@ -17,6 +18,7 @@ router.get('/status', (req, res) => {
router.use('/note', noteRouter);
router.use('/user', userRouter);
router.use('/file', fileRouter);
router.post('/login', (req, res) => {
if (!req.body.username || !req.body.password) {

74
server/fileRouter.js Normal file
View file

@ -0,0 +1,74 @@
var express = require('express');
var router = express.Router();
var utils = require('./utils');
// var bucket = require('./storageManager');
var uuid = require('node-uuid');
var fs = require('fs');
var gcloud = require('gcloud');
var path = require('path');
var gcs = gcloud.storage({
projectId: 'thesocialnotework-api'
});
var bucket = gcs.bucket('avatars-bucket');
router.post('/upload', (req, res) => {
if (!req.body || !req.body.image) {
utils.response_400(res, {
image: 'data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAA..kJggg=='
}, "image data should ba an image Based64 format");
return;
}
var file_ending = req.body.image.split('/')[1].split(';')[0];
var image_name = uuid.v4() + '.' + file_ending;
var file_path = path.join(__dirname, '..', '/Images/', image_name);
// //Permissions to file
// file.acl.add({
// entity: 'allUsers',
// role: gcs.acl.READER_ROLE
// }, function (err, aclObject) {
var data = req.body.image.replace(/^data:image\/\w+;base64,/, '');
// var fd = fs.openSync(path.join(__dirname, '..', '/Images/', image_name), 'w');
fs.writeFile(file_path, data, {
encoding: 'base64'
}, function (err) {
//Finished
if (err)
res.send({
status: "Error",
image_url: null
});
else {
res.send({
status: "OK",
image_url: "http://thesocialnotework-api.appspot.com/file/image/" + image_name
});
}
// });
});
// fs.createReadStream('/Users/stephen/Photos/birthday-at-the-zoo/panda.jpg')
// .pipe(file.createWriteStream({
// metadata: {
// contentType: 'image/jpeg',
// metadata: {
// custom: 'metadata'
// }
// }
// }))
// .on('error', function(err) {})
// .on('finish', function() {
// // The file upload is complete.
// });
});
module.exports = router;

View file

@ -17,6 +17,8 @@ class Server {
// parse application/json
this.app.use(bodyParser.json());
this.app.use('/api', api);
this.app.use('/file/image/', express.static('Images'));
this.app.get('/', (req, res) => {
res.status(200)
.send('<h1>The Social Notework<h1><h4>Under Development</h4>');

1
server/storageManager.js Normal file
View file

@ -0,0 +1 @@
module.exports = myBucket;