Use get_image_info instead
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-util.c
index 85c70fb..8fbb2a8 100644 (file)
 #include <libexif/exif-data.h>
 #include <media-util.h>
 #include <uuid/uuid.h>
-#include <mm_util_magick.h>
 #include <media-thumbnail.h>
 #include "media-svc-util.h"
 #include "media-svc-db-utils.h"
 #include "media-svc-debug.h"
 #include "media-svc-env.h"
-#include "media-svc-hash.h"
 #include "media-svc-album.h"
 /*For ebook metadata */
 #include <zip.h>
@@ -48,7 +46,6 @@
 
 #define MUSIC_MIME_NUM 29
 #define SOUND_MIME_NUM 2
-#define THUMB_HASH_LEN 256
 #define MIME_LENGTH 50
 #define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
 #define IMAGE_PREFIX "image/"
@@ -320,22 +317,18 @@ static int __media_svc_save_image(unsigned char *image, unsigned int size, char
        return MS_MEDIA_ERR_NONE;
 }
 
-static char *__media_svc_get_title_from_filepath(const char *path)
+static char *__media_svc_get_title_from_filename(const char *filename)
 {
-       char *filename = NULL;
        char *title = NULL;
        char *last_dot = NULL;
 
-       media_svc_retvm_if(!STRING_VALID(path), NULL, "Invalid path");
-
-       filename = g_path_get_basename(path);
+       media_svc_retvm_if(!STRING_VALID(filename), g_strdup(MEDIA_SVC_TAG_UNKNOWN), "Invalid path");
 
        last_dot = strrchr(filename, '.');
        if (last_dot) {
                title = g_strndup(filename, last_dot - filename);
-               g_free(filename);
        } else {
-               title = filename;
+               title = g_strdup(filename);
        }
 
        media_svc_debug("extract title is [%s]", title);
@@ -352,38 +345,28 @@ void _media_svc_remove_file(const char *path)
                media_svc_stderror("fail to remove file result");
 }
 
-int _media_svc_get_thumbnail_path(char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
+static int __media_svc_get_thumbnail_path(char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
        char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
-       char hash[THUMB_HASH_LEN] = {0, };
-       char *thumb_dir = NULL;
+       g_autofree gchar *hash = NULL;
+       g_autofree gchar *thumb_dir = NULL;
 
        ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
-       media_svc_retvm_if(!STRING_VALID(thumb_dir), MS_MEDIA_ERR_INTERNAL, "ms_user_get_root_thumb_store_path failed");
-
-       if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
-               media_svc_error("Wrong path[%s]", thumb_dir);
-               g_free(thumb_dir);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
+       media_svc_retvm_if(!STRING_VALID(thumb_dir), ret, "ms_user_get_root_thumb_store_path failed");
+       media_svc_retvm_if(!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR), MS_MEDIA_ERR_INTERNAL, "Not a directory");
 
        memset(file_ext, 0, sizeof(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, THUMB_HASH_LEN);
-       if (ret != MS_MEDIA_ERR_NONE) {
-               media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
-               g_free(thumb_dir);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
+       hash = g_compute_checksum_for_string(G_CHECKSUM_MD5, pathname, -1);
+       media_svc_retvm_if(!hash, MS_MEDIA_ERR_INTERNAL, "Failed to create hashname");
 
        if (img_format) {
                /* 'img_format' is mime-type */
                if (!g_str_has_prefix(img_format, IMAGE_PREFIX) || strlen(img_format) == IMAGE_PREFIX_LEN) {
                        media_svc_error("Not proper img format");
-                       g_free(thumb_dir);
                        return MS_MEDIA_ERR_INTERNAL;
                }
 
@@ -395,8 +378,6 @@ int _media_svc_get_thumbnail_path(char *thumb_path, const char *pathname, const
                        snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
        }
 
-       g_free(thumb_dir);
-
        return MS_MEDIA_ERR_NONE;
 }
 
@@ -468,12 +449,11 @@ int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char
        return MS_MEDIA_ERR_NONE;
 }
 
-static char * __media_svc_get_title(MMHandleType tag, const char *path)
+static char * __media_svc_get_title(MMHandleType tag, const char *filename)
 {
        int ret = FILEINFO_ERROR_NONE;
        char *p = NULL;
        int size = 0;
-       char *title = NULL;
 
        if (tag) {
                ret = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
@@ -485,19 +465,13 @@ static char * __media_svc_get_title(MMHandleType tag, const char *path)
                }
        }
 
-       title = __media_svc_get_title_from_filepath(path);
-       if (title)
-               return title;
-
-       media_svc_error("Can't extract title");
-
-       return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+       return __media_svc_get_title_from_filename(filename);
 }
 
-char * _media_svc_get_title_by_path(const char *path)
+char * _media_svc_get_title_from_filename(const char *filename)
 {
        /* No MMHandleType in media-svc.c */
-       return __media_svc_get_title(NULL, path);
+       return __media_svc_get_title_from_filename(filename);
 }
 
 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
@@ -516,7 +490,7 @@ int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
        media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
 
        path = content_info->path;
-       content_info->media_meta.title = __media_svc_get_title(NULL, path);
+       content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
 
        /* Not used. But to preserved the behavior, set MEDIA_SVC_TAG_UNKNOWN. */
        content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
@@ -538,6 +512,9 @@ int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
                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);
                }
        }
 
@@ -546,9 +523,15 @@ int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
                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 (content_info->media_meta.recorded_date == NULL)
