Modified to delete image files when sticker data is deleted from DB 60/229360/2
authorInHong Han <inhong1.han@samsung.com>
Tue, 31 Mar 2020 09:30:03 +0000 (18:30 +0900)
committerInHong Han <inhong1.han@samsung.com>
Tue, 31 Mar 2020 11:23:08 +0000 (20:23 +0900)
Change-Id: I6c27cee243833a93ce280f97964e3e2c1e8a9ab6

client/sticker_dbus.c
server/stickerd_data_manager.c
server/stickerd_db_manager.c

index 633089c..318782b 100644 (file)
@@ -23,6 +23,8 @@
 #endif
 #define LOG_TAG "STICKER_DBUS"
 
+#define STICKER_DIRECTORY tzplatform_mkpath(TZ_SYS_SHARE, "sticker-data")
+
 static int is_server_started = 0;
 
 static void _server_appeared_cb(GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data)
@@ -682,29 +684,44 @@ int sticker_dbus_update_sticker_info(GDBusConnection *gdbus_connection, sticker_
         return ret;
     }
 
-    if (sticker_data->uri && strcmp(sticker_data->uri, origin_data->uri) != 0) {
-        LOGD("origin_uri : %s, new_uri : %s", origin_data->uri, sticker_data->uri);
-        int is_exist = 0;
-        ret = _send_sync_message(gdbus_connection, g_variant_new("(s)", sticker_data->uri), &reply, "check_file_exists");
-        if (ret == STICKER_CLIENT_ERROR_NONE) {
-            reply_body = g_dbus_message_get_body(reply);
-            g_variant_get(reply_body, "(i)", &is_exist);
-
-            if (is_exist) {
-                LOGE("file already exists");
-                ret = STICKER_CLIENT_ERROR_FILE_EXISTS;
+    if (sticker_data->uri) {
+        int len;
+        char *conv_path = NULL;
+        if (sticker_data->type == STICKER_DATA_URI_LOCAL_PATH) {
+            len = strlen(STICKER_DIRECTORY) + strlen(sticker_data->app_id) + strlen(sticker_data->uri) + 2;
+            conv_path = (char *)calloc(len, sizeof(char));
+            snprintf(conv_path, len, "%s/%s%s", STICKER_DIRECTORY, sticker_data->app_id, sticker_data->uri);
+        } else
+            conv_path = strdup(sticker_data->uri);
+
+        if (strcmp(conv_path, origin_data->uri) != 0) {
+            LOGD("origin_uri : %s, new_uri : %s", origin_data->uri, conv_path);
+            int is_exist = 0;
+            ret = _send_sync_message(gdbus_connection, g_variant_new("(s)", sticker_data->uri), &reply, "check_file_exists");
+            if (ret == STICKER_CLIENT_ERROR_NONE) {
+                reply_body = g_dbus_message_get_body(reply);
+                g_variant_get(reply_body, "(i)", &is_exist);
+
+                if (is_exist) {
+                    LOGE("file already exists");
+                    ret = STICKER_CLIENT_ERROR_FILE_EXISTS;
+                    free(conv_path);
+                    goto cleanup;
+                }
+            } else {
+                LOGE("failed to check file exists");
+                free(conv_path);
                 goto cleanup;
             }
-        } else {
-            LOGE("failed to check file exists");
-            goto cleanup;
-        }
 
-        ret = _send_sync_message(gdbus_connection, g_variant_new("(isis)", sticker_data->sticker_info_id, sticker_data->app_id, sticker_data->type, sticker_data->uri), &reply, "update_sticker_uri");
-        if (ret != STICKER_CLIENT_ERROR_NONE) {
-            LOGE("failed to update sticker uri");
-            goto cleanup;
+            ret = _send_sync_message(gdbus_connection, g_variant_new("(isis)", sticker_data->sticker_info_id, sticker_data->app_id, sticker_data->type, sticker_data->uri), &reply, "update_sticker_uri");
+            if (ret != STICKER_CLIENT_ERROR_NONE) {
+                LOGE("failed to update sticker uri");
+                free(conv_path);
+                goto cleanup;
+            }
         }
+        free(conv_path);
     }
 
     if (sticker_data->type != 0 && sticker_data->type != origin_data->type) {
@@ -716,13 +733,20 @@ int sticker_dbus_update_sticker_info(GDBusConnection *gdbus_connection, sticker_
         }
     }
 
-    if (sticker_data->thumbnail && strcmp(sticker_data->thumbnail, origin_data->thumbnail) != 0) {
-        LOGD("origin_thumbnail : %s, new_thumbnail : %s", origin_data->thumbnail, sticker_data->thumbnail);
-        ret = _send_sync_message(gdbus_connection, g_variant_new("(iss)", sticker_data->sticker_info_id, sticker_data->app_id, sticker_data->thumbnail), &reply, "update_sticker_thumbnail");
-        if (ret != STICKER_CLIENT_ERROR_NONE) {
-            LOGE("failed to update sticker thumbnail");
-            goto cleanup;
+    if (sticker_data->thumbnail) {
+        int len = strlen(STICKER_DIRECTORY) + strlen(sticker_data->app_id) + strlen(sticker_data->thumbnail) + 2;
+        char *conv_path = (char *)calloc(len, sizeof(char));
+        snprintf(conv_path, len, "%s/%s%s", STICKER_DIRECTORY, sticker_data->app_id, sticker_data->thumbnail);
+        if (strcmp(conv_path, origin_data->thumbnail) != 0) {
+            LOGD("origin_thumbnail : %s, new_thumbnail : %s", origin_data->thumbnail, conv_path);
+            ret = _send_sync_message(gdbus_connection, g_variant_new("(iss)", sticker_data->sticker_info_id, sticker_data->app_id, sticker_data->thumbnail), &reply, "update_sticker_thumbnail");
+            if (ret != STICKER_CLIENT_ERROR_NONE) {
+                LOGE("failed to update sticker thumbnail");
+                free(conv_path);
+                goto cleanup;
+            }
         }
+        free(conv_path);
     }
 
     if (sticker_data->description && strcmp(sticker_data->description, origin_data->description) != 0) {
index fc5492b..3585b8d 100644 (file)
@@ -497,7 +497,7 @@ static char* _convert_sticker_uri(const char *uri, const char *appid)
         snprintf(new_path, len, "%s/%s/%s",STICKER_DIRECTORY, appid, uri);
 
     if (access(new_path, F_OK) == 0) {
-        LOGE("sticker file already exists");
+        LOGE("sticker file already exists : %s", new_path);
         ret = -1;
         goto cleanup;
     }
index 52a177c..dbefbb7 100644 (file)
@@ -75,6 +75,8 @@
 #define STICKER_DB_GET_LATEST_RECORD_ID "SELECT sticker_info_id FROM sticker_info ORDER BY sticker_info_id DESC LIMIT 1"
 #define STICKER_DB_GET_STICKER_INFO_BY_RECORD_ID "SELECT * FROM sticker_info WHERE sticker_info_id = ?"
 #define STICKER_DB_GET_KEYWORD_INFO_BY_RECORD_ID "SELECT keyword FROM sticker_keyword_info WHERE sticker_info_id = ?"
+#define STICKER_DB_GET_IMAGE_INFO_BY_RECORED_ID "SELECT type, uri, thumbnail FROM sticker_info WHERE sticker_info_id = ?"
+#define STICKER_DB_GET_IMAGE_INFO_BY_URI "SELECT type, thumbnail FROM sticker_info WHERE uri = ?"
 #define STICKER_DB_GET_ALL_GROUP_LIST "SELECT DISTINCT group_name FROM sticker_info WHERE app_id NOT IN (SELECT DISTINCT provider_id FROM sticker_whitelist_info WHERE provider_id NOT IN (SELECT provider_id FROM sticker_whitelist_info WHERE consumer_id = ?))"
 #define STICKER_DB_GET_ALL_KEYWORD_LIST "SELECT DISTINCT keyword FROM sticker_keyword_info WHERE sticker_info_id IN (SELECT sticker_info_id from sticker_info WHERE app_id NOT IN (SELECT DISTINCT provider_id FROM sticker_whitelist_info WHERE provider_id NOT IN (SELECT provider_id FROM sticker_whitelist_info WHERE consumer_id = ?)))"
 #define STICKER_DB_GET_STICKER_COUNT "SELECT count(*) FROM sticker_info WHERE app_id = ?"
@@ -398,6 +400,33 @@ int stickerd_db_delete_sticker_info(int record_id)
     if (!db)
         return STICKERD_SERVER_ERROR_DB_FAILED;
 
+    ret = sqlite3_prepare_v2(db, STICKER_DB_GET_IMAGE_INFO_BY_RECORED_ID, -1, &stmt, NULL);
+    if (ret != SQLITE_OK) {
+        LOGE("fail to get image files : %s", sqlite3_errmsg(db));
+        goto cleanup;
+    }
+
+    sqlite3_bind_int(stmt, 1, record_id);
+
+    ret = sqlite3_step(stmt);
+    if (ret == SQLITE_ERROR) {
+        LOGE("sqlite3_step() failed : ret(%d)", ret);
+        goto cleanup;
+    }
+
+    int uri_type = sqlite3_column_int(stmt, 0);
+    const unsigned char *uri = sqlite3_column_text(stmt, 1);
+    const unsigned char *thumbnail = sqlite3_column_text(stmt, 2);
+
+    if (uri_type == 1 && unlink((const char *)uri) == -1)
+        LOGE("fail to delete sticker file");
+
+    if (thumbnail && unlink((const char *)thumbnail) == -1)
+        LOGE("fail to delete thumbnail image");
+
+    sqlite3_finalize(stmt);
+    stmt = NULL;
+
     ret = sqlite3_prepare_v2(db, STICKER_DB_DELETE_STICKER_INFO, -1, &stmt, NULL);
     if (ret != SQLITE_OK) {
         LOGE("fail to delete sticker information : %s", sqlite3_errmsg(db));
@@ -437,6 +466,32 @@ int stickerd_db_delete_sticker_info_by_uri(char *uri)
     if (!db)
         return STICKERD_SERVER_ERROR_DB_FAILED;
 
+    ret = sqlite3_prepare_v2(db, STICKER_DB_GET_IMAGE_INFO_BY_URI, -1, &stmt, NULL);
+    if (ret != SQLITE_OK) {
+        LOGE("fail to delete sticker information : %s", sqlite3_errmsg(db));
+        goto cleanup;
+    }
+
+    sqlite3_bind_text(stmt, 1, uri, -1, SQLITE_TRANSIENT);
+
+    ret = sqlite3_step(stmt);
+    if (ret == SQLITE_ERROR) {
+        LOGE("sqlite3_step() failed : ret(%d)", ret);
+        goto cleanup;
+    }
+
+    int uri_type = sqlite3_column_int(stmt, 0);
+    const unsigned char *thumbnail = sqlite3_column_text(stmt, 1);
+
+    if (uri_type == 1 && unlink((const char *)uri) == -1)
+        LOGE("fail to delete sticker file");
+
+    if (thumbnail && unlink((const char *)thumbnail) == -1)
+        LOGE("fail to delete thumbnail image");
+
+    sqlite3_finalize(stmt);
+    stmt = NULL;
+
     ret = sqlite3_prepare_v2(db, STICKER_DB_DELETE_STICKER_INFO_BY_URI, -1, &stmt, NULL);
     if (ret != SQLITE_OK) {
         LOGE("fail to delete sticker information : %s", sqlite3_errmsg(db));