Fix getPostFile() upload function, return bare JSON in list
authorManuel Bachmann <manuel.bachmann@iot.bzh>
Tue, 26 Jan 2016 14:32:37 +0000 (15:32 +0100)
committerManuel Bachmann <manuel.bachmann@iot.bzh>
Tue, 26 Jan 2016 14:32:37 +0000 (15:32 +0100)
getPostFile() does not fail anymore when creating a new
folder (return value was incorrect).

Media Plugin list API now directly returns JSON, with track
list array indexed as "list".

Signed-off-by: Manuel Bachmann <manuel.bachmann@iot.bzh>
plugins/media/media-api.c
plugins/media/media-api.h
plugins/media/media-rygel.c
src/helper-api.c

index 257b1cf..e91eb39 100644 (file)
@@ -64,15 +64,12 @@ STATIC json_object* list (AFB_request *request) {        /* AFB_SESSION_CHECK */
 
     mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
     json_object *jresp;
-    char *result;
 
-    result = _rygel_list (ctx);
+    jresp = _rygel_list (ctx);
 
-    if (!result)
+    if (!jresp)
       return jsonNewMessage(AFB_FAIL, "No content found in media server");
 
-    jresp = json_object_new_object();
-    json_object_object_add(jresp, "list", json_object_new_string (result));
     return jresp;
 }
 
@@ -155,9 +152,11 @@ STATIC json_object* seek (AFB_request *request) {        /* AFB_SESSION_CHECK */
     return jsonNewMessage(AFB_SUCCESS, "Seeked media");
 }
 
-STATIC json_object* upload (AFB_request *request) {      /* AFB_SESSION_CHECK */
+STATIC json_object* upload (AFB_request *request, AFB_PostItem *item) { /* AFB_SESSION_CHECK */
 
     mediaCtxHandleT *ctx = (mediaCtxHandleT*)request->context;
+    AFB_PostCtx *postFileCtx;
+#if 0
     const char *value = getQueryValue (request, "value");
     json_object *jresp;
     char path[256];
@@ -165,7 +164,17 @@ STATIC json_object* upload (AFB_request *request) {      /* AFB_SESSION_CHECK */
     /* no "?value=" parameter : return error */
     if (!value)
       return jsonNewMessage(AFB_FAIL, "You must provide a file name");
+#endif
+    if (item == NULL) {
+        postFileCtx = getPostContext (request);
+        if (postFileCtx) {
+            postFileCtx->errcode = MHD_HTTP_OK;
+            postFileCtx->jresp = jsonNewMessage (AFB_SUCCESS, "upload=%s done", getPostPath (request));
+        }
+    }
 
+    return getPostFile (request, item, "media");
+#if 0
     snprintf (path, sizeof(path), "/tmp/%s", value);
     if (access (path, R_OK) == -1)
       return jsonNewMessage(AFB_FAIL, "File not found");
@@ -174,6 +183,7 @@ STATIC json_object* upload (AFB_request *request) {      /* AFB_SESSION_CHECK */
       return jsonNewMessage(AFB_FAIL, "Error when uploading file... could not complete");
 
     return jsonNewMessage(AFB_SUCCESS, "File successfully uploaded");
+#endif
 }
 
 STATIC json_object* ping (AFB_request *request) {         /* AFB_SESSION_NONE */
index 70dcc1e..5a3cf9f 100644 (file)
@@ -29,6 +29,6 @@ typedef struct {
   unsigned int index;          /* currently selected media file       */
 } mediaCtxHandleT;
 
-PUBLIC char* _rygel_list (mediaCtxHandleT *);
+PUBLIC json_object* _rygel_list (mediaCtxHandleT *);
 
 #endif /* MEDIA_API_H */
index be8cb96..32969b2 100644 (file)
@@ -88,7 +88,7 @@ PUBLIC void _rygel_free (mediaCtxHandleT *ctx) {
     dev_ctx_c->content_res = NULL;
 }
 
-PUBLIC char* _rygel_list (mediaCtxHandleT *ctx) {
+PUBLIC json_object* _rygel_list (mediaCtxHandleT *ctx) {
 
     dev_ctx_T *dev_ctx_c = (dev_ctx_T*)ctx->media_server;
     json_object *json_o, *json_a;
@@ -136,7 +136,7 @@ PUBLIC char* _rygel_list (mediaCtxHandleT *ctx) {
 
     json_object_object_add (json_o, "list", json_a);
 
-    return (char*) json_object_to_json_string (json_o);
+    return json_o;
 }
 
 PUBLIC unsigned char _rygel_select (mediaCtxHandleT *ctx, unsigned int index) {
index ad0daf6..b54d94e 100644 (file)
@@ -177,7 +177,7 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char*
         // make sure destination directory exist
         destDir = opendir (filepath);
         if (destDir == NULL) {
-          if ( 0 <= mkdir(filepath,O_RDWR | S_IRWXU | S_IRGRP)) {
+          if (mkdir(filepath,O_RDWR | S_IRWXU | S_IRGRP) < 0) {
             postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to Create destination directory=[%s] error=%s\n", filepath, strerror(errno));
             goto ExitOnError;
           }