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