Fix coverity issues
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-util.c
index a134c58..16fb4fd 100755 (executable)
 
 #include <unistd.h>
 #include <stdlib.h>
+#ifndef __USE_XOPEN
+#define DEF_XOPEN
+#define __USE_XOPEN /* needed for strptime */
+#endif
 #include <time.h>
+#ifdef DEF_XOPEN
+#undef __USE_XOPEN
+#endif
 #include <string.h>
+#include <system_info.h>
 #include <sys/vfs.h>
 #include <glib/gstdio.h>
 #include <sys/stat.h>
-#include <string.h>
 #include <dirent.h>
 #include <ctype.h>
 #include <aul/aul.h>
 #include <mm_file.h>
 #include <libexif/exif-data.h>
-#include <media-thumbnail.h>
 #include <media-util.h>
 #include <uuid/uuid.h>
-#include <img-codec-parser.h>
-#include <image_util.h>
-#include <grp.h>
-#include <pwd.h>
+#include <mm_util_magick.h>
 #include "media-util-err.h"
 #include "media-svc-util.h"
 #include "media-svc-db-utils.h"
@@ -46,9 +49,7 @@
 #include "media-svc-env.h"
 #include "media-svc-hash.h"
 #include "media-svc-album.h"
-#include "media-svc-localize-utils.h"
 #include "media-svc-localize_ch.h"
-#include "media-svc-localize_tw.h"
 
 #define MEDIA_SVC_FILE_EXT_LEN_MAX                             6                       /**< Maximum file ext lenth*/
 
@@ -59,6 +60,9 @@
 #define MEDIA_SVC_CATEGORY_VIDEO       0x00000004      /**< Video category */
 #define MEDIA_SVC_CATEGORY_MUSIC       0x00000008      /**< Music category */
 #define MEDIA_SVC_CATEGORY_SOUND       0x00000010      /**< Sound category */
+#define MEDIA_SVC_CATEGORY_PVR 0x00000020      /**< PVR category */
+#define MEDIA_SVC_CATEGORY_UHD 0x00000040      /**< UHD category */
+#define MEDIA_SVC_CATEGORY_SCSA        0x00000080      /**< SCSA category */
 
 #define CONTENT_TYPE_NUM 5
 #define MUSIC_MIME_NUM 29
 #define _3GP_FILE ".3gp"
 #define _MP4_FILE ".mp4"
 #define _ASF_FILE ".asf"
-#define MEDIA_SVC_INI_GET_INT(dict, key, value, default) \
-       do { \
-               value = iniparser_getint(dict, key, default); \
-               media_svc_debug("get %s = %d", key, value); \
-       } while (0)
-#define MEDIA_SVC_INI_DEFAULT_PATH "/usr/etc/media_content_config.ini"
 #define MEDIA_SVC_ARTWORK_SIZE 2000
+#define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
+
 
-static int g_ini_value = -1;
+static int media_svc_pinyin_support = -1;
 
 typedef struct {
        char content_type[15];
@@ -131,96 +131,21 @@ static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
        "x-smaf",
 };
 
