Fix issue that fail to insert stickers when using json file 61/250761/4
authorInHong Han <inhong1.han@samsung.com>
Mon, 4 Jan 2021 09:29:23 +0000 (18:29 +0900)
committerInHong Han <inhong1.han@samsung.com>
Mon, 4 Jan 2021 10:22:18 +0000 (19:22 +0900)
Change-Id: I158c0b78195aba31b8e53c712a8f1ac211d192f2

sticker-parser/sticker-parser.c

index 1dc1a48..063aa87 100644 (file)
@@ -30,6 +30,7 @@
 #include <errno.h>
 #include <dirent.h>
 #include <fcntl.h>
+#include <linux/limits.h>
 
 #ifndef EXPORT_API
 #define EXPORT_API __attribute__((visibility("default")))
@@ -364,12 +365,21 @@ cleanup:
 static char* __convert_sticker_uri(char *uri, const char *appid, const char *app_path)
 {
     int ret;
+    char *copy_uri = (char *)calloc(PATH_MAX, sizeof(char));
+    if (copy_uri == NULL) {
+        LOGE("failed to allocate memory");
+        return NULL;
+    }
 
+    strncpy(copy_uri, uri, PATH_MAX - 1);
     __remove_app_path(uri, app_path);
     int len = strlen(STICKER_DIRECTORY) + strlen(appid) + strlen(uri) + 2;
     char *new_path = (char *)calloc(len, sizeof(char));
-    if (new_path == NULL)
-        return NULL;
+    if (new_path == NULL) {
+        LOGE("failed to allocate memory");
+        ret = -1;
+        goto cleanup;
+    }
 
     snprintf(new_path, len, "%s/%s%s",STICKER_DIRECTORY, appid, uri);
 
@@ -385,14 +395,14 @@ static char* __convert_sticker_uri(char *uri, const char *appid, const char *app
         goto cleanup;
     }
 
-    if (__file_copy(uri, new_path) == -1) {
+    if (__file_copy(copy_uri, new_path) == -1) {
         strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
         LOGE("failed to copy sticker file : %s", error_buffer);
         ret = -1;
         goto cleanup;
     }
 
-    ret = unlink(uri);
+    ret = unlink(copy_uri);
     if (ret != 0) {
         strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
         LOGE("failed to remove sticker file : %s", error_buffer);
@@ -402,11 +412,16 @@ static char* __convert_sticker_uri(char *uri, const char *appid, const char *app
         LOGE("failed to change ownership");
 
 cleanup:
+    free(copy_uri);
+    copy_uri = NULL;
+
     if (ret == 0) {
         return new_path;
     } else {
-        free(new_path);
-        new_path = NULL;
+        if (new_path) {
+            free(new_path);
+            new_path = NULL;
+        }
         return NULL;
     }
 }
@@ -605,10 +620,25 @@ static int __get_sticker_info_from_json(const char *appid, const char *file_path
                 goto free_memory;
 
             char *rel_thumbnail = __get_string_from_object(info_object, "thumbnail");
-            if (rel_thumbnail) {
-                if (rel_thumbnail[0] != '\0')
+            if (rel_thumbnail && rel_thumbnail[0] != '\0') {
+                if (access(rel_thumbnail, F_OK) == 0) {
                     thumbnail_path = __convert_sticker_uri(rel_thumbnail, appid, app_path);
-
+                } else {
+                    int len = strlen(app_path) + strlen(rel_thumbnail) + 2;
+                    char *new_thumbnail_path = (char *)calloc(len, sizeof(char));
+                    if (new_thumbnail_path) {
+                        if (rel_thumbnail[0] == '/')
+                            snprintf(new_thumbnail_path, len, "%s%s",app_path, rel_thumbnail);
+                        else
+                            snprintf(new_thumbnail_path, len, "%s%s%s",app_path, "/", rel_thumbnail);
+
+                        if (access(new_thumbnail_path, F_OK) == 0)
+                            thumbnail_path = __convert_sticker_uri(new_thumbnail_path, appid, app_path);
+
+                        free(new_thumbnail_path);
+                        new_thumbnail_path = NULL;
+                    }
+                }
                 free(rel_thumbnail);
             }