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