-typedef enum {
-       MEDIA_SVC_EXTRACTED_FIELD_NONE                  = 0x00000001,
-       MEDIA_SVC_EXTRACTED_FIELD_TITLE                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 1,
-       MEDIA_SVC_EXTRACTED_FIELD_DESC                  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 2,
-       MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT             = MEDIA_SVC_EXTRACTED_FIELD_NONE << 3,
-       MEDIA_SVC_EXTRACTED_FIELD_AUTHOR                = MEDIA_SVC_EXTRACTED_FIELD_NONE << 4,
-       MEDIA_SVC_EXTRACTED_FIELD_ARTIST                        = MEDIA_SVC_EXTRACTED_FIELD_NONE << 5,
-       MEDIA_SVC_EXTRACTED_FIELD_GENRE                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 6,
-       MEDIA_SVC_EXTRACTED_FIELD_ALBUM                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 7,
-       MEDIA_SVC_EXTRACTED_FIELD_TRACKNUM              = MEDIA_SVC_EXTRACTED_FIELD_NONE << 8,
-       MEDIA_SVC_EXTRACTED_FIELD_YEAR                  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 9,
-       MEDIA_SVC_EXTRACTED_FIELD_CATEGORY              = MEDIA_SVC_EXTRACTED_FIELD_NONE << 10,
-       MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 11,
-} media_svc_extracted_field_e;
-
 char *_media_info_generate_uuid(void)
 {
        uuid_t uuid_value;
-       static char uuid_unparsed[50];
+       static char uuid_unparsed[37];
 
+RETRY_GEN:
        uuid_generate(uuid_value);
        uuid_unparse(uuid_value, uuid_unparsed);
 
-       /*media_svc_debug("UUID : %s", uuid_unparsed); */
-       return uuid_unparsed;
-}
-
-void _strncpy_safe(char *x_dst, const char *x_src, int max_len)
-{
-       if (!x_src || strlen(x_src) == 0) {
-               media_svc_error("x_src is NULL");
-               return;
-       }
-
-       if (max_len < 1) {
-               media_svc_error("length is Wrong");
-               return;
-       }
-
-       strncpy(x_dst, x_src, max_len - 1);
-       x_dst[max_len - 1] = '\0';
-}
-
-int __media_svc_malloc_and_strncpy(char **dst, const char *src)
-{
-       int len = 0;
-
-       if (!STRING_VALID(src)) {
-               media_svc_error("invalid src");
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
-       }
-
-       SAFE_FREE(*dst);
-
-       len = strlen(src) + 1;
-       *dst = malloc(len);
-
-       if (*dst == NULL) {
-               media_svc_error("malloc failed");
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-
-       strncpy(*dst, src, len);
-       char *p = *dst;
-       p[len - 1] = '\0';
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-int __media_svc_malloc_and_strncpy_with_size(char **dst, const char *src, int copysize)
-{
-       if (!STRING_VALID(src)) {
-               media_svc_error("invalid src");
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
-       }
-
-       SAFE_FREE(*dst);
-
-       *dst = malloc(copysize + 1);
-
-       if (*dst == NULL) {
-               media_svc_error("malloc failed");
-               return MS_MEDIA_ERR_INTERNAL;
+       if (strlen(uuid_unparsed) < 36) {
+               media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
+               goto RETRY_GEN;
        }
 
-       strncpy(*dst, src, copysize);
-       char *p = *dst;
-       p[copysize] = '\0';
-
-       return MS_MEDIA_ERR_NONE;
+       return uuid_unparsed;
 }
 
 static int __media_svc_split_to_double(char *input, double *arr)
@@ -254,7 +179,7 @@ static int __media_svc_split_to_double(char *input, double *arr)
        }
 
        if (arr_idx != 3) {
-               media_svc_error("Error when parsing GPS [%d]", arr_idx);
+               media_svc_debug("Error when parsing GPS [%d]", arr_idx);
                return MS_MEDIA_ERR_INTERNAL;
        }
 
@@ -266,9 +191,8 @@ static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, doub
        ExifEntry *entry;
        ExifTag tag;
 
-       if (ed == NULL) {
+       if (ed == NULL)
                return MS_MEDIA_ERR_INVALID_PARAMETER;
-       }
 
        tag = tagtype;
 
@@ -281,7 +205,7 @@ static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, doub
                        tag == EXIF_TAG_ISO_SPEED_RATINGS) {
 
                        if (i_value == NULL) {
-                               media_svc_error("i_value is NULL");
+                               media_svc_debug("i_value is NULL");
                                return MS_MEDIA_ERR_INVALID_PARAMETER;
                        }
 
@@ -292,7 +216,7 @@ static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, doub
                } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
 
                        if (d_value == NULL) {
-                               media_svc_error("d_value is NULL");
+                               media_svc_debug("d_value is NULL");
                                return MS_MEDIA_ERR_INVALID_PARAMETER;
                        }
 
@@ -311,7 +235,7 @@ static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, doub
                } else if (tag == EXIF_TAG_EXPOSURE_TIME) {
 
                        if (buf == NULL) {
-                               media_svc_error("buf is NULL");
+                               media_svc_debug("buf is NULL");
                                return MS_MEDIA_ERR_INVALID_PARAMETER;
                        }
 
@@ -324,7 +248,7 @@ static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, doub
                } else if (tag == EXIF_TAG_FNUMBER) {
 
                        if (d_value == NULL) {
-                               media_svc_error("d_value is NULL");
+                               media_svc_debug("d_value is NULL");
                                return MS_MEDIA_ERR_INVALID_PARAMETER;
                        }
 
@@ -338,7 +262,7 @@ static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, doub
                } else {
 
                        if (buf == NULL) {
-                               media_svc_error("buf is NULL");
+                               media_svc_debug("buf is NULL");
                                return MS_MEDIA_ERR_INVALID_PARAMETER;
                        }
 
@@ -372,16 +296,28 @@ time_t __media_svc_get_timeline_from_str(const char *timstr)
 
        if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
                t.tm_isdst = timeinfo.tm_isdst;
-               if (t.tm_isdst != 0) {
-                       media_svc_error("DST %d", t.tm_isdst);
+               if (t.tm_isdst != 0)
+                       media_svc_debug("DST %d", t.tm_isdst);
+
+               /* If time string has timezone */
+               if (strptime(timstr, "%Y:%m:%d %H:%M:%S %z", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S %z", &t)) {
+                       GTimeVal timeval;
+                       char tim_tmp_str[255] = { 0, };
+
+                       /* ISO8601 Time string format */
+                       strftime(tim_tmp_str, 255, "%Y-%m-%dT%H:%M:%S%z", &t);
+                       g_time_val_from_iso8601(tim_tmp_str, &timeval);
+                       modified_t = timeval.tv_sec;
+                       media_svc_debug("Calibrated timeval : [%d][%s]", modified_t, tim_tmp_str);
+               } else {
+                       /* Just localtime */
+                       modified_t = mktime(&t);
                }
 
-               modified_t = mktime(&t);
-               if (modified_t > 0) {
+               if (modified_t > 0)
                        return modified_t;
-               } else {
-                       media_svc_error("Failed to get timeline : [%s] [%d:%d:%d: %d:%d:%d]", timstr, t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
-               }
+               else
+                       media_svc_debug("Failed to get timeline : [%s] [%d:%d:%d: %d:%d:%d]", timstr, t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
        } else {
                media_svc_error("Failed to get timeline : [%s]", timstr);
        }
@@ -403,9 +339,10 @@ static int __media_svc_get_content_type_from_mime(const char *path, const char *
                }
        }
 
-       /*in application type, exitst sound file ex) x-smafs */
+       /*in application type, exitst sound file ex) x-smafs, asf */
        if (*category & MEDIA_SVC_CATEGORY_ETC) {
                int prefix_len = strlen(content_category[0].content_type);
+               char *ext = NULL;
 
                for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
                        if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
@@ -419,9 +356,30 @@ static int __media_svc_get_content_type_from_mime(const char *path, const char *
                        *category ^= MEDIA_SVC_CATEGORY_ETC;
                        *category |= MEDIA_SVC_CATEGORY_SOUND;
                }
+
+               /*"asf" must check video stream and then categorize in directly. */
+               ext = strrchr(path, '.');
+               if (ext != NULL) {
+                       if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
+                               int audio = 0;
+                               int video = 0;
+                               int err = 0;
+
+                               err = mm_file_get_stream_info(path, &audio, &video);
+                               if (err == 0) {
+                                       if (audio > 0 && video == 0) {
+                                               *category ^= MEDIA_SVC_CATEGORY_ETC;
+                                               *category |= MEDIA_SVC_CATEGORY_MUSIC;
+                                       } else {
+                                               *category ^= MEDIA_SVC_CATEGORY_ETC;
+                                               *category |= MEDIA_SVC_CATEGORY_VIDEO;
+                                       }
+                               }
+                       }
+               }
        }
 
-       /*check music file in soun files. */
+       /*check music file in sound files. */
        if (*category & MEDIA_SVC_CATEGORY_SOUND) {
                int prefix_len = strlen(content_category[0].content_type) + 1;
 
@@ -444,7 +402,7 @@ static int __media_svc_get_content_type_from_mime(const char *path, const char *
                /*"3gp" and "mp4" must check video stream and then categorize in directly. */
                ext = strrchr(path, '.');
                if (ext != NULL) {
-                       if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0) || (strncasecmp(ext, _ASF_FILE, 5) == 0)) {
+                       if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
                                int audio = 0;
                                int video = 0;
                                int err = 0;
@@ -472,9 +430,8 @@ static int __media_svc_get_media_type(const char *path, const char *mime_type, m
        media_svc_media_type_e type;
 
        ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
-       if (ret != MS_MEDIA_ERR_NONE) {
+       if (ret != MS_MEDIA_ERR_NONE)
                media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
-       }
 
        if (category & MEDIA_SVC_CATEGORY_SOUND)                type = MEDIA_SVC_MEDIA_TYPE_SOUND;
        else if (category & MEDIA_SVC_CATEGORY_MUSIC)   type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
@@ -511,13 +468,12 @@ static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
 
        for (i = strlen(file_path); i >= 0; i--) {
                if (file_path[i] == '.') {
-                       _strncpy_safe(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
+                       SAFE_STRLCPY(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
                        return true;
                }
 
-               if (file_path[i] == '/') {
+               if (file_path[i] == '/')
                        return false;
-               }
        }
        return false;
 }
@@ -530,27 +486,24 @@ static int __media_svc_get_location_value(MMHandleType tag, double *longitude, d
 
        mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
        if (mmf_error == FILEINFO_ERROR_NONE) {
-               if (longitude != NULL) {
+               if (longitude != NULL)
                        *longitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
-               }
        } else {
                SAFE_FREE(err_attr_name);
        }
 
        mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
        if (mmf_error == FILEINFO_ERROR_NONE) {
-               if (latitude != NULL) {
+               if (latitude != NULL)
                        *latitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
-               }
        } else {
                SAFE_FREE(err_attr_name);
        }
 
        mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
        if (mmf_error == FILEINFO_ERROR_NONE) {
-               if (altitude != NULL) {
+               if (altitude != NULL)
                        *altitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
-               }
        } else {
                SAFE_FREE(err_attr_name);
        }
@@ -558,71 +511,25 @@ static int __media_svc_get_location_value(MMHandleType tag, double *longitude, d
        return MS_MEDIA_ERR_NONE;
 }
 
-static char *__media_svc_get_thumb_path(uid_t uid)
-{
-       char *result_passwd = NULL;
-       struct group *grpinfo = NULL;
-       if (uid == getuid()) {
-               grpinfo = getgrnam("users");
-               if (grpinfo == NULL) {
-                       media_svc_error("getgrnam(users) returns NULL !");
-                       return NULL;
-               }
-               result_passwd = g_strdup(MEDIA_SVC_THUMB_PATH_PREFIX);
-       } else {
-               char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
-               struct passwd *userinfo = getpwuid(uid);
-               if (userinfo == NULL) {
-                       media_svc_error("getpwuid(%d) returns NULL !", uid);
-                       return NULL;
-               }
-               grpinfo = getgrnam("users");
-               if (grpinfo == NULL) {
-                       media_svc_error("getgrnam(users) returns NULL !");
-                       return NULL;
-               }
-               /* Compare git_t type and not group name */
-               if (grpinfo->gr_gid != userinfo->pw_gid) {
-                       media_svc_error("UID [%d] does not belong to 'users' group!", uid);
-                       return NULL;
-               }
-               snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb", userinfo->pw_dir);
-               result_passwd = g_strdup(passwd_str);
-       }
-
-       return result_passwd;
-}
-
-static int __media_svc_resize_artwork(void *image, int size, const char *img_format, void **resize_image, int *resize_size)
+static int __media_svc_resize_artwork(const char *path, const char *img_format)
 {
        int ret = MS_MEDIA_ERR_NONE;
-       void *raw_image = NULL;
-       int width = 0;
-       int height = 0;
-       unsigned int raw_size = 0;
-       void *resized_raw_image = NULL;
-       int resized_width = 0;
-       int resized_height = 0;
-       unsigned int buf_size = 0;
+       unsigned int width = 0;
+       unsigned int height = 0;
+       unsigned int resized_width = 0;
+       unsigned int resized_height = 0;
+       mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
 
        if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
-               media_svc_debug("type [jpeg] size [%d]", size);
-               /* decoding */
-               ret = image_util_decode_jpeg_from_memory(image, size, IMAGE_UTIL_COLORSPACE_RGB888, &raw_image, &width, &height, &raw_size);
-               if (ret != MS_MEDIA_ERR_NONE) {
-                       media_svc_error("image_util_decode_jpeg_from_memory failed");
-                       *resize_image = image;
-                       *resize_size = size;
-                       return MS_MEDIA_ERR_NONE;
-               }
+               media_svc_debug("type [jpeg]");
+
+               mm_util_extract_image_info(path, &img_type, &width, &height);
 
                if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
                        media_svc_debug("No need resizing");
-                       *resize_image = image;
-                       *resize_size = size;
-                       SAFE_FREE(raw_image);
                        return MS_MEDIA_ERR_NONE;
                }
+
                /* resizing */
                if (width > height) {
                        resized_height = MEDIA_SVC_ARTWORK_SIZE;
@@ -632,48 +539,40 @@ static int __media_svc_resize_artwork(void *image, int size, const char *img_for
                        resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
                }
 
-               image_util_calculate_buffer_size(resized_width, resized_height, IMAGE_UTIL_COLORSPACE_RGB888 , &buf_size);
-
-               resized_raw_image = malloc(buf_size);
-               memset(resized_raw_image, 0, buf_size);
-
-               ret = image_util_resize(resized_raw_image, &resized_width, &resized_height, raw_image, width, height, IMAGE_UTIL_COLORSPACE_RGB888);
-               if (ret != MS_MEDIA_ERR_NONE) {
-                       media_svc_error("image_util_resize failed");
-                       *resize_image = image;
-                       *resize_size = size;
-                       SAFE_FREE(raw_image);
-                       return MS_MEDIA_ERR_NONE;
-               }
-               SAFE_FREE(raw_image);
-
-               /* encoding */
-               ret = image_util_encode_jpeg_to_memory(resized_raw_image, resized_width, resized_height, IMAGE_UTIL_COLORSPACE_RGB888, 90, resize_image, resize_size);
-               if (ret != MS_MEDIA_ERR_NONE) {
-                       media_svc_error("image_util_encode_jpeg_to_memory failed");
-                       *resize_image = image;
-                       *resize_size = size;
-                       return MS_MEDIA_ERR_NONE;
-                       SAFE_FREE(resized_raw_image);
-               }
-               SAFE_FREE(resized_raw_image);
+               ret = mm_util_resize_P_P(path, resized_width, resized_height, path);
 
        } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
-               media_svc_debug("type [png] size [%d]", size);
-               *resize_image = image;
-               *resize_size = size;
-
+               media_svc_debug("type [png]");
        } else {
                media_svc_debug("Not proper img format");
-               *resize_image = image;
-               *resize_size = size;
        }
 
        return ret;
 }
 
-static int _media_svc_save_image(void *image, int size, char *image_path, uid_t uid)
+static int __media_svc_safe_atoi(char *buffer, int *si)
+{
+       char *end = NULL;
+       errno = 0;
+       media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
+
+       const long sl = strtol(buffer, &end, 10);
+
+       media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
+       media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
+       media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
+       media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
+       media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
+
+       *si = (int)sl;
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
 {
+       int ret = MS_MEDIA_ERR_NONE;
+
        media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
 
        if (!image) {
@@ -682,11 +581,9 @@ static int _media_svc_save_image(void *image, int size, char *image_path, uid_t
        }
 
        struct statfs fs;
-       char *thumb_path = __media_svc_get_thumb_path(uid);
-       if (!STRING_VALID(thumb_path)) {
-               media_svc_error("fail to get thumb_path");
-               return MS_MEDIA_ERR_INTERNAL;
-       }
+       char *thumb_path = NULL;
+       ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
+       media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path fail");
 
        if (-1 == statfs(thumb_path, &fs)) {
                media_svc_error("error in statfs");
@@ -700,7 +597,7 @@ static int _media_svc_save_image(void *image, int size, char *image_path, uid_t
 
        if ((bsize_kbytes * fs.f_bavail) < 1024) {
                media_svc_error("not enought space...");
-               return MS_MEDIA_ERR_INTERNAL;
+               return MS_MEDIA_ERR_NOT_ENOUGH_SPACE;
        }
 
        FILE *fp = NULL;
@@ -725,7 +622,7 @@ static int _media_svc_save_image(void *image, int size, char *image_path, uid_t
        return MS_MEDIA_ERR_NONE;
 }
 
-static char *_media_svc_get_title_from_filepath(const char *path)
+static char *__media_svc_get_title_from_filepath(const char *path)
 {
        char *filename = NULL;
        char *title = NULL;
@@ -769,6 +666,23 @@ static char *_media_svc_get_title_from_filepath(const char *path)
        return title;
 }
 
+bool __media_svc_check_support_pinyin()
+{
+       int ret = SYSTEM_INFO_ERROR_NONE;
+       bool is_supported = false;
+
+       if (media_svc_pinyin_support == -1) {
+               ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
+               if (ret != SYSTEM_INFO_ERROR_NONE) {
+                       media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
+                       return false;
+               }
+
+               media_svc_pinyin_support = is_supported;
+       }
+
+       return media_svc_pinyin_support;
+}
 int _media_svc_rename_file(const char *old_name, const char *new_name)
 {
        if ((old_name == NULL) || (new_name == NULL)) {
@@ -800,215 +714,87 @@ int _media_svc_remove_file(const char *path)
 
 int _media_svc_remove_all_files_in_dir(const char *dir_path)
 {
-       struct dirent entry;
-       struct dirent *result;
-       struct stat st;
        char filename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
-       DIR *dir = NULL;
-
-       dir = opendir(dir_path);
-       if (dir == NULL) {
-               media_svc_error("%s is not exist", dir_path);
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
-       }
-
-       while (!readdir_r(dir, &entry, &result)) {
-               if (result == NULL)
-                       break;
-
-               if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0) {
-                       continue;
-               }
-               snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry.d_name);
-
-               if (stat(filename, &st) != 0) {
-                       continue;
-               }
-               if (S_ISDIR(st.st_mode)) {
-                       continue;
-               }
-               if (unlink(filename) != 0) {
-                       media_svc_stderror("failed to remove");
-                       closedir(dir);
-                       return MS_MEDIA_ERR_INTERNAL;
-               }
-       }
-
-       closedir(dir);
-       return MS_MEDIA_ERR_NONE;
-}
-
-char *_media_svc_get_thumb_internal_path(uid_t uid)
-{
-       char *result_passwd = NULL;
-       struct group *grpinfo = NULL;
-       if (uid == getuid()) {
-               grpinfo = getgrnam("users");
-               if (grpinfo == NULL) {
-                       media_svc_error("getgrnam(users) returns NULL !");
-                       return NULL;
-               }
-               result_passwd = g_strdup(MEDIA_SVC_THUMB_INTERNAL_PATH);
-       } else {
-               char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
-               struct passwd *userinfo = getpwuid(uid);
-               if (userinfo == NULL) {
-                       media_svc_error("getpwuid(%d) returns NULL !", uid);
-                       return NULL;
-               }
-               grpinfo = getgrnam("users");
-               if (grpinfo == NULL) {
-                       media_svc_error("getgrnam(users) returns NULL !");
-                       return NULL;
-               }
-               /* Compare git_t type and not group name */
-               if (grpinfo->gr_gid != userinfo->pw_gid) {
-                       media_svc_error("UID [%d] does not belong to 'users' group!", uid);
-                       return NULL;
-               }
-               snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/phone", userinfo->pw_dir);
-               result_passwd = g_strdup(passwd_str);
-       }
-
-       return result_passwd;
-}
-
-char *_media_svc_get_thumb_external_path(uid_t uid)
-{
-       char *result_passwd = NULL;
-       struct group *grpinfo = NULL;
-       if (uid == getuid()) {
-               grpinfo = getgrnam("users");
-               if (grpinfo == NULL) {
-                       media_svc_error("getgrnam(users) returns NULL !");
-                       return NULL;
+       GDir *dir = NULL;
+       GError *error = NULL;
+       const char *name;
+
+       dir = g_dir_open(dir_path, 0, &error);
+       if (dir != NULL && error == NULL) {
+               while ((name = g_dir_read_name(dir))) {
+                       memset(filename, 0, sizeof(filename));
+                       snprintf(filename, sizeof(filename), "%s/%s", dir_path, name);
+
+                       if (g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
+                               if (unlink(filename) != 0)
+                                       media_svc_stderror("failed to remove");
+                       }
                }
-               result_passwd = g_strdup(MEDIA_SVC_THUMB_EXTERNAL_PATH);
        } else {
-               char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
-               struct passwd *userinfo = getpwuid(uid);
-               if (userinfo == NULL) {
-                       media_svc_error("getpwuid(%d) returns NULL !", uid);
-                       return NULL;
-               }
-               grpinfo = getgrnam("users");
-               if (grpinfo == NULL) {
-                       media_svc_error("getgrnam(users) returns NULL !");
-                       return NULL;
-               }
-               /* Compare git_t type and not group name */
-               if (grpinfo->gr_gid != userinfo->pw_gid) {
-                       media_svc_error("UID [%d] does not belong to 'users' group!", uid);
-                       return NULL;
-               }
-               snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/mmc", userinfo->pw_dir);
-               result_passwd = g_strdup(passwd_str);
+               media_svc_error("%s is not exist", dir_path);
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
        }
 
-       return result_passwd;
-}
-
-static int __media_svc_check_thumb_dir(const char *thumb_dir)
-{
-       int ret = 0;
-       DIR *dir = NULL;
-
-       dir = opendir(thumb_dir);
-       if (dir != NULL) {
-               closedir(dir);
-       } else {
-               media_svc_stderror("opendir fail");
-               if (errno == ENOENT) {
-                       media_svc_error("[%s] is not exit. So, make it", thumb_dir);
-                       ret = mkdir(thumb_dir, 0777);
-                       if (ret < 0) {
-                               media_svc_error("make fail");
-                               goto ERROR;
-                       }
-               } else {
-                       goto ERROR;
-               }
-
-               ret = chmod(thumb_dir, 0777);
-               if (ret != 0) {
-                       media_svc_stderror("chmod failed");
-               }
-               ret = chown(thumb_dir, 5000, 5000);
-               if (ret != 0) {
-                       media_svc_stderror("chown failed");
-               }
-       }
+       g_dir_close(dir);
 
        return MS_MEDIA_ERR_NONE;
-
-ERROR:
-       return -1;
 }
 
-int _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
+int _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, media_svc_media_type_e media_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
-       char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
        char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
-       char *thumb_dir = NULL;
        char hash[255 + 1] = {0, };
+       char *thumb_dir = NULL;
        char *thumbfile_ext = NULL;
-       char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
-       char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
 
-       if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
-               media_svc_error("fail to get thumbnail path");
-               SAFE_FREE(internal_thumb_path);
-               SAFE_FREE(external_thumb_path);
+       ret = ms_user_get_thumb_store_path(uid, storage_type, &thumb_dir);
+       if (!STRING_VALID(thumb_dir)) {
+               media_svc_error("ms_user_get_thumb_store_path failed");
                return MS_MEDIA_ERR_INTERNAL;
        }
 
-       thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
-
-       ret = __media_svc_check_thumb_dir(thumb_dir);
-       if (ret != MS_MEDIA_ERR_NONE) {
-               media_svc_error("__media_svc_check_thumb_dir");
-               SAFE_FREE(internal_thumb_path);
-               SAFE_FREE(external_thumb_path);
+       if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
+               media_svc_error("Wrong path[%s]", thumb_dir);
+               SAFE_FREE(thumb_dir);
                return MS_MEDIA_ERR_INTERNAL;
        }
 
        memset(file_ext, 0, sizeof(file_ext));
-       if (!__media_svc_get_file_ext(pathname, file_ext)) {
+       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));
        if (ret != MS_MEDIA_ERR_NONE) {
                media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
-               SAFE_FREE(internal_thumb_path);
-               SAFE_FREE(external_thumb_path);
+               SAFE_FREE(thumb_dir);
                return MS_MEDIA_ERR_INTERNAL;
        }
 
-       /*media_svc_debug("img format is [%s]", img_format); */
+       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 {
+                       media_svc_error("Not proper img format");
+                       SAFE_FREE(thumb_dir);
+                       return MS_MEDIA_ERR_INTERNAL;
+               }
 
-       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";
+               snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
        } else {
-               media_svc_error("Not proper img format");
-               SAFE_FREE(internal_thumb_path);
-               SAFE_FREE(external_thumb_path);
-               return MS_MEDIA_ERR_INTERNAL;
+               if (strcasecmp(file_ext, "PNG") == 0)
+                       snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
+               else
+                       snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
        }
 
-       snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
-       _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
-       /*media_svc_debug("thumb_path is [%s]", thumb_path); */
-
-       SAFE_FREE(internal_thumb_path);
-       SAFE_FREE(external_thumb_path);
+       SAFE_FREE(thumb_dir);
 
        return MS_MEDIA_ERR_NONE;
 }
@@ -1030,8 +816,6 @@ int _media_svc_get_file_time(const char *full_path)
 
 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
 {
-       int ret = MS_MEDIA_ERR_NONE;
-
        /* Set default GPS value before extracting meta information */
        content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
        content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
@@ -1039,45 +823,44 @@ int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool re
 
        /* Set filename to title for all media */
        char *title = NULL;
-       title = _media_svc_get_title_from_filepath(content_info->path);
+       title = __media_svc_get_title_from_filepath(content_info->path);
        if (title) {
-               ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
-               if (ret != MS_MEDIA_ERR_NONE)
-                       media_svc_error("strcpy error");
+               content_info->media_meta.title = g_strdup(title);
                SAFE_FREE(title);
+               media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
        } else {
                media_svc_error("Can't extract title");
-               ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
-               media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
+               content_info->media_meta.title = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+               media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
        }
 
        /* Set default value before extracting meta information */
-       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
-       media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
+       content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+       media_svc_retv_del_if(content_info->media_meta.description == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
 
-       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, MEDIA_SVC_TAG_UNKNOWN);
-       media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
+       content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+       media_svc_retv_del_if(content_info->media_meta.copyright == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
 
-       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, MEDIA_SVC_TAG_UNKNOWN);
-       media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
+       content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+       media_svc_retv_del_if(content_info->media_meta.track_num == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
 
-       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
-       media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
+       content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+       media_svc_retv_del_if(content_info->media_meta.album == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
 
-       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
-       media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
+       content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+       media_svc_retv_del_if(content_info->media_meta.artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
 
-       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, MEDIA_SVC_TAG_UNKNOWN);
-       media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
+       content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+       media_svc_retv_del_if(content_info->media_meta.album_artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
 
-       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
-       media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
+       content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+       media_svc_retv_del_if(content_info->media_meta.genre == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
 
-       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
-       media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
+       content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+       media_svc_retv_del_if(content_info->media_meta.composer == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
 
-       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
-       media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
+       content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+       media_svc_retv_del_if(content_info->media_meta.year == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
 
        if (refresh) {
                media_svc_debug("refresh");
@@ -1098,24 +881,20 @@ int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char
 {
        int ret = MS_MEDIA_ERR_NONE;
        char * media_uuid = NULL;
-       char * file_name = NULL;
        bool drm_type = false;
        char mime_type[256] = {0, };
 
-       ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
-       media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
+       content_info->path = g_strdup(path);
+       media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
 
-       if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
-               struct stat st;
-               memset(&st, 0, sizeof(struct stat));
-               if (stat(path, &st) == 0) {
-                       content_info->modified_time = st.st_mtime;
-                       content_info->timeline = content_info->modified_time;
-                       content_info->size = st.st_size;
-                       //media_svc_debug("Modified time : [%d] Size : [%lld]", content_info->modified_time, content_info->size);
-               } else {
-                       media_svc_stderror("stat failed");
-               }
+       struct stat st;
+       memset(&st, 0, sizeof(struct stat));
+       if (stat(path, &st) == 0) {
+               content_info->modified_time = st.st_mtime;
+               content_info->timeline = content_info->modified_time;
+               content_info->size = st.st_size;
+       } else {
+               media_svc_stderror("stat failed");
        }
 
        _media_svc_set_default_value(content_info, refresh);
@@ -1126,8 +905,8 @@ int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char
                return MS_MEDIA_ERR_NONE;
        }
 
-       ret = __media_svc_malloc_and_strncpy(&content_info->storage_uuid, storage_id);
-       media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
+       content_info->storage_uuid = g_strdup(storage_id);
+       media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
 
        content_info->storage_type = storage_type;
        time(&content_info->added_time);
@@ -1138,65 +917,159 @@ int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char
                return MS_MEDIA_ERR_INTERNAL;
        }
 
-       ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
+       content_info->media_uuid = g_strdup(media_uuid);
+       media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
+
+       content_info->file_name = g_path_get_basename(path);
+       media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
+
+       /* if the file is DRM file, drm_type value is DRM_TRUE(1).
+       if drm_contentinfo is not NULL, the file is OMA DRM.*/
+       ret = __media_svc_get_mime_type(path, mime_type);
        media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
 
-       file_name = g_path_get_basename(path);
-       ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
-       SAFE_FREE(file_name);
+       media_svc_debug("mime [%s]", mime_type);
+       content_info->is_drm = drm_type;
+
+       ret = __media_svc_get_media_type(path, mime_type, media_type);
        media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
+       if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
+               media_svc_error("invalid media_type condition[%d]", *media_type);
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
 
-       if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
-               /* if the file is DRM file, drm_type value is DRM_TRUE(1).
-               if drm_contentinfo is not NULL, the file is OMA DRM.*/
-               ret = __media_svc_get_mime_type(path, mime_type);
-               media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
+       content_info->mime_type = g_strdup(mime_type);
+       media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
 
-               media_svc_debug("mime [%s]", mime_type);
-               content_info->is_drm = drm_type;
+       media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
 
-               ret = __media_svc_get_media_type(path, mime_type, media_type);
-               media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
+       content_info->media_type = *media_type;
 
-               if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
-                       media_svc_error("invalid media_type condition[%d]", *media_type);
-                       return MS_MEDIA_ERR_INVALID_PARAMETER;
-               }
+       return MS_MEDIA_ERR_NONE;
+}
+
+int image_360_check(char *path)
+{
+       FILE *fp = NULL;
+       long app1_size = 0;
+       int size = 1;
+       unsigned char exif_header[4] = {0, };
+       unsigned char exif_app1[2] = {0, };
+       unsigned char exif_app1_xmp[2] = {0, };
+       long exif_app1_xmp_size = 0;
+       unsigned char exif_app1_xmp_t[2] = {0, };
+       char *xmp_data = NULL;
+       int size1 = 0;
+       int size2 = 0;
+       int fdata = 0;
+       int temp = 0;
+
+       memset(exif_header, 0x00, sizeof(exif_header));
+       memset(exif_app1, 0x00, sizeof(exif_app1));
+       memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
+       memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
+
+       fp = fopen(path, "rb");
+       if (fp == NULL)
+               goto ERROR;
+
+       size = fread(exif_header, 1, sizeof(exif_header), fp);
+       if (size <= 0)
+               goto ERROR;
+
+       if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
+               size = fread(exif_app1, 1, sizeof(exif_app1), fp);
+               if (size <= 0)
+                       goto ERROR;
 
-               ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
-               media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
+               size1 = exif_app1[0];
+               size2 = exif_app1[1];
 
-               media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
+               app1_size = size1 * 256 + size2 - 2;
 
-               content_info->media_type = *media_type;
-       }
+               if (fseek(fp, app1_size, SEEK_CUR) != 0)
+                       goto ERROR;
 
-       return MS_MEDIA_ERR_NONE;
+               size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
+               if (size <= 0)
+                       goto ERROR;
+
+               if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
+                       int result = 0;
+                       char *ptr = NULL;
+                       size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
+                       if (size <= 0)
+                               goto ERROR;
+
+                       size1 = exif_app1_xmp_t[0];
+                       size2 = exif_app1_xmp_t[1];
+
+                       exif_app1_xmp_size = size1 * 256 + size2 - 2;
+
+                       if (exif_app1_xmp_size > 0) {
+                               xmp_data = (char *)malloc(exif_app1_xmp_size);
+                               memset(xmp_data, 0x0, exif_app1_xmp_size);
+
+                               ptr = xmp_data;
+
+                               while (exif_app1_xmp_size >= 0) {
+                                       exif_app1_xmp_size--;
+                                       fdata = fgetc(fp);
+                                       if (fdata == EOF)
+                                               continue;
+                                       if (fdata == '\0')
+                                               continue;
+                                       *ptr = (char)fdata;
+                                       ptr++;
+                                       temp++;
+                               }
+                               ptr = ptr - temp;
+
+                               if (strstr(ptr, "UsePanoramaViewer")
+                               && strstr(ptr, "True")
+                               && strstr(ptr, "ProjectionType")
+                               && strstr(ptr, "equirectangular"))
+                                       result = 1;
+
+                               SAFE_FREE(xmp_data);
+                       } else {
+                               media_svc_error("invalid exif_app1_xmp_size [%ld]", exif_app1_xmp_size);
+                       }
+
+                       if (fp) {
+                               fclose(fp);
+                               fp = NULL;
+                       }
+                       return result;
+               } else {
+                       goto ERROR;
+               }
+       } else {
+               goto ERROR;
+       }
+ERROR:
+       if (fp) {
+               fclose(fp);
+               fp = NULL;
+       }
+       return 0;
 }
 
 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
 {
-       int ret = MS_MEDIA_ERR_NONE;
        double value = 0.0;
        int orient_value = 0;
        int exif_width = 0;
        int exif_height = 0;
        ExifData *ed = NULL;
        int has_datetaken = FALSE;
-       int datetaken_size = 19;
        double fnumber = 0.0;
        int iso = 0;
        char *path = NULL;
 
        char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
-       char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
-       char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
-       char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
 
        memset(buf, 0x00, sizeof(buf));
-       memset(description_buf, 0x00, sizeof(description_buf));
-       memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
-       memset(model_buf, 0x00, sizeof(model_buf));
 
        if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
                media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
@@ -1217,12 +1090,13 @@ int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s
                goto GET_WIDTH_HEIGHT;
        }
 
+       content_info->media_meta.is_360 = image_360_check(path);
+
        if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
                if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
                        if (strlen(buf) > 0) {
-                               if (strcmp(buf, "S") == 0) {
+                               if (strcmp(buf, "S") == 0)
                                        value = -1 * value;
-                               }
                        }
                        content_info->media_meta.latitude = value;
                } else {
@@ -1237,9 +1111,8 @@ int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s
        if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
                if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
                        if (strlen(buf) > 0) {
-                               if (strcmp(buf, "W") == 0) {
+                               if (strcmp(buf, "W") == 0)
                                        value = -1 * value;
-                               }
                        }
                        content_info->media_meta.longitude = value;
                } else {
@@ -1251,143 +1124,109 @@ int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s
 
        memset(buf, 0x00, sizeof(buf));
 
-       if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
-               if (strlen(description_buf) == 0) {
-                       /*media_svc_debug("Use 'No description'"); */
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-               } else {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-               }
+       if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
+               if (strlen(buf) == 0)
+                       content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+               else
+                       content_info->media_meta.description = g_strdup(buf);
        } else {
-               ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
-               if (ret != MS_MEDIA_ERR_NONE)
-                       media_svc_error("strcpy error");
+               content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
        }
 
        memset(buf, 0x00, sizeof(buf));
 
        if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
-               if (strlen(buf) == 0) {
-                       /*media_svc_debug("time is NULL"); */
-               } else {
-                       ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
-                       if (ret != MS_MEDIA_ERR_NONE) {
-                               media_svc_error("strcpy error");
-                       } else {
-                               has_datetaken = TRUE;
-                               /* This is same as recorded_date */
-                               ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
-                               if (ret != MS_MEDIA_ERR_NONE)
-                                       media_svc_error("strcpy error");
-                       }
+               if (strlen(buf) > 0) {
+                       has_datetaken = TRUE;
+                       content_info->media_meta.datetaken = g_strdup(buf);
+
+                       /* This is same as recorded_date */
+                       content_info->media_meta.recorded_date = g_strdup(buf);
                }
        }
 
        memset(buf, 0x00, sizeof(buf));
 
        if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
-               if (strlen(buf) == 0) {
-                       /*media_svc_debug("time is NULL"); */
-               } else {
-                       ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
-                       if (ret != MS_MEDIA_ERR_NONE) {
-                               media_svc_error("strcpy error");
-                       } else {
-                               has_datetaken = TRUE;
-                               /* This is same as recorded_date */
-                               ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
-                               if (ret != MS_MEDIA_ERR_NONE)
-                                       media_svc_error("strcpy error");
-                       }
+               if (strlen(buf) > 0) {
+                       has_datetaken = TRUE;
+                       content_info->media_meta.datetaken = g_strdup(buf);
+
+                       /* This is same as recorded_date */
+                       content_info->media_meta.recorded_date =  g_strdup(buf);
                }
        }
 
        if (has_datetaken) {
                content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
-               if (content_info->timeline == 0) {
+               if (content_info->timeline == 0)
                        content_info->timeline = content_info->modified_time;
-               } else {
+               else
                        media_svc_debug("Timeline : %ld", content_info->timeline);
-               }
        }
 
+       memset(buf, 0x00, sizeof(buf));
+
        /* Get exposure_time value from exif. */
-       if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
-               if (strlen(exposure_time_buf) == 0) {
-                       //media_svc_debug("exposure_time_buf is NULL");
-               } else {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-               }
+       if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
+               if (strlen(buf) > 0)
+                       content_info->media_meta.exposure_time = g_strdup(buf);
        }
 
        /* Get fnumber value from exif. */
        if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
-               if (fnumber > 0.0) {
+               if (fnumber > 0.0)
                        content_info->media_meta.fnumber = fnumber;
-               } else {
+               else
                        content_info->media_meta.fnumber = 0.0;
-               }
        } else {
                content_info->media_meta.fnumber = 0.0;
        }
 
        /* Get iso value from exif. */
        if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
-               if (iso > 0) {
+               if (iso > 0)
                        content_info->media_meta.iso = iso;
-               } else {
+               else
                        content_info->media_meta.iso = 0;
-               }
        } else {
                content_info->media_meta.iso = 0;
        }
 
+       memset(buf, 0x00, sizeof(buf));
+
        /* Get model value from exif. */
-       if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
-               if (strlen(model_buf) == 0) {
-                       //media_svc_debug("model_buf is NULL");
-               } else {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-               }
+       if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
+               if (strlen(buf) > 0)
+                       content_info->media_meta.model = g_strdup(buf);
        }
 
        /* Get orientation value from exif. */
        if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
-               if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270) {
+               if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
                        content_info->media_meta.orientation = orient_value;
-               } else {
+               else
                        content_info->media_meta.orientation = 0;
-               }
        } else {
                content_info->media_meta.orientation = 0;
        }
 
        /* Get width value from exif. */
        if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
-               if (exif_width > 0) {
+               if (exif_width > 0)
                        content_info->media_meta.width = exif_width;
-               } else {
+               else
                        content_info->media_meta.width = 0;
-               }
        } else {
                content_info->media_meta.width = 0;
        }
 
        /* Get height value from exif. */
        if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
-               if (exif_height > 0) {
+               if (exif_height > 0)
                        content_info->media_meta.height = exif_height;
-               } else {
+               else
                        content_info->media_meta.height = 0;
-               }
        } else {
                content_info->media_meta.height = 0;
        }
@@ -1396,15 +1235,13 @@ int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s
 
 GET_WIDTH_HEIGHT:
 
-       if (content_info->media_meta.width == 0 ||
-               content_info->media_meta.height == 0) {
+       if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
                /*Get image width, height*/
                unsigned int img_width = 0;
                unsigned int img_height = 0;
-               ImgCodecType img_type = IMG_CODEC_NONE;
-
-               ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
+               mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
 
+               mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
                if (content_info->media_meta.width == 0)
                        content_info->media_meta.width = img_width;
 
@@ -1420,58 +1257,36 @@ int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_cont
        MMHandleType tag = 0;
        char *p = NULL;
        int size = -1;
-       int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
        int mmf_error = FILEINFO_ERROR_NONE;
        char *err_attr_name = NULL;
-       char *title = NULL;
        int album_id = 0;
-       int ret = MS_MEDIA_ERR_NONE;
 
        /*Get Content Tag attribute ===========*/
        mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
 
        if (mmf_error == FILEINFO_ERROR_NONE) {
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
-               if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-
-                       /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
-               } else {
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
+                       content_info->media_meta.album = g_strdup(p);
+               else
                        SAFE_FREE(err_attr_name);
-                       /*media_svc_debug("album - unknown"); */
-               }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
-               if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-                       /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
-               } else {
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
+                       content_info->media_meta.artist = g_strdup(p);
+               else
                        SAFE_FREE(err_attr_name);
-                       /*media_svc_debug("artist - unknown"); */
-               }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
-               if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-                       /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
-               } else {
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
+                       content_info->media_meta.album_artist = g_strdup(p);
+               else
                        SAFE_FREE(err_attr_name);
-                       /*media_svc_debug("album_artist - unknown"); */
-               }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
-               if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
+                       content_info->media_meta.genre = g_strdup(p);
 
-                       /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
                        /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
                        /*
                        if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
@@ -1480,17 +1295,12 @@ int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_cont
                        */
                } else {
                        SAFE_FREE(err_attr_name);
-                       /*media_svc_debug("genre - unknown"); */
                }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
