Fix resource leak for 'file_name_pinyin'
[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 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                 if (strcasecmp(file_ext, "PNG") == 0)
710                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
711                 else
712                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
713         }
714
715         SAFE_FREE(thumb_dir);
716
717         return MS_MEDIA_ERR_NONE;
718 }
719
720 int _media_svc_get_file_time(const char *full_path)
721 {
722         struct stat statbuf;
723         int fd = 0;
724
725         memset(&statbuf, 0, sizeof(struct stat));
726         fd = stat(full_path, &statbuf);
727         if (fd == -1) {
728                 media_svc_sec_error("stat(%s) fails.", full_path);
729                 return MS_MEDIA_ERR_INTERNAL;
730         }
731
732         return statbuf.st_mtime;
733 }
734
735 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
736 {
737         /* Set default GPS value before extracting meta information */
738         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
739         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
740         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
741
742         /* Set filename to title for all media */
743         char *title = NULL;
744         title = __media_svc_get_title_from_filepath(content_info->path);
745         if (title) {
746                 content_info->media_meta.title = g_strdup(title);
747                 SAFE_FREE(title);
748                 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
749         } else {
750                 media_svc_error("Can't extract title");
751                 content_info->media_meta.title = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
752                 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
753         }
754
755         /* Set default value before extracting meta information */
756         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
757         media_svc_retv_del_if(content_info->media_meta.description == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
758
759         content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
760         media_svc_retv_del_if(content_info->media_meta.copyright == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
761
762         content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
763         media_svc_retv_del_if(content_info->media_meta.track_num == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
764
765         content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
766         media_svc_retv_del_if(content_info->media_meta.album == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
767
768         content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
769         media_svc_retv_del_if(content_info->media_meta.artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
770
771         content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
772         media_svc_retv_del_if(content_info->media_meta.album_artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
773
774         content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
775         media_svc_retv_del_if(content_info->media_meta.genre == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
776
777         content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
778         media_svc_retv_del_if(content_info->media_meta.composer == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
779
780         content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
781         media_svc_retv_del_if(content_info->media_meta.year == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
782
783         if (refresh) {
784                 media_svc_debug("refresh");
785                 return MS_MEDIA_ERR_NONE;
786         }
787
788         content_info->favourate = 0;
789         content_info->media_meta.rating = 0;
790
791         return MS_MEDIA_ERR_NONE;
792 }
793
794 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)
795 {
796         int ret = MS_MEDIA_ERR_NONE;
797         char * media_uuid = NULL;
798         bool drm_type = false;
799         char mime_type[256] = {0, };
800         media_svc_media_type_e media_type;
801
802         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
803         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
804
805         content_info->path = g_strdup(path);
806         media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
807
808         struct stat st;
809         memset(&st, 0, sizeof(struct stat));
810         if (stat(path, &st) == 0) {
811                 content_info->modified_time = st.st_mtime;
812                 content_info->timeline = content_info->modified_time;
813                 content_info->size = st.st_size;
814         } else {
815                 media_svc_stderror("stat failed");
816         }
817
818         _media_svc_set_default_value(content_info, refresh);
819
820         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
821         if (refresh) {
822                 media_svc_debug("refresh");
823                 return MS_MEDIA_ERR_NONE;
824         }
825
826         content_info->storage_uuid = g_strdup(storage_id);
827         media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
828
829         content_info->storage_type = storage_type;
830         time(&content_info->added_time);
831
832         media_uuid = _media_info_generate_uuid();
833         if (media_uuid == NULL) {
834                 _media_svc_destroy_content_info(content_info);
835                 return MS_MEDIA_ERR_INTERNAL;
836         }
837
838         content_info->media_uuid = g_strdup(media_uuid);
839         media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
840
841         content_info->file_name = g_path_get_basename(path);
842         media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
843
844         /* if the file is DRM file, drm_type value is DRM_TRUE(1).
845         if drm_contentinfo is not NULL, the file is OMA DRM.*/
846         ret = __media_svc_get_mime_type(path, mime_type);
847         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
848
849         media_svc_debug("mime [%s]", mime_type);
850         content_info->is_drm = drm_type;
851
852         ret = __media_svc_get_media_type(path, mime_type, &media_type);
853         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
854
855         content_info->mime_type = g_strdup(mime_type);
856         media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
857
858         media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, media_type);
859
860         content_info->media_type = media_type;
861
862         return MS_MEDIA_ERR_NONE;
863 }
864
865 int image_360_check(char *path)
866 {
867         FILE *fp = NULL;
868         long app1_size = 0;
869         int size = 1;
870         unsigned char exif_header[4] = {0, };
871         unsigned char exif_app1[2] = {0, };
872         unsigned char exif_app1_xmp[2] = {0, };
873         long exif_app1_xmp_size = 0;
874         unsigned char exif_app1_xmp_t[2] = {0, };
875         char *xmp_data = NULL;
876         int size1 = 0;
877         int size2 = 0;
878         int fdata = 0;
879         int temp = 0;
880
881         memset(exif_header, 0x00, sizeof(exif_header));
882         memset(exif_app1, 0x00, sizeof(exif_app1));
883         memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
884         memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
885
886         fp = fopen(path, "rb");
887         if (fp == NULL)
888                 goto ERROR;
889
890         size = fread(exif_header, 1, sizeof(exif_header), fp);
891         if (size <= 0)
892                 goto ERROR;
893
894         if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
895                 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
896                 if (size <= 0)
897                         goto ERROR;
898
899                 size1 = exif_app1[0];
900                 size2 = exif_app1[1];
901
902                 app1_size = size1 * 256 + size2 - 2;
903
904                 if (fseek(fp, app1_size, SEEK_CUR) != 0)
905                         goto ERROR;
906
907                 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
908                 if (size <= 0)
909                         goto ERROR;
910
911                 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
912                         int result = 0;
913                         char *ptr = NULL;
914                         size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
915                         if (size <= 0)
916                                 goto ERROR;
917
918                         size1 = exif_app1_xmp_t[0];
919                         size2 = exif_app1_xmp_t[1];
920
921                         exif_app1_xmp_size = size1 * 256 + size2 - 2;
922
923                         if (exif_app1_xmp_size > 0) {
924                                 xmp_data = (char *)malloc(exif_app1_xmp_size);
925                                 memset(xmp_data, 0x0, exif_app1_xmp_size);
926
927                                 ptr = xmp_data;
928
929                                 while (exif_app1_xmp_size >= 0) {
930                                         exif_app1_xmp_size--;
931                                         fdata = fgetc(fp);
932                                         if (fdata == EOF)
933                                                 continue;
934                                         if (fdata == '\0')
935                                                 continue;
936                                         *ptr = (char)fdata;
937                                         ptr++;
938                                         temp++;
939                                 }
940                                 ptr = ptr - temp;
941
942                                 if (strstr(ptr, "UsePanoramaViewer")
943                                 && strstr(ptr, "True")
944                                 && strstr(ptr, "ProjectionType")
945                                 && strstr(ptr, "equirectangular"))
946                                         result = 1;
947
948                                 SAFE_FREE(xmp_data);
949                         } else {
950                                 media_svc_error("invalid exif_app1_xmp_size [%ld]", exif_app1_xmp_size);
951                         }
952
953                         if (fp) {
954                                 fclose(fp);
955                                 fp = NULL;
956                         }
957                         return result;
958                 } else {
959                         goto ERROR;
960                 }
961         } else {
962                 goto ERROR;
963         }
964 ERROR:
965         if (fp) {
966                 fclose(fp);
967                 fp = NULL;
968         }
969         return 0;
970 }
971
972 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
973 {
974         double value = 0.0;
975         int orient_value = 0;
976         int exif_width = 0;
977         int exif_height = 0;
978         ExifData *ed = NULL;
979         bool has_datetaken = false;
980         double fnumber = 0.0;
981         int iso = 0;
982         char *path = NULL;
983
984         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
985
986         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
987         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);
988         media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
989
990         path = content_info->path;
991
992         /* Load an ExifData object from an EXIF file */
993         ed = exif_data_new_from_file(path);
994
995         if (!ed) {
996                 media_svc_sec_debug("There is no exif data in [ %s ]", path);
997                 goto GET_WIDTH_HEIGHT;
998         }
999
1000         content_info->media_meta.is_360 = image_360_check(path);
1001
1002         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1003         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1004         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1005         content_info->media_meta.fnumber = 0.0;
1006         content_info->media_meta.iso = 0;
1007         content_info->media_meta.orientation = 0;
1008         content_info->media_meta.width = 0;
1009         content_info->media_meta.height = 0;
1010
1011         memset(buf, 0x00, sizeof(buf));
1012         if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1013                 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1014                         if (!g_strcmp0(buf, "S"))
1015                                 value *= -1;
1016                         content_info->media_meta.latitude = value;
1017                 }
1018         }
1019
1020         memset(buf, 0x00, sizeof(buf));
1021         if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1022                 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1023                         if (!g_strcmp0(buf, "W"))
1024                                 value *= -1;
1025
1026                         content_info->media_meta.longitude = value;
1027                 }
1028         }
1029
1030         memset(buf, 0x00, sizeof(buf));
1031         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1032                 if (strlen(buf) > 0) {
1033                         SAFE_FREE(content_info->media_meta.description);
1034                         content_info->media_meta.description = g_strdup(buf);
1035                 }
1036         }
1037
1038         memset(buf, 0x00, sizeof(buf));
1039         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1040                 if (strlen(buf) > 0) {
1041                         has_datetaken = true;
1042                         content_info->media_meta.datetaken = g_strdup(buf);
1043
1044                         /* This is same as recorded_date */
1045                         content_info->media_meta.recorded_date = g_strdup(buf);
1046                 }
1047         }
1048
1049         memset(buf, 0x00, sizeof(buf));
1050         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1051                 if (strlen(buf) > 0) {
1052                         has_datetaken = true;
1053                         content_info->media_meta.datetaken = g_strdup(buf);
1054
1055                         /* This is same as recorded_date */
1056                         content_info->media_meta.recorded_date =  g_strdup(buf);
1057                 }
1058         }
1059
1060         if (has_datetaken) {
1061                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1062                 if (content_info->timeline == 0)
1063                         content_info->timeline = content_info->modified_time;
1064                 else
1065                         media_svc_debug("Timeline : %ld", content_info->timeline);
1066         }
1067
1068         memset(buf, 0x00, sizeof(buf));
1069         /* Get exposure_time value from exif. */
1070         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1071                 if (strlen(buf) > 0)
1072                         content_info->media_meta.exposure_time = g_strdup(buf);
1073         }
1074
1075         /* Get fnumber value from exif. */
1076         if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1077                 if (fnumber > 0.0)
1078                         content_info->media_meta.fnumber = fnumber;
1079         }
1080
1081         /* Get iso value from exif. */
1082         if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1083                 if (iso > 0)
1084                         content_info->media_meta.iso = iso;
1085         }
1086
1087         memset(buf, 0x00, sizeof(buf));
1088         /* Get model value from exif. */
1089         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1090                 if (strlen(buf) > 0)
1091                         content_info->media_meta.model = g_strdup(buf);
1092         }
1093
1094         /* Get orientation value from exif. */
1095         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1096                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1097                         content_info->media_meta.orientation = orient_value;
1098         }
1099
1100         /* Get width value from exif. */
1101         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1102                 if (exif_width > 0)
1103                         content_info->media_meta.width = exif_width;
1104         }
1105
1106         /* Get height value from exif. */
1107         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1108                 if (exif_height > 0)
1109                         content_info->media_meta.height = exif_height;
1110         }
1111
1112         if (ed)
1113                 exif_data_unref(ed);
1114
1115 GET_WIDTH_HEIGHT:
1116
1117         if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
1118                 /*Get image width, height*/
1119                 unsigned int img_width = 0;
1120                 unsigned int img_height = 0;
1121                 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
1122
1123                 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
1124                 if (content_info->media_meta.width == 0)
1125                         content_info->media_meta.width = img_width;
1126
1127                 if (content_info->media_meta.height == 0)
1128                         content_info->media_meta.height = img_height;
1129         }
1130
1131         return MS_MEDIA_ERR_NONE;
1132 }
1133
1134 int _media_svc_extract_music_metadata_for_update(media_svc_content_info_s *content_info, const char *path)
1135 {
1136         MMHandleType tag = 0;
1137         char *p = NULL;
1138         int size = -1;
1139         int mmf_error = FILEINFO_ERROR_NONE;
1140
1141         content_info->path = g_strdup(path);
1142
1143         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1144         if (mmf_error != FILEINFO_ERROR_NONE)
1145                 return MS_MEDIA_ERR_INTERNAL;
1146
1147         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1148         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1149                 content_info->media_meta.album = g_strdup(p);
1150
1151         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1152         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1153                 content_info->media_meta.artist = g_strdup(p);
1154
1155         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1156         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1157                 content_info->media_meta.album_artist = g_strdup(p);
1158
1159         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_GENRE, &p, &size, NULL);
1160         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1161                 content_info->media_meta.genre = g_strdup(p);
1162
1163         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
1164         if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1165                 while(p && isspace(*p))
1166                         p++;
1167
1168                 content_info->media_meta.title = g_strdup(p);
1169         }
1170
1171         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1172         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1173                 content_info->media_meta.description = g_strdup(p);
1174
1175         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1176         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1177                 content_info->media_meta.composer = g_strdup(p);
1178
1179         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1180         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1181                 content_info->media_meta.copyright = g_strdup(p);
1182
1183         mm_file_destroy_tag_attrs(tag);
1184
1185         return MS_MEDIA_ERR_NONE;
1186 }
1187
1188 int _media_svc_extract_media_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
1189 {
1190         MMHandleType content = 0;
1191         MMHandleType tag = 0;
1192         char *p = NULL;
1193         unsigned char *image = NULL;
1194         unsigned int size = 0;
1195         int mmf_error = FILEINFO_ERROR_NONE;
1196         int album_id = 0;
1197         int ret = MS_MEDIA_ERR_NONE;
1198         int cdis_value = 0;
1199
1200         /*Get Content Tag attribute ===========*/
1201         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1202
1203         if (mmf_error == FILEINFO_ERROR_NONE) {
1204                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1205                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1206                         content_info->media_meta.album = g_strdup(p);
1207
1208                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1209                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1210                         content_info->media_meta.artist = g_strdup(p);
1211
1212                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1213                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1214                         content_info->media_meta.album_artist = g_strdup(p);
1215
1216                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_GENRE, &p, &size, NULL);
1217                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1218                         content_info->media_meta.genre = g_strdup(p);
1219
1220                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
1221                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1222                         while(p && isspace(*p))
1223                                 p++;
1224
1225                         content_info->media_meta.title = g_strdup(p);
1226                 }
1227
1228                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1229                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1230                         content_info->media_meta.description = g_strdup(p);
1231
1232                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1233
1234                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1235                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1236                         if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
1237                                 /*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*/
1238                                 char *p_value = g_strdelimit(g_strdup(p), "-", ':');
1239                                 content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
1240                                 SAFE_FREE(p_value);
1241                         } else {
1242                                 content_info->media_meta.recorded_date = g_strdup(p);
1243                         }
1244
1245                         if (STRING_VALID(content_info->media_meta.recorded_date)) {
1246                                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1247                                 if (content_info->timeline == 0)
1248                                         content_info->timeline = content_info->modified_time;
1249
1250                                 /* This is same as datetaken */
1251                                 /* Remove compensation string */
1252                                 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
1253                                         content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
1254                                         g_free(content_info->media_meta.recorded_date);
1255                                         content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
1256                                 } else {
1257                                         content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
1258                                 }
1259                         }
1260                 }
1261
1262                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1263                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1264                         content_info->media_meta.composer = g_strdup(p);
1265
1266                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1267                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1268                         content_info->media_meta.copyright = g_strdup(p);
1269
1270                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1271                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1272                         content_info->media_meta.track_num = g_strdup(p);
1273
1274                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
1275                 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
1276                         int year = 0;
1277                         if ((p != NULL) && (__media_svc_safe_atoi(p, &year) == MS_MEDIA_ERR_NONE))
1278                                 content_info->media_meta.year = g_strdup(p);
1279                         else
1280                                 media_svc_debug("Wrong Year Information [%s]", p);
1281                 }
1282
1283                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RATING, &p, &size, NULL);
1284                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1285                         int rate = 0;
1286                         if ((p != NULL) && (__media_svc_safe_atoi(p, &rate) == MS_MEDIA_ERR_NONE))
1287                                 content_info->media_meta.rating = rate;
1288                 } else {
1289                         content_info->media_meta.rating = 0;
1290                 }
1291
1292                 /*Do not extract artwork for the USB Storage content*/
1293                 if (content_info->storage_type != MS_USER_STORAGE_EXTERNAL_USB) {
1294                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1295                         if (mmf_error != FILEINFO_ERROR_NONE) {
1296                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1297                         } else {
1298                                 /*media_svc_debug("artwork size1 [%d]", size); */
1299                         }
1300
1301                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1302                         if (mmf_error != FILEINFO_ERROR_NONE) {
1303                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1304                         } else {
1305                                 /*media_svc_debug("artwork size2 [%d]", size); */
1306                         }
1307
1308                         if (image != NULL && size > 0) {
1309                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1310                                 int artwork_mime_size = -1;
1311
1312                                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1313                                 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1314                                         ret = _media_svc_get_thumbnail_path(content_info->media_type, thumb_path, content_info->path, p, uid);
1315                                         if (ret != MS_MEDIA_ERR_NONE) {
1316                                                 media_svc_error("Fail to Get Thumbnail Path");
1317                                         } else {
1318                                                 ret = __media_svc_save_image(image, size, thumb_path, uid);
1319                                                 if (ret != MS_MEDIA_ERR_NONE) {
1320                                                         media_svc_error("Fail to Save Image");
1321                                                 } else {
1322                                                         content_info->thumbnail_path = g_strdup(thumb_path);
1323                                                         /* NOTICE : Prevent resize for performance (2020.02.07)
1324                                                         *  In most cases, artwork's format is jpeg and size is under MEDIA_SVC_ARTWORK_SIZE * MEDIA_SVC_ARTWORK_SIZE.
1325                                                         *  So, doing mm_util_extract_image_info to check image size is a time-consuming task.
1326                                                         */
1327 #if 0
1328                                                         /* albumart resizing */
1329                                                         ret = __media_svc_resize_artwork(thumb_path, p);
1330                                                         if (ret != MS_MEDIA_ERR_NONE) {
1331                                                                 media_svc_error("Fail to Make Thumbnail Image");
1332                                                                 _media_svc_remove_file(thumb_path);
1333
1334                                                         } else {
1335                                                                 content_info->thumbnail_path = g_strdup(thumb_path);
1336                                                         }
1337 #endif
1338                                                 }
1339                                         }
1340                                 }
1341                         }
1342                 }
1343
1344                 /*Initialize album_id to 0. below code will set the album_id*/
1345                 content_info->album_id = album_id;
1346                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1347                 if (ret != MS_MEDIA_ERR_NONE) {
1348                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1349                                 media_svc_debug("album does not exist. So start to make album art");
1350                                 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1351                                         (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1352                                         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);
1353                                 else
1354                                         ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1355
1356                                 content_info->album_id = album_id;
1357                         }
1358                 } else {
1359                         content_info->album_id = album_id;
1360                 }
1361
1362                 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1363                         double longitude = 0.0;
1364                         double latitude = 0.0;
1365                         double altitude = 0.0;
1366
1367                         mm_file_get_attrs(tag, MM_FILE_TAG_LONGITUDE, &longitude,
1368                                 MM_FILE_TAG_LATIDUE, &latitude,
1369                                 MM_FILE_TAG_ALTIDUE, &altitude,
1370                                 NULL);
1371
1372                         content_info->media_meta.longitude = (longitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : longitude;
1373                         content_info->media_meta.latitude = (latitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : latitude;
1374                         content_info->media_meta.altitude = (altitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : altitude;
1375
1376                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1377                         if (mmf_error != FILEINFO_ERROR_NONE)
1378                                 cdis_value = 0;
1379
1380                         media_svc_debug("CDIS : %d", cdis_value);
1381
1382                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1383                         if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1384                                 content_info->media_meta.orientation = atoi(p);
1385                         } else {
1386                                 content_info->media_meta.orientation = 0;
1387                                 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1388                         }
1389                 }
1390
1391                 mmf_error = mm_file_destroy_tag_attrs(tag);
1392                 if (mmf_error != FILEINFO_ERROR_NONE)
1393                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1394         }       else {
1395                 content_info->album_id = album_id;
1396         }
1397
1398         /*Get Content attribute ===========*/
1399         if (cdis_value == 1)
1400                 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1401         else
1402                 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1403
1404         media_svc_retvm_if(mmf_error != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_NONE, "mm_file_create_content_attrs failed");
1405
1406         if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1407                 int audio_bitrate = 0;
1408                 int video_bitrate = 0;
1409
1410                 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1411                         MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate,
1412                         MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate,
1413                         MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width,
1414                         MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height,
1415                         NULL);
1416
1417                 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1418         } else {
1419                 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1420                         MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate,
1421                         MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate,
1422                         MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel,
1423                         MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample,
1424                         NULL);
1425         }
1426
1427         mm_file_destroy_content_attrs(content);
1428
1429         return MS_MEDIA_ERR_NONE;
1430 }
1431
1432 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1433 {
1434         media_svc_retm_if(content_info == NULL, "content info is NULL");
1435
1436         /* Delete media_svc_content_info_s */
1437         SAFE_FREE(content_info->media_uuid);
1438         SAFE_FREE(content_info->path);
1439         SAFE_FREE(content_info->file_name);
1440         SAFE_FREE(content_info->mime_type);
1441         SAFE_FREE(content_info->folder_uuid);
1442         SAFE_FREE(content_info->thumbnail_path);
1443         SAFE_FREE(content_info->storage_uuid);
1444
1445         /* Delete media_svc_content_meta_s */
1446         SAFE_FREE(content_info->media_meta.title);
1447         SAFE_FREE(content_info->media_meta.album);
1448         SAFE_FREE(content_info->media_meta.artist);
1449         SAFE_FREE(content_info->media_meta.album_artist);
1450         SAFE_FREE(content_info->media_meta.genre);
1451         SAFE_FREE(content_info->media_meta.composer);
1452         SAFE_FREE(content_info->media_meta.year);
1453         SAFE_FREE(content_info->media_meta.recorded_date);
1454         SAFE_FREE(content_info->media_meta.copyright);
1455         SAFE_FREE(content_info->media_meta.track_num);
1456         SAFE_FREE(content_info->media_meta.description);
1457         SAFE_FREE(content_info->media_meta.datetaken);
1458         SAFE_FREE(content_info->media_meta.exposure_time);
1459         SAFE_FREE(content_info->media_meta.model);
1460
1461         SAFE_FREE(content_info->file_name_pinyin);
1462         SAFE_FREE(content_info->media_meta.title_pinyin);
1463         SAFE_FREE(content_info->media_meta.album_pinyin);
1464         SAFE_FREE(content_info->media_meta.artist_pinyin);
1465         SAFE_FREE(content_info->media_meta.album_artist_pinyin);
1466         SAFE_FREE(content_info->media_meta.genre_pinyin);
1467         SAFE_FREE(content_info->media_meta.composer_pinyin);
1468         SAFE_FREE(content_info->media_meta.copyright_pinyin);
1469         SAFE_FREE(content_info->media_meta.description_pinyin);
1470 }
1471
1472 static void __media_svc_get_proper_thumb_size(unsigned int orig_w, unsigned int orig_h, unsigned int *thumb_w, unsigned int *thumb_h)
1473 {
1474         bool portrait = false;
1475         double ratio = 0.0;
1476
1477         media_svc_retm_if(!orig_w, "Invalid orig_w");
1478         media_svc_retm_if(!orig_h, "Invalid orig_h");
1479         media_svc_retm_if(!thumb_w, "Invalid thumb_w");
1480         media_svc_retm_if(!thumb_h, "Invalid thumb_h");
1481
1482         if (orig_w < orig_h)
1483                 portrait = true;
1484
1485         /* Set smaller length to default size */
1486         if (portrait) {
1487                 if (orig_w < *thumb_w)
1488                         *thumb_w = orig_w;
1489                 ratio = (double)orig_h / (double)orig_w;
1490                 *thumb_h = *thumb_w * ratio;
1491         } else {
1492                 if (orig_h < *thumb_h)
1493                         *thumb_h = orig_h;
1494                 ratio = (double)orig_w / (double)orig_h;
1495                 *thumb_w = *thumb_h * ratio;
1496         }
1497
1498         media_svc_debug("proper thumb w: %d h: %d", *thumb_w, *thumb_h);
1499
1500         return;
1501 }
1502
1503 static void __get_rotation_and_cdis(const char *origin_path, mm_util_magick_rotate_type *rot_type, int *cdis_value)
1504 {
1505         int err = MS_MEDIA_ERR_NONE;
1506         MMHandleType tag = (MMHandleType) NULL;
1507         char *p = NULL;
1508         int size = 0;
1509         int _cdis_value = 0;
1510         mm_util_magick_rotate_type _rot_type = MM_UTIL_ROTATE_NUM;
1511
1512         /* Get Content Tag attribute for orientation */
1513         err = mm_file_create_tag_attrs(&tag, origin_path);
1514         if (err != FILEINFO_ERROR_NONE) {
1515                 *rot_type = MM_UTIL_ROTATE_0;
1516                 *cdis_value = 0;
1517                 return;
1518         }
1519
1520         err = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1521         if (err == FILEINFO_ERROR_NONE && size >= 0) {
1522                 if (p == NULL) {
1523                         _rot_type = MM_UTIL_ROTATE_0;
1524                 } else {
1525                         if (strncmp(p, "90", size) == 0)
1526                                 _rot_type = MM_UTIL_ROTATE_90;
1527                         else if (strncmp(p, "180", size) == 0)
1528                                 _rot_type = MM_UTIL_ROTATE_180;
1529                         else if (strncmp(p, "270", size) == 0)
1530                                 _rot_type = MM_UTIL_ROTATE_270;
1531                         else
1532                                 _rot_type = MM_UTIL_ROTATE_0;
1533                 }
1534                 media_svc_debug("There is tag rotate : %d", _rot_type);
1535         } else {
1536                 media_svc_debug("There is NOT tag rotate");
1537                 _rot_type = MM_UTIL_ROTATE_0;
1538         }
1539
1540         err = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &_cdis_value, NULL);
1541         if (err != FILEINFO_ERROR_NONE)
1542                 _cdis_value = 0;
1543
1544         *rot_type = _rot_type;
1545         *cdis_value = _cdis_value;
1546
1547         err = mm_file_destroy_tag_attrs(tag);
1548         if (err != FILEINFO_ERROR_NONE) {
1549                 media_svc_error("fail to free tag attr - err(%x)", err);
1550         }
1551
1552         return;
1553 }
1554
1555 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)
1556 {
1557         int err = MS_MEDIA_ERR_NONE;
1558         MMHandleType content = (MMHandleType) NULL;
1559         int _video_track_num = 0;
1560         int _width = 0;
1561         int _height = 0;
1562         size_t _size = 0;
1563         void *_frame = NULL;
1564
1565         if (cdis_value == 1) {
1566                 media_svc_debug("This is CDIS vlaue 1");
1567                 err = mm_file_create_content_attrs_safe(&content, origin_path);
1568         } else {
1569                 err = mm_file_create_content_attrs(&content, origin_path);
1570         }
1571
1572         if (err != FILEINFO_ERROR_NONE) {
1573                 media_svc_error("mm_file_create_content_attrs fails : %d", err);
1574                 return MS_MEDIA_ERR_INTERNAL;
1575         }
1576
1577         err = mm_file_get_attrs(content, MM_FILE_CONTENT_VIDEO_TRACK_COUNT, &_video_track_num, NULL);
1578         if (err != FILEINFO_ERROR_NONE) {
1579                 media_svc_error("mm_file_get_attrs fails : %d", err);
1580                 mm_file_destroy_content_attrs(content);
1581                 return MS_MEDIA_ERR_INTERNAL;
1582         }
1583
1584         *video_track_num = _video_track_num;
1585
1586         if (_video_track_num == 0) {
1587                 mm_file_destroy_content_attrs(content);
1588                 return MS_MEDIA_ERR_NONE;
1589         }
1590
1591         err = mm_file_get_attrs(content,
1592                                 MM_FILE_CONTENT_VIDEO_WIDTH,
1593                                 &_width,
1594                                 MM_FILE_CONTENT_VIDEO_HEIGHT,
1595                                 &_height,
1596                                 MM_FILE_CONTENT_VIDEO_THUMBNAIL, &_frame, /* raw image is RGB888 format */
1597                                 &_size, NULL);
1598
1599         if (err != FILEINFO_ERROR_NONE) {
1600                 media_svc_error("mm_file_get_attrs fails : %d", err);
1601                 mm_file_destroy_content_attrs(content);
1602                 return MS_MEDIA_ERR_INTERNAL;
1603         }
1604
1605         media_svc_debug("W[%d] H[%d] Size[%zu] Frame[%p]", _width, _height, _size, _frame);
1606         if (!_frame || !_width || !_height) {
1607                 mm_file_destroy_content_attrs(content);
1608                 return MS_MEDIA_ERR_INTERNAL;
1609         }
1610
1611
1612         *width = _width;
1613         *height = _height;
1614         *size = _size;
1615         *frame = calloc(1, _size);
1616         memcpy(*frame, _frame, _size);
1617
1618         mm_file_destroy_content_attrs(content);
1619
1620         return MS_MEDIA_ERR_NONE;
1621 }
1622
1623 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)
1624 {
1625         int err = MS_MEDIA_ERR_NONE;
1626         mm_util_image_h img = NULL;
1627         mm_util_image_h resize_img = NULL;
1628
1629         media_svc_retvm_if(!STRING_VALID(thumb_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1630         __media_svc_get_proper_thumb_size(width, height, &thumb_width, &thumb_height);
1631         if (thumb_width <= 0 || thumb_height <= 0) {
1632                 media_svc_error("Failed to get thumb size");
1633                 return MS_MEDIA_ERR_INTERNAL;
1634         }
1635
1636         media_svc_debug("Origin:W[%d] H[%d] Proper:W[%d] H[%d]", width, height, thumb_width, thumb_height);
1637
1638         err = mm_image_create_image(width, height, MM_UTIL_COLOR_RGB24, (unsigned char *)frame, size, &img);
1639         media_svc_retvm_if(err != MM_UTIL_ERROR_NONE, err, "fail to mm_image_create_image [%d]", err);
1640
1641         if (width > thumb_width || height > thumb_height) {
1642                 if (rot_type != MM_UTIL_ROTATE_0) {
1643                         err = mm_util_resize_B_B(img, thumb_width, thumb_height, &resize_img);
1644                         if (err != MM_UTIL_ERROR_NONE)
1645                                 goto ERROR;
1646
1647                         err = mm_util_rotate_B_P(resize_img, rot_type, thumb_path);
1648                 } else {
1649                         err = mm_util_resize_B_P(img, thumb_width, thumb_height, thumb_path);
1650                 }
1651         } else {
1652                 if (rot_type != MM_UTIL_ROTATE_0)
1653                         err = mm_util_rotate_B_P(img, rot_type, thumb_path);
1654                 else
1655                         err = mm_util_resize_B_P(img, width, height, thumb_path);
1656         }
1657
1658 ERROR:
1659         mm_image_destroy_image(img);
1660         mm_image_destroy_image(resize_img);
1661         if (err != MS_MEDIA_ERR_NONE)
1662                 return MS_MEDIA_ERR_INTERNAL;
1663
1664         return MS_MEDIA_ERR_NONE;
1665 }
1666
1667 static int __create_video_thumbnail(const char *path, char *thumb_path)
1668 {
1669         int ret = MS_MEDIA_ERR_NONE;
1670         int cdis_value = 0;
1671         unsigned int width = 0;
1672         unsigned int height = 0;
1673         void *frame = NULL;
1674         int video_track_num = 0;
1675         size_t size = 0;
1676         mm_util_magick_rotate_type rot_type = MM_UTIL_ROTATE_NUM;
1677
1678         __get_rotation_and_cdis(path, &rot_type, &cdis_value);
1679         ret = __get_video_info(cdis_value, path, &video_track_num, &width, &height, &frame, &size);
1680         media_svc_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "fail to __get_video_info [%d]", ret);
1681         media_svc_retvm_if(video_track_num == 0, MM_UTIL_ERROR_NONE, "No video track");
1682
1683         ret = __get_video_thumb(width, height, frame, size, rot_type, thumb_path, THUMB_WIDTH, THUMB_HEIGHT);
1684
1685         SAFE_FREE(frame);
1686
1687         return ret;
1688 }
1689
1690 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
1691 {
1692         int ret = MS_MEDIA_ERR_NONE;
1693         unsigned int origin_w = 0;
1694         unsigned int origin_h = 0;
1695         unsigned int thumb_w = THUMB_WIDTH;
1696         unsigned int thumb_h = THUMB_HEIGHT;
1697         mm_util_img_codec_type image_type = IMG_CODEC_UNKNOWN_TYPE;
1698
1699         if (path == NULL || thumb_path == NULL) {
1700                 media_svc_error("Invalid parameter");
1701                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1702         }
1703
1704         if (!g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
1705                         media_svc_error("Original path(%s) doesn't exist.", path);
1706                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1707         }
1708
1709         ms_user_storage_type_e store_type = -1;
1710         ret = ms_user_get_storage_type(uid, path, &store_type);
1711
1712         if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
1713                 media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
1714                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1715         }
1716
1717         media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1718
1719         //1. make hash path
1720         ret = _media_svc_get_thumbnail_path(media_type, thumb_path, path, THUMB_EXT, uid);
1721         if (ret != MS_MEDIA_ERR_NONE) {
1722                 media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
1723                 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1724                 return ret;
1725         }
1726
1727         //2. save thumbnail
1728         if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1729                 ret = mm_util_extract_image_info(path, &image_type, &origin_w, &origin_h);
1730                 if (ret != MS_MEDIA_ERR_NONE) {
1731                         media_svc_error("Getting image info is failed err: %d", ret);
1732                         SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1733                         return MS_MEDIA_ERR_INTERNAL;
1734                 }
1735
1736                 if (image_type == IMG_CODEC_UNKNOWN_TYPE) {
1737                         media_svc_error("Unsupported image codec");
1738                         return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
1739                 }
1740
1741                 if ((image_type != IMG_CODEC_JPEG) && (origin_w * origin_h > THUMB_MAX_ALLOWED_MEM)) {
1742                         media_svc_error("This original image is too big");
1743                         SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1744                         return MS_MEDIA_ERR_THUMB_TOO_BIG;
1745                 }
1746
1747                 __media_svc_get_proper_thumb_size(origin_w, origin_h, &thumb_w, &thumb_h);
1748                 ret = mm_util_resize_P_P(path, thumb_w, thumb_h, thumb_path);
1749                 if (ret != MM_UTIL_ERROR_NONE) {
1750                         media_svc_error("mm_util_resize_P_P err: %d", ret);
1751                         SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1752                         return MS_MEDIA_ERR_INTERNAL;
1753                 }
1754         } else {
1755                 return __create_video_thumbnail(path, thumb_path);
1756         }
1757
1758         return ret;
1759 }
1760
1761 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1762 {
1763         int ret = MS_MEDIA_ERR_NONE;
1764         int size = 0;
1765         pinyin_name_s *pinyinname = NULL;
1766
1767         media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1768         media_svc_retvm_if(pinyin_str == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "pinyin_str is NULL");
1769
1770         *pinyin_str = NULL;
1771
1772         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1773         if (ret == MS_MEDIA_ERR_NONE) {
1774                 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1775                         *pinyin_str = strdup(pinyinname[0].pinyin_name);
1776                 else
1777                         *pinyin_str = strdup(src_str);  /* Return Original Non China Character */
1778         }
1779
1780         _media_svc_pinyin_free(pinyinname, size);
1781
1782         return ret;
1783 }
1784
1785 bool _media_svc_check_pinyin_support(void)
1786 {
1787         int ret = SYSTEM_INFO_ERROR_NONE;
1788         bool is_supported = false;
1789
1790         if (media_svc_pinyin_support == -1) {
1791                 ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
1792                 if (ret != SYSTEM_INFO_ERROR_NONE) {
1793                         media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
1794                         return false;
1795                 }
1796
1797                 media_svc_pinyin_support = is_supported;
1798         }
1799
1800         return media_svc_pinyin_support;
1801 }
1802
1803 int _media_svc_get_media_type(const char *path, int *mediatype)
1804 {
1805         int ret = MS_MEDIA_ERR_NONE;
1806         char mime_type[256] = {0};
1807         media_svc_media_type_e media_type =  MEDIA_SVC_MEDIA_TYPE_OTHER;
1808
1809         media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1810
1811         ret = __media_svc_get_mime_type(path, mime_type);
1812         if (ret == MS_MEDIA_ERR_NONE)
1813                 __media_svc_get_media_type(path, mime_type, &media_type);
1814         else
1815                 media_svc_error("__media_svc_get_mime_type failed");
1816
1817         *mediatype = media_type;
1818
1819         return ret;
1820 }
1821
1822 bool _media_svc_is_valid_storage_type(ms_user_storage_type_e storage_type)
1823 {
1824         switch (storage_type) {
1825         case MS_USER_STORAGE_INTERNAL:
1826         case MS_USER_STORAGE_EXTERNAL:
1827         case MS_USER_STORAGE_EXTERNAL_USB:
1828                 return true;
1829         default:
1830                 media_svc_error("storage type is incorrect[%d]", storage_type);
1831                 return false;
1832         }
1833 }