summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFulup Ar Foll <fulup@iot.bzh>2015-12-21 17:46:13 +0100
committerFulup Ar Foll <fulup@iot.bzh>2015-12-21 17:46:13 +0100
commit923ae026828a5cd325cb0dc658c78f50d029b1bf (patch)
tree7b89c9f0b2b69a2c36242d58df9015cd0fe3153f
parente76dff6729fb9acd1638019507e7edbbd8af23f5 (diff)
Export getFileUpload to helper-api.c
-rw-r--r--include/local-def.h3
-rw-r--r--nbproject/private/private.xml17
-rw-r--r--plugins/samples/SamplePost.c15
-rw-r--r--src/helper-api.c64
4 files changed, 48 insertions, 51 deletions
diff --git a/include/local-def.h b/include/local-def.h
index 8f297c53..68c42867 100644
--- a/include/local-def.h
+++ b/include/local-def.h
@@ -111,7 +111,8 @@ typedef enum {AFB_POST_NONE=0, AFB_POST_JSON, AFB_POST_FORM} AFB_PostType;
typedef struct {
int fd;
char *path;
- json_object* jerror;
+ int errcode;
+ json_object* jresp;
} AFB_PostCtx;
typedef struct {
diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml
index 6d359089..88244563 100644
--- a/nbproject/private/private.xml
+++ b/nbproject/private/private.xml
@@ -7,15 +7,14 @@
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
- <file>file:/home/mbc/git/afb-daemon-newapi2/plugins/audio/audio-api.c</file>
- <file>file:/home/mbc/git/afb-daemon-newapi2/plugins/radio/radio-api.c</file>
- <file>file:/home/mbc/git/afb-daemon-newapi2/src/main.c</file>
- <file>file:/home/mbc/git/afb-daemon-newapi2/plugins/radio/radio-api.h</file>
- <file>file:/home/mbc/git/afb-daemon-newapi2/plugins/audio/audio-alsa.c</file>
- <file>file:/home/mbc/git/afb-daemon-newapi2/plugins/radio/radio-rtlsdr.c</file>
- <file>file:/home/mbc/git/afb-daemon-newapi2/include/local-def.h</file>
- <file>file:/home/mbc/git/afb-daemon-newapi2/plugins/audio/audio-api.h</file>
- <file>file:/home/mbc/git/afb-daemon-newapi2/plugins/audio/audio-alsa.h</file>
+ <file>file:/home/fulup/Workspace/afb-daemon/plugins/samples/SamplePost.c</file>
+ <file>file:/home/fulup/Workspace/afb-daemon/src/session.c</file>
+ <file>file:/home/fulup/Workspace/afb-daemon/src/helper-api.c</file>
+ <file>file:/home/fulup/Workspace/afb-daemon/src/main.c</file>
+ <file>file:/home/fulup/Workspace/afb-daemon/include/local-def.h</file>
+ <file>file:/home/fulup/Workspace/afb-daemon/include/proto-def.h</file>
+ <file>file:/home/fulup/Workspace/afb-daemon/src/rest-api.c</file>
+ <file>file:/home/fulup/Workspace/afb-daemon/src/config.c</file>
</group>
</open-files>
</project-private>
diff --git a/plugins/samples/SamplePost.c b/plugins/samples/SamplePost.c
index 56c3f24b..53d065ae 100644
--- a/plugins/samples/SamplePost.c
+++ b/plugins/samples/SamplePost.c
@@ -48,12 +48,11 @@ STATIC json_object* UploadAppli (AFB_request *request, AFB_PostItem *item) {
// This is called after PostForm and then after DonePostForm
if (item == NULL) {
AFB_PostCtx *postFileCtx = getPostContext(request);
- if (postFileCtx != NULL) {
-
- // request Application Framework to install application
+ if (postFileCtx != NULL) {
+ // Do something intelligent here to install application
- request->errcode = MHD_HTTP_OK; // or error is something went wrong;
- request->jresp = jsonNewMessage(AFB_FAIL,"UploadFile Post Request file=[%s] done", postFileCtx->path);
+ postFileCtx->errcode = MHD_HTTP_OK; // or error is something went wrong;
+ postFileCtx->jresp = jsonNewMessage(AFB_SUCCESS,"UploadFile Post Appli done");
}
}
@@ -64,10 +63,8 @@ STATIC json_object* UploadAppli (AFB_request *request, AFB_PostItem *item) {
// Simples Upload case just upload a file
STATIC json_object* UploadMusic (AFB_request *request, AFB_PostItem *item) {
- char *destination = "musics";
-
// upload multi iteration logic is handle by getPostedFile
- return (getPostFile (request, item, destination));
+ return (getPostFile (request, item, "musics"));
}
// PostForm callback is called multiple times (one or each key within form, or once per file buffer)
@@ -85,7 +82,7 @@ STATIC json_object* UploadImage (AFB_request *request, AFB_PostItem *item) {
if (postFileCtx != NULL) {
// Do something with your newly upload filepath=postFileCtx->path
request->errcode = MHD_HTTP_OK;
- request->jresp = jsonNewMessage(AFB_FAIL,"UploadFile Post Request file=[%s] done", postFileCtx->path);
+ request->jresp = jsonNewMessage(AFB_FAIL,"UploadFile Post Image done");
// Note: should not return here in order getPostedFile to clear Post resources.
}
diff --git a/src/helper-api.c b/src/helper-api.c
index 670c9a1d..77b013e3 100644
--- a/src/helper-api.c
+++ b/src/helper-api.c
@@ -18,6 +18,7 @@
*/
#include "../include/local-def.h"
+#include <dirent.h>
// handle to hold queryAll values
@@ -101,101 +102,100 @@ PUBLIC AFB_PostCtx* getPostContext (AFB_request *request) {
PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* destination) {
AFB_PostHandle *postHandle = getPostHandle(request);
- AFB_PostCtx *appCtx;
+ AFB_PostCtx *postFileCtx;
char filepath[512];
int len;
// This is called after PostForm and then after DonePostForm
if (item == NULL) {
json_object* jresp;
- appCtx = (AFB_PostCtx*) postHandle->ctx;
+ postFileCtx = (AFB_PostCtx*) postHandle->ctx;
// No Post Application Context [something really bad happen]
- if (appCtx == NULL) {
+ if (postFileCtx == NULL) {
request->errcode = MHD_HTTP_EXPECTATION_FAILED;
return(jsonNewMessage(AFB_FAIL,"Error: PostForm no PostContext to free\n"));
}
- // We have a context but last Xform iteration fail.
- if (appCtx->jerror != NULL) {
- // request->errcode = appCtx->errcode;
- jresp = appCtx->jerror; // retrieve previous error from postCtx
- } else jresp = jsonNewMessage(AFB_FAIL,"UploadFile Post Request file=[%s] done", appCtx->path);
+ // We have a context but last Xform iteration fail or application set a message
+ if (postFileCtx->jresp != NULL) {
+ jresp = postFileCtx->jresp; // retrieve previous error from postCtx
+ if (postFileCtx->errcode != 0) request->errcode=postFileCtx->errcode;
+ }
+ else jresp = jsonNewMessage(AFB_FAIL,"getPostFile Post Request done");
// Error or not let's free all resources
- close(appCtx->fd);
- free (appCtx->path);
- free (appCtx);
+ close(postFileCtx->fd);
+ free (postFileCtx->path);
+ free (postFileCtx);
return (jresp);
}
// Make sure it's a valid PostForm request
if (!request->post && request->post->type != AFB_POST_FORM) {
- appCtx->jerror= jsonNewMessage(AFB_FAIL,"This is not a valid PostForm request\n");
+ postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"This is not a valid PostForm request\n");
goto ExitOnError;
}
// Check this is a file element
if (item->filename == NULL) {
- appCtx->jerror= jsonNewMessage(AFB_FAIL,"No Filename attached to key=%s\n", item->key);
+ postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"No Filename attached to key=%s\n", item->key);
goto ExitOnError;
}
// Check we got something in buffer
if (item->len <= 0) {
- appCtx->jerror= jsonNewMessage(AFB_FAIL,"Buffer size NULL key=%s]\n", item->key);
+ postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Buffer size NULL key=%s]\n", item->key);
goto ExitOnError;
}
// Extract Application Context from posthandle [NULL == 1st iteration]
- appCtx = (AFB_PostCtx*) postHandle->ctx;
+ postFileCtx = (AFB_PostCtx*) postHandle->ctx;
// This is the 1st Item iteration let's open output file and allocate necessary resources
- if (appCtx == NULL) {
- int destDir;
+ if (postFileCtx == NULL) {
+ DIR* destDir;
// Create an application specific context
- appCtx = calloc (1, sizeof(AFB_PostCtx)); // May place anything here until post->completeCB handle resources liberation
- appCtx->path = strdup (filepath);
+ postFileCtx = calloc (1, sizeof(AFB_PostCtx)); // May place anything here until post->completeCB handle resources liberation
+ postFileCtx->path = strdup (filepath);
// attach application to postHandle
- postHandle->ctx = (void*) appCtx; // May place anything here until post->completeCB handle resources liberation
+ postHandle->ctx = (void*) postFileCtx; // May place anything here until post->completeCB handle resources liberation
// Build destination directory full path
if (destination[0] != '/') {
strncpy (filepath, request->config->sessiondir, sizeof(filepath));
- strncat (filepath, destination, sizeof(filepath));
strncat (filepath, "/", sizeof(filepath));
strncat (filepath, destination, sizeof(filepath));
} else strncpy (filepath, destination, sizeof(filepath));
// make sure destination directory exist
- destDir = openat (filepath, request->plugin, O_DIRECTORY);
- if (destDir < 0) {
- destDir = mkdir(filepath,O_RDWR | S_IRWXU | S_IRGRP);
- if (destDir < 0) {
- appCtx->jerror= jsonNewMessage(AFB_FAIL,"Fail to Create destination directory=[%s] error=%s\n", filepath, strerror(errno));
+ destDir = opendir (filepath);
+ if (destDir == NULL) {
+ if ( 0 <= mkdir(filepath,O_RDWR | S_IRWXU | S_IRGRP)) {
+ postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to Create destination directory=[%s] error=%s\n", filepath, strerror(errno));
goto ExitOnError;
}
- } else close (destDir);
+ } else closedir (destDir);
strncat (filepath, "/", sizeof(filepath));
strncat (filepath, item->filename, sizeof(filepath));
- if((appCtx->fd = open(filepath, O_RDWR |O_CREAT, S_IRWXU|S_IRGRP)) < 0) {
- appCtx->jerror= jsonNewMessage(AFB_FAIL,"Fail to Create destination=[%s] error=%s\n", filepath, strerror(errno));
+ if((postFileCtx->fd = open(filepath, O_RDWR |O_CREAT, S_IRWXU|S_IRGRP)) <= 0) {
+ postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to Create destination File=[%s] error=%s\n", filepath, strerror(errno));
goto ExitOnError;
}
} else {
// reuse existing application context
- appCtx = (AFB_PostCtx*) postHandle->ctx;
+ postFileCtx = (AFB_PostCtx*) postHandle->ctx;
}
// Check we successfully wrote full buffer
- len = write (appCtx->fd, item->data, item->len);
+ len = write (postFileCtx->fd, item->data, item->len);
if (item->len != len) {
- appCtx->jerror= jsonNewMessage(AFB_FAIL,"Fail to write file [%s] at [%s] error=\n", item->filename, strerror(errno));
+ postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to write file [%s] at [%s] error=\n", item->filename, strerror(errno));
goto ExitOnError;
}