Fix that artwork did not resize
[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 <sys/vfs.h>
34 #include <glib/gstdio.h>
35 #include <sys/stat.h>
36 #include <dirent.h>
37 #include <ctype.h>
38 #include <aul/aul.h>
39 #include <mm_file.h>
40 #include <libexif/exif-data.h>
41 #include <media-thumbnail.h>
42 #include <media-util.h>
43 #include <uuid/uuid.h>
44 #include <img-codec-parser.h>
45 #include <image_util.h>
46 #include <grp.h>
47 #include <pwd.h>
48 #include "media-util-err.h"
49 #include "media-svc-util.h"
50 #include "media-svc-db-utils.h"
51 #include "media-svc-debug.h"
52 #include "media-svc-env.h"
53 #include "media-svc-hash.h"
54 #include "media-svc-album.h"
55 #include "media-svc-localize-utils.h"
56 #include "media-svc-localize_ch.h"
57 #include "media-svc-localize_tw.h"
58
59 #define MEDIA_SVC_FILE_EXT_LEN_MAX                              6                       /**< Maximum file ext lenth*/
60
61 /* Define data structures for media type and mime type */
62 #define MEDIA_SVC_CATEGORY_UNKNOWN      0x00000000      /**< Default */
63 #define MEDIA_SVC_CATEGORY_ETC          0x00000001      /**< ETC category */
64 #define MEDIA_SVC_CATEGORY_IMAGE        0x00000002      /**< Image category */
65 #define MEDIA_SVC_CATEGORY_VIDEO        0x00000004      /**< Video category */
66 #define MEDIA_SVC_CATEGORY_MUSIC        0x00000008      /**< Music category */
67 #define MEDIA_SVC_CATEGORY_SOUND        0x00000010      /**< Sound category */
68 #define MEDIA_SVC_CATEGORY_PVR  0x00000020      /**< PVR category */
69 #define MEDIA_SVC_CATEGORY_UHD  0x00000040      /**< UHD category */
70 #define MEDIA_SVC_CATEGORY_SCSA 0x00000080      /**< SCSA category */
71
72 #define CONTENT_TYPE_NUM 5
73 #define MUSIC_MIME_NUM 29
74 #define SOUND_MIME_NUM 1
75 #define MIME_TYPE_LENGTH 255
76 #define MIME_LENGTH 50
77 #define _3GP_FILE ".3gp"
78 #define _MP4_FILE ".mp4"
79 #define _ASF_FILE ".asf"
80 #define MEDIA_SVC_INI_GET_INT(dict, key, value, default) \
81         do { \
82                 value = iniparser_getint(dict, key, default); \
83                 media_svc_debug("get %s = %d", key, value); \
84         } while (0)
85 #define MEDIA_SVC_INI_DEFAULT_PATH SYSCONFDIR"/multimedia/media_content_config.ini"
86 #define MEDIA_SVC_ARTWORK_SIZE 2000
87
88 static int g_ini_value = -1;
89
90 typedef struct {
91         char content_type[15];
92         int category_by_mime;
93 } _media_svc_content_table_s;
94
95 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
96         {"audio", MEDIA_SVC_CATEGORY_SOUND},
97         {"image", MEDIA_SVC_CATEGORY_IMAGE},
98         {"video", MEDIA_SVC_CATEGORY_VIDEO},
99         {"application", MEDIA_SVC_CATEGORY_ETC},
100         {"text", MEDIA_SVC_CATEGORY_ETC},
101 };
102
103 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
104         /*known mime types of normal files*/
105         "mpeg",
106         "ogg",
107         "x-ms-wma",
108         "x-flac",
109         "mp4",
110         /* known mime types of drm files*/
111         "mp3",
112         "x-mp3", /*alias of audio/mpeg*/
113         "x-mpeg", /*alias of audio/mpeg*/
114         "3gpp",
115         "x-ogg", /*alias of audio/ogg*/
116         "vnd.ms-playready.media.pya:*.pya", /*playready*/
117         "wma",
118         "aac",
119         "x-m4a", /*alias of audio/mp4*/
120         /* below mimes are rare*/
121         "x-vorbis+ogg",
122         "x-flac+ogg",
123         "x-matroska",
124         "ac3",
125         "mp2",
126         "x-ape",
127         "x-ms-asx",
128         "vnd.rn-realaudio",
129
130         "x-vorbis", /*alias of audio/x-vorbis+ogg*/
131         "vorbis", /*alias of audio/x-vorbis+ogg*/
132         "x-oggflac",
133         "x-mp2", /*alias of audio/mp2*/
134         "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
135         "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
136         "x-wav",
137 };
138
139 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
140         "x-smaf",
141 };
142
143 typedef enum {
144         MEDIA_SVC_EXTRACTED_FIELD_NONE                  = 0x00000001,
145         MEDIA_SVC_EXTRACTED_FIELD_TITLE                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 1,
146         MEDIA_SVC_EXTRACTED_FIELD_DESC                  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 2,
147         MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT             = MEDIA_SVC_EXTRACTED_FIELD_NONE << 3,
148         MEDIA_SVC_EXTRACTED_FIELD_AUTHOR                = MEDIA_SVC_EXTRACTED_FIELD_NONE << 4,
149         MEDIA_SVC_EXTRACTED_FIELD_ARTIST                        = MEDIA_SVC_EXTRACTED_FIELD_NONE << 5,
150         MEDIA_SVC_EXTRACTED_FIELD_GENRE                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 6,
151         MEDIA_SVC_EXTRACTED_FIELD_ALBUM                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 7,
152         MEDIA_SVC_EXTRACTED_FIELD_TRACKNUM              = MEDIA_SVC_EXTRACTED_FIELD_NONE << 8,
153         MEDIA_SVC_EXTRACTED_FIELD_YEAR                  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 9,
154         MEDIA_SVC_EXTRACTED_FIELD_CATEGORY              = MEDIA_SVC_EXTRACTED_FIELD_NONE << 10,
155         MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 11,
156 } media_svc_extracted_field_e;
157
158 char *_media_info_generate_uuid(void)
159 {
160         uuid_t uuid_value;
161         static char uuid_unparsed[37];
162
163 RETRY_GEN:
164         uuid_generate(uuid_value);
165         uuid_unparse(uuid_value, uuid_unparsed);
166
167         if (strlen(uuid_unparsed) < 36) {
168                 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
169                 goto RETRY_GEN;
170         }
171
172         return uuid_unparsed;
173 }
174
175 void _strncpy_safe(char *x_dst, const char *x_src, int max_len)
176 {
177         if (!x_src || strlen(x_src) == 0) {
178                 media_svc_error("x_src is NULL");
179                 return;
180         }
181
182         if (max_len < 1) {
183                 media_svc_error("length is Wrong");
184                 return;
185         }
186
187         strncpy(x_dst, x_src, max_len - 1);
188         x_dst[max_len - 1] = '\0';
189 }
190
191 int __media_svc_malloc_and_strncpy(char **dst, const char *src)
192 {
193         int len = 0;
194
195         if (!STRING_VALID(src)) {
196                 media_svc_error("invalid src");
197                 return MS_MEDIA_ERR_INVALID_PARAMETER;
198         }
199
200         SAFE_FREE(*dst);
201
202         len = strlen(src) + 1;
203         *dst = malloc(len);
204
205         if (*dst == NULL) {
206                 media_svc_error("malloc failed");
207                 return MS_MEDIA_ERR_INTERNAL;
208         }
209
210         strncpy(*dst, src, len);
211         char *p = *dst;
212         p[len - 1] = '\0';
213
214         return MS_MEDIA_ERR_NONE;
215 }
216
217 int __media_svc_malloc_and_strncpy_with_size(char **dst, const char *src, int copysize)
218 {
219         if (!STRING_VALID(src)) {
220                 media_svc_error("invalid src");
221                 return MS_MEDIA_ERR_INVALID_PARAMETER;
222         }
223
224         SAFE_FREE(*dst);
225
226         *dst = malloc(copysize + 1);
227
228         if (*dst == NULL) {
229                 media_svc_error("malloc failed");
230                 return MS_MEDIA_ERR_INTERNAL;
231         }
232
233         strncpy(*dst, src, copysize);
234         char *p = *dst;
235         p[copysize] = '\0';
236
237         return MS_MEDIA_ERR_NONE;
238 }
239
240 static int __media_svc_split_to_double(char *input, double *arr)
241 {
242         char tmp_arr[255] = {0, };
243         int len = 0, idx = 0, arr_idx = 0, str_idx = 0;
244
245         if (!STRING_VALID(input)) {
246                 media_svc_error("Invalid parameter");
247                 return MS_MEDIA_ERR_INVALID_PARAMETER;
248         }
249         memset(tmp_arr, 0x0, sizeof(tmp_arr));
250
251         /*media_svc_debug("input: [%s]", input); */
252
253         len = strlen(input);
254
255         for (idx = 0; idx < (len + 1); idx++) {
256                 if (input[idx] == ' ') {
257                         continue;
258                 } else if ((input[idx] == ',') || (idx == len)) {
259                         arr[arr_idx] = atof(tmp_arr);
260                         arr_idx++;
261                         str_idx = 0;
262                         /*media_svc_debug("idx=[%d] arr_idx=[%d] tmp_attr[%s] atof(tmp_arr)=[%f]", idx, arr_idx, tmp_arr, atof(tmp_arr)); */
263                         memset(tmp_arr, 0x0, sizeof(tmp_arr));
264                 } else {
265                         tmp_arr[str_idx] = input[idx];
266                         str_idx++;
267                 }
268         }
269
270         if (arr_idx != 3) {
271                 media_svc_error("Error when parsing GPS [%d]", arr_idx);
272                 return MS_MEDIA_ERR_INTERNAL;
273         }
274
275         return MS_MEDIA_ERR_NONE;
276 }
277
278 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, long tagtype)
279 {
280         ExifEntry *entry;
281         ExifTag tag;
282
283         if (ed == NULL)
284                 return MS_MEDIA_ERR_INVALID_PARAMETER;
285
286         tag = tagtype;
287
288         entry = exif_data_get_entry(ed, tag);
289         if (entry) {
290                 /* Get the contents of the tag in human-readable form */
291                 if (tag == EXIF_TAG_ORIENTATION ||
292                         tag == EXIF_TAG_PIXEL_X_DIMENSION ||
293                         tag == EXIF_TAG_PIXEL_Y_DIMENSION ||
294                         tag == EXIF_TAG_ISO_SPEED_RATINGS) {
295
296                         if (i_value == NULL) {
297                                 media_svc_error("i_value is NULL");
298                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
299                         }
300
301                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
302                         short exif_value = exif_get_short(entry->data, mByteOrder);
303                         *i_value = (int)exif_value;
304
305                 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
306
307                         if (d_value == NULL) {
308                                 media_svc_error("d_value is NULL");
309                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
310                         }
311
312                         /* Get the contents of the tag in human-readable form */
313                         char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
314                         exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
315                         gps_buf[strlen(gps_buf)] = '\0';
316                         int ret = MS_MEDIA_ERR_NONE;
317
318                         double tmp_arr[3] = { 0.0, 0.0, 0.0 };
319
320                         ret = __media_svc_split_to_double(gps_buf, tmp_arr);
321                         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
322
323                         *d_value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
324                 } else if (tag == EXIF_TAG_EXPOSURE_TIME) {
325
326                         if (buf == NULL) {
327                                 media_svc_error("buf is NULL");
328                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
329                         }
330
331                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
332                         ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
333                         long numerator = mRational.numerator;
334                         long denominator = mRational.denominator;
335                         snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
336
337                 } else if (tag == EXIF_TAG_FNUMBER) {
338
339                         if (d_value == NULL) {
340                                 media_svc_error("d_value is NULL");
341                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
342                         }
343
344                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
345                         ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
346                         long numerator = mRational.numerator;
347                         long denominator = mRational.denominator;
348
349                         *d_value = ((numerator*1.0)/(denominator*1.0));
350
351                 } else {
352
353                         if (buf == NULL) {
354                                 media_svc_error("buf is NULL");
355                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
356                         }
357
358                         exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
359                         buf[strlen(buf)] = '\0';
360                 }
361         }
362
363         return MS_MEDIA_ERR_NONE;
364 }
365
366 time_t __media_svc_get_timeline_from_str(const char *timstr)
367 {
368         struct tm t;
369         time_t modified_t = 0;
370         time_t rawtime;
371         struct tm timeinfo;
372
373         if (!STRING_VALID(timstr)) {
374                 media_svc_error("Invalid Parameter");
375                 return 0;
376         }
377
378         /*Exif Format : %Y:%m:%d %H:%M:%S
379         Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
380         memset(&t, 0x00, sizeof(struct tm));
381
382         tzset();
383         time(&rawtime);
384         localtime_r(&rawtime, &timeinfo);
385
386         if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
387                 t.tm_isdst = timeinfo.tm_isdst;
388                 if (t.tm_isdst != 0)
389                         media_svc_error("DST %d", t.tm_isdst);
390
391                 modified_t = mktime(&t);
392                 if (modified_t > 0)
393                         return modified_t;
394                 else
395                         media_svc_error("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);
396         } else {
397                 media_svc_error("Failed to get timeline : [%s]", timstr);
398         }
399
400         return 0;
401 }
402
403 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
404 {
405         int idx = 0;
406
407         *category = MEDIA_SVC_CATEGORY_UNKNOWN;
408
409         /*categorize from mimetype */
410         for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
411                 if (strstr(mimetype, content_category[idx].content_type) != NULL) {
412                         *category = (*category | content_category[idx].category_by_mime);
413                         break;
414                 }
415         }
416
417         /*in application type, exitst sound file ex) x-smafs, asf */
418         if (*category & MEDIA_SVC_CATEGORY_ETC) {
419                 int prefix_len = strlen(content_category[0].content_type);
420                 char *ext = NULL;
421
422                 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
423                         if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
424                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
425                                 *category |= MEDIA_SVC_CATEGORY_SOUND;
426                                 break;
427                         }
428                 }
429
430                 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
431                         *category ^= MEDIA_SVC_CATEGORY_ETC;
432                         *category |= MEDIA_SVC_CATEGORY_SOUND;
433                 }
434
435                 /*"asf" must check video stream and then categorize in directly. */
436                 ext = strrchr(path, '.');
437                 if (ext != NULL) {
438                         if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
439                                 int audio = 0;
440                                 int video = 0;
441                                 int err = 0;
442
443                                 err = mm_file_get_stream_info(path, &audio, &video);
444                                 if (err == 0) {
445                                         if (audio > 0 && video == 0) {
446                                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
447                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
448                                         } else {
449                                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
450                                                 *category |= MEDIA_SVC_CATEGORY_VIDEO;
451                                         }
452                                 }
453                         }
454                 }
455         }
456
457         /*check music file in soun files. */
458         if (*category & MEDIA_SVC_CATEGORY_SOUND) {
459                 int prefix_len = strlen(content_category[0].content_type) + 1;
460
461                 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
462                         if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
463                                 *category ^= MEDIA_SVC_CATEGORY_SOUND;
464                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
465                                 break;
466                         }
467                 }
468
469                 /*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*/
470                 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
471                         *category ^= MEDIA_SVC_CATEGORY_SOUND;
472                         *category |= MEDIA_SVC_CATEGORY_ETC;
473                 }
474         } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
475                 /*some video files don't have video stream. in this case it is categorize as music. */
476                 char *ext = NULL;
477                 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
478                 ext = strrchr(path, '.');
479                 if (ext != NULL) {
480                         if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
481                                 int audio = 0;
482                                 int video = 0;
483                                 int err = 0;
484
485                                 err = mm_file_get_stream_info(path, &audio, &video);
486                                 if (err == 0) {
487                                         if (audio > 0 && video == 0) {
488                                                 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
489                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
490                                         }
491                                 }
492                                 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
493                         }
494                 }
495         }
496
497         return MS_MEDIA_ERR_NONE;
498 }
499
500 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
501 {
502         int ret = MS_MEDIA_ERR_NONE;
503         int category = 0;
504
505         media_svc_media_type_e type;
506
507         ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
508         if (ret != MS_MEDIA_ERR_NONE)
509                 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
510
511         if (category & MEDIA_SVC_CATEGORY_SOUND)                type = MEDIA_SVC_MEDIA_TYPE_SOUND;
512         else if (category & MEDIA_SVC_CATEGORY_MUSIC)   type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
513         else if (category & MEDIA_SVC_CATEGORY_IMAGE)   type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
514         else if (category & MEDIA_SVC_CATEGORY_VIDEO)   type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
515         else    type = MEDIA_SVC_MEDIA_TYPE_OTHER;
516
517         *media_type = type;
518
519         return ret;
520 }
521
522 /*
523 drm_contentifo is not NULL, if the file is OMA DRM.
524 If the file is not OMA DRM, drm_contentinfo must be NULL.
525 */
526 static int __media_svc_get_mime_type(const char *path, char *mimetype)
527 {
528         if (path == NULL)
529                 return MS_MEDIA_ERR_INVALID_PARAMETER;
530
531         /*in case of normal files or failure to get mime in drm */
532         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
533                 media_svc_error("aul_get_mime_from_file fail");
534                 return MS_MEDIA_ERR_INTERNAL;
535         }
536
537         return MS_MEDIA_ERR_NONE;
538 }
539
540 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
541 {
542         int i = 0;
543
544         for (i = strlen(file_path); i >= 0; i--) {
545                 if (file_path[i] == '.') {
546                         _strncpy_safe(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
547                         return true;
548                 }
549
550                 if (file_path[i] == '/')
551                         return false;
552         }
553         return false;
554 }
555
556 static int __media_svc_get_location_value(MMHandleType tag, double *longitude, double *latitude, double *altitude)
557 {
558         char *err_attr_name = NULL;
559         double gps_value = 0.0;
560         int mmf_error = FILEINFO_ERROR_NONE;
561
562         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
563         if (mmf_error == FILEINFO_ERROR_NONE) {
564                 if (longitude != NULL)
565                         *longitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
566         } else {
567                 SAFE_FREE(err_attr_name);
568         }
569
570         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
571         if (mmf_error == FILEINFO_ERROR_NONE) {
572                 if (latitude != NULL)
573                         *latitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
574         } else {
575                 SAFE_FREE(err_attr_name);
576         }
577
578         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
579         if (mmf_error == FILEINFO_ERROR_NONE) {
580                 if (altitude != NULL)
581                         *altitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
582         } else {
583                 SAFE_FREE(err_attr_name);
584         }
585
586         return MS_MEDIA_ERR_NONE;
587 }
588
589 static char *__media_svc_get_thumb_path(uid_t uid)
590 {
591         int len = 0;
592         char *result_passwd = NULL;
593         struct group *grpinfo = NULL;
594         if (uid == getuid()) {
595                 grpinfo = getgrnam("users");
596                 if (grpinfo == NULL) {
597                         media_svc_error("getgrnam(users) returns NULL !");
598                         return NULL;
599                 }
600                 len = strlen(MEDIA_SVC_THUMB_PATH_PREFIX);
601                 if (len > 0)
602                         result_passwd = strndup(MEDIA_SVC_THUMB_PATH_PREFIX, len);
603         } else {
604                 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
605                 struct passwd *userinfo = getpwuid(uid);
606                 if (userinfo == NULL) {
607                         media_svc_error("getpwuid(%d) returns NULL !", uid);
608                         return NULL;
609                 }
610                 grpinfo = getgrnam("users");
611                 if (grpinfo == NULL) {
612                         media_svc_error("getgrnam(users) returns NULL !");
613                         return NULL;
614                 }
615                 /* Compare git_t type and not group name */
616                 if (grpinfo->gr_gid != userinfo->pw_gid) {
617                         media_svc_error("UID [%d] does not belong to 'users' group!", uid);
618                         return NULL;
619                 }
620                 len = snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb", userinfo->pw_dir);
621                 if (len > 0)
622                         result_passwd = strndup(passwd_str, len);
623         }
624
625         return result_passwd;
626 }
627
628 static int __media_svc_encode_jpeg(unsigned char *src, unsigned long width, unsigned long height, image_util_colorspace_e colorspace, int quality, unsigned char **dst, unsigned long long *dst_size) {
629         int res = IMAGE_UTIL_ERROR_NONE;
630         image_util_encode_h encoder = NULL;
631         unsigned char *encoded_data = NULL;
632         res = image_util_encode_create(IMAGE_UTIL_JPEG , &encoder);
633         if (res != IMAGE_UTIL_ERROR_NONE) {
634                 media_svc_error("image_util_encode_create failed! (%d)", res);
635                 return MS_MEDIA_ERR_INTERNAL;
636         }
637         res = image_util_encode_set_resolution(encoder, width, height);
638         if (res != IMAGE_UTIL_ERROR_NONE) {
639                 media_svc_error("image_util_encode_set_resolution failed! (%d)", res);
640                 image_util_encode_destroy(encoder);
641                 return MS_MEDIA_ERR_INTERNAL;
642         }
643         res = image_util_encode_set_colorspace(encoder, colorspace);
644         if (res != IMAGE_UTIL_ERROR_NONE) {
645                 media_svc_error("image_util_encode_set_colorspace failed! (%d)", res);
646                 image_util_encode_destroy(encoder);
647                 return MS_MEDIA_ERR_INTERNAL;
648         }
649         res = image_util_encode_set_quality(encoder, quality);
650         if (res != IMAGE_UTIL_ERROR_NONE) {
651                 media_svc_error("image_util_encode_set_quality failed! (%d)", res);
652                 image_util_encode_destroy(encoder);
653                 return MS_MEDIA_ERR_INTERNAL;
654         }
655         res = image_util_encode_set_input_buffer(encoder, src);
656         if (res != IMAGE_UTIL_ERROR_NONE) {
657                 media_svc_error("image_util_encode_set_input_buffer failed! (%d)", res);
658                 image_util_encode_destroy(encoder);
659                 return MS_MEDIA_ERR_INTERNAL;
660         }
661         res = image_util_encode_set_output_buffer(encoder, &encoded_data);
662         if (res != IMAGE_UTIL_ERROR_NONE) {
663                 media_svc_error("image_util_decode_set_output_buffer failed! (%d)", res);
664                 image_util_encode_destroy(encoder);
665                 return MS_MEDIA_ERR_INTERNAL;
666         }
667         res = image_util_encode_run(encoder, dst_size);
668         if (res != IMAGE_UTIL_ERROR_NONE) {
669                 media_svc_error("image_util_encode_run failed! (%d)", res);
670                 image_util_encode_destroy(encoder);
671                 return MS_MEDIA_ERR_INTERNAL;
672         }
673         if (encoded_data != NULL) {
674                 *dst = (unsigned char *)calloc(1, *dst_size);
675                 if (*dst == NULL) {
676                         media_svc_error("memory allocation failed! (%lld)", *dst_size);
677                         image_util_encode_destroy(encoder);
678                         return MS_MEDIA_ERR_INTERNAL;
679                 }
680                 memcpy(*dst, encoded_data, *dst_size);
681         }
682         res = image_util_encode_destroy(encoder);
683         if (res != IMAGE_UTIL_ERROR_NONE) {
684                 media_svc_error("image_util_encode_destroy failed! (%d)", res);
685                 return MS_MEDIA_ERR_INTERNAL;
686         }
687         SAFE_FREE(encoded_data);
688         return MS_MEDIA_ERR_NONE;
689 }
690
691 static int __media_svc_decode_jpeg(unsigned char *src, unsigned long long size, image_util_colorspace_e colorspace, unsigned char **dst, unsigned long *width, unsigned long *height, unsigned long long *dst_size) {
692         int res = IMAGE_UTIL_ERROR_NONE;
693         image_util_decode_h decoder = NULL;
694         res = image_util_decode_create(&decoder);
695         if (res != IMAGE_UTIL_ERROR_NONE) {
696                 media_svc_error("image_util_decode_create failed! (%d)", res);
697                 return MS_MEDIA_ERR_INTERNAL;
698         }
699         res = image_util_decode_set_input_buffer(decoder, src, size);
700         if (res != IMAGE_UTIL_ERROR_NONE) {
701                 media_svc_error("image_util_decode_set_input_buffer failed! (%d)", res);
702                 image_util_decode_destroy(decoder);
703                 return MS_MEDIA_ERR_INTERNAL;
704         }
705         res = image_util_decode_set_colorspace(decoder, colorspace);
706         if (res != IMAGE_UTIL_ERROR_NONE) {
707                 media_svc_error("image_util_decode_set_colorspace failed! (%d)", res);
708                 image_util_decode_destroy(decoder);
709                 return MS_MEDIA_ERR_INTERNAL;
710         }
711         res = image_util_decode_set_output_buffer(decoder, dst);
712         if (res != IMAGE_UTIL_ERROR_NONE) {
713                 media_svc_error("image_util_decode_set_output_buffer failed! (%d)", res);
714                 image_util_decode_destroy(decoder);
715                 return MS_MEDIA_ERR_INTERNAL;
716         }
717         res = image_util_decode_run(decoder, width, height, dst_size);
718         if (res != IMAGE_UTIL_ERROR_NONE) {
719                 media_svc_error("image_util_decode_run failed! (%d)", res);
720                 image_util_decode_destroy(decoder);
721                 return MS_MEDIA_ERR_INTERNAL;
722         }
723
724         res = image_util_decode_destroy(decoder);
725         if (res != IMAGE_UTIL_ERROR_NONE) {
726                 media_svc_error("image_util_decode_destroy failed! (%d)", res);
727                 return MS_MEDIA_ERR_INTERNAL;
728         }
729         return MS_MEDIA_ERR_NONE;
730 }
731
732 static int __media_svc_resize_artwork(unsigned char *image, unsigned int size, const char *img_format, unsigned char **resize_image, unsigned int *resize_size)
733 {
734         int ret = MS_MEDIA_ERR_NONE;
735         unsigned char *raw_image = NULL;
736         int width = 0;
737         int height = 0;
738         unsigned int raw_size = 0;
739         void *resized_raw_image = NULL;
740         int resized_width = 0;
741         int resized_height = 0;
742         unsigned int buf_size = 0;
743         image_util_colorspace_e colorspace = IMAGE_UTIL_COLORSPACE_RGB888;
744
745         if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
746                 media_svc_debug("type [jpeg] size [%d]", size);
747                 /* decoding */
748                 ret = __media_svc_decode_jpeg(image, (unsigned long long)size, colorspace, &raw_image, (unsigned long *)&width, (unsigned long *)&height, (unsigned long long *)&raw_size);
749                 if (ret != MS_MEDIA_ERR_NONE) {
750                         media_svc_error("__media_svc_decode_jpeg failed");
751                         *resize_image = image;
752                         *resize_size = size;
753                         return MS_MEDIA_ERR_NONE;
754                 }
755
756                 if (raw_image == NULL) {
757                         media_svc_error("raw_image is null");
758                         *resize_image = image;
759                         *resize_size = size;
760                         return MS_MEDIA_ERR_NONE;
761                 }
762
763                 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
764                         media_svc_debug("No need resizing");
765                         *resize_image = image;
766                         *resize_size = size;
767                         SAFE_FREE(raw_image);
768                         return MS_MEDIA_ERR_NONE;
769                 }
770                 /* resizing */
771                 if (width > height) {
772                         resized_height = MEDIA_SVC_ARTWORK_SIZE;
773                         resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
774                 } else {
775                         resized_width = MEDIA_SVC_ARTWORK_SIZE;
776                         resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
777                 }
778
779                 image_util_calculate_buffer_size(resized_width, resized_height, colorspace, &buf_size);
780
781                 resized_raw_image = malloc(buf_size);
782
783                 if (resized_raw_image == NULL) {
784                         media_svc_error("malloc failed");
785                         *resize_image = image;
786                         *resize_size = size;
787                         SAFE_FREE(raw_image);
788                         return MS_MEDIA_ERR_NONE;
789                 }
790
791                 memset(resized_raw_image, 0, buf_size);
792
793                 ret = image_util_resize(resized_raw_image, &resized_width, &resized_height, raw_image, width, height, colorspace);
794                 if (ret != MS_MEDIA_ERR_NONE) {
795                         media_svc_error("image_util_resize failed");
796                         *resize_image = image;
797                         *resize_size = size;
798                         SAFE_FREE(raw_image);
799                         SAFE_FREE(resized_raw_image);
800                         return MS_MEDIA_ERR_NONE;
801                 }
802                 SAFE_FREE(raw_image);
803
804                 /* encoding */
805                 ret = __media_svc_encode_jpeg((unsigned char *)resized_raw_image, (unsigned long)resized_width, (unsigned long)resized_height, colorspace, 90, resize_image, (unsigned long long *)resize_size);
806                 if (ret != MS_MEDIA_ERR_NONE) {
807                         media_svc_error("__media_svc_encode_jpeg failed");
808                         *resize_image = image;
809                         *resize_size = size;
810                         SAFE_FREE(resized_raw_image);
811                         return MS_MEDIA_ERR_NONE;
812                 }
813                 SAFE_FREE(resized_raw_image);
814
815                 if (*resize_image == NULL) {
816                         media_svc_error("*resize_image is null");
817                         *resize_image = image;
818                         *resize_size = size;
819                         return MS_MEDIA_ERR_NONE;
820                 }
821         } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
822                 media_svc_debug("type [png] size [%d]", size);
823                 *resize_image = image;
824                 *resize_size = size;
825
826         } else {
827                 media_svc_debug("Not proper img format");
828                 *resize_image = image;
829                 *resize_size = size;
830         }
831
832         return ret;
833 }
834
835 static int _media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
836 {
837         media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
838
839         if (!image) {
840                 media_svc_error("invalid image..");
841                 return MS_MEDIA_ERR_INVALID_PARAMETER;
842         }
843
844         struct statfs fs;
845         char *thumb_path = __media_svc_get_thumb_path(uid);
846         if (!STRING_VALID(thumb_path)) {
847                 media_svc_error("fail to get thumb_path");
848                 return MS_MEDIA_ERR_INTERNAL;
849         }
850
851         if (-1 == statfs(thumb_path, &fs)) {
852                 media_svc_error("error in statfs");
853                 SAFE_FREE(thumb_path);
854                 return MS_MEDIA_ERR_INTERNAL;
855         }
856
857         SAFE_FREE(thumb_path);
858
859         long bsize_kbytes = fs.f_bsize >> 10;
860
861         if ((bsize_kbytes * fs.f_bavail) < 1024) {
862                 media_svc_error("not enought space...");
863                 return MS_MEDIA_ERR_INTERNAL;
864         }
865
866         FILE *fp = NULL;
867         int nwrite = -1;
868         if (image != NULL && size > 0) {
869                 fp = fopen(image_path, "w");
870
871                 if (fp == NULL) {
872                         media_svc_error("failed to open file");
873                         return MS_MEDIA_ERR_INTERNAL;
874                 }
875
876                 nwrite = fwrite(image, 1, size, fp);
877                 if (nwrite != size) {
878                         media_svc_error("failed to write thumbnail");
879                         fclose(fp);
880                         return MS_MEDIA_ERR_INTERNAL;
881                 }
882                 fclose(fp);
883         }
884
885         return MS_MEDIA_ERR_NONE;
886 }
887
888 static char *_media_svc_get_title_from_filepath(const char *path)
889 {
890         char *filename = NULL;
891         char *title = NULL;
892         char    *ext = NULL;
893         int filename_len = -1;
894         int new_title_len = -1;
895
896         if (!path) {
897                 media_svc_error("path is NULL");
898                 return NULL;
899         }
900
901         filename = g_path_get_basename(path);
902         if (!STRING_VALID(filename)) {
903                 media_svc_error("wrong file name");
904                 SAFE_FREE(filename);
905                 return NULL;
906         }
907
908         filename_len = strlen(filename);
909
910         ext = g_strrstr(filename, ".");
911         if (!ext) {
912                 media_svc_error("there is no file extention");
913                 return filename;
914         }
915
916         new_title_len = filename_len - strlen(ext);
917         if (new_title_len < 1) {
918                 media_svc_error("title length is zero");
919                 SAFE_FREE(filename);
920                 return NULL;
921         }
922
923         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
924
925         SAFE_FREE(filename);
926
927         media_svc_debug("extract title is [%s]", title);
928
929         return title;
930 }
931
932 int _media_svc_rename_file(const char *old_name, const char *new_name)
933 {
934         if ((old_name == NULL) || (new_name == NULL)) {
935                 media_svc_error("invalid file name");
936                 return MS_MEDIA_ERR_INVALID_PARAMETER;
937         }
938
939         if (rename(old_name, new_name) < 0) {
940                 media_svc_stderror(" ");
941                 return MS_MEDIA_ERR_INTERNAL;
942         }
943
944         return MS_MEDIA_ERR_NONE;
945 }
946
947 int _media_svc_remove_file(const char *path)
948 {
949         int result = -1;
950
951         result = remove(path);
952         if (result == 0) {
953                 media_svc_debug("success to remove file");
954                 return MS_MEDIA_ERR_NONE;
955         } else {
956                 media_svc_stderror("fail to remove file result");
957                 return MS_MEDIA_ERR_INTERNAL;
958         }
959 }
960
961 int _media_svc_remove_all_files_in_dir(const char *dir_path)
962 {
963         struct dirent entry;
964         struct dirent *result;
965         struct stat st;
966         char filename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
967         DIR *dir = NULL;
968
969         dir = opendir(dir_path);
970         if (dir == NULL) {
971                 media_svc_error("%s is not exist", dir_path);
972                 return MS_MEDIA_ERR_INVALID_PARAMETER;
973         }
974
975         while (!readdir_r(dir, &entry, &result)) {
976                 if (result == NULL)
977                         break;
978
979                 if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0)
980                         continue;
981
982                 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry.d_name);
983
984                 if (stat(filename, &st) != 0)
985                         continue;
986
987                 if (S_ISDIR(st.st_mode))
988                         continue;
989
990                 if (unlink(filename) != 0) {
991                         media_svc_stderror("failed to remove");
992                         closedir(dir);
993                         return MS_MEDIA_ERR_INTERNAL;
994                 }
995         }
996
997         closedir(dir);
998         return MS_MEDIA_ERR_NONE;
999 }
1000
1001 char *_media_svc_get_thumb_internal_path(uid_t uid)
1002 {
1003         int len = 0;
1004         char *result_passwd = NULL;
1005         struct group *grpinfo = NULL;
1006         if (uid == getuid()) {
1007                 grpinfo = getgrnam("users");
1008                 if (grpinfo == NULL) {
1009                         media_svc_error("getgrnam(users) returns NULL !");
1010                         return NULL;
1011                 }
1012                 len = strlen(MEDIA_SVC_THUMB_INTERNAL_PATH);
1013                 if (len > 0)
1014                         result_passwd = strndup(MEDIA_SVC_THUMB_INTERNAL_PATH, len);
1015         } else {
1016                 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
1017                 struct passwd *userinfo = getpwuid(uid);
1018                 if (userinfo == NULL) {
1019                         media_svc_error("getpwuid(%d) returns NULL !", uid);
1020                         return NULL;
1021                 }
1022                 grpinfo = getgrnam("users");
1023                 if (grpinfo == NULL) {
1024                         media_svc_error("getgrnam(users) returns NULL !");
1025                         return NULL;
1026                 }
1027                 /* Compare git_t type and not group name */
1028                 if (grpinfo->gr_gid != userinfo->pw_gid) {
1029                         media_svc_error("UID [%d] does not belong to 'users' group!", uid);
1030                         return NULL;
1031                 }
1032                 len = snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/phone", userinfo->pw_dir);
1033                 if (len > 0)
1034                         result_passwd = strndup(passwd_str, len);
1035         }
1036
1037         return result_passwd;
1038 }
1039
1040 char *_media_svc_get_thumb_external_path(uid_t uid)
1041 {
1042         int len = 0;
1043         char *result_passwd = NULL;
1044         struct group *grpinfo = NULL;
1045         if (uid == getuid()) {
1046                 grpinfo = getgrnam("users");
1047                 if (grpinfo == NULL) {
1048                         media_svc_error("getgrnam(users) returns NULL !");
1049                         return NULL;
1050                 }
1051                 len = strlen(MEDIA_SVC_THUMB_EXTERNAL_PATH);
1052                 if (len > 0)
1053                         result_passwd = strndup(MEDIA_SVC_THUMB_EXTERNAL_PATH, len);
1054         } else {
1055                 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
1056                 struct passwd *userinfo = getpwuid(uid);
1057                 if (userinfo == NULL) {
1058                         media_svc_error("getpwuid(%d) returns NULL !", uid);
1059                         return NULL;
1060                 }
1061                 grpinfo = getgrnam("users");
1062                 if (grpinfo == NULL) {
1063                         media_svc_error("getgrnam(users) returns NULL !");
1064                         return NULL;
1065                 }
1066                 /* Compare git_t type and not group name */
1067                 if (grpinfo->gr_gid != userinfo->pw_gid) {
1068                         media_svc_error("UID [%d] does not belong to 'users' group!", uid);
1069                         return NULL;
1070                 }
1071                 len = snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/mmc", userinfo->pw_dir);
1072                 if (len > 0)
1073                         result_passwd = strndup(passwd_str, len);
1074         }
1075
1076         return result_passwd;
1077 }
1078
1079 static int __media_svc_check_thumb_dir(const char *thumb_dir)
1080 {
1081         int ret = 0;
1082         DIR *dir = NULL;
1083
1084         dir = opendir(thumb_dir);
1085         if (dir != NULL) {
1086                 closedir(dir);
1087         } else {
1088                 media_svc_stderror("opendir fail");
1089                 if (errno == ENOENT) {
1090                         media_svc_error("[%s] is not exit. So, make it", thumb_dir);
1091                         ret = mkdir(thumb_dir, 0777);
1092                         if (ret < 0) {
1093                                 media_svc_error("make fail");
1094                                 goto ERROR;
1095                         }
1096                 } else {
1097                         goto ERROR;
1098                 }
1099
1100                 ret = chmod(thumb_dir, 0777);
1101                 if (ret != 0)
1102                         media_svc_stderror("chmod failed");
1103
1104                 ret = chown(thumb_dir, 5000, 5000);
1105                 if (ret != 0)
1106                         media_svc_stderror("chown failed");
1107         }
1108
1109         return MS_MEDIA_ERR_NONE;
1110
1111 ERROR:
1112         return -1;
1113 }
1114
1115 int _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
1116 {
1117         int ret = MS_MEDIA_ERR_NONE;
1118         char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
1119         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
1120         char *thumb_dir = NULL;
1121         char hash[255 + 1] = {0, };
1122         char *thumbfile_ext = NULL;
1123         char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
1124         char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
1125
1126         if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
1127                 media_svc_error("fail to get thumbnail path");
1128                 SAFE_FREE(internal_thumb_path);
1129                 SAFE_FREE(external_thumb_path);
1130                 return MS_MEDIA_ERR_INTERNAL;
1131         }
1132
1133         thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
1134
1135         ret = __media_svc_check_thumb_dir(thumb_dir);
1136         if (ret != MS_MEDIA_ERR_NONE) {
1137                 media_svc_error("__media_svc_check_thumb_dir");
1138                 SAFE_FREE(internal_thumb_path);
1139                 SAFE_FREE(external_thumb_path);
1140                 return MS_MEDIA_ERR_INTERNAL;
1141         }
1142
1143         memset(file_ext, 0, sizeof(file_ext));
1144         if (!__media_svc_get_file_ext(pathname, file_ext))
1145                 media_svc_error("get file ext fail");
1146
1147         ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
1148         if (ret != MS_MEDIA_ERR_NONE) {
1149                 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
1150                 SAFE_FREE(internal_thumb_path);
1151                 SAFE_FREE(external_thumb_path);
1152                 return MS_MEDIA_ERR_INTERNAL;
1153         }
1154
1155         /*media_svc_debug("img format is [%s]", img_format); */
1156
1157         if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
1158                 thumbfile_ext = (char *)"jpg";
1159         } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
1160                 thumbfile_ext = (char *)"png";
1161         } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
1162                 thumbfile_ext = (char *)"gif";
1163         } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
1164                 thumbfile_ext = (char *)"bmp";
1165         } else {
1166                 media_svc_error("Not proper img format");
1167                 SAFE_FREE(internal_thumb_path);
1168                 SAFE_FREE(external_thumb_path);
1169                 return MS_MEDIA_ERR_INTERNAL;
1170         }
1171
1172         snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
1173         _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
1174         /*media_svc_debug("thumb_path is [%s]", thumb_path); */
1175
1176         SAFE_FREE(internal_thumb_path);
1177         SAFE_FREE(external_thumb_path);
1178
1179         return MS_MEDIA_ERR_NONE;
1180 }
1181
1182 int _media_svc_get_file_time(const char *full_path)
1183 {
1184         struct stat statbuf;
1185         int fd = 0;
1186
1187         memset(&statbuf, 0, sizeof(struct stat));
1188         fd = stat(full_path, &statbuf);
1189         if (fd == -1) {
1190                 media_svc_error("stat(%s) fails.", full_path);
1191                 return MS_MEDIA_ERR_INTERNAL;
1192         }
1193
1194         return statbuf.st_mtime;
1195 }
1196
1197 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
1198 {
1199         int ret = MS_MEDIA_ERR_NONE;
1200
1201         /* Set default GPS value before extracting meta information */
1202         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1203         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1204         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1205
1206         /* Set filename to title for all media */
1207         char *title = NULL;
1208         title = _media_svc_get_title_from_filepath(content_info->path);
1209         if (title) {
1210                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1211                 if (ret != MS_MEDIA_ERR_NONE)
1212                         media_svc_error("strcpy error");
1213                 SAFE_FREE(title);
1214         } else {
1215                 media_svc_error("Can't extract title");
1216                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
1217                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1218         }
1219
1220         /* Set default value before extracting meta information */
1221         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1222         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1223
1224         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, MEDIA_SVC_TAG_UNKNOWN);
1225         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1226
1227         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, MEDIA_SVC_TAG_UNKNOWN);
1228         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1229
1230         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
1231         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1232
1233         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
1234         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1235
1236         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, MEDIA_SVC_TAG_UNKNOWN);
1237         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1238
1239         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
1240         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1241
1242         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
1243         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1244
1245         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
1246         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1247
1248         if (refresh) {
1249                 media_svc_debug("refresh");
1250                 return MS_MEDIA_ERR_NONE;
1251         }
1252
1253         content_info->played_count = 0;
1254         content_info->last_played_time = 0;
1255         content_info->last_played_position = 0;
1256         content_info->favourate = 0;
1257         content_info->media_meta.rating = 0;
1258
1259         return MS_MEDIA_ERR_NONE;
1260 }
1261
1262 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, media_svc_storage_type_e storage_type,
1263                         const char *path, media_svc_media_type_e *media_type, bool refresh)
1264 {
1265         int ret = MS_MEDIA_ERR_NONE;
1266         char * media_uuid = NULL;
1267         char * file_name = NULL;
1268         bool drm_type = false;
1269         char mime_type[256] = {0, };
1270
1271         ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
1272         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1273
1274         if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1275                 struct stat st;
1276                 memset(&st, 0, sizeof(struct stat));
1277                 if (stat(path, &st) == 0) {
1278                         content_info->modified_time = st.st_mtime;
1279                         content_info->timeline = content_info->modified_time;
1280                         content_info->size = st.st_size;
1281                         /* media_svc_debug("Modified time : [%d] Size : [%lld]", content_info->modified_time, content_info->size); */
1282                 } else {
1283                         media_svc_stderror("stat failed");
1284                 }
1285         }
1286
1287         _media_svc_set_default_value(content_info, refresh);
1288
1289         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
1290         if (refresh) {
1291                 media_svc_debug("refresh");
1292                 return MS_MEDIA_ERR_NONE;
1293         }
1294
1295         ret = __media_svc_malloc_and_strncpy(&content_info->storage_uuid, storage_id);
1296         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1297
1298         content_info->storage_type = storage_type;
1299         time(&content_info->added_time);
1300
1301         media_uuid = _media_info_generate_uuid();
1302         if (media_uuid == NULL) {
1303                 _media_svc_destroy_content_info(content_info);
1304                 return MS_MEDIA_ERR_INTERNAL;
1305         }
1306
1307         ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
1308         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1309
1310         file_name = g_path_get_basename(path);
1311         ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
1312         SAFE_FREE(file_name);
1313         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1314
1315         if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1316                 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
1317                 if drm_contentinfo is not NULL, the file is OMA DRM.*/
1318                 ret = __media_svc_get_mime_type(path, mime_type);
1319                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1320
1321                 media_svc_debug("mime [%s]", mime_type);
1322                 content_info->is_drm = drm_type;
1323
1324                 ret = __media_svc_get_media_type(path, mime_type, media_type);
1325                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1326
1327                 if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1328                         media_svc_error("invalid media_type condition[%d]", *media_type);
1329                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1330                 }
1331
1332                 ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
1333                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1334
1335                 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
1336
1337                 content_info->media_type = *media_type;
1338         }
1339
1340         return MS_MEDIA_ERR_NONE;
1341 }
1342
1343 int image_360_check(char *path)
1344 {
1345         FILE *fp = NULL;
1346         long app1_size = 0;
1347         int size = 1;
1348         unsigned char exif_header[4];
1349         unsigned char exif_app1[2];
1350         unsigned char exif_app1_xmp[2];
1351         long exif_app1_xmp_size = 0;
1352         unsigned char exif_app1_xmp_t[2];
1353         char *xmp_data = 0;
1354         int size1 = 0;
1355         int size2 = 0;
1356         int fdata = 0;
1357         int temp = 0;
1358
1359         fp = fopen(path, "rb");
1360         if (fp == NULL)
1361                 goto ERROR;
1362
1363         size = fread(exif_header, 1, sizeof(exif_header), fp);
1364         if (size <= 0)
1365                 goto ERROR;
1366
1367         if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
1368                 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
1369                 if (size <= 0)
1370                         goto ERROR;
1371
1372                 size1 = exif_app1[0];
1373                 size2 = exif_app1[1];
1374
1375                 app1_size = size1 * 256 + size2 - 2;
1376
1377                 if (fseek(fp, app1_size, SEEK_CUR) != 0)
1378                         goto ERROR;
1379
1380                 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
1381                 if (size <= 0)
1382                         goto ERROR;
1383
1384                 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
1385                         int result = 0;
1386                         char *ptr = NULL;
1387                         size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
1388                         if (size <= 0)
1389                                 goto ERROR;
1390
1391                         size1 = exif_app1_xmp_t[0];
1392                         size2 = exif_app1_xmp_t[1];
1393
1394                         exif_app1_xmp_size = size1 * 256 + size2 - 2;
1395
1396                         xmp_data = (char *)malloc(exif_app1_xmp_size);
1397                         memset(xmp_data, 0x00, exif_app1_xmp_size);
1398                         ptr = xmp_data;
1399                         while (exif_app1_xmp_size >= 0) {
1400                                 exif_app1_xmp_size--;
1401                                 fdata = fgetc(fp);
1402                                 if (fdata == EOF)
1403                                         continue;
1404                                 *ptr = (char)fdata;
1405                                 ptr++;
1406                                 temp++;
1407                         }
1408                         ptr = ptr - temp;
1409
1410                         if (strstr(ptr, "UsePanoramaViewer")
1411                         && strstr(ptr, "True")
1412                         && strstr(ptr, "ProjectionType")
1413                         && strstr(ptr, "equirectangular"))
1414                                 result = 1;
1415
1416                         SAFE_FREE(xmp_data);
1417                         if (fp) {
1418                                 fclose(fp);
1419                                 fp = NULL;
1420                         }
1421                         return result;
1422                 } else {
1423                         goto ERROR;
1424                 }
1425         } else {
1426                 goto ERROR;
1427         }
1428 ERROR:
1429         if (fp) {
1430                 fclose(fp);
1431                 fp = NULL;
1432         }
1433         return 0;
1434 }
1435
1436 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1437 {
1438         int ret = MS_MEDIA_ERR_NONE;
1439         double value = 0.0;
1440         int orient_value = 0;
1441         int exif_width = 0;
1442         int exif_height = 0;
1443         ExifData *ed = NULL;
1444         int has_datetaken = FALSE;
1445         int datetaken_size = 19;
1446         double fnumber = 0.0;
1447         int iso = 0;
1448         char *path = NULL;
1449
1450         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1451         char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
1452         char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1453         char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1454
1455         memset(buf, 0x00, sizeof(buf));
1456         memset(description_buf, 0x00, sizeof(description_buf));
1457         memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
1458         memset(model_buf, 0x00, sizeof(model_buf));
1459
1460         if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1461                 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1462                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1463         }
1464
1465         path = content_info->path;
1466         if (!STRING_VALID(path)) {
1467                 media_svc_error("Invalid Path");
1468                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1469         }
1470
1471         /* Load an ExifData object from an EXIF file */
1472         ed = exif_data_new_from_file(path);
1473
1474         if (!ed) {
1475                 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1476                 goto GET_WIDTH_HEIGHT;
1477         }
1478
1479         content_info->media_meta.is_360 = image_360_check(path);
1480
1481         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1482                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1483                         if (strlen(buf) > 0) {
1484                                 if (strcmp(buf, "S") == 0)
1485                                         value = -1 * value;
1486                         }
1487                         content_info->media_meta.latitude = value;
1488                 } else {
1489                         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1490                 }
1491         } else {
1492                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1493         }
1494
1495         memset(buf, 0x00, sizeof(buf));
1496
1497         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1498                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1499                         if (strlen(buf) > 0) {
1500                                 if (strcmp(buf, "W") == 0)
1501                                         value = -1 * value;
1502                         }
1503                         content_info->media_meta.longitude = value;
1504                 } else {
1505                         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1506                 }
1507         } else {
1508                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1509         }
1510
1511         memset(buf, 0x00, sizeof(buf));
1512
1513         if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1514                 if (strlen(description_buf) == 0) {
1515                         /*media_svc_debug("Use 'No description'"); */
1516                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1517                         if (ret != MS_MEDIA_ERR_NONE)
1518                                 media_svc_error("strcpy error");
1519                 } else {
1520                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
1521                         if (ret != MS_MEDIA_ERR_NONE)
1522                                 media_svc_error("strcpy error");
1523                 }
1524         } else {
1525                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1526                 if (ret != MS_MEDIA_ERR_NONE)
1527                         media_svc_error("strcpy error");
1528         }
1529
1530         memset(buf, 0x00, sizeof(buf));
1531
1532         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1533                 if (strlen(buf) == 0) {
1534                         /*media_svc_debug("time is NULL"); */
1535                 } else {
1536                         ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1537                         if (ret != MS_MEDIA_ERR_NONE) {
1538                                 media_svc_error("strcpy error");
1539                         } else {
1540                                 has_datetaken = TRUE;
1541                                 /* This is same as recorded_date */
1542                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1543                                 if (ret != MS_MEDIA_ERR_NONE)
1544                                         media_svc_error("strcpy error");
1545                         }
1546                 }
1547         }
1548
1549         memset(buf, 0x00, sizeof(buf));
1550
1551         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1552                 if (strlen(buf) == 0) {
1553                         /*media_svc_debug("time is NULL"); */
1554                 } else {
1555                         ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1556                         if (ret != MS_MEDIA_ERR_NONE) {
1557                                 media_svc_error("strcpy error");
1558                         } else {
1559                                 has_datetaken = TRUE;
1560                                 /* This is same as recorded_date */
1561                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1562                                 if (ret != MS_MEDIA_ERR_NONE)
1563                                         media_svc_error("strcpy error");
1564                         }
1565                 }
1566         }
1567
1568         if (has_datetaken) {
1569                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1570                 if (content_info->timeline == 0)
1571                         content_info->timeline = content_info->modified_time;
1572                 else
1573                         media_svc_debug("Timeline : %ld", content_info->timeline);
1574         }
1575
1576         /* Get exposure_time value from exif. */
1577         if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1578                 if (strlen(exposure_time_buf) == 0) {
1579                         /* media_svc_debug("exposure_time_buf is NULL"); */
1580                 } else {
1581                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
1582                         if (ret != MS_MEDIA_ERR_NONE)
1583                                 media_svc_error("strcpy error");
1584                 }
1585         }
1586
1587         /* Get fnumber value from exif. */
1588         if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1589                 if (fnumber > 0.0)
1590                         content_info->media_meta.fnumber = fnumber;
1591                 else
1592                         content_info->media_meta.fnumber = 0.0;
1593         } else {
1594                 content_info->media_meta.fnumber = 0.0;
1595         }
1596
1597         /* Get iso value from exif. */
1598         if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1599                 if (iso > 0)
1600                         content_info->media_meta.iso = iso;
1601                 else
1602                         content_info->media_meta.iso = 0;
1603         } else {
1604                 content_info->media_meta.iso = 0;
1605         }
1606
1607         /* Get model value from exif. */
1608         if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1609                 if (strlen(model_buf) == 0) {
1610                         /* media_svc_debug("model_buf is NULL"); */
1611                 } else {
1612                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
1613                         if (ret != MS_MEDIA_ERR_NONE)
1614                                 media_svc_error("strcpy error");
1615                 }
1616         }
1617
1618         /* Get orientation value from exif. */
1619         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1620                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1621                         content_info->media_meta.orientation = orient_value;
1622                 else
1623                         content_info->media_meta.orientation = 0;
1624         } else {
1625                 content_info->media_meta.orientation = 0;
1626         }
1627
1628         /* Get width value from exif. */
1629         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1630                 if (exif_width > 0)
1631                         content_info->media_meta.width = exif_width;
1632                 else
1633                         content_info->media_meta.width = 0;
1634         } else {
1635                 content_info->media_meta.width = 0;
1636         }
1637
1638         /* Get height value from exif. */
1639         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1640                 if (exif_height > 0)
1641                         content_info->media_meta.height = exif_height;
1642                 else
1643                         content_info->media_meta.height = 0;
1644         } else {
1645                 content_info->media_meta.height = 0;
1646         }
1647
1648         if (ed != NULL) exif_data_unref(ed);
1649
1650 GET_WIDTH_HEIGHT:
1651
1652         if (content_info->media_meta.width == 0 ||
1653                 content_info->media_meta.height == 0) {
1654                 /*Get image width, height*/
1655                 unsigned int img_width = 0;
1656                 unsigned int img_height = 0;
1657                 ImgCodecType img_type = IMG_CODEC_NONE;
1658
1659                 ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1660
1661                 if (content_info->media_meta.width == 0)
1662                         content_info->media_meta.width = img_width;
1663
1664                 if (content_info->media_meta.height == 0)
1665                         content_info->media_meta.height = img_height;
1666         }
1667
1668         return MS_MEDIA_ERR_NONE;
1669 }
1670
1671 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1672 {
1673         MMHandleType tag = 0;
1674         char *p = NULL;
1675         int size = -1;
1676         int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1677         int mmf_error = FILEINFO_ERROR_NONE;
1678         char *err_attr_name = NULL;
1679         char *title = NULL;
1680         int album_id = 0;
1681         int ret = MS_MEDIA_ERR_NONE;
1682
1683         /*Get Content Tag attribute ===========*/
1684         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1685
1686         if (mmf_error == FILEINFO_ERROR_NONE) {
1687                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1688                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1689                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1690                         if (ret != MS_MEDIA_ERR_NONE)
1691                                 media_svc_error("strcpy error");
1692
1693                         /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1694                 } else {
1695                         SAFE_FREE(err_attr_name);
1696                         /*media_svc_debug("album - unknown"); */
1697                 }
1698
1699                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1700                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1701                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1702                         if (ret != MS_MEDIA_ERR_NONE)
1703                                 media_svc_error("strcpy error");
1704                         /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1705                 } else {
1706                         SAFE_FREE(err_attr_name);
1707                         /*media_svc_debug("artist - unknown"); */
1708                 }
1709
1710                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1711                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1712                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1713                         if (ret != MS_MEDIA_ERR_NONE)
1714                                 media_svc_error("strcpy error");
1715                         /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1716                 } else {
1717                         SAFE_FREE(err_attr_name);
1718                         /*media_svc_debug("album_artist - unknown"); */
1719                 }
1720
1721                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1722                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1723                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1724                         if (ret != MS_MEDIA_ERR_NONE)
1725                                 media_svc_error("strcpy error");
1726
1727                         /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1728                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1729                         /*
1730                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1731                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1732                         }
1733                         */
1734                 } else {
1735                         SAFE_FREE(err_attr_name);
1736                         /*media_svc_debug("genre - unknown"); */
1737                 }
1738
1739                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1740                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1741                         if (!isspace(*p)) {
1742                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1743                                 if (ret != MS_MEDIA_ERR_NONE)
1744                                         media_svc_error("strcpy error");
1745
1746                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1747                         } else {
1748                                 int idx = 0;
1749
1750                                 for (idx = 0; idx < size; idx++) {
1751                                         if (isspace(*p)) {
1752                                                 media_svc_debug("SPACE [%s]", p);
1753                                                 p++;
1754                                                 continue;
1755                                         } else {
1756                                                 media_svc_debug("Not SPACE [%s]", p);
1757                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1758                                                 if (ret != MS_MEDIA_ERR_NONE)
1759                                                         media_svc_error("strcpy error");
1760                                                 break;
1761                                         }
1762                                 }
1763
1764                                 if (idx == size) {
1765                                         media_svc_debug("Can't extract title. All string is space");
1766                                         title = _media_svc_get_title_from_filepath(content_info->path);
1767                                         if (title) {
1768                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1769                                                 if (ret != MS_MEDIA_ERR_NONE)
1770                                                         media_svc_error("strcpy error");
1771                                                 SAFE_FREE(title);
1772                                         } else {
1773                                                 media_svc_error("Can't extract title");
1774                                         }
1775                                 }
1776                         }
1777                 } else {
1778                         SAFE_FREE(err_attr_name);
1779                         title = _media_svc_get_title_from_filepath(content_info->path);
1780                         if (title) {
1781                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1782                                 if (ret != MS_MEDIA_ERR_NONE)
1783                                         media_svc_error("strcpy error");
1784                                 SAFE_FREE(title);
1785                         } else {
1786                                 media_svc_error("Can't extract title");
1787                         }
1788                 }
1789
1790                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1791                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1792                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1793                         if (ret != MS_MEDIA_ERR_NONE)
1794                                 media_svc_error("strcpy error");
1795                         /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1796                 } else {
1797                         SAFE_FREE(err_attr_name);
1798                 }
1799
1800                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1801                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1802                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1803                         if (ret != MS_MEDIA_ERR_NONE)
1804                                 media_svc_error("strcpy error");
1805                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1806                         /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1807                 } else {
1808                         /*media_svc_debug("composer - unknown"); */
1809                         SAFE_FREE(err_attr_name);
1810                 }
1811
1812                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1813                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1814                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1815                         if (ret != MS_MEDIA_ERR_NONE)
1816                                 media_svc_error("strcpy error");
1817                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1818                         /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1819                 } else {
1820                         /*media_svc_debug("copyright - unknown"); */
1821                         SAFE_FREE(err_attr_name);
1822                 }
1823
1824                 mmf_error = mm_file_destroy_tag_attrs(tag);
1825                 if (mmf_error != FILEINFO_ERROR_NONE)
1826                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1827         } else {
1828                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1829                 char *no_tag_title = NULL;
1830                 media_svc_error("no tag information");
1831
1832                 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1833                 if (no_tag_title) {
1834                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1835                         if (ret != MS_MEDIA_ERR_NONE)
1836                                 media_svc_error("strcpy error");
1837                         SAFE_FREE(no_tag_title);
1838                 } else {
1839                         media_svc_error("Can't extract title");
1840                 }
1841
1842                 content_info->album_id = album_id;
1843         }
1844
1845         return MS_MEDIA_ERR_NONE;
1846 }
1847
1848 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1849 {
1850         MMHandleType content = 0;
1851         MMHandleType tag = 0;
1852         char *p = NULL;
1853         unsigned char *image = NULL;
1854         unsigned int size = 0;
1855         int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1856         int mmf_error = FILEINFO_ERROR_NONE;
1857         char *err_attr_name = NULL;
1858         char *title = NULL;
1859         bool extract_thumbnail = FALSE;
1860         bool append_album = FALSE;
1861         int album_id = 0;
1862         int ret = MS_MEDIA_ERR_NONE;
1863         int cdis_value = 0;
1864         unsigned int resize_size = 0;
1865         unsigned char *resize_image = NULL;
1866
1867         /*Get Content Tag attribute ===========*/
1868         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1869
1870         if (mmf_error == FILEINFO_ERROR_NONE) {
1871                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1872                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1873                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1874                         if (ret != MS_MEDIA_ERR_NONE)
1875                                 media_svc_error("strcpy error");
1876
1877                         /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1878                 } else {
1879                         SAFE_FREE(err_attr_name);
1880                         /*media_svc_debug("album - unknown"); */
1881                 }
1882
1883                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1884                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1885                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1886                         if (ret != MS_MEDIA_ERR_NONE)
1887                                 media_svc_error("strcpy error");
1888                         /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1889                 } else {
1890                         SAFE_FREE(err_attr_name);
1891                         /*media_svc_debug("artist - unknown"); */
1892                 }
1893
1894                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1895                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1896                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1897                         if (ret != MS_MEDIA_ERR_NONE)
1898                                 media_svc_error("strcpy error");
1899                         /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1900                 } else {
1901                         SAFE_FREE(err_attr_name);
1902                         /*media_svc_debug("album_artist - unknown"); */
1903                 }
1904
1905                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1906                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1907                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1908                         if (ret != MS_MEDIA_ERR_NONE)
1909                                 media_svc_error("strcpy error");
1910
1911                         /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1912                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1913                         /*
1914                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1915                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1916                         }
1917                         */
1918                 } else {
1919                         SAFE_FREE(err_attr_name);
1920                         /*media_svc_debug("genre - unknown"); */
1921                 }
1922
1923                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1924                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1925                         if (!isspace(*p)) {
1926                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1927                                 if (ret != MS_MEDIA_ERR_NONE)
1928                                         media_svc_error("strcpy error");
1929
1930                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1931                         } else {
1932                                 int idx = 0;
1933
1934                                 for (idx = 0; idx < size; idx++) {
1935                                         if (isspace(*p)) {
1936                                                 media_svc_debug("SPACE [%s]", p);
1937                                                 p++;
1938                                                 continue;
1939                                         } else {
1940                                                 media_svc_debug("Not SPACE [%s]", p);
1941                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1942                                                 if (ret != MS_MEDIA_ERR_NONE)
1943                                                         media_svc_error("strcpy error");
1944                                                 break;
1945                                         }
1946                                 }
1947
1948                                 if (idx == size) {
1949                                         media_svc_debug("Can't extract title. All string is space");
1950                                         title = _media_svc_get_title_from_filepath(content_info->path);
1951                                         if (title) {
1952                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1953                                                 if (ret != MS_MEDIA_ERR_NONE)
1954                                                         media_svc_error("strcpy error");
1955                                                 SAFE_FREE(title);
1956                                         } else {
1957                                                 media_svc_error("Can't extract title");
1958                                         }
1959                                 }
1960                         }
1961                 } else {
1962                         SAFE_FREE(err_attr_name);
1963                         title = _media_svc_get_title_from_filepath(content_info->path);
1964                         if (title) {
1965                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1966                                 if (ret != MS_MEDIA_ERR_NONE)
1967                                         media_svc_error("strcpy error");
1968                                 SAFE_FREE(title);
1969                         } else {
1970                                 media_svc_error("Can't extract title");
1971                         }
1972                 }
1973
1974                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1975                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1976                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1977                         if (ret != MS_MEDIA_ERR_NONE)
1978                                 media_svc_error("strcpy error");
1979                         /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1980                 } else {
1981                         SAFE_FREE(err_attr_name);
1982                 }
1983
1984                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_360, &content_info->media_meta.is_360, NULL);
1985
1986                 if (mmf_error != FILEINFO_ERROR_NONE)
1987                         SAFE_FREE(err_attr_name);
1988
1989                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1990                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1991                         if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1992                                 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1993                                 char time_info[64] = {0, };
1994                                 char p_value[64] = {0, };
1995                                 int idx = 0;
1996                                 memset(time_info, 0x00, sizeof(time_info));
1997                                 memset(p_value, 0x00, sizeof(p_value));
1998                                 strncpy(p_value, p, size);
1999                                 for (idx = 0; idx < size; idx++) {
2000                                         if (p_value[idx] == '-') {
2001                                                 time_info[idx] = ':';
2002                                         } else if (p_value[idx] != '\0') {
2003                                                 time_info[idx] = p_value[idx];
2004                                         } else {
2005                                                 media_svc_error("strcpy error");
2006                                                 break;
2007                                         }
2008                                 }
2009                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
2010                         } else {
2011                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
2012                         }
2013
2014                         if (ret != MS_MEDIA_ERR_NONE) {
2015                                 media_svc_error("strcpy error");
2016                         } else {
2017                                 /* This is same as datetaken */
2018 #if 0
2019                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
2020 #else
2021                                 int datetaken_size = 19;
2022                                 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
2023 #endif
2024                                 if (ret != MS_MEDIA_ERR_NONE)
2025                                         media_svc_error("strcpy error");
2026
2027                                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
2028                                 if (content_info->timeline == 0)
2029                                         content_info->timeline = content_info->modified_time;
2030                                 else
2031                                         media_svc_debug("Timeline : %ld", content_info->timeline);
2032                         }
2033                         /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
2034                 } else {
2035                         SAFE_FREE(err_attr_name);
2036                 }
2037
2038                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
2039                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
2040                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
2041                         if (ret != MS_MEDIA_ERR_NONE)
2042                                 media_svc_error("strcpy error");
2043                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
2044                         /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
2045                 } else {
2046                         /*media_svc_debug("composer - unknown"); */
2047                         SAFE_FREE(err_attr_name);
2048                 }
2049
2050                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
2051                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
2052                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
2053                         if (ret != MS_MEDIA_ERR_NONE)
2054                                 media_svc_error("strcpy error");
2055                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
2056                         /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
2057                 } else {
2058                         /*media_svc_debug("copyright - unknown"); */
2059                         SAFE_FREE(err_attr_name);
2060                 }
2061
2062                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
2063                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
2064                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
2065                         if (ret != MS_MEDIA_ERR_NONE)
2066                                 media_svc_error("strcpy error");
2067                 } else {
2068                         SAFE_FREE(err_attr_name);
2069                 }
2070
2071                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
2072                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size == 4)) {
2073                         int year = 0;
2074                         if ((p != NULL) && (sscanf(p, "%d", &year))) {
2075                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
2076                                 if (ret != MS_MEDIA_ERR_NONE)
2077                                         media_svc_error("strcpy error");
2078                         } else {
2079                                 media_svc_debug("Wrong Year Information [%s]", p);
2080                         }
2081                 } else {
2082                         SAFE_FREE(err_attr_name);
2083                 }
2084
2085                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
2086                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
2087                         content_info->media_meta.rating = atoi(p);
2088                 } else {
2089                         SAFE_FREE(err_attr_name);
2090                         content_info->media_meta.rating = 0;
2091                 }
2092
2093                 /*Initialize album_id to 0. below code will set the album_id*/
2094                 content_info->album_id = album_id;
2095                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
2096
2097                 if (ret != MS_MEDIA_ERR_NONE) {
2098                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
2099                                 media_svc_debug("album does not exist. So start to make album art");
2100                                 extract_thumbnail = TRUE;
2101                                 append_album = TRUE;
2102                         } else {
2103                                 extract_thumbnail = TRUE;
2104                                 append_album = FALSE;
2105                         }
2106                 } else {
2107                         content_info->album_id = album_id;
2108                         append_album = FALSE;
2109
2110                         if ((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
2111                                 (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
2112
2113                                 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
2114                                 extract_thumbnail = TRUE;
2115                         } else {
2116                                 media_svc_debug("album already exists. don't need to make album art");
2117                                 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
2118                                 extract_thumbnail = TRUE;
2119                         }
2120                 }
2121
2122                 /*Do not extract artwork for the USB Storage content*/
2123                 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
2124                         extract_thumbnail = FALSE;
2125
2126                 if (extract_thumbnail == TRUE) {
2127                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
2128                         if (mmf_error != FILEINFO_ERROR_NONE) {
2129                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
2130                                 SAFE_FREE(err_attr_name);
2131                         } else {
2132                                 /*media_svc_debug("artwork size1 [%d]", size); */
2133                         }
2134
2135                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
2136                         if (mmf_error != FILEINFO_ERROR_NONE) {
2137                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
2138                                 SAFE_FREE(err_attr_name);
2139                         } else {
2140                                 /*media_svc_debug("artwork size2 [%d]", size); */
2141                         }
2142                         if (image != NULL && size > 0) {
2143                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
2144                                 int artwork_mime_size = -1;
2145
2146                                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
2147                                 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
2148                                         ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
2149                                         if (ret != MS_MEDIA_ERR_NONE)
2150                                                 media_svc_error("Fail to Get Thumbnail Path");
2151                                         /* albumart resizing */
2152                                         __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
2153                                 } else {
2154                                         SAFE_FREE(err_attr_name);
2155                                 }
2156
2157                                 if (strlen(thumb_path) > 0) {
2158                                         ret = _media_svc_save_image(resize_image, resize_size, thumb_path, uid);
2159                                         if (ret != MS_MEDIA_ERR_NONE) {
2160                                                 media_svc_error("Fail to Save Thumbnail Image");
2161                                         } else {
2162                                                 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
2163                                                 if (ret != MS_MEDIA_ERR_NONE)
2164                                                         media_svc_error("strcpy error");
2165                                         }
2166                                 }
2167                         }
2168                 }
2169
2170                 if (append_album == TRUE) {
2171                         if ((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
2172                                 (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
2173                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
2174                         else
2175                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
2176
2177                         content_info->album_id = album_id;
2178                 }
2179
2180                 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
2181                         double longitude = 0.0;
2182                         double latitude = 0.0;
2183                         double altitude = 0.0;
2184
2185                         __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
2186                         content_info->media_meta.longitude = longitude;
2187                         content_info->media_meta.latitude = latitude;
2188                         content_info->media_meta.altitude = altitude;
2189
2190                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
2191                         if (mmf_error != FILEINFO_ERROR_NONE) {
2192                                 cdis_value = 0;
2193                                 SAFE_FREE(err_attr_name);
2194                         }
2195
2196                         media_svc_debug("CDIS : %d", cdis_value);
2197                 }
2198
2199                 mmf_error = mm_file_destroy_tag_attrs(tag);
2200                 if (mmf_error != FILEINFO_ERROR_NONE)
2201                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
2202         }       else {
2203                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
2204                 char *no_tag_title = NULL;
2205                 media_svc_error("no tag information");
2206
2207                 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
2208                 if (no_tag_title) {
2209                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
2210                         if (ret != MS_MEDIA_ERR_NONE)
2211                                 media_svc_error("strcpy error");
2212                         SAFE_FREE(no_tag_title);
2213                 } else {
2214                         media_svc_error("Can't extract title");
2215                 }
2216
2217                 content_info->album_id = album_id;
2218         }
2219
2220         /*Get Content attribute ===========*/
2221         if (cdis_value == 1)
2222                 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
2223         else
2224                 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
2225
2226         if (mmf_error == FILEINFO_ERROR_NONE) {
2227                 /*Common attribute*/
2228                 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
2229                 if (mmf_error != FILEINFO_ERROR_NONE) {
2230                         SAFE_FREE(err_attr_name);
2231                         media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
2232                 } else {
2233                         /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
2234                 }
2235
2236                 /*Sound/Music attribute*/
2237                 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
2238
2239                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
2240                         if (mmf_error != FILEINFO_ERROR_NONE) {
2241                                 SAFE_FREE(err_attr_name);
2242                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2243                         } else {
2244                                 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
2245                         }
2246
2247                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
2248                         if (mmf_error != FILEINFO_ERROR_NONE) {
2249                                 SAFE_FREE(err_attr_name);
2250                                 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
2251                         } else {
2252                                 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
2253                         }
2254
2255                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
2256                         if (mmf_error != FILEINFO_ERROR_NONE) {
2257                                 SAFE_FREE(err_attr_name);
2258                                 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
2259                         } else {
2260                                 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
2261                         }
2262
2263                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
2264                         if (mmf_error != FILEINFO_ERROR_NONE) {
2265                                 SAFE_FREE(err_attr_name);
2266                                 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
2267                         } else {
2268                                 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
2269                         }
2270                 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)      {       /*Video attribute*/
2271                         int audio_bitrate = 0;
2272                         int video_bitrate = 0;
2273
2274                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
2275                         if (mmf_error != FILEINFO_ERROR_NONE) {
2276                                 SAFE_FREE(err_attr_name);
2277                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2278                         } else {
2279                                 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
2280                         }
2281
2282                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
2283                         if (mmf_error != FILEINFO_ERROR_NONE) {
2284                                 SAFE_FREE(err_attr_name);
2285                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2286                         } else {
2287                                 /*media_svc_debug("video bit rate : %d", video_bitrate); */
2288                         }
2289
2290                         content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
2291
2292                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
2293                         if (mmf_error != FILEINFO_ERROR_NONE) {
2294                                 SAFE_FREE(err_attr_name);
2295                                 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
2296                         } else {
2297                                 /*media_svc_debug("width : %d", content_info->media_meta.width); */
2298                         }
2299
2300                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
2301                         if (mmf_error != FILEINFO_ERROR_NONE) {
2302                                 SAFE_FREE(err_attr_name);
2303                                 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
2304                         } else {
2305                                 /*media_svc_debug("height : %d", content_info->media_meta.height); */
2306                         }
2307
2308                 } else {
2309                         media_svc_error("Not support type");
2310                         mmf_error = mm_file_destroy_content_attrs(content);
2311                         if (mmf_error != FILEINFO_ERROR_NONE)
2312                                 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2313
2314                         return MS_MEDIA_ERR_INVALID_PARAMETER;
2315                 }
2316
2317                 mmf_error = mm_file_destroy_content_attrs(content);
2318                 if (mmf_error != FILEINFO_ERROR_NONE)
2319                         media_svc_error("fail to free content attr - err(%x)", mmf_error);
2320         } else {
2321                 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
2322         }
2323
2324         return MS_MEDIA_ERR_NONE;
2325 }
2326
2327 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
2328 {
2329         media_svc_retm_if(content_info == NULL, "content info is NULL");
2330
2331         /* Delete media_svc_content_info_s */
2332         SAFE_FREE(content_info->media_uuid);
2333         SAFE_FREE(content_info->path);
2334         SAFE_FREE(content_info->file_name);
2335         SAFE_FREE(content_info->mime_type);
2336         SAFE_FREE(content_info->folder_uuid);
2337         SAFE_FREE(content_info->thumbnail_path);
2338         SAFE_FREE(content_info->storage_uuid);
2339
2340         /* Delete media_svc_content_meta_s */
2341         SAFE_FREE(content_info->media_meta.title);
2342         SAFE_FREE(content_info->media_meta.album);
2343         SAFE_FREE(content_info->media_meta.artist);
2344         SAFE_FREE(content_info->media_meta.album_artist);
2345         SAFE_FREE(content_info->media_meta.genre);
2346         SAFE_FREE(content_info->media_meta.composer);
2347         SAFE_FREE(content_info->media_meta.year);
2348         SAFE_FREE(content_info->media_meta.recorded_date);
2349         SAFE_FREE(content_info->media_meta.copyright);
2350         SAFE_FREE(content_info->media_meta.track_num);
2351         SAFE_FREE(content_info->media_meta.description);
2352         SAFE_FREE(content_info->media_meta.datetaken);
2353         SAFE_FREE(content_info->media_meta.exposure_time);
2354         SAFE_FREE(content_info->media_meta.model);
2355         SAFE_FREE(content_info->media_meta.weather);
2356         SAFE_FREE(content_info->media_meta.category);
2357         SAFE_FREE(content_info->media_meta.keyword);
2358         SAFE_FREE(content_info->media_meta.location_tag);
2359         SAFE_FREE(content_info->media_meta.content_name);
2360         SAFE_FREE(content_info->media_meta.age_rating);
2361         SAFE_FREE(content_info->media_meta.author);
2362         SAFE_FREE(content_info->media_meta.provider);
2363
2364         SAFE_FREE(content_info->media_meta.title_pinyin);
2365         SAFE_FREE(content_info->media_meta.album_pinyin);
2366         SAFE_FREE(content_info->media_meta.artist_pinyin);
2367         SAFE_FREE(content_info->media_meta.album_artist_pinyin);
2368         SAFE_FREE(content_info->media_meta.genre_pinyin);
2369         SAFE_FREE(content_info->media_meta.composer_pinyin);
2370         SAFE_FREE(content_info->media_meta.copyright_pinyin);
2371         SAFE_FREE(content_info->media_meta.description_pinyin);
2372
2373         return;
2374 }
2375
2376 int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
2377 {
2378         if (STRING_VALID(path)) {
2379                 char *internal_path = _media_svc_get_path(uid);
2380                 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0)) {
2381                         *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
2382                 } else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
2383                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2384                 } else if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
2385                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
2386                 } else {
2387                         media_svc_error("Invalid Path");
2388                         SAFE_FREE(internal_path);
2389                         return MS_MEDIA_ERR_INVALID_PARAMETER;
2390                 }
2391                 SAFE_FREE(internal_path);
2392         } else {
2393                 media_svc_error("INVALID parameter");
2394                 return MS_MEDIA_ERR_INVALID_PARAMETER;
2395         }
2396
2397         return MS_MEDIA_ERR_NONE;
2398 }
2399
2400 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2401 {
2402         char *result, *sr;
2403         size_t i, count = 0;
2404         size_t oldlen = strlen(olds);
2405         if (oldlen < 1) return s;
2406         size_t newlen = strlen(news);
2407
2408         if (newlen != oldlen) {
2409                 for (i = 0; s[i] != '\0';) {
2410                         if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
2411                         else i++;
2412                 }
2413         } else i = strlen(s);
2414
2415
2416         result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
2417         if (result == NULL) return NULL;
2418
2419         sr = result;
2420         while (*s) {
2421                 if (memcmp(s, olds, oldlen) == 0) {
2422                         memcpy(sr, news, newlen);
2423                         sr += newlen;
2424                         s += oldlen;
2425                 } else *sr++ = *s++;
2426         }
2427
2428         *sr = '\0';
2429
2430         return result;
2431 }
2432
2433 bool _media_svc_is_drm_file(const char *path)
2434 {
2435         return FALSE;
2436 }
2437
2438 int _media_svc_request_thumbnail_with_origin_size(const char *path, char *thumb_path, int max_length, int *origin_width, int *origin_height, uid_t uid)
2439 {
2440         int ret = MS_MEDIA_ERR_NONE;
2441
2442         ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
2443
2444         if (ret != MS_MEDIA_ERR_NONE) {
2445                 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2446                 ret = MS_MEDIA_ERR_INTERNAL;
2447         } else {
2448                 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2449         }
2450
2451         return ret;
2452 }
2453
2454 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2455 {
2456         int ret = MS_MEDIA_ERR_NONE;
2457         int size = 0;
2458         pinyin_name_s *pinyinname = NULL;
2459
2460         *pinyin_str = NULL;
2461
2462         if (!STRING_VALID(src_str)) {
2463                 media_svc_debug("String is invalid");
2464                 return ret;
2465         }
2466
2467         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2468         if (ret == MS_MEDIA_ERR_NONE) {
2469                 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
2470                         *pinyin_str = strdup(pinyinname[0].pinyin_name);
2471                 else
2472                         *pinyin_str = strdup(src_str);  /* Return Original Non China Character */
2473         }
2474
2475         _media_svc_pinyin_free(pinyinname, size);
2476
2477         return ret;
2478 }
2479
2480 bool _media_svc_check_pinyin_support(void)
2481 {
2482         /*Check CSC*/
2483         return TRUE;
2484 }
2485
2486 int _media_svc_get_ini_value()
2487 {
2488         if (g_ini_value == -1) {
2489                 dictionary *dict = NULL;
2490
2491                 dict = iniparser_load(MEDIA_SVC_INI_DEFAULT_PATH);
2492                 if (!dict) {
2493                         media_svc_error("%s load failed", MEDIA_SVC_INI_DEFAULT_PATH);
2494                         return -1;
2495                 }
2496
2497                 MEDIA_SVC_INI_GET_INT(dict, "media-content-config:thumbnail_activation", g_ini_value, 0);
2498                 iniparser_freedict(dict);
2499         }
2500         media_svc_debug("Thumb-server activation level = %d", g_ini_value);
2501
2502         return g_ini_value;
2503 }
2504
2505 char* _media_svc_get_title_from_path(const char *path)
2506 {
2507         char *filename = NULL;
2508         char *title = NULL;
2509         char    *ext = NULL;
2510         int filename_len = -1;
2511         int new_title_len = -1;
2512
2513         if (!path) {
2514                 media_svc_error("path is NULL");
2515                 return NULL;
2516         }
2517
2518         filename = g_path_get_basename(path);
2519         if (!STRING_VALID(filename)) {
2520                 media_svc_error("wrong file name");
2521                 SAFE_FREE(filename);
2522                 return NULL;
2523         }
2524
2525         filename_len = strlen(filename);
2526
2527         ext = g_strrstr(filename, ".");
2528         if (!ext) {
2529                 media_svc_error("there is no file extention");
2530                 return filename;
2531         }
2532
2533         new_title_len = filename_len - strlen(ext);
2534         if (new_title_len < 1) {
2535                 media_svc_error("title length is zero");
2536                 SAFE_FREE(filename);
2537                 return NULL;
2538         }
2539
2540         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2541
2542         SAFE_FREE(filename);
2543
2544         media_svc_debug("extract title is [%s]", title);
2545
2546         return title;
2547 }
2548
2549 #define BUF_LENGHT 256
2550
2551 void _media_svc_print_stderror(void)
2552 {
2553         char buf[BUF_LENGHT] = {0,};
2554
2555         media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));
2556 }