-               if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
                        if (!isspace(*p)) {
-                               ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
-                               if (ret != MS_MEDIA_ERR_NONE)
-                                       media_svc_error("strcpy error");
-
-                               extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
+                               content_info->media_meta.title = g_strdup(p);
                        } else {
                                int idx = 0;
 
@@ -1501,92 +1311,35 @@ int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_cont
                                                continue;
                                        } else {
                                                media_svc_debug("Not SPACE [%s]", p);
-                                               ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
-                                               if (ret != MS_MEDIA_ERR_NONE)
-                                                       media_svc_error("strcpy error");
+                                               content_info->media_meta.title = g_strdup(p);
                                                break;
                                        }
                                }
-
-                               if (idx == size) {
-                                       media_svc_debug("Can't extract title. All string is space");
-                                       title = _media_svc_get_title_from_filepath(content_info->path);
-                                       if (title) {
-                                               ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
-                                               if (ret != MS_MEDIA_ERR_NONE)
-                                                       media_svc_error("strcpy error");
-                                               SAFE_FREE(title);
-                                       } else {
-                                               media_svc_error("Can't extract title");
-                                       }
-                               }
-                       }
-               } else {
-                       SAFE_FREE(err_attr_name);
-                       title = _media_svc_get_title_from_filepath(content_info->path);
-                       if (title) {
-                               ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
-                               if (ret != MS_MEDIA_ERR_NONE)
-                                       media_svc_error("strcpy error");
-                               SAFE_FREE(title);
-                       } else {
-                               media_svc_error("Can't extract title");
                        }
                }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
