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