[ACR-1635] Support ebook format
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-util.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <unistd.h>
23 #include <stdlib.h>
24 #ifndef __USE_XOPEN
25 #define DEF_XOPEN
26 #define __USE_XOPEN /* needed for strptime */
27 #endif
28 #include <time.h>
29 #ifdef DEF_XOPEN
30 #undef __USE_XOPEN
31 #endif
32 #include <string.h>
33 #include <system_info.h>
34 #include <sys/vfs.h>
35 #include <glib/gstdio.h>
36 #include <sys/stat.h>
37 #include <dirent.h>
38 #include <ctype.h>
39 #include <aul/aul.h>
40 #include <mm_file.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 /*For ebook*/
55 #include <zip.h>
56 #include <libxml/xmlmemory.h>
57 #include <libxml/parser.h>
58 #include <libxml/HTMLparser.h>
59
60 #define MEDIA_SVC_FILE_EXT_LEN_MAX                              6                       /**< Maximum file ext lenth*/
61
62 /* Define data structures for media type and mime type */
63 #define MEDIA_SVC_CATEGORY_UNKNOWN      0x00000000      /**< Default */
64 #define MEDIA_SVC_CATEGORY_ETC          0x00000001      /**< ETC category */
65 #define MEDIA_SVC_CATEGORY_IMAGE        0x00000002      /**< Image category */
66 #define MEDIA_SVC_CATEGORY_VIDEO        0x00000004      /**< Video category */
67 #define MEDIA_SVC_CATEGORY_MUSIC        0x00000008      /**< Music category */
68 #define MEDIA_SVC_CATEGORY_SOUND        0x00000010      /**< Sound category */
69 #define MEDIA_SVC_CATEGORY_PVR  0x00000020      /**< PVR category */
70 #define MEDIA_SVC_CATEGORY_UHD  0x00000040      /**< UHD category */
71 #define MEDIA_SVC_CATEGORY_SCSA 0x00000080      /**< SCSA category */
72 #define MEDIA_SVC_CATEGORY_BOOK 0x00000100      /**< ebook category */
73
74 #define CONTENT_TYPE_NUM 5
75 #define MUSIC_MIME_NUM 29
76 #define SOUND_MIME_NUM 1
77 #define MIME_TYPE_LENGTH 255
78 #define THUMB_HASH_LEN 256
79 #define MIME_LENGTH 50
80 #define _3GP_FILE ".3gp"
81 #define _MP4_FILE ".mp4"
82 #define _ASF_FILE ".asf"
83 #define MEDIA_SVC_ARTWORK_SIZE 2000
84 #define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
85 #define IMAGE_PREFIX "image/"
86 #define IMAGE_PREFIX_LEN 6
87
88 #define MEDIA_SVC_DEFAULT_GPS_VALUE                     -200                    /**< Default GPS Value*/
89
90 #define MEDIA_SVC_PDF_TAG_TAIL_LEN 12
91 #define MEDIA_SVC_PDF_BUF_SIZE 256
92
93 typedef struct {
94         char content_type[15];
95         int category_by_mime;
96 } _media_svc_content_table_s;
97
98 enum Exif_Orientation {
99         NOT_AVAILABLE = 0,
100         NORMAL = 1,
101         HFLIP = 2,
102         ROT_180 = 3,
103         VFLIP = 4,
104         TRANSPOSE = 5,
105         ROT_90 = 6,
106         TRANSVERSE = 7,
107         ROT_270 = 8
108 };
109
110 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
111         {"audio", MEDIA_SVC_CATEGORY_SOUND},
112         {"image", MEDIA_SVC_CATEGORY_IMAGE},
113         {"video", MEDIA_SVC_CATEGORY_VIDEO},
114         {"application", MEDIA_SVC_CATEGORY_ETC},
115         {"text", MEDIA_SVC_CATEGORY_ETC},
116 };
117
118 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
119         /*known mime types of normal files*/
120         "mpeg",
121         "ogg",
122         "x-ms-wma",
123         "x-flac",
124         "mp4",
125         /* known mime types of drm files*/
126         "mp3",
127         "x-mp3", /*alias of audio/mpeg*/
128         "x-mpeg", /*alias of audio/mpeg*/
129         "3gpp",
130         "x-ogg", /*alias of audio/ogg*/
131         "vnd.ms-playready.media.pya:*.pya", /*playready*/
132         "wma",
133         "aac",
134         "x-m4a", /*alias of audio/mp4*/
135         /* below mimes are rare*/
136         "x-vorbis+ogg",
137         "x-flac+ogg",
138         "x-matroska",
139         "ac3",
140         "mp2",
141         "x-ape",
142         "x-ms-asx",
143         "vnd.rn-realaudio",
144
145         "x-vorbis", /*alias of audio/x-vorbis+ogg*/
146         "vorbis", /*alias of audio/x-vorbis+ogg*/
147         "x-oggflac",
148         "x-mp2", /*alias of audio/mp2*/
149         "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
150         "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
151         "x-wav",
152 };
153
154 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
155         "x-smaf",
156 };
157
158 char *_media_info_generate_uuid(void)
159 {
160         uuid_t uuid_value;
161         char uuid_unparsed[37];
162
163 RETRY_GEN:
164         uuid_generate(uuid_value);
165         uuid_unparse(uuid_value, uuid_unparsed);
166
167         if (strlen(uuid_unparsed) < 36) {
168                 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
169                 goto RETRY_GEN;
170         }
171
172         return g_strdup(uuid_unparsed);
173 }
174
175 /* GPS information is not ExifTag member */
176 static int __media_svc_get_exif_gps_double(ExifData *ed, double *value, long tagtype)
177 {
178         ExifEntry *entry;
179         char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
180         double tmp_arr[3] = { 0.0, 0.0, 0.0 };
181         int i = 0;
182         char **tmp_split = NULL;
183
184         media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
185         media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
186
187         entry = exif_data_get_entry(ed, tagtype);
188         media_svc_retv_if(!entry, MS_MEDIA_ERR_INTERNAL);
189
190         exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
191         gps_buf[strlen(gps_buf)] = '\0';
192
193         tmp_split = g_strsplit(gps_buf, ",", -1);
194         if (g_strv_length(tmp_split) != 3) {
195                 g_strfreev(tmp_split);
196                 media_svc_error("Wrong GPS format");
197                 return MS_MEDIA_ERR_INTERNAL;
198         }
199
200         for (i = 0; i < 3; i++)
201                 tmp_arr[i] = g_strtod(tmp_split[i], NULL);
202
203         g_strfreev(tmp_split);
204
205         *value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
206
207         return MS_MEDIA_ERR_NONE;
208 }
209
210 static int __media_svc_get_exif_gps_str(ExifData *ed, char *value, long tagtype)
211 {
212         ExifEntry *entry;
213
214         media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
215         media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
216
217         entry = exif_data_get_entry(ed, tagtype);
218         if (entry) {
219                 exif_entry_get_value(entry, value, MEDIA_SVC_METADATA_LEN_MAX);
220                 value[strlen(value)] = '\0';
221         }
222
223         return MS_MEDIA_ERR_NONE;
224 }
225
226 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, ExifTag tagtype)
227 {
228         ExifEntry *entry;
229         ExifByteOrder mByteOrder;
230         ExifRational mRational;
231         long numerator, denominator;
232
233         media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
234
235         entry = exif_data_get_entry(ed, tagtype);
236         media_svc_retv_if(!entry, MS_MEDIA_ERR_NONE);
237
238         switch (tagtype) {
239         case EXIF_TAG_ORIENTATION:
240         case EXIF_TAG_PIXEL_X_DIMENSION:
241         case EXIF_TAG_PIXEL_Y_DIMENSION:
242         case EXIF_TAG_ISO_SPEED_RATINGS:
243                 media_svc_retvm_if(!i_value, MS_MEDIA_ERR_INVALID_PARAMETER, "i_value is NULL");
244
245                 mByteOrder = exif_data_get_byte_order(ed);
246                 short exif_value = exif_get_short(entry->data, mByteOrder);
247                 *i_value = (int)exif_value;
248                 break;
249         case EXIF_TAG_EXPOSURE_TIME:
250                 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
251
252                 mByteOrder = exif_data_get_byte_order(ed);
253                 mRational = exif_get_rational(entry->data, mByteOrder);
254                 numerator = mRational.numerator;
255                 denominator = mRational.denominator;
256                 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
257                 break;
258         case EXIF_TAG_FNUMBER:
259                 media_svc_retvm_if(!d_value, MS_MEDIA_ERR_INVALID_PARAMETER, "d_value is NULL");
260
261                 mByteOrder = exif_data_get_byte_order(ed);
262                 mRational = exif_get_rational(entry->data, mByteOrder);
263                 numerator = mRational.numerator;
264                 denominator = mRational.denominator;
265
266                 *d_value = ((numerator*1.0)/(denominator*1.0));
267                 break;
268         default:
269                 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
270
271                 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
272                 buf[strlen(buf)] = '\0';
273         }
274
275         return MS_MEDIA_ERR_NONE;
276 }
277
278 static time_t __media_svc_get_timeline_from_str(const char *timstr)
279 {
280         struct tm t;
281         time_t modified_t = 0;
282         time_t rawtime;
283         struct tm timeinfo;
284
285         if (!STRING_VALID(timstr)) {
286                 media_svc_error("Invalid Parameter");
287                 return 0;
288         }
289
290         /*Exif Format : %Y:%m:%d %H:%M:%S
291         Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
292         memset(&t, 0x00, sizeof(struct tm));
293
294         tzset();
295         time(&rawtime);
296         localtime_r(&rawtime, &timeinfo);
297
298         if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
299                 t.tm_isdst = timeinfo.tm_isdst;
300                 if (t.tm_isdst != 0)
301                         media_svc_debug("DST %d", t.tm_isdst);
302
303                 /* If time string has timezone */
304                 if (strptime(timstr, "%Y:%m:%d %H:%M:%S %z", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S %z", &t)) {
305                         char tim_tmp_str[255] = { 0, };
306
307                         /* ISO8601 Time string format */
308                         strftime(tim_tmp_str, 255, "%Y-%m-%dT%H:%M:%S%z", &t);
309                         GDateTime *pdatetime = g_date_time_new_from_iso8601(tim_tmp_str, NULL);
310                         if (pdatetime)
311                                 modified_t = g_date_time_to_unix(pdatetime);
312                         g_date_time_unref(pdatetime);
313                         media_svc_debug("Calibrated timeval : [%ld][%s]", modified_t, tim_tmp_str);
314                 } else {
315                         /* Just localtime */
316                         modified_t = mktime(&t);
317                 }
318
319                 if (modified_t > 0)
320                         return modified_t;
321                 else
322                         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);
323         } else {
324                 media_svc_error("Failed to get timeline : [%s]", timstr);
325         }
326
327         return 0;
328 }
329
330 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
331 {
332         int idx = 0;
333
334         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is null");
335         media_svc_retvm_if(mimetype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mimetype is null");
336         media_svc_retvm_if(category == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "category is null");
337
338         *category = MEDIA_SVC_CATEGORY_UNKNOWN;
339
340         /*categorize from mimetype */
341         for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
342                 if (strncmp(mimetype, content_category[idx].content_type, strlen(content_category[idx].content_type)) == 0) {
343                         *category = (*category | content_category[idx].category_by_mime);
344                         break;
345                 }
346         }
347
348         /*in application type, exitst sound file ex) x-smafs, asf */
349         if (*category & MEDIA_SVC_CATEGORY_ETC) {
350                 int prefix_len = strlen(content_category[3].content_type) + 1;
351                 char *ext = NULL;
352
353                 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
354                         if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
355                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
356                                 *category |= MEDIA_SVC_CATEGORY_SOUND;
357                                 break;
358                         }
359                 }
360
361                 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
362                         *category ^= MEDIA_SVC_CATEGORY_ETC;
363                         *category |= MEDIA_SVC_CATEGORY_SOUND;
364                 }
365
366                 /*"asf" must check video stream and then categorize in directly. */
367                 ext = strrchr(path, '.');
368                 if (ext != NULL) {
369                         if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
370                                 int audio = 0;
371                                 int video = 0;
372                                 int err = 0;
373
374                                 err = mm_file_get_stream_info(path, &audio, &video);
375                                 if (err == 0) {
376                                         if (audio > 0 && video == 0) {
377                                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
378                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
379                                         } else {
380                                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
381                                                 *category |= MEDIA_SVC_CATEGORY_VIDEO;
382                                         }
383                                 }
384                         }
385                 }
386
387                 if (g_str_has_suffix(mimetype, "epub+zip") || g_str_has_suffix(mimetype, "pdf")) {
388                         *category ^= MEDIA_SVC_CATEGORY_ETC;
389                         *category |= MEDIA_SVC_CATEGORY_BOOK;
390                 }
391         }
392
393         /*check music file in sound files. */
394         if (*category & MEDIA_SVC_CATEGORY_SOUND) {
395                 int prefix_len = strlen(content_category[0].content_type) + 1;
396
397                 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
398                         if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
399                                 *category ^= MEDIA_SVC_CATEGORY_SOUND;
400                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
401                                 break;
402                         }
403                 }
404
405                 /*m3u file is playlist but mime type is "audio/x-mpegurl". but It has to be classified into MS_CATEGORY_ETC since playlist is not a sound track*/
406                 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
407                         *category ^= MEDIA_SVC_CATEGORY_SOUND;
408                         *category |= MEDIA_SVC_CATEGORY_ETC;
409                 }
410         } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
411                 /*some video files don't have video stream. in this case it is categorize as music. */
412                 char *ext = NULL;
413                 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
414                 ext = strrchr(path, '.');
415                 if (ext != NULL) {
416                         if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
417                                 int audio = 0;
418                                 int video = 0;
419                                 int err = 0;
420
421                                 err = mm_file_get_stream_info(path, &audio, &video);
422                                 if (err == 0) {
423                                         if (audio > 0 && video == 0) {
424                                                 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
425                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
426                                         }
427                                 }
428                                 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
429                         }
430                 }
431         }
432
433         return MS_MEDIA_ERR_NONE;
434 }
435
436 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
437 {
438         int ret = MS_MEDIA_ERR_NONE;
439         int category = 0;
440
441         ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
442         if (ret != MS_MEDIA_ERR_NONE)
443                 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
444
445         switch (category) {
446         case MEDIA_SVC_CATEGORY_SOUND:
447                 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
448                 break;
449         case MEDIA_SVC_CATEGORY_MUSIC:
450                 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
451                 break;
452         case MEDIA_SVC_CATEGORY_IMAGE:
453                 *media_type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
454                 break;
455         case MEDIA_SVC_CATEGORY_VIDEO:
456                 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
457                 break;
458         case MEDIA_SVC_CATEGORY_BOOK:
459                 *media_type = MEDIA_SVC_MEDIA_TYPE_BOOK;
460                 break;
461         default:
462                 *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
463         }
464
465         return ret;
466 }
467
468 /*
469 drm_contentifo is not NULL, if the file is OMA DRM.
470 If the file is not OMA DRM, drm_contentinfo must be NULL.
471 */
472 static int __media_svc_get_mime_type(const char *path, char *mimetype)
473 {
474         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
475
476         /*in case of normal files or failure to get mime in drm */
477         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
478                 media_svc_error("aul_get_mime_from_file fail");
479                 return MS_MEDIA_ERR_INTERNAL;
480         }
481
482         return MS_MEDIA_ERR_NONE;
483 }
484
485 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
486 {
487         int i = 0;
488
489         for (i = strlen(file_path); i >= 0; i--) {
490                 if (file_path[i] == '.') {
491                         SAFE_STRLCPY(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
492                         return true;
493                 }
494
495                 if (file_path[i] == '/')
496                         return false;
497         }
498         return false;
499 }
500
501 static int __media_svc_safe_atoi(char *buffer, int *si)
502 {
503         char *end = NULL;
504         errno = 0;
505         media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
506
507         const long sl = strtol(buffer, &end, 10);
508
509         media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
510         media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
511         media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
512         media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
513         media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
514
515         *si = (int)sl;
516
517         return MS_MEDIA_ERR_NONE;
518 }
519
520 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
521 {
522         int ret = MS_MEDIA_ERR_NONE;
523         struct statfs fs;
524         char *thumb_path = NULL;
525         long bsize_kbytes = 0;
526         GError *error = NULL;
527
528         media_svc_retvm_if(!image || size == 0, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid image");
529         media_svc_sec_debug("start save image, path [%s] image size [%d]", image_path, size);
530
531         ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
532         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path error");
533
534         ret = statfs(thumb_path, &fs);
535         g_free(thumb_path);
536         media_svc_retvm_if(ret == -1, MS_MEDIA_ERR_INTERNAL, "statfs failed");
537
538         bsize_kbytes = fs.f_bsize >> 10;
539         media_svc_retvm_if((bsize_kbytes * fs.f_bavail) < 1024, MS_MEDIA_ERR_NOT_ENOUGH_SPACE, "Not enough space");
540
541         if (!g_file_set_contents(image_path, (const gchar *)image, (gssize)size, &error)) {
542                 media_svc_error("g_file_set_contents faild:%s", error->message);
543                 g_error_free(error);
544                 return MS_MEDIA_ERR_INTERNAL;
545         }
546
547         return MS_MEDIA_ERR_NONE;
548 }
549
550 static char *__media_svc_get_title_from_filepath(const char *path)
551 {
552         char *filename = NULL;
553         char *title = NULL;
554         char *last_dot = NULL;
555
556         media_svc_retvm_if(!STRING_VALID(path), NULL, "Invalid path");
557
558         filename = g_path_get_basename(path);
559
560         last_dot = strrchr(filename, '.');
561         if (last_dot) {
562                 title = g_strndup(filename, last_dot - filename);
563                 g_free(filename);
564         } else {
565                 title = filename;
566         }
567
568         media_svc_debug("extract title is [%s]", title);
569
570         return title;
571 }
572
573 void _media_svc_remove_file(const char *path)
574 {
575         if (!STRING_VALID(path))
576                 return;
577
578         if (remove(path) != 0)
579                 media_svc_stderror("fail to remove file result");
580 }
581
582 int _media_svc_get_thumbnail_path(char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
583 {
584         int ret = MS_MEDIA_ERR_NONE;
585         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
586         char hash[THUMB_HASH_LEN] = {0, };
587         char *thumb_dir = NULL;
588
589         ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
590         media_svc_retvm_if(!STRING_VALID(thumb_dir), MS_MEDIA_ERR_INTERNAL, "ms_user_get_root_thumb_store_path failed");
591
592         if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
593                 media_svc_error("Wrong path[%s]", thumb_dir);
594                 g_free(thumb_dir);
595                 return MS_MEDIA_ERR_INTERNAL;
596         }
597
598         memset(file_ext, 0, sizeof(file_ext));
599         if (!__media_svc_get_file_ext(pathname, file_ext))
600                 media_svc_error("get file ext fail");
601
602         ret = mb_svc_generate_hash_code(pathname, hash, THUMB_HASH_LEN);
603         if (ret != MS_MEDIA_ERR_NONE) {
604                 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
605                 g_free(thumb_dir);
606                 return MS_MEDIA_ERR_INTERNAL;
607         }
608
609         if (img_format) {
610                 /* 'img_format' is mime-type */
611                 if (!g_str_has_prefix(img_format, IMAGE_PREFIX) || strlen(img_format) == IMAGE_PREFIX_LEN) {
612                         media_svc_error("Not proper img format");
613                         g_free(thumb_dir);
614                         return MS_MEDIA_ERR_INTERNAL;
615                 }
616
617                 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, img_format + IMAGE_PREFIX_LEN);
618         } else {
619                 if (strcasecmp(file_ext, "PNG") == 0)
620                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
621                 else
622                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
623         }
624
625         g_free(thumb_dir);
626
627         return MS_MEDIA_ERR_NONE;
628 }
629
630 int _media_svc_get_file_time(const char *full_path)
631 {
632         struct stat statbuf = { 0, };
633
634         if (stat(full_path, &statbuf) == -1) {
635                 media_svc_stderror("stat fails.");
636                 return 0;
637         }
638
639         return statbuf.st_mtime;
640 }
641
642 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)
643 {
644         int ret = MS_MEDIA_ERR_NONE;
645         bool drm_type = false;
646         char mime_type[256] = {0, };
647         media_svc_media_type_e media_type;
648
649         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
650         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
651
652         content_info->path = g_strdup(path);
653         media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
654
655         struct stat st;
656         memset(&st, 0, sizeof(struct stat));
657         if (stat(path, &st) == 0) {
658                 content_info->modified_time = st.st_mtime;
659                 content_info->timeline = content_info->modified_time;
660                 content_info->size = st.st_size;
661         } else {
662                 media_svc_stderror("stat failed");
663         }
664
665         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
666         if (refresh) {
667                 media_svc_debug("refresh");
668                 return MS_MEDIA_ERR_NONE;
669         }
670
671         content_info->storage_uuid = g_strdup(storage_id);
672         media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
673
674         content_info->storage_type = storage_type;
675         time(&content_info->added_time);
676
677         content_info->media_uuid = _media_info_generate_uuid();
678         media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
679
680         content_info->file_name = g_path_get_basename(path);
681         media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
682
683         /* if the file is DRM file, drm_type value is DRM_TRUE(1).
684         if drm_contentinfo is not NULL, the file is OMA DRM.*/
685         ret = __media_svc_get_mime_type(path, mime_type);
686         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
687
688         media_svc_debug("mime [%s]", mime_type);
689         content_info->is_drm = drm_type;
690
691         ret = __media_svc_get_media_type(path, mime_type, &media_type);
692         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
693
694         content_info->mime_type = g_strdup(mime_type);
695         media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
696
697         media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, media_type);
698
699         content_info->media_type = media_type;
700
701         return MS_MEDIA_ERR_NONE;
702 }
703
704 static int __image_360_check(const char *path)
705 {
706         FILE *fp = NULL;
707         size_t size = 0, app1_size = 0, exif_app1_xmp_size = 0;
708         unsigned char exif_header[4] = {0, };
709         unsigned char exif_app1[2] = {0, };
710         unsigned char exif_app1_xmp[2] = {0, };
711         unsigned char exif_app1_xmp_t[2] = {0, };
712         GString *xmp_data = NULL;
713         int fdata = 0;
714         int result = 0;
715
716         memset(exif_header, 0x00, sizeof(exif_header));
717         memset(exif_app1, 0x00, sizeof(exif_app1));
718         memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
719         memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
720
721         fp = fopen(path, "rb");
722         if (fp == NULL)
723                 goto ERROR;
724
725         size = fread(exif_header, 1, sizeof(exif_header), fp);
726         if (size != sizeof(exif_header))
727                 goto ERROR;
728
729         if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
730                 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
731                 if (size != sizeof(exif_app1))
732                         goto ERROR;
733
734                 if ((size_t)((exif_app1[0] << 8) | (exif_app1[1])) <= 2)
735                         goto ERROR;
736
737                 app1_size = (size_t)((exif_app1[0] << 8) | (exif_app1[1])) - 2 ;
738                 if (fseek(fp, app1_size, SEEK_CUR) != 0)
739                         goto ERROR;
740
741                 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
742                 if (size != sizeof(exif_app1_xmp))
743                         goto ERROR;
744
745                 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
746                         size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
747                         if (size != sizeof(exif_app1_xmp_t))
748                                 goto ERROR;
749
750                         if ((size_t)((exif_app1_xmp_t[0] << 8) | (exif_app1_xmp_t[1])) <= 2)
751                                 goto ERROR;
752
753                         exif_app1_xmp_size = (size_t)((exif_app1_xmp_t[0] << 8) | (exif_app1_xmp_t[1])) - 2;
754
755                         xmp_data = g_string_sized_new(exif_app1_xmp_size);
756
757                         do {
758                                 exif_app1_xmp_size--;
759                                 fdata = fgetc(fp);
760                                 if (fdata == EOF)
761                                         continue;
762                                 if (fdata == '\0')
763                                         continue;
764
765                                 xmp_data = g_string_append_c(xmp_data, (gchar)fdata);
766                         } while (exif_app1_xmp_size > 0);
767
768                         if (strstr(xmp_data->str, "UsePanoramaViewer") &&
769                                 strstr(xmp_data->str, "True") &&
770                                 strstr(xmp_data->str, "ProjectionType") &&
771                                 strstr(xmp_data->str, "equirectangular"))
772                                 result = 1;
773
774                         g_string_free(xmp_data, TRUE);
775                 }
776         }
777
778 ERROR:
779         if (fp) {
780                 fclose(fp);
781                 fp = NULL;
782         }
783
784         return result;
785 }
786
787 static char * __media_svc_get_title(MMHandleType tag, const char *path)
788 {
789         int ret = FILEINFO_ERROR_NONE;
790         char *p = NULL;
791         int size = 0;
792         char *title = NULL;
793
794         if (tag) {
795                 ret = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
796                 if (ret == FILEINFO_ERROR_NONE && size > 0) {
797                         while(p && isspace(*p))
798                                 p++;
799
800                         return g_strdup(p);
801                 }
802         }
803
804         title = __media_svc_get_title_from_filepath(path);
805         if (title)
806                 return title;
807
808         media_svc_error("Can't extract title");
809
810         return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
811 }
812
813 char * _media_svc_get_title_by_path(const char *path)
814 {
815         /* No MMHandleType in media-svc.c */
816         return __media_svc_get_title(NULL, path);
817 }
818
819 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
820 {
821         double value = 0.0;
822         int orient_value = 0;
823         int exif_width = 0;
824         int exif_height = 0;
825         ExifData *ed = NULL;
826         bool has_datetaken = false;
827         double fnumber = 0.0;
828         int iso = 0;
829         char *path = NULL;
830
831         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
832
833         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
834         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);
835         media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
836
837         path = content_info->path;
838         content_info->media_meta.title = __media_svc_get_title(NULL, path);
839
840         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
841         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
842         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
843
844         /* Not used. But to preserved the behavior, set MEDIA_SVC_TAG_UNKNOWN. */
845         content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
846         content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
847         content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
848         content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
849         content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
850         content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
851         content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
852         content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
853
854         /* Load an ExifData object from an EXIF file */
855         ed = exif_data_new_from_file(path);
856         if (!ed) {
857                 media_svc_sec_debug("There is no exif data in [ %s ]", path);
858                 goto GET_WIDTH_HEIGHT;
859         }
860
861         content_info->media_meta.is_360 = __image_360_check(path);
862
863         memset(buf, 0x00, sizeof(buf));
864         if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
865                 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
866                         if (!g_strcmp0(buf, "S"))
867                                 value *= -1;
868                         content_info->media_meta.latitude = value;
869                 }
870         }
871
872         memset(buf, 0x00, sizeof(buf));
873         if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
874                 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
875                         if (!g_strcmp0(buf, "W"))
876                                 value *= -1;
877
878                         content_info->media_meta.longitude = value;
879                 }
880         }
881
882         memset(buf, 0x00, sizeof(buf));
883         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
884                 if (strlen(buf) > 0)
885                         content_info->media_meta.description = g_strdup(buf);
886                 else
887                         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
888         }
889
890         memset(buf, 0x00, sizeof(buf));
891         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
892                 if (strlen(buf) > 0) {
893                         has_datetaken = true;
894                         content_info->media_meta.datetaken = g_strdup(buf);
895
896                         /* This is same as recorded_date */
897                         content_info->media_meta.recorded_date = g_strdup(buf);
898                 }
899         }
900
901         memset(buf, 0x00, sizeof(buf));
902         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
903                 if (strlen(buf) > 0) {
904                         has_datetaken = true;
905                         content_info->media_meta.datetaken = g_strdup(buf);
906
907                         /* This is same as recorded_date */
908                         content_info->media_meta.recorded_date = g_strdup(buf);
909                 }
910         }
911
912         if (has_datetaken) {
913                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
914                 if (content_info->timeline == 0)
915                         content_info->timeline = content_info->modified_time;
916                 else
917                         media_svc_debug("Timeline : %ld", content_info->timeline);
918         }
919
920         memset(buf, 0x00, sizeof(buf));
921         /* Get exposure_time value from exif. */
922         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
923                 if (strlen(buf) > 0)
924                         content_info->media_meta.exposure_time = g_strdup(buf);
925         }
926
927         /* Get fnumber value from exif. */
928         if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE)
929                 content_info->media_meta.fnumber = fnumber;
930
931         /* Get iso value from exif. */
932         if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE)
933                 content_info->media_meta.iso = iso;
934
935         memset(buf, 0x00, sizeof(buf));
936         /* Get model value from exif. */
937         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
938                 if (strlen(buf) > 0)
939                         content_info->media_meta.model = g_strdup(buf);
940         }
941
942         /* Get orientation value from exif. */
943         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
944                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
945                         content_info->media_meta.orientation = orient_value;
946         }
947
948         /* Get width value from exif. */
949         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
950                 if (exif_width > 0)
951                         content_info->media_meta.width = exif_width;
952         }
953
954         /* Get height value from exif. */
955         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
956                 if (exif_height > 0)
957                         content_info->media_meta.height = exif_height;
958         }
959
960         if (ed)
961                 exif_data_unref(ed);
962
963 GET_WIDTH_HEIGHT:
964
965         if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
966                 /*Get image width, height*/
967                 unsigned int img_width = 0;
968                 unsigned int img_height = 0;
969                 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
970
971                 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
972                 if (content_info->media_meta.width == 0)
973                         content_info->media_meta.width = img_width;
974
975                 if (content_info->media_meta.height == 0)
976                         content_info->media_meta.height = img_height;
977         }
978
979         return MS_MEDIA_ERR_NONE;
980 }
981
982 static char * __media_svc_get_tag_str_value(MMHandleType tag, const char *tag_name)
983 {
984         int ret = FILEINFO_ERROR_NONE;
985         char *p = NULL;
986         int size = 0;
987
988         ret = mm_file_get_attrs(tag, tag_name, &p, &size, NULL);
989         if (ret == FILEINFO_ERROR_NONE && size > 0)
990                 return g_strdup(p);
991
992         return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
993 }
994
995 int _media_svc_extract_music_metadata_for_update(media_svc_content_info_s *content_info, const char *path)
996 {
997         MMHandleType tag = 0;
998         int mmf_error = FILEINFO_ERROR_NONE;
999
1000         content_info->path = g_strdup(path);
1001
1002         mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
1003         if (mmf_error == FILEINFO_ERROR_NONE) {
1004                 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
1005                 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
1006                 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
1007                 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
1008                 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
1009                 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
1010                 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
1011                 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
1012         } else {
1013                 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1014         }
1015
1016         mm_file_destroy_tag_attrs(tag);
1017
1018         return MS_MEDIA_ERR_NONE;
1019 }
1020
1021 int _media_svc_extract_media_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
1022 {
1023         MMHandleType content = 0;
1024         MMHandleType tag = 0;
1025         char *p = NULL;
1026         unsigned char *image = NULL;
1027         unsigned int size = 0;
1028         int mmf_error = FILEINFO_ERROR_NONE;
1029         int album_id = 0;
1030         int ret = MS_MEDIA_ERR_NONE;
1031         int convert_value = 0;
1032         int cdis_value = 0;
1033
1034         /*Get Content Tag attribute ===========*/
1035         if (content_info->storage_type == MS_USER_STORAGE_EXTERNAL_USB)
1036                 mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
1037         else
1038                 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1039
1040         if (mmf_error == FILEINFO_ERROR_NONE) {
1041                 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
1042                 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
1043                 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
1044                 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
1045                 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
1046                 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
1047                 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
1048                 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
1049                 content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
1050
1051                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1052                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1053                         if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
1054                                 /*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*/
1055                                 char *p_value = g_strdelimit(g_strdup(p), "-", ':');
1056                                 content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
1057                                 g_free(p_value);
1058                         } else {
1059                                 content_info->media_meta.recorded_date = g_strdup(p);
1060                         }
1061
1062                         if (STRING_VALID(content_info->media_meta.recorded_date)) {
1063                                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1064                                 if (content_info->timeline == 0)
1065                                         content_info->timeline = content_info->modified_time;
1066
1067                                 /* This is same as datetaken */
1068                                 /* Remove compensation string */
1069                                 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
1070                                         content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
1071                                         g_free(content_info->media_meta.recorded_date);
1072                                         content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
1073                                 } else {
1074                                         content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
1075                                 }
1076                         }
1077                 }
1078
1079                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
1080                 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
1081                         if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1082                                 content_info->media_meta.year = g_strdup(p);
1083                 }
1084
1085                 if (!content_info->media_meta.year)
1086                                 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1087
1088                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RATING, &p, &size, NULL);
1089                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1090                         if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1091                                 content_info->media_meta.rating = convert_value;
1092                 } else {
1093                         content_info->media_meta.rating = 0;
1094                 }
1095
1096                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1097
1098                 /*Do not extract artwork for the USB Storage content*/
1099                 if (content_info->storage_type != MS_USER_STORAGE_EXTERNAL_USB) {
1100                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1101                         if (mmf_error != FILEINFO_ERROR_NONE)
1102                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1103
1104                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1105                         if (mmf_error != FILEINFO_ERROR_NONE)
1106                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1107
1108                         if (image != NULL && size > 0) {
1109                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1110                                 int artwork_mime_size = -1;
1111
1112                                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1113                                 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1114                                         ret = _media_svc_get_thumbnail_path(thumb_path, content_info->path, p, uid);
1115                                         if (ret != MS_MEDIA_ERR_NONE) {
1116                                                 media_svc_error("Fail to Get Thumbnail Path");
1117                                         } else {
1118                                                 ret = __media_svc_save_image(image, size, thumb_path, uid);
1119                                                 if (ret != MS_MEDIA_ERR_NONE) {
1120                                                         media_svc_error("Fail to Save Image");
1121                                                 } else {
1122                                                         content_info->thumbnail_path = g_strdup(thumb_path);
1123                                                 }
1124                                         }
1125                                 }
1126                         }
1127                 }
1128
1129                 /*Initialize album_id to 0. below code will set the album_id*/
1130                 content_info->album_id = album_id;
1131                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1132                 if (ret != MS_MEDIA_ERR_NONE) {
1133                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1134                                 media_svc_debug("album does not exist. So start to make album art");
1135                                 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1136                                         (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1137                                         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);
1138                                 else
1139                                         ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1140
1141                                 content_info->album_id = album_id;
1142                         }
1143                 } else {
1144                         content_info->album_id = album_id;
1145                 }
1146
1147                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1148                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1149                 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1150
1151                 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1152                         double longitude = 0.0;
1153                         double latitude = 0.0;
1154                         double altitude = 0.0;
1155
1156                         mm_file_get_attrs(tag, MM_FILE_TAG_LONGITUDE, &longitude,
1157                                 MM_FILE_TAG_LATIDUE, &latitude,
1158                                 MM_FILE_TAG_ALTIDUE, &altitude,
1159                                 NULL);
1160
1161                         content_info->media_meta.longitude = (longitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : longitude;
1162                         content_info->media_meta.latitude = (latitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : latitude;
1163                         content_info->media_meta.altitude = (altitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : altitude;
1164
1165                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1166                         if (mmf_error != FILEINFO_ERROR_NONE)
1167                                 cdis_value = 0;
1168
1169                         media_svc_debug("CDIS : %d", cdis_value);
1170
1171                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1172                         if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1173                                 content_info->media_meta.orientation = atoi(p);
1174                         } else {
1175                                 content_info->media_meta.orientation = 0;
1176                                 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1177                         }
1178                 }
1179
1180                 mmf_error = mm_file_destroy_tag_attrs(tag);
1181                 if (mmf_error != FILEINFO_ERROR_NONE)
1182                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1183         }       else {
1184                 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1185                 content_info->album_id = album_id;
1186         }
1187
1188         /*Get Content attribute ===========*/
1189         if (cdis_value == 1)
1190                 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1191         else
1192                 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1193
1194         media_svc_retvm_if(mmf_error != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_NONE, "mm_file_create_content_attrs failed");
1195
1196         if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1197                 int audio_bitrate = 0;
1198                 int video_bitrate = 0;
1199
1200                 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1201                         MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate,
1202                         MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate,
1203                         MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width,
1204                         MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height,
1205                         NULL);
1206
1207                 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1208         } else {
1209                 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1210                         MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate,
1211                         MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate,
1212                         MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel,
1213                         MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample,
1214                         NULL);
1215         }
1216
1217         mm_file_destroy_content_attrs(content);
1218
1219         return MS_MEDIA_ERR_NONE;
1220 }
1221
1222 static gchar * __media_svc_get_zipfile_data(zip_t *z, const char *fname)
1223 {
1224         int err = 0;
1225         zip_int64_t index_num = 0;
1226         zip_file_t *file = NULL;
1227         zip_stat_t sb = {0, };
1228         gchar *buf = NULL;
1229
1230         media_svc_retvm_if(!z, NULL, "z is NULL");
1231         media_svc_retvm_if(!fname, NULL, "fname is NULL");
1232
1233         index_num = zip_name_locate(z, fname, ZIP_FL_NOCASE);
1234         media_svc_retvm_if(index_num == -1, NULL, "fname is not exists [%s]", fname);
1235
1236         err = zip_stat_index(z, index_num, ZIP_STAT_SIZE, &sb);
1237         media_svc_retvm_if(err == -1, NULL, "zip_stat_index failed");
1238
1239         file = zip_fopen_index(z, index_num, ZIP_FL_UNCHANGED);
1240         media_svc_retvm_if(!file, NULL, "zip_fopen_index failed");
1241
1242         buf = g_malloc0(sb.size);
1243
1244         err = zip_fread(file, buf, sb.size);
1245         zip_fclose(file);
1246
1247         if (err == -1) {
1248                 g_free(buf);
1249                 buf = NULL;
1250         }
1251
1252         return buf;
1253 }
1254
1255 static xmlNodePtr __media_svc_find_node(xmlNodePtr node, const char *key)
1256 {
1257         xmlNodePtr tmp = NULL;
1258
1259         media_svc_retvm_if(!node, NULL, "node is NULL");
1260         media_svc_retvm_if(!key, NULL, "key is NULL");
1261
1262         for (tmp = node->children; tmp; tmp = tmp->next) {
1263                 if (xmlIsBlankNode(tmp))
1264                         continue;
1265
1266                 if (g_str_has_suffix((gchar *)tmp->name, key))
1267                         return tmp;
1268         }
1269
1270         return NULL;
1271 }
1272
1273 static char * __media_svc_remove_escape_c(const char *value)
1274 {
1275         int start = -1;
1276         int end = 0;
1277         int len, i;
1278
1279         media_svc_retv_if(!value, NULL);
1280
1281         len = strlen(value);
1282
1283         for (i = 0; i < len; i++) {
1284                 if (value[i] != 10 && value[i] != 32) { // 10='\n' 32=' '
1285                         if (start == -1)
1286                                 start = i;
1287
1288                         end = i;
1289                 }
1290         }
1291
1292         end = end - start + 1;
1293
1294         return g_strndup(value + start, end);
1295 }
1296
1297 static char * __media_svc_find_and_get_value(xmlNodePtr node, const char *key)
1298 {
1299         xmlNodePtr tmp = NULL;
1300         char *tmp_res = NULL;
1301         char *res = NULL;
1302
1303         media_svc_retvm_if(!node, NULL, "node is NULL");
1304         media_svc_retvm_if(!key, NULL, "key is NULL");
1305
1306         for (tmp = node->children; tmp; tmp = tmp->next) {
1307                 if (xmlIsBlankNode(tmp))
1308                         continue;
1309
1310                 if (tmp->children) {
1311                         tmp_res = __media_svc_find_and_get_value(tmp, key);
1312                         if (tmp_res) {
1313                                 res = __media_svc_remove_escape_c(tmp_res);
1314                                 xmlFree(tmp_res);
1315                                 return res;
1316                         }
1317                 }
1318
1319                 if (g_str_has_suffix((gchar *)tmp->name, key))
1320                         return (char *)xmlNodeGetContent(tmp);
1321         }
1322
1323         return NULL;
1324 }
1325
1326 static gboolean __media_svc_get_epub_root_file(zip_t *z, char **opf_file)
1327 {
1328         gchar *buf = NULL;
1329         xmlDocPtr doc = NULL;
1330         xmlNodePtr node = NULL;
1331
1332         media_svc_retvm_if(!z, FALSE, "z is NULL");
1333         media_svc_retvm_if(!opf_file, FALSE, "opf_file is NULL");
1334
1335         buf = __media_svc_get_zipfile_data(z, "META-INF/container.xml");
1336         media_svc_retvm_if(!buf, FALSE, "buf is NULL");
1337
1338         doc = xmlParseDoc((const xmlChar *)buf);
1339         g_free(buf);
1340         media_svc_retvm_if(!doc, FALSE, "doc is NULL");
1341
1342         node = xmlDocGetRootElement(doc);
1343         node = __media_svc_find_node(node, "rootfiles");
1344         node = __media_svc_find_node(node, "rootfile");
1345
1346         *opf_file = (char *)xmlGetProp(node, (const xmlChar *)"full-path");
1347         media_svc_sec_debug("OPF [%s]", *opf_file);
1348         xmlFreeDoc(doc);
1349
1350         return TRUE;
1351 }
1352
1353 static gboolean __media_svc_get_xml_metadata(const xmlChar *buffer, gboolean is_pdf, media_svc_content_info_s *content_info)
1354 {
1355         xmlDocPtr doc = NULL;
1356         xmlNodePtr root = NULL;
1357
1358         media_svc_retvm_if(!buffer, FALSE, "buffer is NULL");
1359         media_svc_retvm_if(!content_info, FALSE, "content_info is NULL");
1360
1361         doc = xmlParseDoc(buffer);
1362         media_svc_retv_if(!doc, FALSE);
1363
1364         root = xmlDocGetRootElement(doc);
1365         if (!root) {
1366                 xmlFreeDoc(doc);
1367                 return FALSE;
1368         }
1369
1370         content_info->media_meta.title = __media_svc_find_and_get_value(root, "title");
1371         if (is_pdf && !content_info->media_meta.title) {
1372                 xmlFreeDoc(doc);
1373                 return FALSE;
1374         }
1375
1376         content_info->media_meta.composer = __media_svc_find_and_get_value(root, "creator");
1377         if (!content_info->media_meta.composer)
1378                 content_info->media_meta.composer = __media_svc_find_and_get_value(root, "author");
1379
1380         content_info->media_meta.copyright = __media_svc_find_and_get_value(root, "publisher");
1381         content_info->media_meta.recorded_date = __media_svc_find_and_get_value(root, "date");
1382         content_info->media_meta.genre = __media_svc_find_and_get_value(root, "subject");
1383         content_info->media_meta.description = __media_svc_find_and_get_value(root, "description");
1384
1385         xmlFreeDoc(doc);
1386
1387         return TRUE;
1388 }
1389
1390 static int __media_svc_get_epub_metadata(media_svc_content_info_s *content_info)
1391 {
1392         int err = 0;
1393         zip_t *z = NULL;
1394         gchar *buf = NULL;
1395         char *opf_path = NULL;
1396
1397         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1398
1399         //1. open epub
1400         z = zip_open(content_info->path, ZIP_RDONLY, &err);
1401         media_svc_retvm_if(err == -1, MS_MEDIA_ERR_INTERNAL, "zip_open failed");
1402
1403         //2. find and read opf file
1404         if (!__media_svc_get_epub_root_file(z, &opf_path)) {
1405                 media_svc_error("__media_svc_get_epub_root_file failed");
1406                 zip_close(z);
1407                 return MS_MEDIA_ERR_INTERNAL;
1408         }
1409
1410         //3. get metadata
1411         buf = __media_svc_get_zipfile_data(z, opf_path);
1412         xmlFree(opf_path);
1413         zip_close(z);
1414         media_svc_retvm_if(!buf, MS_MEDIA_ERR_INTERNAL, "__media_svc_get_zipfile_data failed");
1415
1416         if (!__media_svc_get_xml_metadata((const xmlChar *)buf, FALSE, content_info))
1417                 media_svc_error("__media_svc_get_xml_metadata failed");
1418
1419         g_free(buf);
1420
1421         return MS_MEDIA_ERR_NONE;
1422 }
1423
1424 static int __media_svc_get_pdf_metadata(media_svc_content_info_s *content_info)
1425 {
1426     int fd = 0;
1427     int start_pos = 0;
1428     int end_pos = 0;
1429     int cur_pos = 0;
1430     int search_limit = 0;
1431     char tmp[MEDIA_SVC_PDF_BUF_SIZE + 1] = {0, };
1432     gchar *meta_buf = NULL;
1433     char *found = NULL;
1434
1435         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1436         media_svc_retvm_if(content_info->size < 256, MS_MEDIA_ERR_INTERNAL, "open failed");
1437
1438         fd = open(content_info->path, O_RDONLY);
1439         media_svc_retvm_if(fd < 0, MS_MEDIA_ERR_INTERNAL, "open failed");
1440
1441         search_limit = content_info->size - MEDIA_SVC_PDF_TAG_TAIL_LEN;
1442
1443         while (cur_pos <= search_limit) {
1444                 lseek(fd, cur_pos, SEEK_SET);
1445
1446                 if (read(fd, &tmp, MEDIA_SVC_PDF_BUF_SIZE) != MEDIA_SVC_PDF_BUF_SIZE) {
1447                         media_svc_error("read failed");
1448                         break;
1449                 }
1450
1451                 //1.Find <x:xmpmeta .. </x:xmpmeta> block
1452                 if (start_pos == 0 && (found = strstr(tmp, "<x:xmpmeta"))) {
1453                         start_pos = cur_pos + (found - tmp);
1454 //                      media_svc_error("FIND START_POS[%d]", start_pos);
1455                         found = NULL;
1456                 }
1457
1458
1459                 if (start_pos != 0 && (found = strstr(tmp, "</x:xmpmeta>"))) {
1460                         end_pos = cur_pos + (found - tmp) + MEDIA_SVC_PDF_TAG_TAIL_LEN;
1461 //                      media_svc_error("FIND END_POS[%d]", end_pos);
1462                         found = NULL;
1463                 }
1464
1465                 //2.get metadata using xml parser
1466                 if (start_pos && end_pos) {
1467                         meta_buf = g_malloc0(end_pos - start_pos + 1);
1468
1469                         lseek(fd, start_pos, SEEK_SET);
1470                         if (read(fd, meta_buf, end_pos - start_pos) != end_pos - start_pos)
1471                                 goto NEXT;
1472
1473                         if (__media_svc_get_xml_metadata((const xmlChar *)meta_buf, TRUE, content_info)) {
1474                                 g_free(meta_buf);
1475                                 break;
1476                         }
1477
1478                         g_free(meta_buf);
1479                         meta_buf = NULL;
1480 NEXT:
1481                         start_pos = 0;
1482                         end_pos = 0;
1483                 }
1484
1485                 cur_pos += 240;
1486
1487         }
1488
1489         close(fd);
1490
1491         return MS_MEDIA_ERR_NONE;
1492 }
1493
1494 int _media_svc_extract_book_metadata(media_svc_content_info_s *content_info)
1495 {
1496         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content info is NULL");
1497
1498         if (g_str_has_suffix(content_info->mime_type, "epub+zip"))
1499                 return __media_svc_get_epub_metadata(content_info);
1500         else
1501                 return __media_svc_get_pdf_metadata(content_info);
1502 }
1503
1504 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1505 {
1506         media_svc_retm_if(!content_info, "content info is NULL");
1507
1508         /* Delete media_svc_content_info_s */
1509         g_free(content_info->media_uuid);
1510         g_free(content_info->path);
1511         g_free(content_info->file_name);
1512         g_free(content_info->mime_type);
1513         g_free(content_info->folder_uuid);
1514         g_free(content_info->thumbnail_path);
1515         g_free(content_info->storage_uuid);
1516
1517         /* Delete media_svc_content_meta_s */
1518         g_free(content_info->media_meta.title);
1519         g_free(content_info->media_meta.album);
1520         g_free(content_info->media_meta.artist);
1521         g_free(content_info->media_meta.album_artist);
1522         g_free(content_info->media_meta.genre);
1523         g_free(content_info->media_meta.composer);
1524         g_free(content_info->media_meta.year);
1525         g_free(content_info->media_meta.recorded_date);
1526         g_free(content_info->media_meta.copyright);
1527         g_free(content_info->media_meta.track_num);
1528         g_free(content_info->media_meta.description);
1529         g_free(content_info->media_meta.datetaken);
1530         g_free(content_info->media_meta.exposure_time);
1531         g_free(content_info->media_meta.model);
1532
1533         g_free(content_info->file_name_pinyin);
1534         g_free(content_info->media_meta.title_pinyin);
1535         g_free(content_info->media_meta.album_pinyin);
1536         g_free(content_info->media_meta.artist_pinyin);
1537         g_free(content_info->media_meta.album_artist_pinyin);
1538         g_free(content_info->media_meta.genre_pinyin);
1539         g_free(content_info->media_meta.composer_pinyin);
1540         g_free(content_info->media_meta.copyright_pinyin);
1541         g_free(content_info->media_meta.description_pinyin);
1542 }
1543
1544 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
1545 {
1546         int ret = MS_MEDIA_ERR_NONE;
1547
1548         media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
1549         media_svc_retvm_if(!thumb_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1550         media_svc_retvm_if(!g_file_test(path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "File doesn't exist[%s]", path);
1551
1552         ms_user_storage_type_e store_type = -1;
1553         ret = ms_user_get_storage_type(uid, path, &store_type);
1554
1555         if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
1556                 media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
1557                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1558         }
1559
1560         media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1561
1562         //1. make hash path
1563         ret = _media_svc_get_thumbnail_path(thumb_path, path, NULL, uid);
1564         if (ret != MS_MEDIA_ERR_NONE) {
1565                 media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
1566                 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1567                 return ret;
1568         }
1569
1570         //2. save thumbnail
1571         if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1572                 return create_image_thumbnail_to_file(path, CONTENT_THUMB_DEFAULT_WIDTH, CONTENT_THUMB_DEFAULT_HEIGHT, thumb_path, true);
1573         else
1574                 return create_video_thumbnail_to_file(path, CONTENT_THUMB_DEFAULT_WIDTH, CONTENT_THUMB_DEFAULT_HEIGHT, thumb_path, true);
1575
1576 }
1577
1578 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1579 {
1580         int ret = MS_MEDIA_ERR_NONE;
1581         int size = 0;
1582         pinyin_name_s *pinyinname = NULL;
1583
1584         media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1585         media_svc_retvm_if(pinyin_str == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "pinyin_str is NULL");
1586
1587         *pinyin_str = NULL;
1588
1589         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1590         if (ret == MS_MEDIA_ERR_NONE) {
1591                 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1592                         *pinyin_str = g_strdup(pinyinname[0].pinyin_name);
1593                 else
1594                         *pinyin_str = g_strdup(src_str);        /* Return Original Non China Character */
1595         }
1596
1597         _media_svc_pinyin_free(pinyinname, size);
1598
1599         return ret;
1600 }
1601
1602 bool _media_svc_check_pinyin_support(void)
1603 {
1604         int ret = SYSTEM_INFO_ERROR_NONE;
1605         bool is_supported = false;
1606         static int media_svc_pinyin_support = -1;
1607
1608         if (media_svc_pinyin_support == -1) {
1609                 ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
1610                 if (ret != SYSTEM_INFO_ERROR_NONE) {
1611                         media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
1612                         return false;
1613                 }
1614
1615                 media_svc_pinyin_support = is_supported;
1616         }
1617
1618         return media_svc_pinyin_support;
1619 }
1620
1621 int _media_svc_get_media_type(const char *path, int *mediatype)
1622 {
1623         int ret = MS_MEDIA_ERR_NONE;
1624         char mime_type[256] = {0};
1625         media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1626
1627         media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1628
1629         ret = __media_svc_get_mime_type(path, mime_type);
1630         if (ret == MS_MEDIA_ERR_NONE)
1631                 __media_svc_get_media_type(path, mime_type, &media_type);
1632         else
1633                 media_svc_error("__media_svc_get_mime_type failed");
1634
1635         *mediatype = media_type;
1636
1637         return ret;
1638 }
1639
1640 bool _media_svc_is_valid_storage_type(ms_user_storage_type_e storage_type)
1641 {
1642         switch (storage_type) {
1643         case MS_USER_STORAGE_INTERNAL:
1644         case MS_USER_STORAGE_EXTERNAL:
1645         case MS_USER_STORAGE_EXTERNAL_USB:
1646                 return true;
1647         default:
1648                 media_svc_error("storage type is incorrect[%d]", storage_type);
1649                 return false;
1650         }
1651 }