-               if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-                       /*media_svc_debug("desc : %s", content_info->media_meta.description); */
-               } else {
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
+                       content_info->media_meta.description = g_strdup(p);
+               else
                        SAFE_FREE(err_attr_name);
-               }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
-               if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-                       extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
-                       /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
-               } else {
-                       /*media_svc_debug("composer - unknown"); */
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
+                       content_info->media_meta.composer = g_strdup(p);
+               else
                        SAFE_FREE(err_attr_name);
-               }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
-               if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-                       extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
-                       /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
-               } else {
-                       /*media_svc_debug("copyright - unknown"); */
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
+                       content_info->media_meta.copyright = g_strdup(p);
+               else
                        SAFE_FREE(err_attr_name);
-               }
 
                mmf_error = mm_file_destroy_tag_attrs(tag);
-               if (mmf_error != FILEINFO_ERROR_NONE) {
+               if (mmf_error != FILEINFO_ERROR_NONE)
                        media_svc_error("fail to free tag attr - err(%x)", mmf_error);
-               }
-       }       else {
-               /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
-               char *no_tag_title = NULL;
-               media_svc_error("no tag information");
-
-               no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
-               if (no_tag_title) {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-                       SAFE_FREE(no_tag_title);
-               } else {
-                       media_svc_error("Can't extract title");
-               }
-
+       } else {
                content_info->album_id = album_id;
        }
 
