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