]> git.ipfire.org Git - pakfire.git/commitdiff
buildservice: Use the URL to upload the payload
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 29 Jan 2025 10:40:57 +0000 (10:40 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 29 Jan 2025 10:40:57 +0000 (10:40 +0000)
This is just a change in the protocol which hopefully will give us some
more flexibility in the future.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/buildservice.c

index 67e602a47fbb0211b0d3bd793241bbff9acbbcda..ed4a0b721a661dff2d9d693244496c61bbe05a3b 100644 (file)
@@ -224,15 +224,55 @@ ERROR:
        return r;
 }
 
+static int pakfire_buildservice_api_response_string(struct pakfire_buildservice* self,
+               struct json_object* response, const char* key, char** value) {
+       struct json_object* object = NULL;
+       const char* s = NULL;
+       char* buffer = NULL;
+       int r;
+
+       // Fetch the object at key
+       r = json_object_object_get_ex(response, key, &object);
+       if (r == 0) {
+               ERROR(self->ctx, "Could not fetch '%s' from response\n", key);
+               r = -EBADMSG;
+               goto ERROR;
+       }
+
+       // Read the string
+       s = json_object_get_string(object);
+       if (!s) {
+               ERROR(self->ctx, "Could not fetch value from '%s'\n", key);
+               r = -EBADMSG;
+               goto ERROR;
+       }
+
+       // Copy the value
+       buffer = strdup(s);
+       if (!buffer) {
+               ERROR(self->ctx, "Could not copy the string: %m\n");
+               r = -errno;
+               goto ERROR;
+       }
+
+       // Return the value
+       *value = buffer;
+       return 0;
+
+ERROR:
+       if (buffer)
+               free(buffer);
+
+       return r;
+}
+
 // Uploads
 
 static int pakfire_buildservice_create_upload(struct pakfire_buildservice* service,
-               const char* path, const char* filename, FILE* f, char** uuid) {
+               const char* path, const char* filename, FILE* f, char** url, char** uuid) {
        struct pakfire_xfer* xfer = NULL;
        struct pakfire_digests digests = {};
        struct json_object* response = NULL;
-       struct json_object* id = NULL;
-       const char* __id = NULL;
        char* hexdigest = NULL;
        struct stat stat;
        int r;
@@ -294,27 +334,18 @@ static int pakfire_buildservice_create_upload(struct pakfire_buildservice* servi
        if (r)
                goto ERROR;
 
-       // Fetch the ID
-       r = json_object_object_get_ex(response, "id", &id);
-       if (r == 0) {
-               ERROR(service->ctx, "Could not fetch ID from response\n");
-               r = -EBADMSG;
-               goto ERROR;
-       }
-
-       // Extract the UUID
-       __id = json_object_get_string(id);
-       if (!__id) {
-               ERROR(service->ctx, "Could not fetch ID from response\n");
-               r = -EBADMSG;
-               goto ERROR;
+       // Fetch the URL
+       if (url) {
+               r = pakfire_buildservice_api_response_string(service, response, "url", url);
+               if (r < 0)
+                       goto ERROR;
        }
 
-       // Return the UUID
-       *uuid = strdup(__id);
-       if (!*uuid) {
-               r = -errno;
-               goto ERROR;
+       // Fetch the ID
+       if (uuid) {
+               r = pakfire_buildservice_api_response_string(service, response, "id", uuid);
+               if (r < 0)
+                       goto ERROR;
        }
 
        // Success
@@ -332,12 +363,12 @@ ERROR:
 }
 
 static int pakfire_buildservice_upload_payload(struct pakfire_buildservice* service,
-               const char* filename, const char* uuid, FILE* f) {
+               const char* filename, const char* url, FILE* f) {
        struct pakfire_xfer* xfer = NULL;
        int r;
 
        // Create a new xfer
-       r = pakfire_buildservice_xfer_create(&xfer, service, "/api/v1/uploads/%s", uuid);
+       r = pakfire_buildservice_xfer_create(&xfer, service, "%s", url);
        if (r)
                goto ERROR;
 
@@ -371,6 +402,7 @@ ERROR:
 int pakfire_buildservice_upload(struct pakfire_buildservice* service,
                const char* path, const char* filename, char** uuid) {
        char basename[NAME_MAX];
+       char* url = NULL;
        FILE* f = NULL;
        int r;
 
@@ -391,14 +423,14 @@ int pakfire_buildservice_upload(struct pakfire_buildservice* service,
        }
 
        // Create a new upload
-       r = pakfire_buildservice_create_upload(service, path, filename, f, uuid);
+       r = pakfire_buildservice_create_upload(service, path, filename, f, &url, uuid);
        if (r)
                goto ERROR;
 
-       DEBUG(service->ctx, "Created a new download (%s)\n", *uuid);
+       DEBUG(service->ctx, "Created a new upload (%s)\n", *uuid);
 
        // Send the payload
-       r = pakfire_buildservice_upload_payload(service, filename, *uuid, f);
+       r = pakfire_buildservice_upload_payload(service, filename, url, f);
        if (r)
                goto ERROR;
 
@@ -409,6 +441,8 @@ ERROR:
 
                *uuid = NULL;
        }
+       if (url)
+               free(url);
        if (f)
                fclose(f);