@@ -1598,65 +1351,40 @@ int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s
        MMHandleType content = 0;
        MMHandleType tag = 0;
        char *p = NULL;
-       void *image = NULL;
-       int size = -1;
-       int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
+       unsigned char *image = NULL;
+       unsigned int size = 0;
        int mmf_error = FILEINFO_ERROR_NONE;
        char *err_attr_name = NULL;
-       char *title = NULL;
-       bool extract_thumbnail = FALSE;
-       bool append_album = FALSE;
        int album_id = 0;
        int ret = MS_MEDIA_ERR_NONE;
        int cdis_value = 0;
-       int resize_size = -1;
-       void *resize_image = NULL;
 
        /*Get Content Tag attribute ===========*/
        mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
 
        if (mmf_error == FILEINFO_ERROR_NONE) {
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
-               if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-
-                       /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
-               } else {
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
+                       content_info->media_meta.album = g_strdup(p);
+               else
                        SAFE_FREE(err_attr_name);
-                       /*media_svc_debug("album - unknown"); */
-               }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
-               if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-                       /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
-               } else {
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
+                       content_info->media_meta.artist = g_strdup(p);
+               else
                        SAFE_FREE(err_attr_name);
-                       /*media_svc_debug("artist - unknown"); */
-               }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
-               if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-                       /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
-               } else {
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
+                       content_info->media_meta.album_artist = g_strdup(p);
+               else
                        SAFE_FREE(err_attr_name);
-                       /*media_svc_debug("album_artist - unknown"); */
-               }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
-               if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
+                       content_info->media_meta.genre = g_strdup(p);
 