+               content_info->media_meta.recorded_date = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+
        /* Get orientation value from exif. */
        if (__media_svc_get_exif_info(ed, NULL, &orient_value, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
                if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
@@ -571,14 +554,14 @@ int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
                exif_data_unref(ed);
 
 GET_WIDTH_HEIGHT:
-
        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;
-               mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
 
-               mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
+               if (get_image_info(path, &img_width, &img_height) != THUMB_OK)
+                       return MS_MEDIA_ERR_NONE;
+
                if (content_info->media_meta.width == 0)
                        content_info->media_meta.width = img_width;
 
@@ -621,12 +604,28 @@ int _media_svc_extract_audio_metadata(sqlite3 *handle, bool is_direct, media_svc
                mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
 
        if (mmf_error == FILEINFO_ERROR_NONE) {
-               content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
+               content_info->media_meta.title = __media_svc_get_title(tag, content_info->file_name);
                content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
                content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
                content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
                content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
                content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
+               content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
+
+               mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
+               if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
+                       if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
+                               /*Creation time format is 20130101 00:00:00 +0000. change it to 2013:01:01 00:00:00  +0000 like exif time format*/
+                               char *p_value = g_strdelimit(g_strdup(p), "-", ':');
+                               content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
+                               g_free(p_value);
+                       } else {
+                               content_info->media_meta.recorded_date = g_strdup(p);
+                       }
+               }
+
+               if (content_info->media_meta.recorded_date == NULL)
+                       content_info->media_meta.recorded_date = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
 
                mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
                if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
@@ -653,7 +652,7 @@ int _media_svc_extract_audio_metadata(sqlite3 *handle, bool is_direct, media_svc
 
                                mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
                                if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
-                                       ret = _media_svc_get_thumbnail_path(thumb_path, content_info->path, p, uid);
+                                       ret = __media_svc_get_thumbnail_path(thumb_path, content_info->path, p, uid);
                                        if (ret != MS_MEDIA_ERR_NONE) {
                                                media_svc_error("Fail to Get Thumbnail Path");
                                        } else {
@@ -690,7 +689,7 @@ int _media_svc_extract_audio_metadata(sqlite3 *handle, bool is_direct, media_svc
                if (mmf_error != FILEINFO_ERROR_NONE)
                        media_svc_error("fail to free tag attr - err(%x)", mmf_error);
        }       else {
-               content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
+               content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
                content_info->album_id = album_id;
        }
 
@@ -699,13 +698,51 @@ int _media_svc_extract_audio_metadata(sqlite3 *handle, bool is_direct, media_svc
 
 int _media_svc_extract_video_metadata(media_svc_content_info_s *content_info)
 {
-       content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
+       int mmf_error = FILEINFO_ERROR_NONE;
+       MMHandleType tag = 0;
+       MMHandleType content = 0;
+       char *p = NULL;
+       unsigned int size = 0;
+
+       mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
+
+       if (mmf_error == FILEINFO_ERROR_NONE) {
+               mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
+               if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
+                       if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
+                               /*Creation time format is 20130101 00:00:00 +0000. change it to 2013:01:01 00:00:00  +0000 like exif time format*/
+                               content_info->media_meta.recorded_date = g_strdelimit(g_strdup(p), "-", ':');
+                       } else {
+                               content_info->media_meta.recorded_date = g_strdup(p);
+                       }
+               }
+
+               if (content_info->media_meta.recorded_date == NULL)
+                       content_info->media_meta.recorded_date = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+               content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
+
+               mmf_error = mm_file_destroy_tag_attrs(tag);
+               if (mmf_error != FILEINFO_ERROR_NONE)
+                       media_svc_error("fail to free tag attr - err(%x)", mmf_error);
+       }
+       /*Get Content attribute ===========*/
+       mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
+       media_svc_retvm_if(mmf_error != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_NONE, "mm_file_create_content_attrs failed");
+
+       mm_file_get_attrs(content, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width,
+               MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height,
+               NULL);
+
+       mm_file_destroy_content_attrs(content);
+
+       content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
        content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
        content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
        content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
        content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
        content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
        content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+       content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
        content_info->album_id = 0;
 
        return MS_MEDIA_ERR_NONE;
@@ -874,6 +911,8 @@ static gboolean __media_svc_get_xml_metadata(const xmlChar *buffer, gboolean is_
        if (!content_info->media_meta.artist)
                content_info->media_meta.artist = __media_svc_find_and_get_value(root, "author");
        content_info->media_meta.genre = __media_svc_find_and_get_value(root, "subject");
+       content_info->media_meta.copyright = __media_svc_find_and_get_value(root, "publisher");
+       content_info->media_meta.recorded_date = __media_svc_find_and_get_value(root, "date");
 
        xmlFreeDoc(doc);
 
@@ -1016,6 +1055,8 @@ void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
        g_free(content_info->media_meta.album_artist);
        g_free(content_info->media_meta.genre);
        g_free(content_info->media_meta.year);
+       g_free(content_info->media_meta.recorded_date);
+       g_free(content_info->media_meta.copyright);
        g_free(content_info->media_meta.track_num);
        g_free(content_info->media_meta.datetaken);
 }
@@ -1035,10 +1076,10 @@ int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_me
 
        media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
 
-       //1. make hash path
-       ret = _media_svc_get_thumbnail_path(thumb_path, path, NULL, uid);
+       //1. make thumb path
+       ret = __media_svc_get_thumbnail_path(thumb_path, path, NULL, uid);
        if (ret != MS_MEDIA_ERR_NONE) {
-               media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
+               media_svc_error("Failed to create thumbnail path[%d]", ret);
                SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
                return ret;
        }