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