-                       /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
                        /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
                        /*
                        if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
@@ -1665,17 +1393,12 @@ int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s
                        */
                } else {
                        SAFE_FREE(err_attr_name);
-                       /*media_svc_debug("genre - unknown"); */
                }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
-               if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
                        if (!isspace(*p)) {
-                               ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
-                               if (ret != MS_MEDIA_ERR_NONE)
-                                       media_svc_error("strcpy error");
-
-                               extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
+                               content_info->media_meta.title = g_strdup(p);
                        } else {
                                int idx = 0;
 
@@ -1686,188 +1409,117 @@ int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s
                                                continue;
                                        } else {
                                                media_svc_debug("Not SPACE [%s]", p);
-                                               ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
-                                               if (ret != MS_MEDIA_ERR_NONE)
-                                                       media_svc_error("strcpy error");
+                                               content_info->media_meta.title = g_strdup(p);
                                                break;
                                        }
                                }
-
-                               if (idx == size) {
-                                       media_svc_debug("Can't extract title. All string is space");
-                                       title = _media_svc_get_title_from_filepath(content_info->path);
-                                       if (title) {
-                                               ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
-                                               if (ret != MS_MEDIA_ERR_NONE)
-                                                       media_svc_error("strcpy error");
-                                               SAFE_FREE(title);
-                                       } else {
-                                               media_svc_error("Can't extract title");
-                                       }
-                               }
-                       }
-               } else {
-                       SAFE_FREE(err_attr_name);
-                       title = _media_svc_get_title_from_filepath(content_info->path);
-                       if (title) {
-                               ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
-                               if (ret != MS_MEDIA_ERR_NONE)
-                                       media_svc_error("strcpy error");
-                               SAFE_FREE(title);
-                       } else {
-                               media_svc_error("Can't extract title");
                        }
                }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
-               if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-                       /*media_svc_debug("desc : %s", content_info->media_meta.description); */
-               } else {
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
+                       content_info->media_meta.description = g_strdup(p);
+               else
+                       SAFE_FREE(err_attr_name);
+
+               mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
+               if (mmf_error != FILEINFO_ERROR_NONE)
                        SAFE_FREE(err_attr_name);
-               }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
                if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
+                       char mime_type[255] = {0, };
+                       ret = __media_svc_get_mime_type(content_info->path, mime_type);
+                       /*if 3gp that audio only, media_type is music */
+                       if ((ret == MS_MEDIA_ERR_NONE) &&
+                               ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO && strcmp(mime_type, "video/mp4") == 0) ||
+                               (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO && strcmp(mime_type, "video/3gpp") == 0) ||
+                               (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC && strcmp(mime_type, "video/3gpp") == 0) ||
+                               (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC && strcmp(mime_type, "audio/mp4") == 0))) {
                                /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
-                               char time_info[64] = {0, };
-                               char p_value[64] = {0, };
-                               int idx = 0;
-                               memset(time_info, 0x00, sizeof(time_info));
-                               memset(p_value, 0x00, sizeof(p_value));
-                               strncpy(p_value, p, size);
-                               for (idx = 0; idx < size; idx++) {
-                                       if (p_value[idx] == '-') {
-                                               time_info[idx] = ':';
-                                       } else if (p_value[idx] != '\0') {
-                                               time_info[idx] = p_value[idx];
-                                       } else {
-                                               media_svc_error("strcpy error");
-                                               break;
+                               char *time_info = g_strdup_printf("0000:00:00 00:00:00 +0000");
+                               char *p_value = p;
+                               char *time_value = time_info;
+                               if (time_info != NULL) {
+                                       while (*p_value != '\0') {
+                                               if (*p_value == '-')
+                                                       *time_value = ':';
+                                               else
+                                                       *time_value = *p_value;
+                                               time_value++;
+                                               p_value++;
                                        }
+                                       content_info->media_meta.recorded_date = g_strdup(time_info);
+                                       SAFE_FREE(time_info);
+                               } else {
+                                       media_svc_error("memory allocation error");
                                }
-                               ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
                        } else {
-                               ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
+                               content_info->media_meta.recorded_date = g_strdup(p);
                        }
 
-                       if (ret != MS_MEDIA_ERR_NONE) {
-                               media_svc_error("strcpy error");
-                       } else {
-                               /* This is same as datetaken */
-#if 0
-                               ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
-#else
-                               int datetaken_size = 19;
-                               ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
-#endif
-                               if (ret != MS_MEDIA_ERR_NONE)
-                                       media_svc_error("strcpy error");
-
+                       if (STRING_VALID(content_info->media_meta.recorded_date)) {
                                content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
-                               if (content_info->timeline == 0) {
+                               if (content_info->timeline == 0)
                                        content_info->timeline = content_info->modified_time;
-                               } else {
+                               else
                                        media_svc_debug("Timeline : %ld", content_info->timeline);
+
+                               /* This is same as datetaken */
+                               /* Remove compensation string */
+                               if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
+                                       content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
+                                       G_SAFE_FREE(content_info->media_meta.recorded_date);
+                                       content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
+                               } else {
+                                       content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
                                }
                        }
-                       /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
                } else {
                        SAFE_FREE(err_attr_name);
                }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
-               if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-                       extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
-                       /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
-               } else {
-                       /*media_svc_debug("composer - unknown"); */
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
+                       content_info->media_meta.composer = g_strdup(p);
+               else
                        SAFE_FREE(err_attr_name);
-               }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
-               if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-                       extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
-                       /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
-               } else {
-                       /*media_svc_debug("copyright - unknown"); */
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
+                       content_info->media_meta.copyright = g_strdup(p);
+               else
                        SAFE_FREE(err_attr_name);
-               }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
-               if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-               } else {
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
+                       content_info->media_meta.track_num = g_strdup(p);
+               else
                        SAFE_FREE(err_attr_name);
-               }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
-               if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size == 4)) {
+               if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
                        int year = 0;
-                       if ((p != NULL) && (sscanf(p, "%d", &year))) {
-                               ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
-                               if (ret != MS_MEDIA_ERR_NONE)
-                                       media_svc_error("strcpy error");
-                       } else {
+                       if ((p != NULL) && (__media_svc_safe_atoi(p, &year) == MS_MEDIA_ERR_NONE))
+                               content_info->media_meta.year = g_strdup(p);
+                       else
                                media_svc_debug("Wrong Year Information [%s]", p);
-                       }
                } else {
                        SAFE_FREE(err_attr_name);
                }
 
                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
-               if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       content_info->media_meta.rating = atoi(p);
+               if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
+                       int rate = 0;
+                       if ((p != NULL) && (__media_svc_safe_atoi(p, &rate) == MS_MEDIA_ERR_NONE))
+                               content_info->media_meta.rating = rate;
                } else {
                        SAFE_FREE(err_attr_name);
                        content_info->media_meta.rating = 0;
                }
 
