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