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