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