7bc71f72498f896ddcb63450a7be688dfb357b34
[src/app-framework-demo.git] / afm-client / app / Backend / RestApis / PostMockApi.js
1 /* 
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  * 
18  * References: https://github.com/expressjs/multer
19  */
20
21 var fs = require('fs');
22 var multer = require('multer');
23  
24 function NewApi(handle, prefix) {
25     var scope=this; // make sure not to loose object context in async callback
26     
27     // defined upload directory and check it's a valid one
28     var upload = multer({ dest: handle.config.UPLOAD_DIR});
29     // WARNING: single('avatar') should match with <upload-image name="avatar">
30     handle.app.post(prefix +'/upload-image', upload.any(), function (req, res) {
31         
32         handle.trace (scope, 1, "%s/upload file=%s dest=%s/%s", prefix, req.files[0].originalname, req.files[0].destination, req.files[0].filename);
33         res.send({"jtype": "TEST_message", "status": "success", "info": "done"});
34     });
35     
36     // WARNING: single('music') should match with <upload-audio name="music">
37     handle.app.post(prefix +'/upload-music', upload.any(), function (req, res) {
38         
39         handle.trace (scope, 1, "%s/upload file=%s dest=%s/%s", prefix, req.files[0].originalname, req.files[0].destination, req.files[0].filename);
40         res.send({"jtype": "TEST_message", "status": "success", "info": "done"});
41     });
42     
43     // WARNING: single('appli') should match with <upload-audio name="appli">
44     handle.app.post(prefix +'/upload-appli', upload.any(), function (req, res) {
45         
46         handle.trace (scope, 1, "%s/upload file=%s dest=%s/%s", prefix, req.files[0].originalname, req.files[0].destination, req.files[0].filename);
47         res.send({"jtype": "TEST_message", "status": "success", "info": "done"});
48     });
49     
50 }
51
52 // Export Class
53 module.exports = NewApi;