Improve mime type verification 88/257788/8
authorMinje Ahn <minje.ahn@samsung.com>
Mon, 3 May 2021 01:14:14 +0000 (10:14 +0900)
committerMinje ahn <minje.ahn@samsung.com>
Tue, 4 May 2021 00:51:50 +0000 (00:51 +0000)
Change-Id: I6b45ec158e247ed1dc28e563e34f59fe54944199
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/common/media-svc-util.c
src/include/common/media-svc-util.h

index f0c10ada2e092fefdcbfdf26a665d637c8309bab..df875dafee476169103c35f15eedfa31dd1ab2e4 100644 (file)
 #define MUSIC_MIME_NUM 29
 #define SOUND_MIME_NUM 1
 #define MIME_TYPE_LENGTH 255
+#define THUMB_HASH_LEN 256
 #define MIME_LENGTH 50
 #define _3GP_FILE ".3gp"
 #define _MP4_FILE ".mp4"
 #define _ASF_FILE ".asf"
 #define MEDIA_SVC_ARTWORK_SIZE 2000
 #define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
+#define IMAGE_PREFIX "image/"
+#define IMAGE_PREFIX_LEN 6
 
 #define MEDIA_SVC_DEFAULT_GPS_VALUE                    -200                    /**< Default GPS Value*/
 
@@ -559,19 +562,15 @@ void _media_svc_remove_file(const char *path)
                media_svc_stderror("fail to remove file result");
 }
 
-int _media_svc_get_thumbnail_path(media_svc_media_type_e media_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
+int _media_svc_get_thumbnail_path(char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
        char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
-       char hash[255 + 1] = {0, };
+       char hash[THUMB_HASH_LEN] = {0, };
        char *thumb_dir = NULL;
-       char *thumbfile_ext = NULL;
 
        ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
-       if (!STRING_VALID(thumb_dir)) {
-               media_svc_error("ms_user_get_root_thumb_store_path failed");
-               return MS_MEDIA_ERR_INTERNAL;
-       }
+       media_svc_retvm_if(!STRING_VALID(thumb_dir), MS_MEDIA_ERR_INTERNAL, "ms_user_get_root_thumb_store_path failed");
 
        if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
                media_svc_error("Wrong path[%s]", thumb_dir);
@@ -583,29 +582,22 @@ int _media_svc_get_thumbnail_path(media_svc_media_type_e media_type, char *thumb
        if (!__media_svc_get_file_ext(pathname, file_ext))
                media_svc_error("get file ext fail");
 
-       ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
+       ret = mb_svc_generate_hash_code(pathname, hash, THUMB_HASH_LEN);
        if (ret != MS_MEDIA_ERR_NONE) {
                media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
                g_free(thumb_dir);
                return MS_MEDIA_ERR_INTERNAL;
        }
 
-       if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
-               if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
-                       thumbfile_ext = (char *)"jpg";
-               } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
-                       thumbfile_ext = (char *)"png";
-               } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
-                       thumbfile_ext = (char *)"gif";
-               } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
-                       thumbfile_ext = (char *)"bmp";
-               } else {
+       if (img_format) {
+               /* 'img_format' is mime-type */
+               if (!g_str_has_prefix(img_format, IMAGE_PREFIX) || strlen(img_format) == IMAGE_PREFIX_LEN) {
                        media_svc_error("Not proper img format");
                        g_free(thumb_dir);
                        return MS_MEDIA_ERR_INTERNAL;
                }
 
-               snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
+               snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, img_format + IMAGE_PREFIX_LEN);
        } else {
                if (strcasecmp(file_ext, "PNG") == 0)
                        snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
@@ -1102,7 +1094,7 @@ int _media_svc_extract_media_metadata(sqlite3 *handle, bool is_direct, media_svc
 
                                mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
                                if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
-                                       ret = _media_svc_get_thumbnail_path(content_info->media_type, thumb_path, content_info->path, p, uid);
+                                       ret = _media_svc_get_thumbnail_path(thumb_path, content_info->path, p, uid);
                                        if (ret != MS_MEDIA_ERR_NONE) {
                                                media_svc_error("Fail to Get Thumbnail Path");
                                        } else {
@@ -1269,7 +1261,7 @@ int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_me
        media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
 
        //1. make hash path
-       ret = _media_svc_get_thumbnail_path(media_type, thumb_path, path, NULL, uid);
+       ret = _media_svc_get_thumbnail_path(thumb_path, path, NULL, uid);
        if (ret != MS_MEDIA_ERR_NONE) {
                media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
                SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
index 9f5ffd275afb0eecc012157f8bba352332e98777..0cf68fd163a9ba15a497f9d38f82acd94c08b8ba 100755 (executable)
@@ -127,7 +127,7 @@ typedef enum {
 
 char * _media_info_generate_uuid(void);
 void _media_svc_remove_file(const char *path);
-int _media_svc_get_thumbnail_path(media_svc_media_type_e media_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid);
+int _media_svc_get_thumbnail_path(char *thumb_path, const char *pathname, const char *img_format, uid_t uid);
 int _media_svc_get_file_time(const char *full_path);
 char * _media_svc_get_title_by_path(const char *path);
 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, bool refresh);