// Uploads
static int pakfire_buildservice_create_upload(struct pakfire_buildservice* service,
- const char* path, const char* filename, FILE* f, char** url, char** uuid) {
+ const char* path, const char* filename, FILE* f, char** uuid) {
struct pakfire_hashes hashes = {};
struct pakfire_xfer* xfer = NULL;
struct json_object* response = NULL;
+ struct json_object* request = NULL;
char* hexdigest_blake2b512 = NULL;
struct stat stat;
int r;
r = fstat(fd, &stat);
if (r) {
ERROR(service->ctx, "Could not stat %s: %s\n", path, strerror(errno));
+ r = -errno;
goto ERROR;
}
// Create a new xfer
r = pakfire_buildservice_xfer_create(&xfer, service, "/api/v1/uploads");
- if (r)
+ if (r < 0)
goto ERROR;
// Enable authentication
if (r < 0)
goto ERROR;
+ // Make the request
+ r = pakfire_json_new_object(&request);
+ if (r < 0)
+ goto ERROR;
+
// Add the filename parameter
- r = pakfire_xfer_add_param(xfer, "filename", "%s", filename);
- if (r)
+ r = pakfire_json_add_string(request, "filename", filename);
+ if (r < 0)
goto ERROR;
// Add the size parameter
- r = pakfire_xfer_add_param(xfer, "size", "%jd", stat.st_size);
- if (r)
+ r = pakfire_json_add_int64(request, "size", stat.st_size);
+ if (r < 0)
goto ERROR;
// Add the hexdigest parameter
- r = pakfire_xfer_add_param(xfer, "hexdigest_blake2b512", "%s", hexdigest_blake2b512);
- if (r)
+ r = pakfire_json_add_string(request, "hexdigest_blake2b512", hexdigest_blake2b512);
+ if (r < 0)
+ goto ERROR;
+
+ // Add the payload
+ r = pakfire_xfer_set_json_payload(xfer, request);
+ if (r < 0)
goto ERROR;
// Send the request
r = pakfire_xfer_run_api_request(xfer, &response);
- if (r)
+ if (r < 0)
goto ERROR;
- // Fetch the URL
- if (url) {
- r = pakfire_buildservice_api_response_string(service, response, "url", url);
- if (r < 0)
- goto ERROR;
- }
-
// Fetch the ID
if (uuid) {
- r = pakfire_buildservice_api_response_string(service, response, "id", uuid);
+ r = pakfire_buildservice_api_response_string(service, response, "uuid", uuid);
if (r < 0)
goto ERROR;
}
pakfire_xfer_unref(xfer);
if (response)
json_object_put(response);
+ if (request)
+ json_object_put(request);
if (hexdigest_blake2b512)
free(hexdigest_blake2b512);
}
static int pakfire_buildservice_upload_payload(struct pakfire_buildservice* service,
- const char* filename, const char* url, FILE* f) {
+ const char* filename, const char* uuid, FILE* f) {
struct pakfire_xfer* xfer = NULL;
int r;
// Create a new xfer
- r = pakfire_buildservice_xfer_create(&xfer, service, "%s", url);
- if (r)
+ r = pakfire_buildservice_xfer_create(&xfer, service, "/api/v1/uploads/%s", uuid);
+ if (r < 0)
goto ERROR;
// Set the title
r = pakfire_xfer_set_title(xfer, filename);
- if (r)
+ if (r < 0)
goto ERROR;
// Enable authentication
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, &url, uuid);
- if (r)
+ r = pakfire_buildservice_create_upload(service, path, filename, f, uuid);
+ if (r < 0) {
+ ERROR(service->ctx, "Failed to create upload: %s\n", strerror(-r));
goto ERROR;
+ }
DEBUG(service->ctx, "Created a new upload (%s)\n", *uuid);
// Send the payload
- r = pakfire_buildservice_upload_payload(service, filename, url, f);
- if (r)
+ r = pakfire_buildservice_upload_payload(service, filename, *uuid, f);
+ if (r < 0) {
+ ERROR(service->ctx, "Failed to upload the payload for %s: %s\n", *uuid, strerror(-r));
goto ERROR;
+ }
ERROR:
if (r) {
*uuid = NULL;
}
- if (url)
- free(url);
if (f)
fclose(f);