4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
26 #define __USE_XOPEN /* needed for strptime */
33 #include <system_info.h>
35 #include <glib/gstdio.h>
41 #include <libexif/exif-data.h>
42 #include <media-util.h>
43 #include <uuid/uuid.h>
44 #include <mm_util_magick.h>
45 #include <media-thumbnail.h>
46 #include "media-util-err.h"
47 #include "media-svc-util.h"
48 #include "media-svc-db-utils.h"
49 #include "media-svc-debug.h"
50 #include "media-svc-env.h"
51 #include "media-svc-hash.h"
52 #include "media-svc-album.h"
53 #include "media-svc-localize_ch.h"
56 #include <libxml/xmlmemory.h>
57 #include <libxml/parser.h>
58 #include <libxml/HTMLparser.h>
60 #define MEDIA_SVC_FILE_EXT_LEN_MAX 6 /**< Maximum file ext lenth*/
62 #define MUSIC_MIME_NUM 29
63 #define SOUND_MIME_NUM 2
64 #define MIME_TYPE_LENGTH 255
65 #define THUMB_HASH_LEN 256
66 #define MIME_LENGTH 50
67 #define MEDIA_SVC_ARTWORK_SIZE 2000
68 #define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
69 #define IMAGE_PREFIX "image/"
70 #define IMAGE_PREFIX_LEN 6
71 #define AUDIO_PREFIX "audio/"
72 #define AUDIO_PREFIX_LEN 6
73 #define VIDEO_PREFIX "video/"
74 #define VIDEO_PREFIX_LEN 6
76 #define MEDIA_SVC_DEFAULT_GPS_VALUE -200 /**< Default GPS Value*/
78 #define MEDIA_SVC_PDF_TAG_TAIL_LEN 12
79 #define MEDIA_SVC_PDF_BUF_SIZE 256
81 enum Exif_Orientation {
93 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
94 /*known mime types of normal files*/
100 /* known mime types of drm files*/
102 "x-mp3", /*alias of audio/mpeg*/
103 "x-mpeg", /*alias of audio/mpeg*/
105 "x-ogg", /*alias of audio/ogg*/
106 "vnd.ms-playready.media.pya:*.pya", /*playready*/
109 "x-m4a", /*alias of audio/mp4*/
110 /* below mimes are rare*/
120 "x-vorbis", /*alias of audio/x-vorbis+ogg*/
121 "vorbis", /*alias of audio/x-vorbis+ogg*/
123 "x-mp2", /*alias of audio/mp2*/
124 "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
125 "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
129 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
130 "application/x-smaf",
134 char *_media_info_generate_uuid(void)
137 char uuid_unparsed[37];
140 uuid_generate(uuid_value);
141 uuid_unparse(uuid_value, uuid_unparsed);
143 if (strlen(uuid_unparsed) < 36) {
144 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
148 return g_strdup(uuid_unparsed);
151 /* GPS information is not ExifTag member */
152 static int __media_svc_get_exif_gps_double(ExifData *ed, double *value, long tagtype)
155 char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
156 double tmp_arr[3] = { 0.0, 0.0, 0.0 };
158 char **tmp_split = NULL;
160 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
161 media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
163 entry = exif_data_get_entry(ed, tagtype);
164 media_svc_retv_if(!entry, MS_MEDIA_ERR_INTERNAL);
166 exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
167 gps_buf[strlen(gps_buf)] = '\0';
169 tmp_split = g_strsplit(gps_buf, ",", -1);
170 if (g_strv_length(tmp_split) != 3) {
171 g_strfreev(tmp_split);
172 media_svc_error("Wrong GPS format");
173 return MS_MEDIA_ERR_INTERNAL;
176 for (i = 0; i < 3; i++)
177 tmp_arr[i] = g_strtod(tmp_split[i], NULL);
179 g_strfreev(tmp_split);
181 *value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
183 return MS_MEDIA_ERR_NONE;
186 static int __media_svc_get_exif_gps_str(ExifData *ed, char *value, long tagtype)
190 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
191 media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
193 entry = exif_data_get_entry(ed, tagtype);
195 exif_entry_get_value(entry, value, MEDIA_SVC_METADATA_LEN_MAX);
196 value[strlen(value)] = '\0';
199 return MS_MEDIA_ERR_NONE;
202 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, ExifTag tagtype)
205 ExifByteOrder mByteOrder;
206 ExifRational mRational;
207 long numerator, denominator;
209 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
211 entry = exif_data_get_entry(ed, tagtype);
212 media_svc_retv_if(!entry, MS_MEDIA_ERR_NONE);
215 case EXIF_TAG_ORIENTATION:
216 case EXIF_TAG_PIXEL_X_DIMENSION:
217 case EXIF_TAG_PIXEL_Y_DIMENSION:
218 case EXIF_TAG_ISO_SPEED_RATINGS:
219 media_svc_retvm_if(!i_value, MS_MEDIA_ERR_INVALID_PARAMETER, "i_value is NULL");
221 mByteOrder = exif_data_get_byte_order(ed);
222 short exif_value = exif_get_short(entry->data, mByteOrder);
223 *i_value = (int)exif_value;
225 case EXIF_TAG_EXPOSURE_TIME:
226 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
228 mByteOrder = exif_data_get_byte_order(ed);
229 mRational = exif_get_rational(entry->data, mByteOrder);
230 numerator = mRational.numerator;
231 denominator = mRational.denominator;
232 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
234 case EXIF_TAG_FNUMBER:
235 media_svc_retvm_if(!d_value, MS_MEDIA_ERR_INVALID_PARAMETER, "d_value is NULL");
237 mByteOrder = exif_data_get_byte_order(ed);
238 mRational = exif_get_rational(entry->data, mByteOrder);
239 numerator = mRational.numerator;
240 denominator = mRational.denominator;
242 *d_value = ((numerator*1.0)/(denominator*1.0));
245 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
247 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
248 buf[strlen(buf)] = '\0';
251 return MS_MEDIA_ERR_NONE;
254 static time_t __media_svc_get_timeline_from_str(const char *timstr)
257 time_t modified_t = 0;
261 if (!STRING_VALID(timstr)) {
262 media_svc_error("Invalid Parameter");
266 /*Exif Format : %Y:%m:%d %H:%M:%S
267 Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
268 memset(&t, 0x00, sizeof(struct tm));
272 localtime_r(&rawtime, &timeinfo);
274 if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
275 t.tm_isdst = timeinfo.tm_isdst;
277 media_svc_debug("DST %d", t.tm_isdst);
279 /* If time string has timezone */
280 if (strptime(timstr, "%Y:%m:%d %H:%M:%S %z", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S %z", &t)) {
281 char tim_tmp_str[255] = { 0, };
283 /* ISO8601 Time string format */
284 strftime(tim_tmp_str, 255, "%Y-%m-%dT%H:%M:%S%z", &t);
285 GDateTime *pdatetime = g_date_time_new_from_iso8601(tim_tmp_str, NULL);
287 modified_t = g_date_time_to_unix(pdatetime);
288 g_date_time_unref(pdatetime);
289 media_svc_debug("Calibrated timeval : [%ld][%s]", modified_t, tim_tmp_str);
292 modified_t = mktime(&t);
298 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);
300 media_svc_error("Failed to get timeline : [%s]", timstr);
306 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
312 media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "path is null");
313 media_svc_retvm_if(!mime_type, MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is null");
314 media_svc_retvm_if(!media_type, MS_MEDIA_ERR_INVALID_PARAMETER, "media_type is null");
317 if (strncmp(mime_type, IMAGE_PREFIX, IMAGE_PREFIX_LEN) == 0) {
318 *media_type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
319 return MS_MEDIA_ERR_NONE;
323 if (strncmp(mime_type, AUDIO_PREFIX, AUDIO_PREFIX_LEN) == 0) {
324 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
326 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
327 if (strcmp(mime_type + AUDIO_PREFIX_LEN, music_mime_table[idx]) == 0) {
328 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
333 /* audio/x-mpegurl : .m3u file (playlist file) */
334 if (strcmp(mime_type + AUDIO_PREFIX_LEN, "x-mpegurl") == 0)
335 *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
337 return MS_MEDIA_ERR_NONE;
341 if (strncmp(mime_type, VIDEO_PREFIX, VIDEO_PREFIX_LEN) == 0) {
342 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
344 /*some video files don't have video stream. in this case it is categorize as music. */
345 if (strcmp(mime_type + VIDEO_PREFIX_LEN, "3gpp") == 0 ||
346 strcmp(mime_type + VIDEO_PREFIX_LEN, "mp4") == 0) {
347 if (mm_file_get_stream_info(path, &audio, &video) == FILEINFO_ERROR_NONE) {
348 if (audio > 0 && video == 0)
349 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
353 return MS_MEDIA_ERR_NONE;
357 *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
359 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
360 if (strcmp(mime_type, sound_mime_table[idx]) == 0) {
361 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
362 return MS_MEDIA_ERR_NONE;
366 /*"asf" must check video stream and then categorize in directly. */
367 if (strcmp(mime_type, "application/vnd.ms-asf") == 0) {
368 if (mm_file_get_stream_info(path, &audio, &video) == FILEINFO_ERROR_NONE) {
369 if (audio > 0 && video == 0)
370 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
372 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
375 return MS_MEDIA_ERR_NONE;
378 if (strcmp(mime_type, "application/epub+zip") == 0 || strcmp(mime_type, "application/pdf") == 0)
379 *media_type = MEDIA_SVC_MEDIA_TYPE_BOOK;
381 return MS_MEDIA_ERR_NONE;
385 drm_contentifo is not NULL, if the file is OMA DRM.
386 If the file is not OMA DRM, drm_contentinfo must be NULL.
388 static int __media_svc_get_mime_type(const char *path, char *mimetype)
390 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
392 /*in case of normal files or failure to get mime in drm */
393 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
394 media_svc_error("aul_get_mime_from_file fail");
395 return MS_MEDIA_ERR_INTERNAL;
398 return MS_MEDIA_ERR_NONE;
401 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
405 for (i = strlen(file_path); i >= 0; i--) {
406 if (file_path[i] == '.') {
407 SAFE_STRLCPY(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
411 if (file_path[i] == '/')
417 static int __media_svc_safe_atoi(char *buffer, int *si)
421 media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
423 const long sl = strtol(buffer, &end, 10);
425 media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
426 media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
427 media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
428 media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
429 media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
433 return MS_MEDIA_ERR_NONE;
436 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
438 int ret = MS_MEDIA_ERR_NONE;
440 char *thumb_path = NULL;
441 long bsize_kbytes = 0;
442 GError *error = NULL;
444 media_svc_retvm_if(!image || size == 0, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid image");
445 media_svc_sec_debug("start save image, path [%s] image size [%d]", image_path, size);
447 ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
448 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path error");
450 ret = statfs(thumb_path, &fs);
452 media_svc_retvm_if(ret == -1, MS_MEDIA_ERR_INTERNAL, "statfs failed");
454 bsize_kbytes = fs.f_bsize >> 10;
455 media_svc_retvm_if((bsize_kbytes * fs.f_bavail) < 1024, MS_MEDIA_ERR_NOT_ENOUGH_SPACE, "Not enough space");
457 if (!g_file_set_contents(image_path, (const gchar *)image, (gssize)size, &error)) {
458 media_svc_error("g_file_set_contents faild:%s", error->message);
460 return MS_MEDIA_ERR_INTERNAL;
463 return MS_MEDIA_ERR_NONE;
466 static char *__media_svc_get_title_from_filepath(const char *path)
468 char *filename = NULL;
470 char *last_dot = NULL;
472 media_svc_retvm_if(!STRING_VALID(path), NULL, "Invalid path");
474 filename = g_path_get_basename(path);
476 last_dot = strrchr(filename, '.');
478 title = g_strndup(filename, last_dot - filename);
484 media_svc_debug("extract title is [%s]", title);
489 void _media_svc_remove_file(const char *path)
491 if (!STRING_VALID(path))
494 if (remove(path) != 0)
495 media_svc_stderror("fail to remove file result");
498 int _media_svc_get_thumbnail_path(char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
500 int ret = MS_MEDIA_ERR_NONE;
501 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
502 char hash[THUMB_HASH_LEN] = {0, };
503 char *thumb_dir = NULL;
505 ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
506 media_svc_retvm_if(!STRING_VALID(thumb_dir), MS_MEDIA_ERR_INTERNAL, "ms_user_get_root_thumb_store_path failed");
508 if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
509 media_svc_error("Wrong path[%s]", thumb_dir);
511 return MS_MEDIA_ERR_INTERNAL;
514 memset(file_ext, 0, sizeof(file_ext));
515 if (!__media_svc_get_file_ext(pathname, file_ext))
516 media_svc_error("get file ext fail");
518 ret = mb_svc_generate_hash_code(pathname, hash, THUMB_HASH_LEN);
519 if (ret != MS_MEDIA_ERR_NONE) {
520 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
522 return MS_MEDIA_ERR_INTERNAL;
526 /* 'img_format' is mime-type */
527 if (!g_str_has_prefix(img_format, IMAGE_PREFIX) || strlen(img_format) == IMAGE_PREFIX_LEN) {
528 media_svc_error("Not proper img format");
530 return MS_MEDIA_ERR_INTERNAL;
533 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, img_format + IMAGE_PREFIX_LEN);
535 if (strcasecmp(file_ext, "PNG") == 0)
536 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
538 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
543 return MS_MEDIA_ERR_NONE;
546 int _media_svc_get_file_time(const char *full_path)
548 struct stat statbuf = { 0, };
550 if (stat(full_path, &statbuf) == -1) {
551 media_svc_stderror("stat fails.");
555 return statbuf.st_mtime;
558 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, bool refresh)
560 int ret = MS_MEDIA_ERR_NONE;
561 bool drm_type = false;
562 char mime_type[256] = {0, };
563 media_svc_media_type_e media_type;
565 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
566 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
568 content_info->path = g_strdup(path);
569 media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
572 memset(&st, 0, sizeof(struct stat));
573 if (stat(path, &st) == 0) {
574 content_info->modified_time = st.st_mtime;
575 content_info->timeline = content_info->modified_time;
576 content_info->size = st.st_size;
578 media_svc_stderror("stat failed");
581 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
583 media_svc_debug("refresh");
584 return MS_MEDIA_ERR_NONE;
587 content_info->storage_uuid = g_strdup(storage_id);
588 media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
590 content_info->storage_type = storage_type;
591 time(&content_info->added_time);
593 content_info->media_uuid = _media_info_generate_uuid();
594 media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
596 content_info->file_name = g_path_get_basename(path);
597 media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
599 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
600 if drm_contentinfo is not NULL, the file is OMA DRM.*/
601 ret = __media_svc_get_mime_type(path, mime_type);
602 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
604 media_svc_debug("mime [%s]", mime_type);
605 content_info->is_drm = drm_type;
607 ret = __media_svc_get_media_type(path, mime_type, &media_type);
608 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
610 content_info->mime_type = g_strdup(mime_type);
611 media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
613 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, media_type);
615 content_info->media_type = media_type;
617 return MS_MEDIA_ERR_NONE;
620 static int __image_360_check(const char *path)
623 size_t size = 0, app1_size = 0, exif_app1_xmp_size = 0;
624 unsigned char exif_header[4] = {0, };
625 unsigned char exif_app1[2] = {0, };
626 unsigned char exif_app1_xmp[2] = {0, };
627 unsigned char exif_app1_xmp_t[2] = {0, };
628 GString *xmp_data = NULL;
632 memset(exif_header, 0x00, sizeof(exif_header));
633 memset(exif_app1, 0x00, sizeof(exif_app1));
634 memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
635 memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
637 fp = fopen(path, "rb");
641 size = fread(exif_header, 1, sizeof(exif_header), fp);
642 if (size != sizeof(exif_header))
645 if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
646 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
647 if (size != sizeof(exif_app1))
650 if ((size_t)((exif_app1[0] << 8) | (exif_app1[1])) <= 2)
653 app1_size = (size_t)((exif_app1[0] << 8) | (exif_app1[1])) - 2 ;
654 if (fseek(fp, app1_size, SEEK_CUR) != 0)
657 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
658 if (size != sizeof(exif_app1_xmp))
661 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
662 size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
663 if (size != sizeof(exif_app1_xmp_t))
666 if ((size_t)((exif_app1_xmp_t[0] << 8) | (exif_app1_xmp_t[1])) <= 2)
669 exif_app1_xmp_size = (size_t)((exif_app1_xmp_t[0] << 8) | (exif_app1_xmp_t[1])) - 2;
671 xmp_data = g_string_sized_new(exif_app1_xmp_size);
674 exif_app1_xmp_size--;
681 xmp_data = g_string_append_c(xmp_data, (gchar)fdata);
682 } while (exif_app1_xmp_size > 0);
684 if (strstr(xmp_data->str, "UsePanoramaViewer") &&
685 strstr(xmp_data->str, "True") &&
686 strstr(xmp_data->str, "ProjectionType") &&
687 strstr(xmp_data->str, "equirectangular"))
690 g_string_free(xmp_data, TRUE);
703 static char * __media_svc_get_title(MMHandleType tag, const char *path)
705 int ret = FILEINFO_ERROR_NONE;
711 ret = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
712 if (ret == FILEINFO_ERROR_NONE && size > 0) {
713 while(p && isspace(*p))
720 title = __media_svc_get_title_from_filepath(path);
724 media_svc_error("Can't extract title");
726 return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
729 char * _media_svc_get_title_by_path(const char *path)
731 /* No MMHandleType in media-svc.c */
732 return __media_svc_get_title(NULL, path);
735 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
738 int orient_value = 0;
742 bool has_datetaken = false;
743 double fnumber = 0.0;
747 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
749 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
750 media_svc_retvm_if(content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid media_type [%d]", content_info->media_type);
751 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
753 path = content_info->path;
754 content_info->media_meta.title = __media_svc_get_title(NULL, path);
756 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
757 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
758 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
760 /* Not used. But to preserved the behavior, set MEDIA_SVC_TAG_UNKNOWN. */
761 content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
762 content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
763 content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
764 content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
765 content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
766 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
767 content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
768 content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
770 /* Load an ExifData object from an EXIF file */
771 ed = exif_data_new_from_file(path);
773 media_svc_sec_debug("There is no exif data in [ %s ]", path);
774 goto GET_WIDTH_HEIGHT;
777 content_info->media_meta.is_360 = __image_360_check(path);
779 memset(buf, 0x00, sizeof(buf));
780 if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
781 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
782 if (!g_strcmp0(buf, "S"))
784 content_info->media_meta.latitude = value;
788 memset(buf, 0x00, sizeof(buf));
789 if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
790 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
791 if (!g_strcmp0(buf, "W"))
794 content_info->media_meta.longitude = value;
798 memset(buf, 0x00, sizeof(buf));
799 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
801 content_info->media_meta.description = g_strdup(buf);
803 content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
806 memset(buf, 0x00, sizeof(buf));
807 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
808 if (strlen(buf) > 0) {
809 has_datetaken = true;
810 content_info->media_meta.datetaken = g_strdup(buf);
812 /* This is same as recorded_date */
813 content_info->media_meta.recorded_date = g_strdup(buf);
817 memset(buf, 0x00, sizeof(buf));
818 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
819 if (strlen(buf) > 0) {
820 has_datetaken = true;
821 content_info->media_meta.datetaken = g_strdup(buf);
823 /* This is same as recorded_date */
824 content_info->media_meta.recorded_date = g_strdup(buf);
829 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
830 if (content_info->timeline == 0)
831 content_info->timeline = content_info->modified_time;
833 media_svc_debug("Timeline : %ld", content_info->timeline);
836 memset(buf, 0x00, sizeof(buf));
837 /* Get exposure_time value from exif. */
838 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
840 content_info->media_meta.exposure_time = g_strdup(buf);
843 /* Get fnumber value from exif. */
844 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE)
845 content_info->media_meta.fnumber = fnumber;
847 /* Get iso value from exif. */
848 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE)
849 content_info->media_meta.iso = iso;
851 memset(buf, 0x00, sizeof(buf));
852 /* Get model value from exif. */
853 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
855 content_info->media_meta.model = g_strdup(buf);
858 /* Get orientation value from exif. */
859 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
860 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
861 content_info->media_meta.orientation = orient_value;
864 /* Get width value from exif. */
865 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
867 content_info->media_meta.width = exif_width;
870 /* Get height value from exif. */
871 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
873 content_info->media_meta.height = exif_height;
881 if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
882 /*Get image width, height*/
883 unsigned int img_width = 0;
884 unsigned int img_height = 0;
885 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
887 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
888 if (content_info->media_meta.width == 0)
889 content_info->media_meta.width = img_width;
891 if (content_info->media_meta.height == 0)
892 content_info->media_meta.height = img_height;
895 return MS_MEDIA_ERR_NONE;
898 static char * __media_svc_get_tag_str_value(MMHandleType tag, const char *tag_name)
900 int ret = FILEINFO_ERROR_NONE;
904 ret = mm_file_get_attrs(tag, tag_name, &p, &size, NULL);
905 if (ret == FILEINFO_ERROR_NONE && size > 0)
908 return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
911 int _media_svc_extract_music_metadata_for_update(media_svc_content_info_s *content_info, const char *path)
913 MMHandleType tag = 0;
914 int mmf_error = FILEINFO_ERROR_NONE;
916 content_info->path = g_strdup(path);
918 mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
919 if (mmf_error == FILEINFO_ERROR_NONE) {
920 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
921 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
922 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
923 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
924 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
925 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
926 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
927 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
929 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
932 mm_file_destroy_tag_attrs(tag);
934 return MS_MEDIA_ERR_NONE;
937 int _media_svc_extract_media_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
939 MMHandleType content = 0;
940 MMHandleType tag = 0;
942 unsigned char *image = NULL;
943 unsigned int size = 0;
944 int mmf_error = FILEINFO_ERROR_NONE;
946 int ret = MS_MEDIA_ERR_NONE;
947 int convert_value = 0;
950 /*Get Content Tag attribute ===========*/
951 if (content_info->storage_type == MS_USER_STORAGE_EXTERNAL_USB)
952 mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
954 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
956 if (mmf_error == FILEINFO_ERROR_NONE) {
957 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
958 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
959 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
960 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
961 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
962 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
963 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
964 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
965 content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
967 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
968 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
969 if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
970 /*Creation time format is 2013-01-01 00:00:00 +0000. change it to 2013:01:01 00:00:00 +0000 like exif time format*/
971 char *p_value = g_strdelimit(g_strdup(p), "-", ':');
972 content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
975 content_info->media_meta.recorded_date = g_strdup(p);
978 if (STRING_VALID(content_info->media_meta.recorded_date)) {
979 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
980 if (content_info->timeline == 0)
981 content_info->timeline = content_info->modified_time;
983 /* This is same as datetaken */
984 /* Remove compensation string */
985 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
986 content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
987 g_free(content_info->media_meta.recorded_date);
988 content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
990 content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
995 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
996 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
997 if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
998 content_info->media_meta.year = g_strdup(p);
1001 if (!content_info->media_meta.year)
1002 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1004 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RATING, &p, &size, NULL);
1005 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1006 if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1007 content_info->media_meta.rating = convert_value;
1009 content_info->media_meta.rating = 0;
1012 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1014 /*Do not extract artwork for the USB Storage content*/
1015 if (content_info->storage_type != MS_USER_STORAGE_EXTERNAL_USB) {
1016 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1017 if (mmf_error != FILEINFO_ERROR_NONE)
1018 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1020 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1021 if (mmf_error != FILEINFO_ERROR_NONE)
1022 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1024 if (image != NULL && size > 0) {
1025 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1026 int artwork_mime_size = -1;
1028 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1029 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1030 ret = _media_svc_get_thumbnail_path(thumb_path, content_info->path, p, uid);
1031 if (ret != MS_MEDIA_ERR_NONE) {
1032 media_svc_error("Fail to Get Thumbnail Path");
1034 ret = __media_svc_save_image(image, size, thumb_path, uid);
1035 if (ret != MS_MEDIA_ERR_NONE) {
1036 media_svc_error("Fail to Save Image");
1038 content_info->thumbnail_path = g_strdup(thumb_path);
1045 /*Initialize album_id to 0. below code will set the album_id*/
1046 content_info->album_id = album_id;
1047 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1048 if (ret != MS_MEDIA_ERR_NONE) {
1049 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1050 media_svc_debug("album does not exist. So start to make album art");
1051 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1052 (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1053 ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1055 ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1057 content_info->album_id = album_id;
1060 content_info->album_id = album_id;
1063 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1064 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1065 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1067 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1068 double longitude = 0.0;
1069 double latitude = 0.0;
1070 double altitude = 0.0;
1072 mm_file_get_attrs(tag, MM_FILE_TAG_LONGITUDE, &longitude,
1073 MM_FILE_TAG_LATIDUE, &latitude,
1074 MM_FILE_TAG_ALTIDUE, &altitude,
1077 content_info->media_meta.longitude = (longitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : longitude;
1078 content_info->media_meta.latitude = (latitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : latitude;
1079 content_info->media_meta.altitude = (altitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : altitude;
1081 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1082 if (mmf_error != FILEINFO_ERROR_NONE)
1085 media_svc_debug("CDIS : %d", cdis_value);
1087 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1088 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1089 content_info->media_meta.orientation = atoi(p);
1091 content_info->media_meta.orientation = 0;
1092 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1096 mmf_error = mm_file_destroy_tag_attrs(tag);
1097 if (mmf_error != FILEINFO_ERROR_NONE)
1098 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1100 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1101 content_info->album_id = album_id;
1104 /*Get Content attribute ===========*/
1105 if (cdis_value == 1)
1106 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1108 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1110 media_svc_retvm_if(mmf_error != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_NONE, "mm_file_create_content_attrs failed");
1112 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1113 int audio_bitrate = 0;
1114 int video_bitrate = 0;
1116 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1117 MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate,
1118 MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate,
1119 MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width,
1120 MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height,
1123 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1125 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1126 MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate,
1127 MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate,
1128 MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel,
1129 MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample,
1133 mm_file_destroy_content_attrs(content);
1135 return MS_MEDIA_ERR_NONE;
1138 static gchar * __media_svc_get_zipfile_data(zip_t *z, const char *fname)
1141 zip_int64_t index_num = 0;
1142 zip_file_t *file = NULL;
1143 zip_stat_t sb = {0, };
1146 media_svc_retvm_if(!z, NULL, "z is NULL");
1147 media_svc_retvm_if(!fname, NULL, "fname is NULL");
1149 index_num = zip_name_locate(z, fname, ZIP_FL_NOCASE);
1150 media_svc_retvm_if(index_num == -1, NULL, "fname is not exists [%s]", fname);
1152 err = zip_stat_index(z, index_num, ZIP_STAT_SIZE, &sb);
1153 media_svc_retvm_if(err == -1, NULL, "zip_stat_index failed");
1155 file = zip_fopen_index(z, index_num, ZIP_FL_UNCHANGED);
1156 media_svc_retvm_if(!file, NULL, "zip_fopen_index failed");
1158 buf = g_malloc0(sb.size);
1160 err = zip_fread(file, buf, sb.size);
1171 static xmlNodePtr __media_svc_find_node(xmlNodePtr node, const char *key)
1173 xmlNodePtr tmp = NULL;
1175 media_svc_retvm_if(!node, NULL, "node is NULL");
1176 media_svc_retvm_if(!key, NULL, "key is NULL");
1178 for (tmp = node->children; tmp; tmp = tmp->next) {
1179 if (xmlIsBlankNode(tmp))
1182 if (g_str_has_suffix((gchar *)tmp->name, key))
1189 static char * __media_svc_remove_escape_c(const char *value)
1195 media_svc_retv_if(!value, NULL);
1197 len = strlen(value);
1199 for (i = 0; i < len; i++) {
1200 if (value[i] != 10 && value[i] != 32) { // 10='\n' 32=' '
1208 end = end - start + 1;
1210 return g_strndup(value + start, end);
1213 static char * __media_svc_find_and_get_value(xmlNodePtr node, const char *key)
1215 xmlNodePtr tmp = NULL;
1216 char *tmp_res = NULL;
1219 media_svc_retvm_if(!node, NULL, "node is NULL");
1220 media_svc_retvm_if(!key, NULL, "key is NULL");
1222 for (tmp = node->children; tmp; tmp = tmp->next) {
1223 if (xmlIsBlankNode(tmp))
1226 if (tmp->children) {
1227 tmp_res = __media_svc_find_and_get_value(tmp, key);
1229 res = __media_svc_remove_escape_c(tmp_res);
1235 if (g_str_has_suffix((gchar *)tmp->name, key))
1236 return (char *)xmlNodeGetContent(tmp);
1242 static gboolean __media_svc_get_epub_root_file(zip_t *z, char **opf_file)
1245 xmlDocPtr doc = NULL;
1246 xmlNodePtr node = NULL;
1248 media_svc_retvm_if(!z, FALSE, "z is NULL");
1249 media_svc_retvm_if(!opf_file, FALSE, "opf_file is NULL");
1251 buf = __media_svc_get_zipfile_data(z, "META-INF/container.xml");
1252 media_svc_retvm_if(!buf, FALSE, "buf is NULL");
1254 doc = xmlParseDoc((const xmlChar *)buf);
1256 media_svc_retvm_if(!doc, FALSE, "doc is NULL");
1258 node = xmlDocGetRootElement(doc);
1259 node = __media_svc_find_node(node, "rootfiles");
1260 node = __media_svc_find_node(node, "rootfile");
1262 *opf_file = (char *)xmlGetProp(node, (const xmlChar *)"full-path");
1263 media_svc_sec_debug("OPF [%s]", *opf_file);
1269 static gboolean __media_svc_get_xml_metadata(const xmlChar *buffer, gboolean is_pdf, media_svc_content_info_s *content_info)
1271 xmlDocPtr doc = NULL;
1272 xmlNodePtr root = NULL;
1274 media_svc_retvm_if(!buffer, FALSE, "buffer is NULL");
1275 media_svc_retvm_if(!content_info, FALSE, "content_info is NULL");
1277 doc = xmlParseDoc(buffer);
1278 media_svc_retv_if(!doc, FALSE);
1280 root = xmlDocGetRootElement(doc);
1286 content_info->media_meta.title = __media_svc_find_and_get_value(root, "title");
1287 if (is_pdf && !content_info->media_meta.title) {
1292 content_info->media_meta.composer = __media_svc_find_and_get_value(root, "creator");
1293 if (!content_info->media_meta.composer)
1294 content_info->media_meta.composer = __media_svc_find_and_get_value(root, "author");
1296 content_info->media_meta.copyright = __media_svc_find_and_get_value(root, "publisher");
1297 content_info->media_meta.recorded_date = __media_svc_find_and_get_value(root, "date");
1298 content_info->media_meta.genre = __media_svc_find_and_get_value(root, "subject");
1299 content_info->media_meta.description = __media_svc_find_and_get_value(root, "description");
1306 static int __media_svc_get_epub_metadata(media_svc_content_info_s *content_info)
1311 char *opf_path = NULL;
1313 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1316 z = zip_open(content_info->path, ZIP_RDONLY, &err);
1317 media_svc_retvm_if(err == -1, MS_MEDIA_ERR_INTERNAL, "zip_open failed");
1319 //2. find and read opf file
1320 if (!__media_svc_get_epub_root_file(z, &opf_path)) {
1321 media_svc_error("__media_svc_get_epub_root_file failed");
1323 return MS_MEDIA_ERR_INTERNAL;
1327 buf = __media_svc_get_zipfile_data(z, opf_path);
1330 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INTERNAL, "__media_svc_get_zipfile_data failed");
1332 if (!__media_svc_get_xml_metadata((const xmlChar *)buf, FALSE, content_info))
1333 media_svc_error("__media_svc_get_xml_metadata failed");
1337 return MS_MEDIA_ERR_NONE;
1340 static int __media_svc_get_pdf_metadata(media_svc_content_info_s *content_info)
1346 int search_limit = 0;
1347 char tmp[MEDIA_SVC_PDF_BUF_SIZE + 1] = {0, };
1348 gchar *meta_buf = NULL;
1351 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1352 media_svc_retvm_if(content_info->size < 256, MS_MEDIA_ERR_INTERNAL, "open failed");
1354 fd = open(content_info->path, O_RDONLY);
1355 media_svc_retvm_if(fd < 0, MS_MEDIA_ERR_INTERNAL, "open failed");
1357 search_limit = content_info->size - MEDIA_SVC_PDF_TAG_TAIL_LEN;
1359 while (cur_pos <= search_limit) {
1360 lseek(fd, cur_pos, SEEK_SET);
1362 if (read(fd, &tmp, MEDIA_SVC_PDF_BUF_SIZE) != MEDIA_SVC_PDF_BUF_SIZE) {
1363 media_svc_error("read failed");
1367 //1.Find <x:xmpmeta .. </x:xmpmeta> block
1368 if (start_pos == 0 && (found = strstr(tmp, "<x:xmpmeta"))) {
1369 start_pos = cur_pos + (found - tmp);
1370 // media_svc_error("FIND START_POS[%d]", start_pos);
1375 if (start_pos != 0 && (found = strstr(tmp, "</x:xmpmeta>"))) {
1376 end_pos = cur_pos + (found - tmp) + MEDIA_SVC_PDF_TAG_TAIL_LEN;
1377 // media_svc_error("FIND END_POS[%d]", end_pos);
1381 //2.get metadata using xml parser
1382 if (start_pos && end_pos) {
1383 meta_buf = g_malloc0(end_pos - start_pos + 1);
1385 lseek(fd, start_pos, SEEK_SET);
1386 if (read(fd, meta_buf, end_pos - start_pos) != end_pos - start_pos)
1389 if (__media_svc_get_xml_metadata((const xmlChar *)meta_buf, TRUE, content_info)) {
1407 return MS_MEDIA_ERR_NONE;
1410 int _media_svc_extract_book_metadata(media_svc_content_info_s *content_info)
1412 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content info is NULL");
1414 if (g_str_has_suffix(content_info->mime_type, "epub+zip"))
1415 return __media_svc_get_epub_metadata(content_info);
1417 return __media_svc_get_pdf_metadata(content_info);
1420 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1422 media_svc_retm_if(!content_info, "content info is NULL");
1424 /* Delete media_svc_content_info_s */
1425 g_free(content_info->media_uuid);
1426 g_free(content_info->path);
1427 g_free(content_info->file_name);
1428 g_free(content_info->mime_type);
1429 g_free(content_info->folder_uuid);
1430 g_free(content_info->thumbnail_path);
1431 g_free(content_info->storage_uuid);
1433 /* Delete media_svc_content_meta_s */
1434 g_free(content_info->media_meta.title);
1435 g_free(content_info->media_meta.album);
1436 g_free(content_info->media_meta.artist);
1437 g_free(content_info->media_meta.album_artist);
1438 g_free(content_info->media_meta.genre);
1439 g_free(content_info->media_meta.composer);
1440 g_free(content_info->media_meta.year);
1441 g_free(content_info->media_meta.recorded_date);
1442 g_free(content_info->media_meta.copyright);
1443 g_free(content_info->media_meta.track_num);
1444 g_free(content_info->media_meta.description);
1445 g_free(content_info->media_meta.datetaken);
1446 g_free(content_info->media_meta.exposure_time);
1447 g_free(content_info->media_meta.model);
1449 g_free(content_info->file_name_pinyin);
1450 g_free(content_info->media_meta.title_pinyin);
1451 g_free(content_info->media_meta.album_pinyin);
1452 g_free(content_info->media_meta.artist_pinyin);
1453 g_free(content_info->media_meta.album_artist_pinyin);
1454 g_free(content_info->media_meta.genre_pinyin);
1455 g_free(content_info->media_meta.composer_pinyin);
1456 g_free(content_info->media_meta.copyright_pinyin);
1457 g_free(content_info->media_meta.description_pinyin);
1460 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
1462 int ret = MS_MEDIA_ERR_NONE;
1464 media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
1465 media_svc_retvm_if(!thumb_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1466 media_svc_retvm_if(!g_file_test(path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "File doesn't exist[%s]", path);
1468 ms_user_storage_type_e store_type = -1;
1469 ret = ms_user_get_storage_type(uid, path, &store_type);
1471 if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
1472 media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
1473 return MS_MEDIA_ERR_INVALID_PARAMETER;
1476 media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1479 ret = _media_svc_get_thumbnail_path(thumb_path, path, NULL, uid);
1480 if (ret != MS_MEDIA_ERR_NONE) {
1481 media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
1482 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1487 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1488 return create_image_thumbnail_to_file(path, CONTENT_THUMB_DEFAULT_WIDTH, CONTENT_THUMB_DEFAULT_HEIGHT, thumb_path, true);
1490 return create_video_thumbnail_to_file(path, CONTENT_THUMB_DEFAULT_WIDTH, CONTENT_THUMB_DEFAULT_HEIGHT, thumb_path, true);
1494 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1496 int ret = MS_MEDIA_ERR_NONE;
1498 pinyin_name_s *pinyinname = NULL;
1500 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1501 media_svc_retvm_if(pinyin_str == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "pinyin_str is NULL");
1505 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1506 if (ret == MS_MEDIA_ERR_NONE) {
1507 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1508 *pinyin_str = g_strdup(pinyinname[0].pinyin_name);
1510 *pinyin_str = g_strdup(src_str); /* Return Original Non China Character */
1513 _media_svc_pinyin_free(pinyinname, size);
1518 bool _media_svc_check_pinyin_support(void)
1520 int ret = SYSTEM_INFO_ERROR_NONE;
1521 bool is_supported = false;
1522 static int media_svc_pinyin_support = -1;
1524 if (media_svc_pinyin_support == -1) {
1525 ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
1526 if (ret != SYSTEM_INFO_ERROR_NONE) {
1527 media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
1531 media_svc_pinyin_support = is_supported;
1534 return media_svc_pinyin_support;
1537 int _media_svc_get_media_type(const char *path, int *mediatype)
1539 int ret = MS_MEDIA_ERR_NONE;
1540 char mime_type[256] = {0};
1541 media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1543 media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1545 ret = __media_svc_get_mime_type(path, mime_type);
1546 if (ret == MS_MEDIA_ERR_NONE)
1547 __media_svc_get_media_type(path, mime_type, &media_type);
1549 media_svc_error("__media_svc_get_mime_type failed");
1551 *mediatype = media_type;
1556 bool _media_svc_is_valid_storage_type(ms_user_storage_type_e storage_type)
1558 switch (storage_type) {
1559 case MS_USER_STORAGE_INTERNAL:
1560 case MS_USER_STORAGE_EXTERNAL:
1561 case MS_USER_STORAGE_EXTERNAL_USB:
1564 media_svc_error("storage type is incorrect[%d]", storage_type);