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"
54 #include "media-svc-util-pdf.h"
55 #include "media-svc-util-epub.h"
56 /*For ebook metadata */
58 #include <libxml/xmlmemory.h>
59 #include <libxml/parser.h>
60 #include <libxml/HTMLparser.h>
62 #define MEDIA_SVC_FILE_EXT_LEN_MAX 6 /**< Maximum file ext lenth*/
64 #define MUSIC_MIME_NUM 29
65 #define SOUND_MIME_NUM 2
66 #define MIME_TYPE_LENGTH 255
67 #define THUMB_HASH_LEN 256
68 #define MIME_LENGTH 50
69 #define MEDIA_SVC_ARTWORK_SIZE 2000
70 #define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
71 #define IMAGE_PREFIX "image/"
72 #define IMAGE_PREFIX_LEN 6
73 #define AUDIO_PREFIX "audio/"
74 #define AUDIO_PREFIX_LEN 6
75 #define VIDEO_PREFIX "video/"
76 #define VIDEO_PREFIX_LEN 6
78 #define MEDIA_SVC_DEFAULT_GPS_VALUE -200 /**< Default GPS Value*/
80 #define MEDIA_SVC_PDF_TAG_TAIL_LEN 12
81 #define MEDIA_SVC_PDF_BUF_SIZE 256
83 enum Exif_Orientation {
95 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
96 /*known mime types of normal files*/
102 /* known mime types of drm files*/
104 "x-mp3", /*alias of audio/mpeg*/
105 "x-mpeg", /*alias of audio/mpeg*/
107 "x-ogg", /*alias of audio/ogg*/
108 "vnd.ms-playready.media.pya:*.pya", /*playready*/
111 "x-m4a", /*alias of audio/mp4*/
112 /* below mimes are rare*/
122 "x-vorbis", /*alias of audio/x-vorbis+ogg*/
123 "vorbis", /*alias of audio/x-vorbis+ogg*/
125 "x-mp2", /*alias of audio/mp2*/
126 "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
127 "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
131 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
132 "application/x-smaf",
136 char *_media_info_generate_uuid(void)
139 char uuid_unparsed[37];
142 uuid_generate(uuid_value);
143 uuid_unparse(uuid_value, uuid_unparsed);
145 if (strlen(uuid_unparsed) < 36) {
146 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
150 return g_strdup(uuid_unparsed);
153 /* GPS information is not ExifTag member */
154 static int __media_svc_get_exif_gps_double(ExifData *ed, double *value, long tagtype)
157 char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
158 double tmp_arr[3] = { 0.0, 0.0, 0.0 };
160 char **tmp_split = NULL;
162 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
163 media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
165 entry = exif_data_get_entry(ed, tagtype);
166 media_svc_retv_if(!entry, MS_MEDIA_ERR_INTERNAL);
168 exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
169 gps_buf[strlen(gps_buf)] = '\0';
171 tmp_split = g_strsplit(gps_buf, ",", -1);
172 if (g_strv_length(tmp_split) != 3) {
173 g_strfreev(tmp_split);
174 media_svc_error("Wrong GPS format");
175 return MS_MEDIA_ERR_INTERNAL;
178 for (i = 0; i < 3; i++)
179 tmp_arr[i] = g_strtod(tmp_split[i], NULL);
181 g_strfreev(tmp_split);
183 *value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
185 return MS_MEDIA_ERR_NONE;
188 static int __media_svc_get_exif_gps_str(ExifData *ed, char *value, long tagtype)
192 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
193 media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
195 entry = exif_data_get_entry(ed, tagtype);
197 exif_entry_get_value(entry, value, MEDIA_SVC_METADATA_LEN_MAX);
198 value[strlen(value)] = '\0';
201 return MS_MEDIA_ERR_NONE;
204 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, ExifTag tagtype)
207 ExifByteOrder mByteOrder;
208 ExifRational mRational;
209 long numerator, denominator;
211 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
213 entry = exif_data_get_entry(ed, tagtype);
214 media_svc_retv_if(!entry, MS_MEDIA_ERR_NONE);
217 case EXIF_TAG_ORIENTATION:
218 case EXIF_TAG_PIXEL_X_DIMENSION:
219 case EXIF_TAG_PIXEL_Y_DIMENSION:
220 case EXIF_TAG_ISO_SPEED_RATINGS:
221 media_svc_retvm_if(!i_value, MS_MEDIA_ERR_INVALID_PARAMETER, "i_value is NULL");
223 mByteOrder = exif_data_get_byte_order(ed);
224 short exif_value = exif_get_short(entry->data, mByteOrder);
225 *i_value = (int)exif_value;
227 case EXIF_TAG_EXPOSURE_TIME:
228 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
230 mByteOrder = exif_data_get_byte_order(ed);
231 mRational = exif_get_rational(entry->data, mByteOrder);
232 numerator = mRational.numerator;
233 denominator = mRational.denominator;
234 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
236 case EXIF_TAG_FNUMBER:
237 media_svc_retvm_if(!d_value, MS_MEDIA_ERR_INVALID_PARAMETER, "d_value is NULL");
239 mByteOrder = exif_data_get_byte_order(ed);
240 mRational = exif_get_rational(entry->data, mByteOrder);
241 numerator = mRational.numerator;
242 denominator = mRational.denominator;
244 *d_value = ((numerator*1.0)/(denominator*1.0));
247 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
249 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
250 buf[strlen(buf)] = '\0';
253 return MS_MEDIA_ERR_NONE;
256 static time_t __media_svc_get_timeline_from_str(const char *timstr)
259 time_t modified_t = 0;
263 if (!STRING_VALID(timstr)) {
264 media_svc_error("Invalid Parameter");
268 /*Exif Format : %Y:%m:%d %H:%M:%S
269 Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
270 memset(&t, 0x00, sizeof(struct tm));
274 localtime_r(&rawtime, &timeinfo);
276 if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
277 t.tm_isdst = timeinfo.tm_isdst;
279 media_svc_debug("DST %d", t.tm_isdst);
281 /* If time string has timezone */
282 if (strptime(timstr, "%Y:%m:%d %H:%M:%S %z", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S %z", &t)) {
283 char tim_tmp_str[255] = { 0, };
285 /* ISO8601 Time string format */
286 strftime(tim_tmp_str, 255, "%Y-%m-%dT%H:%M:%S%z", &t);
287 GDateTime *pdatetime = g_date_time_new_from_iso8601(tim_tmp_str, NULL);
289 modified_t = g_date_time_to_unix(pdatetime);
290 g_date_time_unref(pdatetime);
291 media_svc_debug("Calibrated timeval : [%ld][%s]", modified_t, tim_tmp_str);
294 modified_t = mktime(&t);
300 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);
302 media_svc_error("Failed to get timeline : [%s]", timstr);
308 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
314 media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "path is null");
315 media_svc_retvm_if(!mime_type, MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is null");
316 media_svc_retvm_if(!media_type, MS_MEDIA_ERR_INVALID_PARAMETER, "media_type is null");
319 if (strncmp(mime_type, IMAGE_PREFIX, IMAGE_PREFIX_LEN) == 0) {
320 *media_type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
321 return MS_MEDIA_ERR_NONE;
325 if (strncmp(mime_type, AUDIO_PREFIX, AUDIO_PREFIX_LEN) == 0) {
326 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
328 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
329 if (strcmp(mime_type + AUDIO_PREFIX_LEN, music_mime_table[idx]) == 0) {
330 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
335 /* audio/x-mpegurl : .m3u file (playlist file) */
336 if (strcmp(mime_type + AUDIO_PREFIX_LEN, "x-mpegurl") == 0)
337 *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
339 return MS_MEDIA_ERR_NONE;
343 if (strncmp(mime_type, VIDEO_PREFIX, VIDEO_PREFIX_LEN) == 0) {
344 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
346 /*some video files don't have video stream. in this case it is categorize as music. */
347 if (strcmp(mime_type + VIDEO_PREFIX_LEN, "3gpp") == 0 ||
348 strcmp(mime_type + VIDEO_PREFIX_LEN, "mp4") == 0) {
349 if (mm_file_get_stream_info(path, &audio, &video) == FILEINFO_ERROR_NONE) {
350 if (audio > 0 && video == 0)
351 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
355 return MS_MEDIA_ERR_NONE;
359 *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
361 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
362 if (strcmp(mime_type, sound_mime_table[idx]) == 0) {
363 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
364 return MS_MEDIA_ERR_NONE;
368 /*"asf" must check video stream and then categorize in directly. */
369 if (strcmp(mime_type, "application/vnd.ms-asf") == 0) {
370 if (mm_file_get_stream_info(path, &audio, &video) == FILEINFO_ERROR_NONE) {
371 if (audio > 0 && video == 0)
372 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
374 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
377 return MS_MEDIA_ERR_NONE;
380 if (strcmp(mime_type, "application/epub+zip") == 0 || strcmp(mime_type, "application/pdf") == 0)
381 *media_type = MEDIA_SVC_MEDIA_TYPE_BOOK;
383 return MS_MEDIA_ERR_NONE;
387 drm_contentifo is not NULL, if the file is OMA DRM.
388 If the file is not OMA DRM, drm_contentinfo must be NULL.
390 static int __media_svc_get_mime_type(const char *path, char *mimetype)
392 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
394 /*in case of normal files or failure to get mime in drm */
395 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
396 media_svc_error("aul_get_mime_from_file fail");
397 return MS_MEDIA_ERR_INTERNAL;
400 return MS_MEDIA_ERR_NONE;
403 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
407 for (i = strlen(file_path); i >= 0; i--) {
408 if (file_path[i] == '.') {
409 SAFE_STRLCPY(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
413 if (file_path[i] == '/')
419 static int __media_svc_safe_atoi(char *buffer, int *si)
423 media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
425 const long sl = strtol(buffer, &end, 10);
427 media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
428 media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
429 media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
430 media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
431 media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
435 return MS_MEDIA_ERR_NONE;
438 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
440 int ret = MS_MEDIA_ERR_NONE;
442 char *thumb_path = NULL;
443 long bsize_kbytes = 0;
444 GError *error = NULL;
446 media_svc_retvm_if(!image || size == 0, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid image");
447 media_svc_sec_debug("start save image, path [%s] image size [%d]", image_path, size);
449 ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
450 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path error");
452 ret = statfs(thumb_path, &fs);
454 media_svc_retvm_if(ret == -1, MS_MEDIA_ERR_INTERNAL, "statfs failed");
456 bsize_kbytes = fs.f_bsize >> 10;
457 media_svc_retvm_if((bsize_kbytes * fs.f_bavail) < 1024, MS_MEDIA_ERR_NOT_ENOUGH_SPACE, "Not enough space");
459 if (!g_file_set_contents(image_path, (const gchar *)image, (gssize)size, &error)) {
460 media_svc_error("g_file_set_contents faild:%s", error->message);
462 return MS_MEDIA_ERR_INTERNAL;
465 return MS_MEDIA_ERR_NONE;
468 static char *__media_svc_get_title_from_filepath(const char *path)
470 char *filename = NULL;
472 char *last_dot = NULL;
474 media_svc_retvm_if(!STRING_VALID(path), NULL, "Invalid path");
476 filename = g_path_get_basename(path);
478 last_dot = strrchr(filename, '.');
480 title = g_strndup(filename, last_dot - filename);
486 media_svc_debug("extract title is [%s]", title);
491 void _media_svc_remove_file(const char *path)
493 if (!STRING_VALID(path))
496 if (remove(path) != 0)
497 media_svc_stderror("fail to remove file result");
500 int _media_svc_get_thumbnail_path(char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
502 int ret = MS_MEDIA_ERR_NONE;
503 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
504 char hash[THUMB_HASH_LEN] = {0, };
505 char *thumb_dir = NULL;
507 ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
508 media_svc_retvm_if(!STRING_VALID(thumb_dir), MS_MEDIA_ERR_INTERNAL, "ms_user_get_root_thumb_store_path failed");
510 if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
511 media_svc_error("Wrong path[%s]", thumb_dir);
513 return MS_MEDIA_ERR_INTERNAL;
516 memset(file_ext, 0, sizeof(file_ext));
517 if (!__media_svc_get_file_ext(pathname, file_ext))
518 media_svc_error("get file ext fail");
520 ret = mb_svc_generate_hash_code(pathname, hash, THUMB_HASH_LEN);
521 if (ret != MS_MEDIA_ERR_NONE) {
522 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
524 return MS_MEDIA_ERR_INTERNAL;
528 /* 'img_format' is mime-type */
529 if (!g_str_has_prefix(img_format, IMAGE_PREFIX) || strlen(img_format) == IMAGE_PREFIX_LEN) {
530 media_svc_error("Not proper img format");
532 return MS_MEDIA_ERR_INTERNAL;
535 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, img_format + IMAGE_PREFIX_LEN);
537 if (strcasecmp(file_ext, "PNG") == 0)
538 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
540 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
545 return MS_MEDIA_ERR_NONE;
548 int _media_svc_get_file_time(const char *full_path)
550 struct stat statbuf = { 0, };
552 if (stat(full_path, &statbuf) == -1) {
553 media_svc_stderror("stat fails.");
557 return statbuf.st_mtime;
560 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)
562 int ret = MS_MEDIA_ERR_NONE;
563 bool drm_type = false;
564 char mime_type[256] = {0, };
565 media_svc_media_type_e media_type;
567 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
568 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
570 content_info->path = g_strdup(path);
571 media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
574 memset(&st, 0, sizeof(struct stat));
575 if (stat(path, &st) == 0) {
576 content_info->modified_time = st.st_mtime;
577 content_info->timeline = content_info->modified_time;
578 content_info->size = st.st_size;
580 media_svc_stderror("stat failed");
583 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
585 media_svc_debug("refresh");
586 return MS_MEDIA_ERR_NONE;
589 content_info->storage_uuid = g_strdup(storage_id);
590 media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
592 content_info->storage_type = storage_type;
593 time(&content_info->added_time);
595 content_info->media_uuid = _media_info_generate_uuid();
596 media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
598 content_info->file_name = g_path_get_basename(path);
599 media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
601 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
602 if drm_contentinfo is not NULL, the file is OMA DRM.*/
603 ret = __media_svc_get_mime_type(path, mime_type);
604 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
606 media_svc_debug("mime [%s]", mime_type);
607 content_info->is_drm = drm_type;
609 ret = __media_svc_get_media_type(path, mime_type, &media_type);
610 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
612 content_info->mime_type = g_strdup(mime_type);
613 media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
615 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, media_type);
617 content_info->media_type = media_type;
619 return MS_MEDIA_ERR_NONE;
622 static int __image_360_check(const char *path)
625 size_t size = 0, app1_size = 0, exif_app1_xmp_size = 0;
626 unsigned char exif_header[4] = {0, };
627 unsigned char exif_app1[2] = {0, };
628 unsigned char exif_app1_xmp[2] = {0, };
629 unsigned char exif_app1_xmp_t[2] = {0, };
630 GString *xmp_data = NULL;
634 memset(exif_header, 0x00, sizeof(exif_header));
635 memset(exif_app1, 0x00, sizeof(exif_app1));
636 memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
637 memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
639 fp = fopen(path, "rb");
643 size = fread(exif_header, 1, sizeof(exif_header), fp);
644 if (size != sizeof(exif_header))
647 if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
648 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
649 if (size != sizeof(exif_app1))
652 if ((size_t)((exif_app1[0] << 8) | (exif_app1[1])) <= 2)
655 app1_size = (size_t)((exif_app1[0] << 8) | (exif_app1[1])) - 2 ;
656 if (fseek(fp, app1_size, SEEK_CUR) != 0)
659 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
660 if (size != sizeof(exif_app1_xmp))
663 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
664 size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
665 if (size != sizeof(exif_app1_xmp_t))
668 if ((size_t)((exif_app1_xmp_t[0] << 8) | (exif_app1_xmp_t[1])) <= 2)
671 exif_app1_xmp_size = (size_t)((exif_app1_xmp_t[0] << 8) | (exif_app1_xmp_t[1])) - 2;
673 xmp_data = g_string_sized_new(exif_app1_xmp_size);
676 exif_app1_xmp_size--;
683 xmp_data = g_string_append_c(xmp_data, (gchar)fdata);
684 } while (exif_app1_xmp_size > 0);
686 if (strstr(xmp_data->str, "UsePanoramaViewer") &&
687 strstr(xmp_data->str, "True") &&
688 strstr(xmp_data->str, "ProjectionType") &&
689 strstr(xmp_data->str, "equirectangular"))
692 g_string_free(xmp_data, TRUE);
705 static char * __media_svc_get_title(MMHandleType tag, const char *path)
707 int ret = FILEINFO_ERROR_NONE;
713 ret = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
714 if (ret == FILEINFO_ERROR_NONE && size > 0) {
715 while(p && isspace(*p))
722 title = __media_svc_get_title_from_filepath(path);
726 media_svc_error("Can't extract title");
728 return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
731 char * _media_svc_get_title_by_path(const char *path)
733 /* No MMHandleType in media-svc.c */
734 return __media_svc_get_title(NULL, path);
737 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
740 int orient_value = 0;
744 bool has_datetaken = false;
745 double fnumber = 0.0;
749 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
751 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
752 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);
753 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
755 path = content_info->path;
756 content_info->media_meta.title = __media_svc_get_title(NULL, path);
758 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
759 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
760 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
762 /* Not used. But to preserved the behavior, set MEDIA_SVC_TAG_UNKNOWN. */
763 content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
764 content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
765 content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
766 content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
767 content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
768 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
769 content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
770 content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
772 /* Load an ExifData object from an EXIF file */
773 ed = exif_data_new_from_file(path);
775 media_svc_sec_debug("There is no exif data in [ %s ]", path);
776 goto GET_WIDTH_HEIGHT;
779 content_info->media_meta.is_360 = __image_360_check(path);
781 memset(buf, 0x00, sizeof(buf));
782 if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
783 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
784 if (!g_strcmp0(buf, "S"))
786 content_info->media_meta.latitude = value;
790 memset(buf, 0x00, sizeof(buf));
791 if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
792 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
793 if (!g_strcmp0(buf, "W"))
796 content_info->media_meta.longitude = value;
800 memset(buf, 0x00, sizeof(buf));
801 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
803 content_info->media_meta.description = g_strdup(buf);
805 content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
808 memset(buf, 0x00, sizeof(buf));
809 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
810 if (strlen(buf) > 0) {
811 has_datetaken = true;
812 content_info->media_meta.datetaken = g_strdup(buf);
814 /* This is same as recorded_date */
815 content_info->media_meta.recorded_date = g_strdup(buf);
819 memset(buf, 0x00, sizeof(buf));
820 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
821 if (strlen(buf) > 0) {
822 has_datetaken = true;
823 content_info->media_meta.datetaken = g_strdup(buf);
825 /* This is same as recorded_date */
826 content_info->media_meta.recorded_date = g_strdup(buf);
831 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
832 if (content_info->timeline == 0)
833 content_info->timeline = content_info->modified_time;
835 media_svc_debug("Timeline : %ld", content_info->timeline);
838 memset(buf, 0x00, sizeof(buf));
839 /* Get exposure_time value from exif. */
840 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
842 content_info->media_meta.exposure_time = g_strdup(buf);
845 /* Get fnumber value from exif. */
846 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE)
847 content_info->media_meta.fnumber = fnumber;
849 /* Get iso value from exif. */
850 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE)
851 content_info->media_meta.iso = iso;
853 memset(buf, 0x00, sizeof(buf));
854 /* Get model value from exif. */
855 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
857 content_info->media_meta.model = g_strdup(buf);
860 /* Get orientation value from exif. */
861 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
862 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
863 content_info->media_meta.orientation = orient_value;
866 /* Get width value from exif. */
867 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
869 content_info->media_meta.width = exif_width;
872 /* Get height value from exif. */
873 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
875 content_info->media_meta.height = exif_height;
883 if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
884 /*Get image width, height*/
885 unsigned int img_width = 0;
886 unsigned int img_height = 0;
887 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
889 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
890 if (content_info->media_meta.width == 0)
891 content_info->media_meta.width = img_width;
893 if (content_info->media_meta.height == 0)
894 content_info->media_meta.height = img_height;
897 return MS_MEDIA_ERR_NONE;
900 static char * __media_svc_get_tag_str_value(MMHandleType tag, const char *tag_name)
902 int ret = FILEINFO_ERROR_NONE;
906 ret = mm_file_get_attrs(tag, tag_name, &p, &size, NULL);
907 if (ret == FILEINFO_ERROR_NONE && size > 0)
910 return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
913 int _media_svc_extract_music_metadata_for_update(media_svc_content_info_s *content_info, const char *path)
915 MMHandleType tag = 0;
916 int mmf_error = FILEINFO_ERROR_NONE;
918 content_info->path = g_strdup(path);
920 mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
921 if (mmf_error == FILEINFO_ERROR_NONE) {
922 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
923 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
924 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
925 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
926 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
927 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
928 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
929 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
931 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
934 mm_file_destroy_tag_attrs(tag);
936 return MS_MEDIA_ERR_NONE;
939 int _media_svc_extract_media_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
941 MMHandleType content = 0;
942 MMHandleType tag = 0;
944 unsigned char *image = NULL;
945 unsigned int size = 0;
946 int mmf_error = FILEINFO_ERROR_NONE;
948 int ret = MS_MEDIA_ERR_NONE;
949 int convert_value = 0;
952 /*Get Content Tag attribute ===========*/
953 if (content_info->storage_type == MS_USER_STORAGE_EXTERNAL_USB)
954 mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
956 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
958 if (mmf_error == FILEINFO_ERROR_NONE) {
959 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
960 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
961 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
962 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
963 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
964 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
965 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
966 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
967 content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
969 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
970 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
971 if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
972 /*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*/
973 char *p_value = g_strdelimit(g_strdup(p), "-", ':');
974 content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
977 content_info->media_meta.recorded_date = g_strdup(p);
980 if (STRING_VALID(content_info->media_meta.recorded_date)) {
981 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
982 if (content_info->timeline == 0)
983 content_info->timeline = content_info->modified_time;
985 /* This is same as datetaken */
986 /* Remove compensation string */
987 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
988 content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
989 g_free(content_info->media_meta.recorded_date);
990 content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
992 content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
997 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
998 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
999 if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1000 content_info->media_meta.year = g_strdup(p);
1003 if (!content_info->media_meta.year)
1004 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1006 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RATING, &p, &size, NULL);
1007 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1008 if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1009 content_info->media_meta.rating = convert_value;
1011 content_info->media_meta.rating = 0;
1014 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1016 /*Do not extract artwork for the USB Storage content*/
1017 if (content_info->storage_type != MS_USER_STORAGE_EXTERNAL_USB) {
1018 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1019 if (mmf_error != FILEINFO_ERROR_NONE)
1020 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1022 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1023 if (mmf_error != FILEINFO_ERROR_NONE)
1024 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1026 if (image != NULL && size > 0) {
1027 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1028 int artwork_mime_size = -1;
1030 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1031 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1032 ret = _media_svc_get_thumbnail_path(thumb_path, content_info->path, p, uid);
1033 if (ret != MS_MEDIA_ERR_NONE) {
1034 media_svc_error("Fail to Get Thumbnail Path");
1036 ret = __media_svc_save_image(image, size, thumb_path, uid);
1037 if (ret != MS_MEDIA_ERR_NONE) {
1038 media_svc_error("Fail to Save Image");
1040 content_info->thumbnail_path = g_strdup(thumb_path);
1047 /*Initialize album_id to 0. below code will set the album_id*/
1048 content_info->album_id = album_id;
1049 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1050 if (ret != MS_MEDIA_ERR_NONE) {
1051 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1052 media_svc_debug("album does not exist. So start to make album art");
1053 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1054 (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1055 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);
1057 ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1059 content_info->album_id = album_id;
1062 content_info->album_id = album_id;
1065 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1066 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1067 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1069 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1070 double longitude = 0.0;
1071 double latitude = 0.0;
1072 double altitude = 0.0;
1074 mm_file_get_attrs(tag, MM_FILE_TAG_LONGITUDE, &longitude,
1075 MM_FILE_TAG_LATIDUE, &latitude,
1076 MM_FILE_TAG_ALTIDUE, &altitude,
1079 content_info->media_meta.longitude = (longitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : longitude;
1080 content_info->media_meta.latitude = (latitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : latitude;
1081 content_info->media_meta.altitude = (altitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : altitude;
1083 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1084 if (mmf_error != FILEINFO_ERROR_NONE)
1087 media_svc_debug("CDIS : %d", cdis_value);
1089 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1090 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1091 content_info->media_meta.orientation = atoi(p);
1093 content_info->media_meta.orientation = 0;
1094 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1098 mmf_error = mm_file_destroy_tag_attrs(tag);
1099 if (mmf_error != FILEINFO_ERROR_NONE)
1100 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1102 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1103 content_info->album_id = album_id;
1106 /*Get Content attribute ===========*/
1107 if (cdis_value == 1)
1108 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1110 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1112 media_svc_retvm_if(mmf_error != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_NONE, "mm_file_create_content_attrs failed");
1114 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1115 int audio_bitrate = 0;
1116 int video_bitrate = 0;
1118 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1119 MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate,
1120 MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate,
1121 MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width,
1122 MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height,
1125 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1127 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1128 MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate,
1129 MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate,
1130 MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel,
1131 MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample,
1135 mm_file_destroy_content_attrs(content);
1137 return MS_MEDIA_ERR_NONE;
1140 static gchar * __media_svc_get_zipfile_data(zip_t *z, const char *fname)
1143 zip_int64_t index_num = 0;
1144 zip_file_t *file = NULL;
1145 zip_stat_t sb = {0, };
1148 media_svc_retvm_if(!z, NULL, "z is NULL");
1149 media_svc_retvm_if(!fname, NULL, "fname is NULL");
1151 index_num = zip_name_locate(z, fname, ZIP_FL_NOCASE);
1152 media_svc_retvm_if(index_num == -1, NULL, "fname is not exists [%s]", fname);
1154 err = zip_stat_index(z, index_num, ZIP_STAT_SIZE, &sb);
1155 media_svc_retvm_if(err == -1, NULL, "zip_stat_index failed");
1157 file = zip_fopen_index(z, index_num, ZIP_FL_UNCHANGED);
1158 media_svc_retvm_if(!file, NULL, "zip_fopen_index failed");
1160 buf = g_malloc0(sb.size);
1162 err = zip_fread(file, buf, sb.size);
1173 static xmlNodePtr __media_svc_find_node(xmlNodePtr node, const char *key)
1175 xmlNodePtr tmp = NULL;
1177 media_svc_retvm_if(!node, NULL, "node is NULL");
1178 media_svc_retvm_if(!key, NULL, "key is NULL");
1180 for (tmp = node->children; tmp; tmp = tmp->next) {
1181 if (xmlIsBlankNode(tmp))
1184 if (g_str_has_suffix((gchar *)tmp->name, key))
1191 static char * __media_svc_remove_escape_c(const char *value)
1197 media_svc_retv_if(!value, NULL);
1199 len = strlen(value);
1201 for (i = 0; i < len; i++) {
1202 if (value[i] != 10 && value[i] != 32) { // 10='\n' 32=' '
1210 end = end - start + 1;
1212 return g_strndup(value + start, end);
1215 static char * __media_svc_find_and_get_value(xmlNodePtr node, const char *key)
1217 xmlNodePtr tmp = NULL;
1218 char *tmp_res = NULL;
1221 media_svc_retvm_if(!node, NULL, "node is NULL");
1222 media_svc_retvm_if(!key, NULL, "key is NULL");
1224 for (tmp = node->children; tmp; tmp = tmp->next) {
1225 if (xmlIsBlankNode(tmp))
1228 if (tmp->children) {
1229 tmp_res = __media_svc_find_and_get_value(tmp, key);
1231 res = __media_svc_remove_escape_c(tmp_res);
1237 if (g_str_has_suffix((gchar *)tmp->name, key))
1238 return (char *)xmlNodeGetContent(tmp);
1244 static gboolean __media_svc_get_epub_root_file(zip_t *z, char **opf_file)
1247 gchar *tmp_buf = NULL;
1248 xmlDocPtr doc = NULL;
1249 xmlNodePtr node = NULL;
1251 media_svc_retvm_if(!z, FALSE, "z is NULL");
1252 media_svc_retvm_if(!opf_file, FALSE, "opf_file is NULL");
1254 buf = __media_svc_get_zipfile_data(z, "META-INF/container.xml");
1255 media_svc_retvm_if(!buf, FALSE, "buf is NULL");
1258 tmp_buf = g_strrstr(buf, ">");
1260 *(tmp_buf + 1) = '\0';
1262 doc = xmlParseDoc((const xmlChar *)buf);
1264 media_svc_retvm_if(!doc, FALSE, "doc is NULL");
1266 node = xmlDocGetRootElement(doc);
1267 node = __media_svc_find_node(node, "rootfiles");
1268 node = __media_svc_find_node(node, "rootfile");
1270 *opf_file = (char *)xmlGetProp(node, (const xmlChar *)"full-path");
1271 media_svc_sec_debug("OPF [%s]", *opf_file);
1277 static gboolean __media_svc_get_xml_metadata(const xmlChar *buffer, gboolean is_pdf, media_svc_content_info_s *content_info)
1279 xmlDocPtr doc = NULL;
1280 xmlNodePtr root = NULL;
1282 media_svc_retvm_if(!buffer, FALSE, "buffer is NULL");
1283 media_svc_retvm_if(!content_info, FALSE, "content_info is NULL");
1285 doc = xmlParseDoc(buffer);
1286 media_svc_retv_if(!doc, FALSE);
1288 root = xmlDocGetRootElement(doc);
1294 content_info->media_meta.title = __media_svc_find_and_get_value(root, "title");
1295 if (is_pdf && !content_info->media_meta.title) {
1300 content_info->media_meta.composer = __media_svc_find_and_get_value(root, "creator");
1301 if (!content_info->media_meta.composer)
1302 content_info->media_meta.composer = __media_svc_find_and_get_value(root, "author");
1304 content_info->media_meta.copyright = __media_svc_find_and_get_value(root, "publisher");
1305 content_info->media_meta.recorded_date = __media_svc_find_and_get_value(root, "date");
1306 content_info->media_meta.genre = __media_svc_find_and_get_value(root, "subject");
1307 content_info->media_meta.description = __media_svc_find_and_get_value(root, "description");
1314 static int __media_svc_get_epub_metadata(media_svc_content_info_s *content_info)
1319 char *opf_path = NULL;
1321 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1324 z = zip_open(content_info->path, ZIP_RDONLY, &err);
1325 media_svc_retvm_if(err == -1, MS_MEDIA_ERR_INTERNAL, "zip_open failed");
1327 //2. find and read opf file
1328 if (!__media_svc_get_epub_root_file(z, &opf_path)) {
1329 media_svc_error("__media_svc_get_epub_root_file failed");
1331 return MS_MEDIA_ERR_INTERNAL;
1335 buf = __media_svc_get_zipfile_data(z, opf_path);
1338 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INTERNAL, "__media_svc_get_zipfile_data failed");
1340 if (!__media_svc_get_xml_metadata((const xmlChar *)buf, FALSE, content_info))
1341 media_svc_error("__media_svc_get_xml_metadata failed");
1345 return MS_MEDIA_ERR_NONE;
1348 static int __media_svc_get_pdf_metadata(media_svc_content_info_s *content_info)
1354 int search_limit = 0;
1355 char tmp[MEDIA_SVC_PDF_BUF_SIZE + 1] = {0, };
1356 gchar *meta_buf = NULL;
1359 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1360 media_svc_retvm_if(content_info->size < 256, MS_MEDIA_ERR_INTERNAL, "open failed");
1362 fd = open(content_info->path, O_RDONLY);
1363 media_svc_retvm_if(fd < 0, MS_MEDIA_ERR_INTERNAL, "open failed");
1365 search_limit = content_info->size - MEDIA_SVC_PDF_TAG_TAIL_LEN;
1367 while (cur_pos <= search_limit) {
1368 if (lseek(fd, cur_pos, SEEK_SET) == -1)
1371 memset(&tmp, 0x00, MEDIA_SVC_PDF_BUF_SIZE + 1);
1373 if (read(fd, &tmp, MEDIA_SVC_PDF_BUF_SIZE) != MEDIA_SVC_PDF_BUF_SIZE) {
1374 media_svc_error("read failed");
1378 //1.Find <x:xmpmeta .. </x:xmpmeta> block
1379 if (start_pos == 0 && (found = strstr(tmp, "<x:xmpmeta"))) {
1380 start_pos = cur_pos + (found - tmp);
1381 // media_svc_error("FIND START_POS[%d]", start_pos);
1386 if (start_pos != 0 && (found = strstr(tmp, "</x:xmpmeta>"))) {
1387 end_pos = cur_pos + (found - tmp) + MEDIA_SVC_PDF_TAG_TAIL_LEN;
1388 // media_svc_error("FIND END_POS[%d]", end_pos);
1392 //2.get metadata using xml parser
1393 if (start_pos && end_pos) {
1394 if (lseek(fd, start_pos, SEEK_SET) == -1)
1397 meta_buf = g_malloc0(end_pos - start_pos + 1);
1399 if (read(fd, meta_buf, end_pos - start_pos) == end_pos - start_pos) {
1400 if (__media_svc_get_xml_metadata((const xmlChar *)meta_buf, TRUE, content_info)) {
1418 return MS_MEDIA_ERR_NONE;
1421 int _media_svc_extract_book_metadata(media_svc_content_info_s *content_info)
1423 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content info is NULL");
1425 if (g_str_has_suffix(content_info->mime_type, "epub+zip"))
1426 return __media_svc_get_epub_metadata(content_info);
1428 return __media_svc_get_pdf_metadata(content_info);
1431 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1433 media_svc_retm_if(!content_info, "content info is NULL");
1435 /* Delete media_svc_content_info_s */
1436 g_free(content_info->media_uuid);
1437 g_free(content_info->path);
1438 g_free(content_info->file_name);
1439 g_free(content_info->mime_type);
1440 g_free(content_info->folder_uuid);
1441 g_free(content_info->thumbnail_path);
1442 g_free(content_info->storage_uuid);
1444 /* Delete media_svc_content_meta_s */
1445 g_free(content_info->media_meta.title);
1446 g_free(content_info->media_meta.album);
1447 g_free(content_info->media_meta.artist);
1448 g_free(content_info->media_meta.album_artist);
1449 g_free(content_info->media_meta.genre);
1450 g_free(content_info->media_meta.composer);
1451 g_free(content_info->media_meta.year);
1452 g_free(content_info->media_meta.recorded_date);
1453 g_free(content_info->media_meta.copyright);
1454 g_free(content_info->media_meta.track_num);
1455 g_free(content_info->media_meta.description);
1456 g_free(content_info->media_meta.datetaken);
1457 g_free(content_info->media_meta.exposure_time);
1458 g_free(content_info->media_meta.model);
1460 g_free(content_info->file_name_pinyin);
1461 g_free(content_info->media_meta.title_pinyin);
1462 g_free(content_info->media_meta.album_pinyin);
1463 g_free(content_info->media_meta.artist_pinyin);
1464 g_free(content_info->media_meta.album_artist_pinyin);
1465 g_free(content_info->media_meta.genre_pinyin);
1466 g_free(content_info->media_meta.composer_pinyin);
1467 g_free(content_info->media_meta.copyright_pinyin);
1468 g_free(content_info->media_meta.description_pinyin);
1471 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
1473 int ret = MS_MEDIA_ERR_NONE;
1475 media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
1476 media_svc_retvm_if(!thumb_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1477 media_svc_retvm_if(!g_file_test(path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "File doesn't exist[%s]", path);
1479 ms_user_storage_type_e store_type = -1;
1480 ret = ms_user_get_storage_type(uid, path, &store_type);
1482 if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
1483 media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
1484 return MS_MEDIA_ERR_INVALID_PARAMETER;
1487 media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1490 ret = _media_svc_get_thumbnail_path(thumb_path, path, NULL, uid);
1491 if (ret != MS_MEDIA_ERR_NONE) {
1492 media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
1493 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1498 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1499 return create_image_thumbnail_to_file(path, CONTENT_THUMB_DEFAULT_WIDTH, CONTENT_THUMB_DEFAULT_HEIGHT, thumb_path, true);
1501 return create_video_thumbnail_to_file(path, CONTENT_THUMB_DEFAULT_WIDTH, CONTENT_THUMB_DEFAULT_HEIGHT, thumb_path, true);
1505 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1507 int ret = MS_MEDIA_ERR_NONE;
1509 pinyin_name_s *pinyinname = NULL;
1511 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1512 media_svc_retvm_if(pinyin_str == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "pinyin_str is NULL");
1516 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1517 if (ret == MS_MEDIA_ERR_NONE) {
1518 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1519 *pinyin_str = g_strdup(pinyinname[0].pinyin_name);
1521 *pinyin_str = g_strdup(src_str); /* Return Original Non China Character */
1524 _media_svc_pinyin_free(pinyinname, size);
1529 bool _media_svc_check_pinyin_support(void)
1531 int ret = SYSTEM_INFO_ERROR_NONE;
1532 bool is_supported = false;
1533 static int media_svc_pinyin_support = -1;
1535 if (media_svc_pinyin_support == -1) {
1536 ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
1537 if (ret != SYSTEM_INFO_ERROR_NONE) {
1538 media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
1542 media_svc_pinyin_support = is_supported;
1545 return media_svc_pinyin_support;
1548 int _media_svc_get_media_type(const char *path, int *mediatype)
1550 int ret = MS_MEDIA_ERR_NONE;
1551 char mime_type[256] = {0};
1552 media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1554 media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1556 ret = __media_svc_get_mime_type(path, mime_type);
1557 if (ret == MS_MEDIA_ERR_NONE)
1558 __media_svc_get_media_type(path, mime_type, &media_type);
1560 media_svc_error("__media_svc_get_mime_type failed");
1562 *mediatype = media_type;
1567 bool _media_svc_is_valid_storage_type(ms_user_storage_type_e storage_type)
1569 switch (storage_type) {
1570 case MS_USER_STORAGE_INTERNAL:
1571 case MS_USER_STORAGE_EXTERNAL:
1572 case MS_USER_STORAGE_EXTERNAL_USB:
1575 media_svc_error("storage type is incorrect[%d]", storage_type);
1580 bool _media_svc_is_keyword_included(const char *path, const char *keyword)
1582 media_svc_retvm_if(!path, false, "Invalid path");
1583 media_svc_retvm_if(!keyword, false, "Invalid keyword");
1585 if (g_str_has_suffix(path, "epub") || g_str_has_suffix(path, "EPUB"))
1586 return _media_svc_epub_is_keyword_included(path, keyword);
1588 return _media_svc_pdf_is_keyword_included(path, keyword);