summaryrefslogtreecommitdiffstats
path: root/plugins/media/media-api.c
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2016-05-13 10:33:29 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2016-05-13 10:33:29 +0200
commitaacefae70dd8c6b21398fa7a60574a24e1287eb0 (patch)
tree71f54bc373c5ede616d83261281074b17d2deadb /plugins/media/media-api.c
parent7b97a7f27578d7f4ce7cc994e5df303ac5d9c886 (diff)
media: uploading - first step
Change-Id: I469dac8883f2c8e94fa2384612723ff29e6eae41 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'plugins/media/media-api.c')
-rw-r--r--plugins/media/media-api.c149
1 files changed, 114 insertions, 35 deletions
diff --git a/plugins/media/media-api.c b/plugins/media/media-api.c
index 460894b1..7b4fa18f 100644
--- a/plugins/media/media-api.c
+++ b/plugins/media/media-api.c
@@ -16,6 +16,9 @@
*/
#define _GNU_SOURCE
+
+#include <string.h>
+
#include <json-c/json.h>
#include "media-api.h"
@@ -44,7 +47,7 @@ static mediaCtxHandleT* initMediaCtx () {
static void init (struct afb_req request) { /* AFB_SESSION_CHECK */
- mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request);
+ mediaCtxHandleT *ctx = afb_req_context_get(request);
json_object *jresp;
/* create a private client context */
@@ -64,9 +67,15 @@ static void init (struct afb_req request) { /* AFB_SESSION_CHECK */
static void list (struct afb_req request) { /* AFB_SESSION_CHECK */
- mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request);
+ mediaCtxHandleT *ctx = afb_req_context_get(request);
json_object *jresp;
+ /* check that context is initialized */
+ if (ctx == NULL) {
+ afb_req_fail (request, "failed", "uninitialized");
+ return;
+ }
+
jresp = _rygel_list (ctx);
if (!jresp) {
@@ -79,12 +88,18 @@ static void list (struct afb_req request) { /* AFB_SESSION_CHECK */
static void selecting (struct afb_req request) { /* AFB_SESSION_CHECK */
- mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request);
+ mediaCtxHandleT *ctx = afb_req_context_get(request);
const char *value = afb_req_value (request, "value");
json_object *jresp;
unsigned int index;
char index_str[5];
+ /* check that context is initialized */
+ if (ctx == NULL) {
+ afb_req_fail (request, "failed", "uninitialized");
+ return;
+ }
+
/* no "?value=" parameter : return current index */
if (!value) {
snprintf (index_str, sizeof(index_str), "%d", ctx->index);
@@ -119,9 +134,15 @@ static void selecting (struct afb_req request) { /* AFB_SESSION_CHECK */
static void play (struct afb_req request) { /* AFB_SESSION_CHECK */
- mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request);
+ mediaCtxHandleT *ctx = afb_req_context_get(request);
json_object *jresp;
+ /* check that context is initialized */
+ if (ctx == NULL) {
+ afb_req_fail (request, "failed", "uninitialized");
+ return;
+ }
+
if (!_rygel_do (ctx, PLAY, NULL)) {
afb_req_fail (request, "failed", "could not play chosen media");
return;
@@ -134,9 +155,15 @@ static void play (struct afb_req request) { /* AFB_SESSION_CHECK */
static void stop (struct afb_req request) { /* AFB_SESSION_CHECK */
- mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request);
+ mediaCtxHandleT *ctx = afb_req_context_get(request);
json_object *jresp;
+ /* check that context is initialized */
+ if (ctx == NULL) {
+ afb_req_fail (request, "failed", "uninitialized");
+ return;
+ }
+
if (!_rygel_do (ctx, STOP, NULL)) {
afb_req_fail (request, "failed", "could not stop chosen media");
return;
@@ -149,9 +176,15 @@ static void stop (struct afb_req request) { /* AFB_SESSION_CHECK */
static void pausing (struct afb_req request) { /* AFB_SESSION_CHECK */
- mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request);
+ mediaCtxHandleT *ctx = afb_req_context_get(request);
json_object *jresp;
+ /* check that context is initialized */
+ if (ctx == NULL) {
+ afb_req_fail (request, "failed", "uninitialized");
+ return;
+ }
+
if (!_rygel_do (ctx, PAUSE, NULL)) {
afb_req_fail (request, "failed", "could not pause chosen media");
return;
@@ -164,10 +197,16 @@ static void pausing (struct afb_req request) { /* AFB_SESSION_CHECK */
static void seek (struct afb_req request) { /* AFB_SESSION_CHECK */
- mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request);
+ mediaCtxHandleT *ctx = afb_req_context_get(request);
const char *value = afb_req_value (request, "value");
json_object *jresp;
+ /* check that context is initialized */
+ if (ctx == NULL) {
+ afb_req_fail (request, "failed", "uninitialized");
+ return;
+ }
+
/* no "?value=" parameter : return error */
if (!value) {
afb_req_fail (request, "failed", "you must provide a time");
@@ -184,38 +223,78 @@ static void seek (struct afb_req request) { /* AFB_SESSION_CHECK */
afb_req_success (request, jresp, "Media - Sought");
}
-#if 0
-static void upload (AFB_request *request, AFB_PostItem *item) { /* AFB_SESSION_CHECK */
+static char *renamed_filename(struct afb_arg argfile)
+{
+ char *result;
+ const char *e = strrchr(argfile.path, '/');
+ if (e == NULL)
+ result = strdup(argfile.value);
+ else {
+ result = malloc((++e - argfile.path) + strlen(argfile.value) + 1);
+ if (result != NULL)
+ strcpy(stpncpy(result, argfile.path, e - argfile.path), argfile.value);
+ }
+ return result;
+}
- mediaCtxHandleT *ctx = (mediaCtxHandleT*) afb_req_context_get(request);
- AFB_PostCtx *postFileCtx;
- json_object *jresp;
- char *path;
+static void on_uploaded(struct afb_req *prequest, int status)
+{
+ struct afb_req request = afb_req_unstore(prequest);
+ struct afb_arg argfile = afb_req_get(request, "file-upload");
+ char *file = renamed_filename(argfile);
+ if (file != NULL)
+ unlink(file);
+ free(file);
+ if (status)
+ afb_req_fail (request, "failed", "expected file not received");
+ else
+ afb_req_success_f (request, NULL, "uploaded file %s", argfile.value);
+}
- /* item is !NULL until transfer is complete */
- if (item != NULL)
- return getPostFile (request, item, "media");
+static void upload (struct afb_req request) { /* AFB_SESSION_CHECK */
- /* target intermediary file path */
- path = getPostPath (request);
+ mediaCtxHandleT *ctx = afb_req_context_get(request);
+ struct afb_req *prequest;
+ struct afb_arg argfile;
+ char *path;
- if (!path)
- fprintf (stderr, "Error encoutered during intermediary file transfer\n");
+ /* check that context is initialized */
+ if (ctx == NULL) {
+ afb_req_fail (request, "failed", "uninitialized");
+ return;
+ }
- else if (!_rygel_upload (ctx, path)) {
- request->errcode = MHD_HTTP_EXPECTATION_FAILED;
- request->jresp = jsonNewMessage (AFB_FAIL, "Error when uploading file to media server... could not complete");
+ /* get the file */
+ argfile = afb_req_get(request, "file-upload");
+ if (!argfile.value || !argfile.path) {
+ afb_req_fail (request, "failed", "expected file not received");
+ return;
}
- else {
- request->errcode = MHD_HTTP_OK;
- request->jresp = jsonNewMessage (AFB_SUCCESS, "upload=%s done", path);
+ /* rename the file */
+ path = renamed_filename(argfile);
+ if (path == NULL) {
+ afb_req_fail (request, "failed", "out of memory");
+ return;
+ }
+ if (rename(argfile.path, path) != 0) {
+ free(path);
+ afb_req_fail (request, "failed", "system error");
+ return;
}
- /* finalizes file transfer */
- return getPostFile (request, item, NULL);
+ /* for asynchronous processing */
+ prequest = afb_req_store(request);
+ if (path == NULL) {
+ unlink(path);
+ afb_req_fail (request, "failed", "out of memory");
+ }
+ else if (!_rygel_upload (ctx, path, (void*)on_uploaded, prequest)) {
+ unlink(path);
+ afb_req_fail (afb_req_unstore(prequest), "failed", "Error when uploading file to media server... could not complete");
+ }
+ free(path);
}
-#endif
static void ping (struct afb_req request) { /* AFB_SESSION_NONE */
afb_req_success (request, NULL, "Media - Ping succeeded");
@@ -230,19 +309,19 @@ static const struct AFB_restapi pluginApis[]= {
{"stop" , AFB_SESSION_CHECK, stop , "Media API - stop" },
{"pause" , AFB_SESSION_CHECK, pausing , "Media API - pause" },
{"seek" , AFB_SESSION_CHECK, seek , "Media API - seek" },
-// {"upload" , AFB_SESSION_CHECK, (AFB_apiCB)upload , "Media API - upload" },
+// {"upload" , AFB_SESSION_CHECK, upload , "Media API - upload" },
{"ping" , AFB_SESSION_NONE, ping , "Media API - ping" },
{NULL}
};
static const struct AFB_plugin pluginDesc = {
- .type = AFB_PLUGIN_JSON,
- .info = "Application Framework Binder - Media plugin",
- .prefix = "media",
- .apis = pluginApis
+ .type = AFB_PLUGIN_JSON,
+ .info = "Application Framework Binder - Media plugin",
+ .prefix = "media",
+ .apis = pluginApis
};
const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)
{
- return &pluginDesc;
+ return &pluginDesc;
}