Added File Upload Method
This commit is contained in:
parent
aa20f92f88
commit
033d807071
11 changed files with 81 additions and 1 deletions
BIN
Images/3f7e345e-5e2a-46b6-8f5f-123c4fa15943
Normal file
BIN
Images/3f7e345e-5e2a-46b6-8f5f-123c4fa15943
Normal file
Binary file not shown.
After Width: | Height: | Size: 55 KiB |
BIN
Images/44660795-703c-40fb-ae3f-120134152d58.png
Normal file
BIN
Images/44660795-703c-40fb-ae3f-120134152d58.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.8 KiB |
BIN
Images/62e7de8a-914e-4f0d-af01-2e372f661dad
Normal file
BIN
Images/62e7de8a-914e-4f0d-af01-2e372f661dad
Normal file
Binary file not shown.
After Width: | Height: | Size: 55 KiB |
BIN
Images/a52f137d-0e13-48ac-8e51-55cb96fb2098
Normal file
BIN
Images/a52f137d-0e13-48ac-8e51-55cb96fb2098
Normal file
Binary file not shown.
After Width: | Height: | Size: 55 KiB |
BIN
Images/c3948c16-946b-4bfe-b407-9b2ad3e1f892png
Normal file
BIN
Images/c3948c16-946b-4bfe-b407-9b2ad3e1f892png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.8 KiB |
BIN
Images/d77263ed-64cb-4dd5-a248-3f9eb637827a
Normal file
BIN
Images/d77263ed-64cb-4dd5-a248-3f9eb637827a
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.8 KiB |
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
74
server/fileRouter.js
Normal 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;
|
|
@ -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
1
server/storageManager.js
Normal file
|
@ -0,0 +1 @@
|
|||
module.exports = myBucket;
|
Loading…
Reference in a new issue