-               /*Initialize album_id to 0. below code will set the album_id*/
-               content_info->album_id = album_id;
-               ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
-
-               if (ret != MS_MEDIA_ERR_NONE) {
-                       if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
-                               media_svc_debug("album does not exist. So start to make album art");
-                               extract_thumbnail = TRUE;
-                               append_album = TRUE;
-                       } else {
-                               extract_thumbnail = TRUE;
-                               append_album = FALSE;
-                       }
-               } else {
-                       content_info->album_id = album_id;
-                       append_album = FALSE;
-
-                       if ((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
-                               (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
-
-                               media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
-                               extract_thumbnail = TRUE;
-                       } else {
-                               media_svc_debug("album already exists. don't need to make album art");
-                               ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
-                               extract_thumbnail = TRUE;
-                       }
-               }
-
                /*Do not extract artwork for the USB Storage content*/
-               if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
-                       extract_thumbnail = FALSE;
-
-               if (extract_thumbnail == TRUE) {
+               if (content_info->storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
                        mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
                        if (mmf_error != FILEINFO_ERROR_NONE) {
                                media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
@@ -1883,42 +1535,54 @@ int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s
                        } else {
                                /*media_svc_debug("artwork size2 [%d]", size); */
                        }
+
                        if (image != NULL && size > 0) {
                                char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
                                int artwork_mime_size = -1;
 
                                mmf_error = mm_file_get_attrs(tag, &err_attr_name, 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->storage_type, thumb_path, content_info->path, p, uid);
-                                       if (ret != MS_MEDIA_ERR_NONE) {
+                                       ret = _media_svc_get_thumbnail_path(content_info->storage_type, content_info->media_type, thumb_path, content_info->path, p, uid);
+                                       if (ret != MS_MEDIA_ERR_NONE)
                                                media_svc_error("Fail to Get Thumbnail Path");
+
+                                       if (strlen(thumb_path) > 0) {
+                                               ret = __media_svc_save_image(image, size, thumb_path, uid);
+                                               if (ret != MS_MEDIA_ERR_NONE) {
+                                                       media_svc_error("Fail to Save Image");
+                                               } else {
+                                                       /* albumart resizing */
+                                                       ret = __media_svc_resize_artwork(thumb_path, p);
+                                                       if (ret != MS_MEDIA_ERR_NONE) {
+                                                               media_svc_error("Fail to Make Thumbnail Image");
+                                                               _media_svc_remove_file(thumb_path);
+
+                                                       } else {
+                                                               content_info->thumbnail_path = g_strdup(thumb_path);
+                                                       }
+                                               }
                                        }
-                                       /* albumart resizing */
-                                       __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
                                } else {
                                        SAFE_FREE(err_attr_name);
                                }
-
-                               if (strlen(thumb_path) > 0) {
-                                       ret = _media_svc_save_image(resize_image, resize_size, thumb_path, uid);
-                                       if (ret != MS_MEDIA_ERR_NONE) {
-                                               media_svc_error("Fail to Save Thumbnail Image");
-                                       } else {
-                                               ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
-                                               if (ret != MS_MEDIA_ERR_NONE)
-                                                       media_svc_error("strcpy error");
-                                       }
-                               }
                        }
                }
 
-               if (append_album == TRUE) {
-                       if ((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
-                               (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
-                               ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
-                       else
-                               ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
+               /*Initialize album_id to 0. below code will set the album_id*/
+               content_info->album_id = album_id;
+               ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
+                               media_svc_debug("album does not exist. So start to make album art");
+                               if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
+                                       (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
+                                       ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
+                               else
+                                       ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
 
+                               content_info->album_id = album_id;
+                       }
+               } else {
                        content_info->album_id = album_id;
                }
 
@@ -1939,36 +1603,29 @@ int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s
                        }
 
                        media_svc_debug("CDIS : %d", cdis_value);
+
+                       mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ROTATE, &p, &size, NULL);
+                       if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
+                               content_info->media_meta.orientation = atoi(p);
+                       } else {
+                               SAFE_FREE(err_attr_name);
+                               content_info->media_meta.orientation = 0;
+                               media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
+                       }
                }
 
                mmf_error = mm_file_destroy_tag_attrs(tag);
-               if (mmf_error != FILEINFO_ERROR_NONE) {
+               if (mmf_error != FILEINFO_ERROR_NONE)
                        media_svc_error("fail to free tag attr - err(%x)", mmf_error);
-               }
        }       else {
-               /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
-               char *no_tag_title = NULL;
-               media_svc_error("no tag information");
-
-               no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
-               if (no_tag_title) {
-                       ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
-                       if (ret != MS_MEDIA_ERR_NONE)
-                               media_svc_error("strcpy error");
-                       SAFE_FREE(no_tag_title);
-               } else {
-                       media_svc_error("Can't extract title");
-               }
-
                content_info->album_id = album_id;
        }
 
        /*Get Content attribute ===========*/
-       if (cdis_value == 1) {
+       if (cdis_value == 1)
                mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
-       } else {
+       else
                mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
-       }
 
        if (mmf_error == FILEINFO_ERROR_NONE) {
                /*Common attribute*/
@@ -2051,20 +1708,18 @@ int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s
                        } else {
                                /*media_svc_debug("height : %d", content_info->media_meta.height); */
                        }
-
                } else {
                        media_svc_error("Not support type");
                        mmf_error = mm_file_destroy_content_attrs(content);
-                       if (mmf_error != FILEINFO_ERROR_NONE) {
+                       if (mmf_error != FILEINFO_ERROR_NONE)
                                media_svc_error("fail to free content attr - err(%x)", mmf_error);
-                       }
+
                        return MS_MEDIA_ERR_INVALID_PARAMETER;
                }
 
                mmf_error = mm_file_destroy_content_attrs(content);
-               if (mmf_error != FILEINFO_ERROR_NONE) {
+               if (mmf_error != FILEINFO_ERROR_NONE)
                        media_svc_error("fail to free content attr - err(%x)", mmf_error);
-               }
        } else {
                media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
        }
@@ -2121,79 +1776,225 @@ void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
        return;
 }
 
-int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
+int __media_svc_get_proper_thumb_size(unsigned int orig_w, unsigned int orig_h, unsigned int *thumb_w, unsigned int *thumb_h)
 {
-       if (STRING_VALID(path)) {
-               char *internal_path = _media_svc_get_path(uid);
-               if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0)) {
-                       *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
-               } else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
-                       *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
-               } else if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
-                       *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
-               } else {
-                       media_svc_error("Invalid Path");
-                       SAFE_FREE(internal_path);
-                       return MS_MEDIA_ERR_INVALID_PARAMETER;
-               }
-               SAFE_FREE(internal_path);
+       bool portrait = false;
+       double ratio;
+
+       if (orig_w < orig_h)
+               portrait = true;
+
+       /* Set smaller length to default size */
+       if (portrait) {
+               if (orig_w < *thumb_w)
+                       *thumb_w = orig_w;
+               ratio = (double)orig_h / (double)orig_w;
+               *thumb_h = *thumb_w * ratio;
        } else {
-               media_svc_error("INVALID parameter");
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
+               if (orig_h < *thumb_h)
+                       *thumb_h = orig_h;
+               ratio = (double)orig_w / (double)orig_h;
+               *thumb_w = *thumb_h * ratio;
        }
 
+       media_svc_debug("proper thumb w: %d h: %d", *thumb_w, *thumb_h);
+
        return MS_MEDIA_ERR_NONE;
 }
 
