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;
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
}
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;
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;
}
// 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;
*uuid = NULL;
}
+ if (url)
+ free(url);
if (f)
fclose(f);