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