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