-char *_media_svc_replace_path(char *s, const char *olds, const char *news)
+int _media_svc_create_thumbnail(const char *path, char *thumb_path, int max_length, media_svc_media_type_e media_type, uid_t uid)
 {
-       char *result, *sr;
-       size_t i, count = 0;
-       size_t oldlen = strlen(olds);
-       if (oldlen < 1) return s;
-       size_t newlen = strlen(news);
-
-       if (newlen != oldlen) {
-               for (i = 0; s[i] != '\0';) {
-                       if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
-                       else i++;
-               }
-       } else i = strlen(s);
+       int ret = MS_MEDIA_ERR_NONE;
+       unsigned int origin_w = 0;
+       unsigned int origin_h = 0;
+       unsigned int thumb_w = THUMB_WIDTH;
+       unsigned int thumb_h = THUMB_HEIGHT;
+       mm_util_img_codec_type image_type = IMG_CODEC_UNKNOWN_TYPE;
 
+       if (path == NULL || thumb_path == NULL) {
+               media_svc_error("Invalid parameter");
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
 
-       result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
-       if (result == NULL) return NULL;
+       if (!g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
+                       media_svc_error("Original path(%s) doesn't exist.", path);
+                       return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
 
-       sr = result;
-       while (*s) {
-               if (memcmp(s, olds, oldlen) == 0) {
-                       memcpy(sr, news, newlen);
-                       sr += newlen;
-                       s += oldlen;
-               } else *sr++ = *s++;
+       if (max_length <= 0) {
+               media_svc_error("Length is invalid");
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
        }
 
-       *sr = '\0';
+       ms_user_storage_type_e store_type = -1;
+       ret = ms_user_get_storage_type(uid, path, &store_type);
 
-       return result;
-}
+       if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
+               media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
 
-bool _media_svc_is_drm_file(const char *path)
-{
-       return FALSE;
-}
+       media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
 
-int _media_svc_request_thumbnail_with_origin_size(const char *path, char *thumb_path, int max_length, int *origin_width, int *origin_height, uid_t uid)
-{
-       int ret = MS_MEDIA_ERR_NONE;
+       //1. make hash path
+       ret = _media_svc_get_thumbnail_path(store_type, media_type, thumb_path, path, THUMB_EXT, 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);
+               return ret;
+       }
 
-       ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
+       //2. save thumbnail
+       if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
+               ret = mm_util_extract_image_info(path, &image_type, &origin_w, &origin_h);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       media_svc_error("Getting image info is failed err: %d", ret);
+                       SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
+                       return MS_MEDIA_ERR_INTERNAL;
+               }
 
-       if (ret != MS_MEDIA_ERR_NONE) {
-               media_svc_error("thumbnail_request_from_db failed: %d", ret);
-               ret = MS_MEDIA_ERR_INTERNAL;
+               if ((image_type != IMG_CODEC_JPEG) && (origin_w * origin_h > THUMB_MAX_ALLOWED_MEM)) {
+                       media_svc_error("This original image is too big");
+                       SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
+                       return MS_MEDIA_ERR_THUMB_TOO_BIG;
+               }
+
+               __media_svc_get_proper_thumb_size(origin_w, origin_h, &thumb_w, &thumb_h);
+               ret = mm_util_resize_P_P(path, thumb_w, thumb_h, thumb_path);
+               if (ret != MM_UTIL_ERROR_NONE) {
+                       media_svc_error("mm_util_resize_P_P err: %d", ret);
+                       SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
+                       return MS_MEDIA_ERR_INTERNAL;
+               }
        } else {
-               media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
+               MMHandleType content = (MMHandleType) NULL;
+               MMHandleType tag = (MMHandleType) NULL;
+
+               char *p = NULL;
+               int cdis_value = 0;
+               void *frame = NULL;
+               int video_track_num = 0;
+               char *err_msg = NULL;
+               int size = 0;
+               mm_util_image_h img = NULL;
+               mm_util_image_h resize_img = NULL;
+
+               /* Get Content Tag attribute for orientation */
+               ret = mm_file_create_tag_attrs(&tag, path);
+               mm_util_magick_rotate_type rot_type = MM_UTIL_ROTATE_NUM;
+
+               if (ret == FILEINFO_ERROR_NONE) {
+                       ret = mm_file_get_attrs(tag, &err_msg, MM_FILE_TAG_ROTATE, &p, &size, NULL);
+                       if (ret == FILEINFO_ERROR_NONE && size >= 0) {
+                               if (p == NULL) {
+                                       rot_type = MM_UTIL_ROTATE_0;
+                               } else {
+                                       if (strncmp(p, "90", size) == 0)
+                                               rot_type = MM_UTIL_ROTATE_90;
+                                       else if (strncmp(p, "180", size) == 0)
+                                               rot_type = MM_UTIL_ROTATE_180;
+                                       else if (strncmp(p, "270", size) == 0)
+                                               rot_type = MM_UTIL_ROTATE_270;
+                                       else
+                                               rot_type = MM_UTIL_ROTATE_0;
+                               }
+                               media_svc_debug("There is tag rotate : %d", rot_type);
+                       } else {
+                               media_svc_debug("There is NOT tag rotate");
+                               rot_type = MM_UTIL_ROTATE_0;
+                               SAFE_FREE(err_msg);
+                       }
+
+                       ret = mm_file_get_attrs(tag, &err_msg, MM_FILE_TAG_CDIS, &cdis_value, NULL);
+                       if (ret != FILEINFO_ERROR_NONE) {
+                               cdis_value = 0;
+                               SAFE_FREE(err_msg);
+                       }
+               } else {
+                       rot_type = MM_UTIL_ROTATE_0;
+                       cdis_value = 0;
+               }
+
+               ret = mm_file_destroy_tag_attrs(tag);
+               if (ret != FILEINFO_ERROR_NONE) {
+                       media_svc_error("fail to free tag attr - err(%x)", ret);
+               }
+
+               if (cdis_value == 1) {
+                       media_svc_debug("This is CDIS vlaue 1");
+                       ret = mm_file_create_content_attrs_safe(&content, path);
+               } else {
+                       ret = mm_file_create_content_attrs(&content, path);
+               }
+
+               if (ret != FILEINFO_ERROR_NONE) {
+                       media_svc_error("mm_file_create_content_attrs fails : %d", ret);
+                       return MS_MEDIA_ERR_INTERNAL;
+               }
+
+               ret = mm_file_get_attrs(content, &err_msg, MM_FILE_CONTENT_VIDEO_TRACK_COUNT, &video_track_num, NULL);
+               if (ret != FILEINFO_ERROR_NONE) {
+                       media_svc_error("mm_file_get_attrs fails : %s", err_msg);
+                       SAFE_FREE(err_msg);
+                       goto ERROR;
+               }
+
+               if (video_track_num > 0) {
+                       ret = mm_file_get_attrs(content, &err_msg,
+                                               MM_FILE_CONTENT_VIDEO_WIDTH,
+                                               &origin_w,
+                                               MM_FILE_CONTENT_VIDEO_HEIGHT,
+                                               &origin_h,
+                                               MM_FILE_CONTENT_VIDEO_THUMBNAIL, &frame, /* raw image is RGB888 format */
+                                               &size, NULL);
+
+                       if (ret != FILEINFO_ERROR_NONE) {
+                               media_svc_error("mm_file_get_attrs fails : %s", err_msg);
+                               SAFE_FREE(err_msg);
+                               goto ERROR;
+                       }
+
+                       media_svc_debug("W[%d] H[%d] Size[%d] Frame[%p] Rotate[%d]", origin_w, origin_h, size, frame, rot_type);
+                       if (frame == NULL || origin_w == 0 || origin_h == 0) {
+                               media_svc_error("Failed to get frame data");
+                               goto ERROR;
+                       }
+
+                       ret = __media_svc_get_proper_thumb_size(origin_w, origin_h, &thumb_w, &thumb_h);
+                       if (thumb_w <= 0 || thumb_h <= 0) {
+                               media_svc_error("Failed to get thumb size");
+                               goto ERROR;
+                       }
+
+                       media_svc_debug("Origin:W[%d] H[%d] Proper:W[%d] H[%d]", origin_w, origin_h, thumb_w, thumb_h);
+
+                       ret = mm_util_create_handle(&img, (unsigned char *)frame, origin_w, origin_h, size, MM_UTIL_COLOR_RGB24);
+                       if (origin_w > thumb_w || origin_h > thumb_h) {
+                               if (rot_type != MM_UTIL_ROTATE_0) {
+                                       ret = mm_util_resize_B_B(img, thumb_w, thumb_h, &resize_img);
+                                       if (ret != MM_UTIL_ERROR_NONE)
+                                               goto ERROR;
+                                       ret = mm_util_rotate_B_P(resize_img, rot_type, thumb_path);
+                               } else {
+                                       ret = mm_util_resize_B_P(img, thumb_w, thumb_h, thumb_path);
+                               }
+                       } else {
+                               if (rot_type != MM_UTIL_ROTATE_0)
+                                       ret = mm_util_rotate_B_P(img, rot_type, thumb_path);
+                               else
+                                       ret = mm_util_resize_B_P(img, origin_w, origin_h, thumb_path);
+                       }
+               }
+
+       ERROR:
+               mm_util_destroy_handle(img);
+               mm_util_destroy_handle(resize_img);
+               mm_file_destroy_content_attrs(content);
+
+               if (ret != MS_MEDIA_ERR_NONE)
+                       return MS_MEDIA_ERR_INTERNAL;
+
+               return MS_MEDIA_ERR_NONE;
        }
 
        return ret;
@@ -2214,10 +2015,10 @@ int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
 
        ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
        if (ret == MS_MEDIA_ERR_NONE) {
-               if (STRING_VALID(pinyinname[0].pinyin_name))
+               if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
                        *pinyin_str = strdup(pinyinname[0].pinyin_name);
                else
-                       *pinyin_str = strdup(src_str);  //Return Original Non China Character
+                       *pinyin_str = strdup(src_str);  /* Return Original Non China Character */
        }
 
        _media_svc_pinyin_free(pinyinname, size);
@@ -2227,78 +2028,26 @@ int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
 
 bool _media_svc_check_pinyin_support(void)
 {
-       /*Check CSC*/
-       return TRUE;
-}
-
-int _media_svc_get_ini_value()
-{
-       if (g_ini_value == -1) {
-               dictionary *dict = NULL;
-
-               dict = iniparser_load(MEDIA_SVC_INI_DEFAULT_PATH);
-               if (!dict) {
-                       media_svc_error("%s load failed", MEDIA_SVC_INI_DEFAULT_PATH);
-                       return -1;
-               }
-
-               MEDIA_SVC_INI_GET_INT(dict, "media-content-config:thumbnail_activation", g_ini_value, 0);
-               iniparser_freedict(dict);
-       }
-       media_svc_debug("Thumb-server activation level = %d", g_ini_value);
+       /* Check CSC : TODO : need to check CSC */
 
-       return g_ini_value;
+       /* Check content.filter.pinyin feature */
+       return __media_svc_check_support_pinyin();
 }
 
-char* _media_svc_get_title_from_path(const char *path)
+int _media_svc_get_media_type(const char *path, int *mediatype)
 {
-       char *filename = NULL;
-       char *title = NULL;
-       char    *ext = NULL;
-       int filename_len = -1;
-       int new_title_len = -1;
-
-       if (!path) {
-               media_svc_error("path is NULL");
-               return NULL;
-       }
-
-       filename = g_path_get_basename(path);
-       if (!STRING_VALID(filename)) {
-               media_svc_error("wrong file name");
-               SAFE_FREE(filename);
-               return NULL;
-       }
-
-       filename_len = strlen(filename);
-
-       ext = g_strrstr(filename, ".");
-       if (!ext) {
-               media_svc_error("there is no file extention");
-               return filename;
-       }
-
-       new_title_len = filename_len - strlen(ext);
-       if (new_title_len < 1) {
-               media_svc_error("title length is zero");
-               SAFE_FREE(filename);
-               return NULL;
-       }
-
-       title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
+       int ret = MS_MEDIA_ERR_NONE;
+       char mime_type[256] = {0};
+       media_svc_media_type_e media_type =  MEDIA_SVC_MEDIA_TYPE_OTHER;
 
-       SAFE_FREE(filename);
+       ret = __media_svc_get_mime_type(path, mime_type);
+       if (ret == MS_MEDIA_ERR_NONE)
+               __media_svc_get_media_type(path, mime_type, &media_type);
+       else
+               media_svc_error("__media_svc_get_mime_type failed");
 
-       media_svc_debug("extract title is [%s]", title);
+       *mediatype = media_type;
 
-       return title;
+       return ret;
 }
 
-#define BUF_LENGHT 256
-
-void _media_svc_print_stderror(void)
-{
-       char buf[BUF_LENGHT] = {0,};
-
-       media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));
-}