summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAlejandro Sirgo Rica <asirgo@soleta.eu>2024-02-19 12:28:28 +0100
committerOpenGnSys Support Team <soporte-og@soleta.eu>2024-02-19 12:46:51 +0100
commit5ea25a11b206a3776f2f849c9a7f632b35f13223 (patch)
tree43c8169d6f53aa913b02fd4c37eb0339c1b46e63 /src
parentabfe81c1fa43e83d4072b19a497bb74047dfd8df (diff)
rest: delete images that belong to deleted centers
When og_cmd_post_center_delete is invoked by the REST API the images that contain the same id as the center being deleted are also deleted. Move the image deletion functionality into its own function called og_delete_image to prevent code duplication as it is now required in og_cmd_post_center_delete and og_cmd_delete_image.
Diffstat (limited to 'src')
-rw-r--r--src/rest.c109
1 files changed, 72 insertions, 37 deletions
diff --git a/src/rest.c b/src/rest.c
index 4709fc9..a426a0f 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -3354,51 +3354,23 @@ static int og_cmd_restore_image(json_t *element, struct og_msg_params *params)
clients);
}
-static int og_cmd_delete_image(json_t *element, struct og_msg_params *params)
+static int og_delete_image(struct og_dbi *dbi, const uint32_t image_id)
{
char filename[PATH_MAX + 1], checksum[PATH_MAX + 1];
- const char *key, *image;
- struct og_dbi *dbi;
+ const char *image;
dbi_result result;
- json_t *value;
- int err = 0;
-
- if (json_typeof(element) != JSON_OBJECT)
- return -1;
-
- json_object_foreach(element, key, value) {
- if (!strcmp(key, "image")) {
- err = og_json_parse_string(value, &params->id);
- params->flags |= OG_REST_PARAM_ID;
- }
-
- if (err < 0)
- return err;
- }
-
- if (!og_msg_params_validate(params, OG_REST_PARAM_ID))
- return -1;
-
- dbi = og_dbi_open(&ogconfig.db);
- if (!dbi) {
- syslog(LOG_ERR, "cannot open connection database (%s:%d)\n",
- __func__, __LINE__);
- return -1;
- }
result = dbi_conn_queryf(dbi->conn,
"SELECT nombreca FROM imagenes "
- "WHERE idimagen='%s'",
- params->id);
+ "WHERE idimagen='%u'",
+ image_id);
if (!result) {
- og_dbi_close(dbi);
syslog(LOG_ERR, "failed to query database\n");
return -1;
}
if (!dbi_result_next_row(result)) {
dbi_result_free(result);
- og_dbi_close(dbi);
- syslog(LOG_ERR, "image does not exist in database\n");
+ syslog(LOG_ERR, "image \"%u\" does not exist in database\n", image_id);
return -1;
}
@@ -3411,10 +3383,9 @@ static int og_cmd_delete_image(json_t *element, struct og_msg_params *params)
result = dbi_conn_queryf(dbi->conn,
"DELETE FROM imagenes "
- "WHERE idimagen='%s'",
- params->id);
+ "WHERE idimagen='%u'",
+ image_id);
if (!result) {
- og_dbi_close(dbi);
syslog(LOG_ERR, "failed to query database\n");
return -1;
}
@@ -3422,7 +3393,6 @@ static int og_cmd_delete_image(json_t *element, struct og_msg_params *params)
syslog(LOG_ERR, "delete did not modify any row (%s:%d)\n",
__func__, __LINE__);
dbi_result_free(result);
- og_dbi_close(dbi);
return -1;
}
dbi_result_free(result);
@@ -3430,6 +3400,45 @@ static int og_cmd_delete_image(json_t *element, struct og_msg_params *params)
unlink(filename);
unlink(checksum);
+ return 0;
+}
+
+static int og_cmd_delete_image(json_t *element, struct og_msg_params *params)
+{
+ struct og_dbi *dbi;
+ const char *key;
+ json_t *value;
+ int err = 0;
+
+ if (json_typeof(element) != JSON_OBJECT)
+ return -1;
+
+ json_object_foreach(element, key, value) {
+ if (!strcmp(key, "image")) {
+ err = og_json_parse_string(value, &params->id);
+ params->flags |= OG_REST_PARAM_ID;
+ }
+
+ if (err < 0)
+ return err;
+ }
+
+ if (!og_msg_params_validate(params, OG_REST_PARAM_ID))
+ return -1;
+
+ dbi = og_dbi_open(&ogconfig.db);
+ if (!dbi) {
+ syslog(LOG_ERR, "cannot open connection database (%s:%d)\n",
+ __func__, __LINE__);
+ return -1;
+ }
+
+ err = og_delete_image(dbi, atoi(params->id));
+ if (err < 0) {
+ og_dbi_close(dbi);
+ return err;
+ }
+
og_dbi_close(dbi);
return 0;
@@ -5553,6 +5562,7 @@ static int og_cmd_post_center_delete(json_t *element,
const char *key, *msglog;
struct og_dbi *dbi;
dbi_result result;
+ uint32_t image_id;
json_t *value;
int err = 0;
@@ -5597,7 +5607,32 @@ static int og_cmd_post_center_delete(json_t *element,
dbi_result_free(result);
+ result = dbi_conn_queryf(dbi->conn,
+ "SELECT idimagen FROM imagenes WHERE idcentro=%s",
+ params->id);
+
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ og_dbi_close(dbi);
+ return -1;
+ }
+
+ while (dbi_result_next_row(result)) {
+ image_id = dbi_result_get_uint(result, "idimagen");
+
+ err = og_delete_image(dbi, image_id);
+ if (err < 0) {
+ dbi_result_free(result);
+ og_dbi_close(dbi);
+ return err;
+ }
+ }
+
+ dbi_result_free(result);
og_dbi_close(dbi);
+
return 0;
}