9828349a4dbe4a6d70a2e00ade99c93c6f9ef942
[src/app-framework-binder.git] / plugins / samples / SamplePost.c
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
19
20 #include "local-def.h"
21
22 // With content-type=json data are directly avaliable in request->post->data
23 STATIC json_object* GetJsonByPost (AFB_request *request) {
24     json_object* jresp;
25     char query [256];
26     int  len;
27     
28     // check if we have some post data
29     if (request->post == NULL)  request->post->data="NoData"; 
30     
31     // Get all query string [Note real app should probably use value=getQueryValue(request,"key")]
32     len = getQueryAll (request, query, sizeof(query));
33     if (len == 0) strncpy (query, "NoSearchQueryList", sizeof(query));
34     
35     // for debug/test return response to caller
36     jresp = jsonNewMessage(AFB_SUCCESS, "GetJsonByPost query={%s} PostData: [%s]", query, request->post->data);
37     
38     return (jresp);    
39 }
40
41
42
43 // Upload a file and execute a function when upload is done
44 STATIC json_object* UploadAppli (AFB_request *request, AFB_PostItem *item) {
45     
46     char *destination = "applications";
47
48     // This is called after PostForm and then after DonePostForm
49     if (item == NULL) {
50         AFB_PostCtx *postFileCtx = getPostContext(request);      
51         if (postFileCtx != NULL) {
52             
53             // Do something intelligent here to install application
54             
55             postFileCtx->errcode = MHD_HTTP_OK;   // or error is something went wrong;   
56             postFileCtx->jresp   = jsonNewMessage(AFB_SUCCESS,"UploadFile Post Appli=%s done", getPostPath (request));
57         }
58     }
59     
60     // upload multi iteration logic is handle by getPostedFile
61     return (getPostFile (request, item, destination));
62 }
63
64 // Simples Upload case just upload a file
65 STATIC json_object* UploadMusic (AFB_request *request, AFB_PostItem *item) {
66     
67     // upload multi iteration logic is handle by getPostedFile
68     return (getPostFile (request, item, "musics"));
69 }
70
71 // PostForm callback is called multiple times (one or each key within form, or once per file buffer)
72 // When file has been fully uploaded call is call with item==NULL 
73 STATIC json_object* UploadImage (AFB_request *request, AFB_PostItem *item) {
74     
75     // note if directory is relative it will be prefixed by request->config->sessiondir
76     char *destination = "images";
77
78     // This is called after PostForm and then after DonePostForm
79     if (item == NULL) {
80         AFB_PostCtx *postFileCtx = getPostContext(request);
81         
82         // if postFileCtx == NULL then an error happen [getPostedFile automatically reports errors]
83         if (postFileCtx != NULL) {
84             // Do something with your newly upload filepath=postFileCtx->path
85             request->errcode = MHD_HTTP_OK;     
86             request->jresp   = jsonNewMessage(AFB_FAIL,"UploadFile Post Image done");    
87             
88             // Note: should not return here in order getPostedFile to clear Post resources.
89         }
90     }
91     
92     // upload multi iteration logic is handle by getPostedFile
93     return (getPostFile (request, item, destination));
94 }
95
96
97 // NOTE: this sample does not use session to keep test a basic as possible
98 //       in real application upload-xxx should be protected with AFB_SESSION_CHECK
99 STATIC  AFB_restapi pluginApis[]= {
100   {"ping"         , AFB_SESSION_NONE  , (AFB_apiCB)getPingTest    ,"Ping Rest Test Service"},
101   {"upload-json"  , AFB_SESSION_NONE  , (AFB_apiCB)GetJsonByPost  ,"Demo for Json Buffer on Post"},
102   {"upload-image" , AFB_SESSION_NONE  , (AFB_apiCB)UploadImage    ,"Demo for file upload"},
103   {"upload-music" , AFB_SESSION_NONE  , (AFB_apiCB)UploadMusic    ,"Demo for file upload"},
104   {"upload-appli" , AFB_SESSION_NONE  , (AFB_apiCB)UploadAppli    ,"Demo for file upload"},
105   {NULL}
106 };
107
108 PUBLIC AFB_plugin *pluginRegister () {
109     AFB_plugin *plugin = malloc (sizeof (AFB_plugin));
110     plugin->type  = AFB_PLUGIN_JSON; 
111     plugin->info  = "Sample with Post Upload Files";
112     plugin->prefix= "post";  // url base
113     plugin->apis  = pluginApis;
114     plugin->handle= (void*) "What ever you want";
115     
116     return (plugin);
117 };