Sync with tizen_3.0 patch
[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_debug("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_debug("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_debug("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_debug("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_debug("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_debug("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_debug("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_debug("Failed to get timeline : [%s] [%d:%d:%d: %d:%d:%d]", timstr, t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
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 {
623         int res = IMAGE_UTIL_ERROR_NONE;
624         image_util_encode_h encoder = NULL;
625         unsigned char *encoded_data = NULL;
626         res = image_util_encode_create(IMAGE_UTIL_JPEG , &encoder);
627         if (res != IMAGE_UTIL_ERROR_NONE) {
628                 media_svc_error("image_util_encode_create failed! (%d)", res);
629                 return MS_MEDIA_ERR_INTERNAL;
630         }
631         res = image_util_encode_set_resolution(encoder, width, height);
632         if (res != IMAGE_UTIL_ERROR_NONE) {
633                 media_svc_error("image_util_encode_set_resolution failed! (%d)", res);
634                 image_util_encode_destroy(encoder);
635                 return MS_MEDIA_ERR_INTERNAL;
636         }
637         res = image_util_encode_set_colorspace(encoder, colorspace);
638         if (res != IMAGE_UTIL_ERROR_NONE) {
639                 media_svc_error("image_util_encode_set_colorspace failed! (%d)", res);
640                 image_util_encode_destroy(encoder);
641                 return MS_MEDIA_ERR_INTERNAL;
642         }
643         res = image_util_encode_set_quality(encoder, quality);
644         if (res != IMAGE_UTIL_ERROR_NONE) {
645                 media_svc_error("image_util_encode_set_quality failed! (%d)", res);
646                 image_util_encode_destroy(encoder);
647                 return MS_MEDIA_ERR_INTERNAL;
648         }
649         res = image_util_encode_set_input_buffer(encoder, src);
650         if (res != IMAGE_UTIL_ERROR_NONE) {
651                 media_svc_error("image_util_encode_set_input_buffer failed! (%d)", res);
652                 image_util_encode_destroy(encoder);
653                 return MS_MEDIA_ERR_INTERNAL;
654         }
655         res = image_util_encode_set_output_buffer(encoder, &encoded_data);
656         if (res != IMAGE_UTIL_ERROR_NONE) {
657                 media_svc_error("image_util_decode_set_output_buffer failed! (%d)", res);
658                 image_util_encode_destroy(encoder);
659                 return MS_MEDIA_ERR_INTERNAL;
660         }
661         res = image_util_encode_run(encoder, dst_size);
662         if (res != IMAGE_UTIL_ERROR_NONE) {
663                 media_svc_error("image_util_encode_run failed! (%d)", res);
664                 image_util_encode_destroy(encoder);
665                 return MS_MEDIA_ERR_INTERNAL;
666         }
667         if (encoded_data != NULL) {
668                 *dst = (unsigned char *)calloc(1, *dst_size);
669                 if (*dst == NULL) {
670                         media_svc_error("memory allocation failed! (%lld)", *dst_size);
671                         image_util_encode_destroy(encoder);
672                         return MS_MEDIA_ERR_INTERNAL;
673                 }
674                 memcpy(*dst, encoded_data, *dst_size);
675         }
676         res = image_util_encode_destroy(encoder);
677         if (res != IMAGE_UTIL_ERROR_NONE) {
678                 media_svc_error("image_util_encode_destroy failed! (%d)", res);
679                 return MS_MEDIA_ERR_INTERNAL;
680         }
681         SAFE_FREE(encoded_data);
682         return MS_MEDIA_ERR_NONE;
683 }
684
685 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)
686 {
687         int res = IMAGE_UTIL_ERROR_NONE;
688         image_util_decode_h decoder = NULL;
689         res = image_util_decode_create(&decoder);
690         if (res != IMAGE_UTIL_ERROR_NONE) {
691                 media_svc_error("image_util_decode_create failed! (%d)", res);
692                 return MS_MEDIA_ERR_INTERNAL;
693         }
694         res = image_util_decode_set_input_buffer(decoder, src, size);
695         if (res != IMAGE_UTIL_ERROR_NONE) {
696                 media_svc_error("image_util_decode_set_input_buffer failed! (%d)", res);
697                 image_util_decode_destroy(decoder);
698                 return MS_MEDIA_ERR_INTERNAL;
699         }
700         res = image_util_decode_set_colorspace(decoder, colorspace);
701         if (res != IMAGE_UTIL_ERROR_NONE) {
702                 media_svc_error("image_util_decode_set_colorspace failed! (%d)", res);
703                 image_util_decode_destroy(decoder);
704                 return MS_MEDIA_ERR_INTERNAL;
705         }
706         res = image_util_decode_set_output_buffer(decoder, dst);
707         if (res != IMAGE_UTIL_ERROR_NONE) {
708                 media_svc_error("image_util_decode_set_output_buffer failed! (%d)", res);
709                 image_util_decode_destroy(decoder);
710                 return MS_MEDIA_ERR_INTERNAL;
711         }
712         res = image_util_decode_run(decoder, width, height, dst_size);
713         if (res != IMAGE_UTIL_ERROR_NONE) {
714                 media_svc_error("image_util_decode_run failed! (%d)", res);
715                 image_util_decode_destroy(decoder);
716                 return MS_MEDIA_ERR_INTERNAL;
717         }
718
719         res = image_util_decode_destroy(decoder);
720         if (res != IMAGE_UTIL_ERROR_NONE) {
721                 media_svc_error("image_util_decode_destroy failed! (%d)", res);
722                 return MS_MEDIA_ERR_INTERNAL;
723         }
724         return MS_MEDIA_ERR_NONE;
725 }
726
727 static int __media_svc_resize_artwork(unsigned char *image, unsigned int size, const char *img_format, unsigned char **resize_image, unsigned int *resize_size)
728 {
729         int ret = MS_MEDIA_ERR_NONE;
730         unsigned char *raw_image = NULL;
731         int width = 0;
732         int height = 0;
733         unsigned int raw_size = 0;
734         void *resized_raw_image = NULL;
735         int resized_width = 0;
736         int resized_height = 0;
737         unsigned int buf_size = 0;
738         image_util_colorspace_e colorspace = IMAGE_UTIL_COLORSPACE_RGB888;
739
740         if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
741                 media_svc_debug("type [jpeg] size [%d]", size);
742                 /* decoding */
743                 ret = __media_svc_decode_jpeg(image, (unsigned long long)size, colorspace, &raw_image, (unsigned long *)&width, (unsigned long *)&height, (unsigned long long *)&raw_size);
744                 if (ret != MS_MEDIA_ERR_NONE) {
745                         media_svc_error("__media_svc_decode_jpeg failed");
746                         *resize_image = image;
747                         *resize_size = size;
748                         return MS_MEDIA_ERR_NONE;
749                 }
750
751                 if (raw_image == NULL) {
752                         media_svc_error("raw_image is null");
753                         *resize_image = image;
754                         *resize_size = size;
755                         return MS_MEDIA_ERR_NONE;
756                 }
757
758                 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
759                         media_svc_debug("No need resizing");
760                         *resize_image = image;
761                         *resize_size = size;
762                         SAFE_FREE(raw_image);
763                         return MS_MEDIA_ERR_NONE;
764                 }
765                 /* resizing */
766                 if (width > height) {
767                         resized_height = MEDIA_SVC_ARTWORK_SIZE;
768                         resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
769                 } else {
770                         resized_width = MEDIA_SVC_ARTWORK_SIZE;
771                         resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
772                 }
773
774                 image_util_calculate_buffer_size(resized_width, resized_height, colorspace, &buf_size);
775
776                 resized_raw_image = malloc(buf_size);
777
778                 if (resized_raw_image == NULL) {
779                         media_svc_error("malloc failed");
780                         *resize_image = image;
781                         *resize_size = size;
782                         SAFE_FREE(raw_image);
783                         return MS_MEDIA_ERR_NONE;
784                 }
785
786                 memset(resized_raw_image, 0, buf_size);
787
788                 ret = image_util_resize(resized_raw_image, &resized_width, &resized_height, raw_image, width, height, colorspace);
789                 if (ret != MS_MEDIA_ERR_NONE) {
790                         media_svc_error("image_util_resize failed");
791                         *resize_image = image;
792                         *resize_size = size;
793                         SAFE_FREE(raw_image);
794                         SAFE_FREE(resized_raw_image);
795                         return MS_MEDIA_ERR_NONE;
796                 }
797                 SAFE_FREE(raw_image);
798
799                 /* encoding */
800                 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);
801                 if (ret != MS_MEDIA_ERR_NONE) {
802                         media_svc_error("__media_svc_encode_jpeg failed");
803                         *resize_image = image;
804                         *resize_size = size;
805                         SAFE_FREE(resized_raw_image);
806                         return MS_MEDIA_ERR_NONE;
807                 }
808                 SAFE_FREE(resized_raw_image);
809
810                 if (*resize_image == NULL) {
811                         media_svc_error("*resize_image is null");
812                         *resize_image = image;
813                         *resize_size = size;
814                         return MS_MEDIA_ERR_NONE;
815                 }
816         } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
817                 media_svc_debug("type [png] size [%d]", size);
818                 *resize_image = image;
819                 *resize_size = size;
820
821         } else {
822                 media_svc_debug("Not proper img format");
823                 *resize_image = image;
824                 *resize_size = size;
825         }
826
827         return ret;
828 }
829
830 static int _media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
831 {
832         media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
833
834         if (!image) {
835                 media_svc_error("invalid image..");
836                 return MS_MEDIA_ERR_INVALID_PARAMETER;
837         }
838
839         struct statfs fs;
840         char *thumb_path = __media_svc_get_thumb_path(uid);
841         if (!STRING_VALID(thumb_path)) {
842                 media_svc_error("fail to get thumb_path");
843                 return MS_MEDIA_ERR_INTERNAL;
844         }
845
846         if (-1 == statfs(thumb_path, &fs)) {
847                 media_svc_error("error in statfs");
848                 SAFE_FREE(thumb_path);
849                 return MS_MEDIA_ERR_INTERNAL;
850         }
851
852         SAFE_FREE(thumb_path);
853
854         long bsize_kbytes = fs.f_bsize >> 10;
855
856         if ((bsize_kbytes * fs.f_bavail) < 1024) {
857                 media_svc_error("not enought space...");
858                 return MS_MEDIA_ERR_INTERNAL;
859         }
860
861         FILE *fp = NULL;
862         int nwrite = -1;
863         if (image != NULL && size > 0) {
864                 fp = fopen(image_path, "w");
865
866                 if (fp == NULL) {
867                         media_svc_error("failed to open file");
868                         return MS_MEDIA_ERR_INTERNAL;
869                 }
870
871                 nwrite = fwrite(image, 1, size, fp);
872                 if (nwrite != size) {
873                         media_svc_error("failed to write thumbnail");
874                         fclose(fp);
875                         return MS_MEDIA_ERR_INTERNAL;
876                 }
877                 fclose(fp);
878         }
879
880         return MS_MEDIA_ERR_NONE;
881 }
882
883 static char *_media_svc_get_title_from_filepath(const char *path)
884 {
885         char *filename = NULL;
886         char *title = NULL;
887         char    *ext = NULL;
888         int filename_len = -1;
889         int new_title_len = -1;
890
891         if (!path) {
892                 media_svc_error("path is NULL");
893                 return NULL;
894         }
895
896         filename = g_path_get_basename(path);
897         if (!STRING_VALID(filename)) {
898                 media_svc_error("wrong file name");
899                 SAFE_FREE(filename);
900                 return NULL;
901         }
902
903         filename_len = strlen(filename);
904
905         ext = g_strrstr(filename, ".");
906         if (!ext) {
907                 media_svc_error("there is no file extention");
908                 return filename;
909         }
910
911         new_title_len = filename_len - strlen(ext);
912         if (new_title_len < 1) {
913                 media_svc_error("title length is zero");
914                 SAFE_FREE(filename);
915                 return NULL;
916         }
917
918         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
919
920         SAFE_FREE(filename);
921
922         media_svc_debug("extract title is [%s]", title);
923
924         return title;
925 }
926
927 int _media_svc_rename_file(const char *old_name, const char *new_name)
928 {
929         if ((old_name == NULL) || (new_name == NULL)) {
930                 media_svc_error("invalid file name");
931                 return MS_MEDIA_ERR_INVALID_PARAMETER;
932         }
933
934         if (rename(old_name, new_name) < 0) {
935                 media_svc_stderror(" ");
936                 return MS_MEDIA_ERR_INTERNAL;
937         }
938
939         return MS_MEDIA_ERR_NONE;
940 }
941
942 int _media_svc_remove_file(const char *path)
943 {
944         int result = -1;
945
946         result = remove(path);
947         if (result == 0) {
948                 media_svc_debug("success to remove file");
949                 return MS_MEDIA_ERR_NONE;
950         } else {
951                 media_svc_stderror("fail to remove file result");
952                 return MS_MEDIA_ERR_INTERNAL;
953         }
954 }
955
956 int _media_svc_remove_all_files_in_dir(const char *dir_path)
957 {
958         struct dirent entry;
959         struct dirent *result;
960         struct stat st;
961         char filename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
962         DIR *dir = NULL;
963
964         dir = opendir(dir_path);
965         if (dir == NULL) {
966                 media_svc_error("%s is not exist", dir_path);
967                 return MS_MEDIA_ERR_INVALID_PARAMETER;
968         }
969
970         while (!readdir_r(dir, &entry, &result)) {
971                 if (result == NULL)
972                         break;
973
974                 if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0)
975                         continue;
976
977                 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry.d_name);
978
979                 if (stat(filename, &st) != 0)
980                         continue;
981
982                 if (S_ISDIR(st.st_mode))
983                         continue;
984
985                 if (unlink(filename) != 0) {
986                         media_svc_stderror("failed to remove");
987                         closedir(dir);
988                         return MS_MEDIA_ERR_INTERNAL;
989                 }
990         }
991
992         closedir(dir);
993         return MS_MEDIA_ERR_NONE;
994 }
995
996 char *_media_svc_get_thumb_internal_path(uid_t uid)
997 {
998         int len = 0;
999         char *result_passwd = NULL;
1000         struct group *grpinfo = NULL;
1001         if (uid == getuid()) {
1002                 grpinfo = getgrnam("users");
1003                 if (grpinfo == NULL) {
1004                         media_svc_error("getgrnam(users) returns NULL !");
1005                         return NULL;
1006                 }
1007                 len = strlen(MEDIA_SVC_THUMB_INTERNAL_PATH);
1008                 if (len > 0)
1009                         result_passwd = strndup(MEDIA_SVC_THUMB_INTERNAL_PATH, len);
1010         } else {
1011                 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
1012                 struct passwd *userinfo = getpwuid(uid);
1013                 if (userinfo == NULL) {
1014                         media_svc_error("getpwuid(%d) returns NULL !", uid);
1015                         return NULL;
1016                 }
1017                 grpinfo = getgrnam("users");
1018                 if (grpinfo == NULL) {
1019                         media_svc_error("getgrnam(users) returns NULL !");
1020                         return NULL;
1021                 }
1022                 /* Compare git_t type and not group name */
1023                 if (grpinfo->gr_gid != userinfo->pw_gid) {
1024                         media_svc_error("UID [%d] does not belong to 'users' group!", uid);
1025                         return NULL;
1026                 }
1027                 len = snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/phone", userinfo->pw_dir);
1028                 if (len > 0)
1029                         result_passwd = strndup(passwd_str, len);
1030         }
1031
1032         return result_passwd;
1033 }
1034
1035 char *_media_svc_get_thumb_external_path(uid_t uid)
1036 {
1037         int len = 0;
1038         char *result_passwd = NULL;
1039         struct group *grpinfo = NULL;
1040         if (uid == getuid()) {
1041                 grpinfo = getgrnam("users");
1042                 if (grpinfo == NULL) {
1043                         media_svc_error("getgrnam(users) returns NULL !");
1044                         return NULL;
1045                 }
1046                 len = strlen(MEDIA_SVC_THUMB_EXTERNAL_PATH);
1047                 if (len > 0)
1048                         result_passwd = strndup(MEDIA_SVC_THUMB_EXTERNAL_PATH, len);
1049         } else {
1050                 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
1051                 struct passwd *userinfo = getpwuid(uid);
1052                 if (userinfo == NULL) {
1053                         media_svc_error("getpwuid(%d) returns NULL !", uid);
1054                         return NULL;
1055                 }
1056                 grpinfo = getgrnam("users");
1057                 if (grpinfo == NULL) {
1058                         media_svc_error("getgrnam(users) returns NULL !");
1059                         return NULL;
1060                 }
1061                 /* Compare git_t type and not group name */
1062                 if (grpinfo->gr_gid != userinfo->pw_gid) {
1063                         media_svc_error("UID [%d] does not belong to 'users' group!", uid);
1064                         return NULL;
1065                 }
1066                 len = snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/mmc", userinfo->pw_dir);
1067                 if (len > 0)
1068                         result_passwd = strndup(passwd_str, len);
1069         }
1070
1071         return result_passwd;
1072 }
1073
1074 static int __media_svc_check_thumb_dir(const char *thumb_dir)
1075 {
1076         int ret = 0;
1077         DIR *dir = NULL;
1078
1079         dir = opendir(thumb_dir);
1080         if (dir != NULL) {
1081                 closedir(dir);
1082         } else {
1083                 media_svc_stderror("opendir fail");
1084                 if (errno == ENOENT) {
1085                         media_svc_error("[%s] is not exit. So, make it", thumb_dir);
1086                         ret = mkdir(thumb_dir, 0777);
1087                         if (ret < 0) {
1088                                 media_svc_error("make fail");
1089                                 goto ERROR;
1090                         }
1091                 } else {
1092                         goto ERROR;
1093                 }
1094
1095                 ret = chmod(thumb_dir, 0777);
1096                 if (ret != 0)
1097                         media_svc_stderror("chmod failed");
1098
1099                 ret = chown(thumb_dir, 5000, 5000);
1100                 if (ret != 0)
1101                         media_svc_stderror("chown failed");
1102         }
1103
1104         return MS_MEDIA_ERR_NONE;
1105
1106 ERROR:
1107         return -1;
1108 }
1109
1110 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)
1111 {
1112         int ret = MS_MEDIA_ERR_NONE;
1113         char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
1114         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
1115         char *thumb_dir = NULL;
1116         char hash[255 + 1] = {0, };
1117         char *thumbfile_ext = NULL;
1118         char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
1119         char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
1120
1121         if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
1122                 media_svc_error("fail to get thumbnail path");
1123                 SAFE_FREE(internal_thumb_path);
1124                 SAFE_FREE(external_thumb_path);
1125                 return MS_MEDIA_ERR_INTERNAL;
1126         }
1127
1128         thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
1129
1130         ret = __media_svc_check_thumb_dir(thumb_dir);
1131         if (ret != MS_MEDIA_ERR_NONE) {
1132                 media_svc_error("__media_svc_check_thumb_dir");
1133                 SAFE_FREE(internal_thumb_path);
1134                 SAFE_FREE(external_thumb_path);
1135                 return MS_MEDIA_ERR_INTERNAL;
1136         }
1137
1138         memset(file_ext, 0, sizeof(file_ext));
1139         if (!__media_svc_get_file_ext(pathname, file_ext))
1140                 media_svc_error("get file ext fail");
1141
1142         ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
1143         if (ret != MS_MEDIA_ERR_NONE) {
1144                 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
1145                 SAFE_FREE(internal_thumb_path);
1146                 SAFE_FREE(external_thumb_path);
1147                 return MS_MEDIA_ERR_INTERNAL;
1148         }
1149
1150         /*media_svc_debug("img format is [%s]", img_format); */
1151
1152         if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
1153                 thumbfile_ext = (char *)"jpg";
1154         } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
1155                 thumbfile_ext = (char *)"png";
1156         } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
1157                 thumbfile_ext = (char *)"gif";
1158         } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
1159                 thumbfile_ext = (char *)"bmp";
1160         } else {
1161                 media_svc_error("Not proper img format");
1162                 SAFE_FREE(internal_thumb_path);
1163                 SAFE_FREE(external_thumb_path);
1164                 return MS_MEDIA_ERR_INTERNAL;
1165         }
1166
1167         snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
1168         _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
1169         /*media_svc_debug("thumb_path is [%s]", thumb_path); */
1170
1171         SAFE_FREE(internal_thumb_path);
1172         SAFE_FREE(external_thumb_path);
1173
1174         return MS_MEDIA_ERR_NONE;
1175 }
1176
1177 int _media_svc_get_file_time(const char *full_path)
1178 {
1179         struct stat statbuf;
1180         int fd = 0;
1181
1182         memset(&statbuf, 0, sizeof(struct stat));
1183         fd = stat(full_path, &statbuf);
1184         if (fd == -1) {
1185                 media_svc_error("stat(%s) fails.", full_path);
1186                 return MS_MEDIA_ERR_INTERNAL;
1187         }
1188
1189         return statbuf.st_mtime;
1190 }
1191
1192 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
1193 {
1194         int ret = MS_MEDIA_ERR_NONE;
1195
1196         /* Set default GPS value before extracting meta information */
1197         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1198         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1199         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1200
1201         /* Set filename to title for all media */
1202         char *title = NULL;
1203         title = _media_svc_get_title_from_filepath(content_info->path);
1204         if (title) {
1205                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1206                 if (ret != MS_MEDIA_ERR_NONE)
1207                         media_svc_error("strcpy error");
1208                 SAFE_FREE(title);
1209         } else {
1210                 media_svc_error("Can't extract title");
1211                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
1212                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1213         }
1214
1215         /* Set default value before extracting meta information */
1216         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1217         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1218
1219         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, MEDIA_SVC_TAG_UNKNOWN);
1220         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1221
1222         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, MEDIA_SVC_TAG_UNKNOWN);
1223         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1224
1225         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
1226         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1227
1228         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
1229         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1230
1231         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, MEDIA_SVC_TAG_UNKNOWN);
1232         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1233
1234         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
1235         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1236
1237         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
1238         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1239
1240         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
1241         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1242
1243         if (refresh) {
1244                 media_svc_debug("refresh");
1245                 return MS_MEDIA_ERR_NONE;
1246         }
1247
1248         content_info->played_count = 0;
1249         content_info->last_played_time = 0;
1250         content_info->last_played_position = 0;
1251         content_info->favourate = 0;
1252         content_info->media_meta.rating = 0;
1253
1254         return MS_MEDIA_ERR_NONE;
1255 }
1256
1257 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, media_svc_storage_type_e storage_type,
1258                         const char *path, media_svc_media_type_e *media_type, bool refresh)
1259 {
1260         int ret = MS_MEDIA_ERR_NONE;
1261         char * media_uuid = NULL;
1262         char * file_name = NULL;
1263         bool drm_type = false;
1264         char mime_type[256] = {0, };
1265
1266         ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
1267         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1268
1269         if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1270                 struct stat st;
1271                 memset(&st, 0, sizeof(struct stat));
1272                 if (stat(path, &st) == 0) {
1273                         content_info->modified_time = st.st_mtime;
1274                         content_info->timeline = content_info->modified_time;
1275                         content_info->size = st.st_size;
1276                         /* media_svc_debug("Modified time : [%d] Size : [%lld]", content_info->modified_time, content_info->size); */
1277                 } else {
1278                         media_svc_stderror("stat failed");
1279                 }
1280         }
1281
1282         _media_svc_set_default_value(content_info, refresh);
1283
1284         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
1285         if (refresh) {
1286                 media_svc_debug("refresh");
1287                 return MS_MEDIA_ERR_NONE;
1288         }
1289
1290         ret = __media_svc_malloc_and_strncpy(&content_info->storage_uuid, storage_id);
1291         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1292
1293         content_info->storage_type = storage_type;
1294         time(&content_info->added_time);
1295
1296         media_uuid = _media_info_generate_uuid();
1297         if (media_uuid == NULL) {
1298                 _media_svc_destroy_content_info(content_info);
1299                 return MS_MEDIA_ERR_INTERNAL;
1300         }
1301
1302         ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
1303         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1304
1305         file_name = g_path_get_basename(path);
1306         ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
1307         SAFE_FREE(file_name);
1308         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1309
1310         if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1311                 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
1312                 if drm_contentinfo is not NULL, the file is OMA DRM.*/
1313                 ret = __media_svc_get_mime_type(path, mime_type);
1314                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1315
1316                 media_svc_debug("mime [%s]", mime_type);
1317                 content_info->is_drm = drm_type;
1318
1319                 ret = __media_svc_get_media_type(path, mime_type, media_type);
1320                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1321
1322                 if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1323                         media_svc_error("invalid media_type condition[%d]", *media_type);
1324                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1325                 }
1326
1327                 ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
1328                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1329
1330                 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
1331
1332                 content_info->media_type = *media_type;
1333         }
1334
1335         return MS_MEDIA_ERR_NONE;
1336 }
1337
1338 int image_360_check(char *path)
1339 {
1340         FILE *fp = NULL;
1341         long app1_size = 0;
1342         int size = 1;
1343         unsigned char exif_header[4];
1344         unsigned char exif_app1[2];
1345         unsigned char exif_app1_xmp[2];
1346         long exif_app1_xmp_size = 0;
1347         unsigned char exif_app1_xmp_t[2];
1348         char *xmp_data = NULL;
1349         int size1 = 0;
1350         int size2 = 0;
1351         int fdata = 0;
1352         int temp = 0;
1353
1354         fp = fopen(path, "rb");
1355         if (fp == NULL)
1356                 goto ERROR;
1357
1358         size = fread(exif_header, 1, sizeof(exif_header), fp);
1359         if (size <= 0)
1360                 goto ERROR;
1361
1362         if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
1363                 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
1364                 if (size <= 0)
1365                         goto ERROR;
1366
1367                 size1 = exif_app1[0];
1368                 size2 = exif_app1[1];
1369
1370                 app1_size = size1 * 256 + size2 - 2;
1371
1372                 if (fseek(fp, app1_size, SEEK_CUR) != 0)
1373                         goto ERROR;
1374
1375                 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
1376                 if (size <= 0)
1377                         goto ERROR;
1378
1379                 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
1380                         int result = 0;
1381                         char *ptr = NULL;
1382                         size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
1383                         if (size <= 0)
1384                                 goto ERROR;
1385
1386                         size1 = exif_app1_xmp_t[0];
1387                         size2 = exif_app1_xmp_t[1];
1388
1389                         exif_app1_xmp_size = size1 * 256 + size2 - 2;
1390
1391                         xmp_data = (char *)malloc(exif_app1_xmp_size);
1392                         if (xmp_data != NULL) {
1393                                 memset(xmp_data, 0x0, exif_app1_xmp_size);
1394                                 ptr = xmp_data;
1395
1396                                 while (exif_app1_xmp_size >= 0) {
1397                                         exif_app1_xmp_size--;
1398                                         fdata = fgetc(fp);
1399                                         if (fdata == EOF)
1400                                                 continue;
1401                                         if (fdata == '\0')
1402                                                 continue;
1403                                         *ptr = (char)fdata;
1404                                         ptr++;
1405                                         temp++;
1406                                 }
1407                                 ptr = ptr - temp;
1408
1409                                 if (strstr(ptr, "UsePanoramaViewer")
1410                                 && strstr(ptr, "True")
1411                                 && strstr(ptr, "ProjectionType")
1412                                 && strstr(ptr, "equirectangular"))
1413                                         result = 1;
1414
1415                                 SAFE_FREE(xmp_data);
1416                         } else {
1417                                 media_svc_error("malloc failed");
1418                         }
1419
1420                         if (fp) {
1421                                 fclose(fp);
1422                                 fp = NULL;
1423                         }
1424                         return result;
1425                 } else {
1426                         goto ERROR;
1427                 }
1428         } else {
1429                 goto ERROR;
1430         }
1431 ERROR:
1432         if (fp) {
1433                 fclose(fp);
1434                 fp = NULL;
1435         }
1436         return 0;
1437 }
1438
1439 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1440 {
1441         int ret = MS_MEDIA_ERR_NONE;
1442         double value = 0.0;
1443         int orient_value = 0;
1444         int exif_width = 0;
1445         int exif_height = 0;
1446         ExifData *ed = NULL;
1447         int has_datetaken = FALSE;
1448         int datetaken_size = 19;
1449         double fnumber = 0.0;
1450         int iso = 0;
1451         char *path = NULL;
1452
1453         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1454         char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
1455         char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1456         char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1457
1458         memset(buf, 0x00, sizeof(buf));
1459         memset(description_buf, 0x00, sizeof(description_buf));
1460         memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
1461         memset(model_buf, 0x00, sizeof(model_buf));
1462
1463         if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1464                 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1465                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1466         }
1467
1468         path = content_info->path;
1469         if (!STRING_VALID(path)) {
1470                 media_svc_error("Invalid Path");
1471                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1472         }
1473
1474         /* Load an ExifData object from an EXIF file */
1475         ed = exif_data_new_from_file(path);
1476
1477         if (!ed) {
1478                 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1479                 goto GET_WIDTH_HEIGHT;
1480         }
1481
1482         content_info->media_meta.is_360 = image_360_check(path);
1483
1484         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1485                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1486                         if (strlen(buf) > 0) {
1487                                 if (strcmp(buf, "S") == 0)
1488                                         value = -1 * value;
1489                         }
1490                         content_info->media_meta.latitude = value;
1491                 } else {
1492                         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1493                 }
1494         } else {
1495                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1496         }
1497
1498         memset(buf, 0x00, sizeof(buf));
1499
1500         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1501                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1502                         if (strlen(buf) > 0) {
1503                                 if (strcmp(buf, "W") == 0)
1504                                         value = -1 * value;
1505                         }
1506                         content_info->media_meta.longitude = value;
1507                 } else {
1508                         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1509                 }
1510         } else {
1511                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1512         }
1513
1514         memset(buf, 0x00, sizeof(buf));
1515
1516         if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1517                 if (strlen(description_buf) == 0) {
1518                         /*media_svc_debug("Use 'No description'"); */
1519                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1520                         if (ret != MS_MEDIA_ERR_NONE)
1521                                 media_svc_error("strcpy error");
1522                 } else {
1523                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
1524                         if (ret != MS_MEDIA_ERR_NONE)
1525                                 media_svc_error("strcpy error");
1526                 }
1527         } else {
1528                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1529                 if (ret != MS_MEDIA_ERR_NONE)
1530                         media_svc_error("strcpy error");
1531         }
1532
1533         memset(buf, 0x00, sizeof(buf));
1534
1535         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1536                 if (strlen(buf) == 0) {
1537                         /*media_svc_debug("time is NULL"); */
1538                 } else {
1539                         ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1540                         if (ret != MS_MEDIA_ERR_NONE) {
1541                                 media_svc_error("strcpy error");
1542                         } else {
1543                                 has_datetaken = TRUE;
1544                                 /* This is same as recorded_date */
1545                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1546                                 if (ret != MS_MEDIA_ERR_NONE)
1547                                         media_svc_error("strcpy error");
1548                         }
1549                 }
1550         }
1551
1552         memset(buf, 0x00, sizeof(buf));
1553
1554         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1555                 if (strlen(buf) == 0) {
1556                         /*media_svc_debug("time is NULL"); */
1557                 } else {
1558                         ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1559                         if (ret != MS_MEDIA_ERR_NONE) {
1560                                 media_svc_error("strcpy error");
1561                         } else {
1562                                 has_datetaken = TRUE;
1563                                 /* This is same as recorded_date */
1564                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1565                                 if (ret != MS_MEDIA_ERR_NONE)
1566                                         media_svc_error("strcpy error");
1567                         }
1568                 }
1569         }
1570
1571         if (has_datetaken) {
1572                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1573                 if (content_info->timeline == 0)
1574                         content_info->timeline = content_info->modified_time;
1575                 else
1576                         media_svc_debug("Timeline : %ld", content_info->timeline);
1577         }
1578
1579         /* Get exposure_time value from exif. */
1580         if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1581                 if (strlen(exposure_time_buf) == 0) {
1582                         /* media_svc_debug("exposure_time_buf is NULL"); */
1583                 } else {
1584                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
1585                         if (ret != MS_MEDIA_ERR_NONE)
1586                                 media_svc_error("strcpy error");
1587                 }
1588         }
1589
1590         /* Get fnumber value from exif. */
1591         if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1592                 if (fnumber > 0.0)
1593                         content_info->media_meta.fnumber = fnumber;
1594                 else
1595                         content_info->media_meta.fnumber = 0.0;
1596         } else {
1597                 content_info->media_meta.fnumber = 0.0;
1598         }
1599
1600         /* Get iso value from exif. */
1601         if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1602                 if (iso > 0)
1603                         content_info->media_meta.iso = iso;
1604                 else
1605                         content_info->media_meta.iso = 0;
1606         } else {
1607                 content_info->media_meta.iso = 0;
1608         }
1609
1610         /* Get model value from exif. */
1611         if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1612                 if (strlen(model_buf) == 0) {
1613                         /* media_svc_debug("model_buf is NULL"); */
1614                 } else {
1615                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
1616                         if (ret != MS_MEDIA_ERR_NONE)
1617                                 media_svc_error("strcpy error");
1618                 }
1619         }
1620
1621         /* Get orientation value from exif. */
1622         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1623                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1624                         content_info->media_meta.orientation = orient_value;
1625                 else
1626                         content_info->media_meta.orientation = 0;
1627         } else {
1628                 content_info->media_meta.orientation = 0;
1629         }
1630
1631         /* Get width value from exif. */
1632         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1633                 if (exif_width > 0)
1634                         content_info->media_meta.width = exif_width;
1635                 else
1636                         content_info->media_meta.width = 0;
1637         } else {
1638                 content_info->media_meta.width = 0;
1639         }
1640
1641         /* Get height value from exif. */
1642         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1643                 if (exif_height > 0)
1644                         content_info->media_meta.height = exif_height;
1645                 else
1646                         content_info->media_meta.height = 0;
1647         } else {
1648                 content_info->media_meta.height = 0;
1649         }
1650
1651         if (ed != NULL) exif_data_unref(ed);
1652
1653 GET_WIDTH_HEIGHT:
1654
1655         if (content_info->media_meta.width == 0 ||
1656                 content_info->media_meta.height == 0) {
1657                 /*Get image width, height*/
1658                 unsigned int img_width = 0;
1659                 unsigned int img_height = 0;
1660                 ImgCodecType img_type = IMG_CODEC_NONE;
1661
1662                 ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1663
1664                 if (content_info->media_meta.width == 0)
1665                         content_info->media_meta.width = img_width;
1666
1667                 if (content_info->media_meta.height == 0)
1668                         content_info->media_meta.height = img_height;
1669         }
1670
1671         return MS_MEDIA_ERR_NONE;
1672 }
1673
1674 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1675 {
1676         MMHandleType tag = 0;
1677         char *p = NULL;
1678         int size = -1;
1679         int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1680         int mmf_error = FILEINFO_ERROR_NONE;
1681         char *err_attr_name = NULL;
1682         char *title = NULL;
1683         int album_id = 0;
1684         int ret = MS_MEDIA_ERR_NONE;
1685
1686         /*Get Content Tag attribute ===========*/
1687         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1688
1689         if (mmf_error == FILEINFO_ERROR_NONE) {
1690                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1691                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1692                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1693                         if (ret != MS_MEDIA_ERR_NONE)
1694                                 media_svc_error("strcpy error");
1695
1696                         /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1697                 } else {
1698                         SAFE_FREE(err_attr_name);
1699                         /*media_svc_debug("album - unknown"); */
1700                 }
1701
1702                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1703                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1704                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1705                         if (ret != MS_MEDIA_ERR_NONE)
1706                                 media_svc_error("strcpy error");
1707                         /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1708                 } else {
1709                         SAFE_FREE(err_attr_name);
1710                         /*media_svc_debug("artist - unknown"); */
1711                 }
1712
1713                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1714                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1715                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1716                         if (ret != MS_MEDIA_ERR_NONE)
1717                                 media_svc_error("strcpy error");
1718                         /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1719                 } else {
1720                         SAFE_FREE(err_attr_name);
1721                         /*media_svc_debug("album_artist - unknown"); */
1722                 }
1723
1724                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1725                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1726                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1727                         if (ret != MS_MEDIA_ERR_NONE)
1728                                 media_svc_error("strcpy error");
1729
1730                         /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1731                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1732                         /*
1733                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1734                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1735                         }
1736                         */
1737                 } else {
1738                         SAFE_FREE(err_attr_name);
1739                         /*media_svc_debug("genre - unknown"); */
1740                 }
1741
1742                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1743                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1744                         if (!isspace(*p)) {
1745                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1746                                 if (ret != MS_MEDIA_ERR_NONE)
1747                                         media_svc_error("strcpy error");
1748
1749                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1750                         } else {
1751                                 int idx = 0;
1752
1753                                 for (idx = 0; idx < size; idx++) {
1754                                         if (isspace(*p)) {
1755                                                 media_svc_debug("SPACE [%s]", p);
1756                                                 p++;
1757                                                 continue;
1758                                         } else {
1759                                                 media_svc_debug("Not SPACE [%s]", p);
1760                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1761                                                 if (ret != MS_MEDIA_ERR_NONE)
1762                                                         media_svc_error("strcpy error");
1763                                                 break;
1764                                         }
1765                                 }
1766
1767                                 if (idx == size) {
1768                                         media_svc_debug("Can't extract title. All string is space");
1769                                         title = _media_svc_get_title_from_filepath(content_info->path);
1770                                         if (title) {
1771                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1772                                                 if (ret != MS_MEDIA_ERR_NONE)
1773                                                         media_svc_error("strcpy error");
1774                                                 SAFE_FREE(title);
1775                                         } else {
1776                                                 media_svc_error("Can't extract title");
1777                                         }
1778                                 }
1779                         }
1780                 } else {
1781                         SAFE_FREE(err_attr_name);
1782                         title = _media_svc_get_title_from_filepath(content_info->path);
1783                         if (title) {
1784                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1785                                 if (ret != MS_MEDIA_ERR_NONE)
1786                                         media_svc_error("strcpy error");
1787                                 SAFE_FREE(title);
1788                         } else {
1789                                 media_svc_error("Can't extract title");
1790                         }
1791                 }
1792
1793                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1794                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1795                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1796                         if (ret != MS_MEDIA_ERR_NONE)
1797                                 media_svc_error("strcpy error");
1798                         /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1799                 } else {
1800                         SAFE_FREE(err_attr_name);
1801                 }
1802
1803                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1804                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1805                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1806                         if (ret != MS_MEDIA_ERR_NONE)
1807                                 media_svc_error("strcpy error");
1808                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1809                         /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1810                 } else {
1811                         /*media_svc_debug("composer - unknown"); */
1812                         SAFE_FREE(err_attr_name);
1813                 }
1814
1815                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1816                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1817                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1818                         if (ret != MS_MEDIA_ERR_NONE)
1819                                 media_svc_error("strcpy error");
1820                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1821                         /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1822                 } else {
1823                         /*media_svc_debug("copyright - unknown"); */
1824                         SAFE_FREE(err_attr_name);
1825                 }
1826
1827                 mmf_error = mm_file_destroy_tag_attrs(tag);
1828                 if (mmf_error != FILEINFO_ERROR_NONE)
1829                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1830         } else {
1831                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1832                 char *no_tag_title = NULL;
1833                 media_svc_error("no tag information");
1834
1835                 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1836                 if (no_tag_title) {
1837                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1838                         if (ret != MS_MEDIA_ERR_NONE)
1839                                 media_svc_error("strcpy error");
1840                         SAFE_FREE(no_tag_title);
1841                 } else {
1842                         media_svc_error("Can't extract title");
1843                 }
1844
1845                 content_info->album_id = album_id;
1846         }
1847
1848         return MS_MEDIA_ERR_NONE;
1849 }
1850
1851 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1852 {
1853         MMHandleType content = 0;
1854         MMHandleType tag = 0;
1855         char *p = NULL;
1856         unsigned char *image = NULL;
1857         unsigned int size = 0;
1858         int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1859         int mmf_error = FILEINFO_ERROR_NONE;
1860         char *err_attr_name = NULL;
1861         char *title = NULL;
1862         bool extract_thumbnail = FALSE;
1863         bool append_album = FALSE;
1864         int album_id = 0;
1865         int ret = MS_MEDIA_ERR_NONE;
1866         int cdis_value = 0;
1867         unsigned int resize_size = 0;
1868         unsigned char *resize_image = NULL;
1869
1870         /*Get Content Tag attribute ===========*/
1871         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1872
1873         if (mmf_error == FILEINFO_ERROR_NONE) {
1874                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1875                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1876                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1877                         if (ret != MS_MEDIA_ERR_NONE)
1878                                 media_svc_error("strcpy error");
1879
1880                         /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1881                 } else {
1882                         SAFE_FREE(err_attr_name);
1883                         /*media_svc_debug("album - unknown"); */
1884                 }
1885
1886                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1887                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1888                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1889                         if (ret != MS_MEDIA_ERR_NONE)
1890                                 media_svc_error("strcpy error");
1891                         /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1892                 } else {
1893                         SAFE_FREE(err_attr_name);
1894                         /*media_svc_debug("artist - unknown"); */
1895                 }
1896
1897                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1898                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1899                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1900                         if (ret != MS_MEDIA_ERR_NONE)
1901                                 media_svc_error("strcpy error");
1902                         /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1903                 } else {
1904                         SAFE_FREE(err_attr_name);
1905                         /*media_svc_debug("album_artist - unknown"); */
1906                 }
1907
1908                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1909                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1910                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1911                         if (ret != MS_MEDIA_ERR_NONE)
1912                                 media_svc_error("strcpy error");
1913
1914                         /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1915                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1916                         /*
1917                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1918                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1919                         }
1920                         */
1921                 } else {
1922                         SAFE_FREE(err_attr_name);
1923                         /*media_svc_debug("genre - unknown"); */
1924                 }
1925
1926                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1927                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1928                         if (!isspace(*p)) {
1929                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1930                                 if (ret != MS_MEDIA_ERR_NONE)
1931                                         media_svc_error("strcpy error");
1932
1933                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1934                         } else {
1935                                 int idx = 0;
1936
1937                                 for (idx = 0; idx < size; idx++) {
1938                                         if (isspace(*p)) {
1939                                                 media_svc_debug("SPACE [%s]", p);
1940                                                 p++;
1941                                                 continue;
1942                                         } else {
1943                                                 media_svc_debug("Not SPACE [%s]", p);
1944                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1945                                                 if (ret != MS_MEDIA_ERR_NONE)
1946                                                         media_svc_error("strcpy error");
1947                                                 break;
1948                                         }
1949                                 }
1950
1951                                 if (idx == size) {
1952                                         media_svc_debug("Can't extract title. All string is space");
1953                                         title = _media_svc_get_title_from_filepath(content_info->path);
1954                                         if (title) {
1955                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1956                                                 if (ret != MS_MEDIA_ERR_NONE)
1957                                                         media_svc_error("strcpy error");
1958                                                 SAFE_FREE(title);
1959                                         } else {
1960                                                 media_svc_error("Can't extract title");
1961                                         }
1962                                 }
1963                         }
1964                 } else {
1965                         SAFE_FREE(err_attr_name);
1966                         title = _media_svc_get_title_from_filepath(content_info->path);
1967                         if (title) {
1968                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1969                                 if (ret != MS_MEDIA_ERR_NONE)
1970                                         media_svc_error("strcpy error");
1971                                 SAFE_FREE(title);
1972                         } else {
1973                                 media_svc_error("Can't extract title");
1974                         }
1975                 }
1976
1977                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1978                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1979                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1980                         if (ret != MS_MEDIA_ERR_NONE)
1981                                 media_svc_error("strcpy error");
1982                         /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1983                 } else {
1984                         SAFE_FREE(err_attr_name);
1985                 }
1986
1987                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_360, &content_info->media_meta.is_360, NULL);
1988
1989                 if (mmf_error != FILEINFO_ERROR_NONE)
1990                         SAFE_FREE(err_attr_name);
1991
1992                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1993                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1994                         if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1995                                 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1996                                 char *time_info = (char*)calloc(1, (size + 1));
1997                                 char *p_value = p;
1998                                 char *time_value = time_info;
1999                                 if (time_info != NULL) {
2000                                         while (*p_value != '\0') {
2001                                                 if (*p_value == '-')
2002                                                         *time_value = ':';
2003                                                 else
2004                                                         *time_value = *p_value;
2005                                                 time_value++;
2006                                                 p_value++;
2007                                         }
2008                                         *time_value = '\0';
2009                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
2010                                         SAFE_FREE(time_info);
2011                                 } else {
2012                                         media_svc_error("memory allocation error");
2013                                         ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
2014                                 }
2015                         } else {
2016                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
2017                         }
2018
2019                         if (ret != MS_MEDIA_ERR_NONE) {
2020                                 media_svc_error("strcpy error");
2021                         } else {
2022                                 /* This is same as datetaken */
2023 #if 0
2024                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
2025 #else
2026                                 int datetaken_size = 19;
2027                                 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
2028 #endif
2029                                 if (ret != MS_MEDIA_ERR_NONE)
2030                                         media_svc_error("strcpy error");
2031
2032                                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
2033                                 if (content_info->timeline == 0)
2034                                         content_info->timeline = content_info->modified_time;
2035                                 else
2036                                         media_svc_debug("Timeline : %ld", content_info->timeline);
2037                         }
2038                         /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
2039                 } else {
2040                         SAFE_FREE(err_attr_name);
2041                 }
2042
2043                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
2044                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
2045                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
2046                         if (ret != MS_MEDIA_ERR_NONE)
2047                                 media_svc_error("strcpy error");
2048                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
2049                         /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
2050                 } else {
2051                         /*media_svc_debug("composer - unknown"); */
2052                         SAFE_FREE(err_attr_name);
2053                 }
2054
2055                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
2056                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
2057                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
2058                         if (ret != MS_MEDIA_ERR_NONE)
2059                                 media_svc_error("strcpy error");
2060                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
2061                         /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
2062                 } else {
2063                         /*media_svc_debug("copyright - unknown"); */
2064                         SAFE_FREE(err_attr_name);
2065                 }
2066
2067                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
2068                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
2069                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
2070                         if (ret != MS_MEDIA_ERR_NONE)
2071                                 media_svc_error("strcpy error");
2072                 } else {
2073                         SAFE_FREE(err_attr_name);
2074                 }
2075
2076                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
2077                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size == 4)) {
2078                         int year = 0;
2079                         if ((p != NULL) && (sscanf(p, "%d", &year))) {
2080                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
2081                                 if (ret != MS_MEDIA_ERR_NONE)
2082                                         media_svc_error("strcpy error");
2083                         } else {
2084                                 media_svc_debug("Wrong Year Information [%s]", p);
2085                         }
2086                 } else {
2087                         SAFE_FREE(err_attr_name);
2088                 }
2089
2090                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
2091                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
2092                         content_info->media_meta.rating = atoi(p);
2093                 } else {
2094                         SAFE_FREE(err_attr_name);
2095                         content_info->media_meta.rating = 0;
2096                 }
2097
2098                 /*Initialize album_id to 0. below code will set the album_id*/
2099                 content_info->album_id = album_id;
2100                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
2101
2102                 if (ret != MS_MEDIA_ERR_NONE) {
2103                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
2104                                 media_svc_debug("album does not exist. So start to make album art");
2105                                 extract_thumbnail = TRUE;
2106                                 append_album = TRUE;
2107                         } else {
2108                                 extract_thumbnail = TRUE;
2109                                 append_album = FALSE;
2110                         }
2111                 } else {
2112                         content_info->album_id = album_id;
2113                         append_album = FALSE;
2114
2115                         if ((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
2116                                 (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
2117
2118                                 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
2119                                 extract_thumbnail = TRUE;
2120                         } else {
2121                                 media_svc_debug("album already exists. don't need to make album art");
2122                                 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
2123                                 extract_thumbnail = TRUE;
2124                         }
2125                 }
2126
2127                 /*Do not extract artwork for the USB Storage content*/
2128                 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
2129                         extract_thumbnail = FALSE;
2130
2131                 if (extract_thumbnail == TRUE) {
2132                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
2133                         if (mmf_error != FILEINFO_ERROR_NONE) {
2134                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
2135                                 SAFE_FREE(err_attr_name);
2136                         } else {
2137                                 /*media_svc_debug("artwork size1 [%d]", size); */
2138                         }
2139
2140                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
2141                         if (mmf_error != FILEINFO_ERROR_NONE) {
2142                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
2143                                 SAFE_FREE(err_attr_name);
2144                         } else {
2145                                 /*media_svc_debug("artwork size2 [%d]", size); */
2146                         }
2147                         if (image != NULL && size > 0) {
2148                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
2149                                 int artwork_mime_size = -1;
2150
2151                                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
2152                                 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
2153                                         ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
2154                                         if (ret != MS_MEDIA_ERR_NONE)
2155                                                 media_svc_error("Fail to Get Thumbnail Path");
2156                                         /* albumart resizing */
2157                                         __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
2158                                 } else {
2159                                         SAFE_FREE(err_attr_name);
2160                                 }
2161
2162                                 if (strlen(thumb_path) > 0) {
2163                                         ret = _media_svc_save_image(resize_image, resize_size, thumb_path, uid);
2164                                         if (ret != MS_MEDIA_ERR_NONE) {
2165                                                 media_svc_error("Fail to Save Thumbnail Image");
2166                                         } else {
2167                                                 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
2168                                                 if (ret != MS_MEDIA_ERR_NONE)
2169                                                         media_svc_error("strcpy error");
2170                                         }
2171                                 }
2172                         }
2173                 }
2174
2175                 if (append_album == TRUE) {
2176                         if ((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
2177                                 (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
2178                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
2179                         else
2180                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
2181
2182                         content_info->album_id = album_id;
2183                 }
2184
2185                 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
2186                         double longitude = 0.0;
2187                         double latitude = 0.0;
2188                         double altitude = 0.0;
2189
2190                         __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
2191                         content_info->media_meta.longitude = longitude;
2192                         content_info->media_meta.latitude = latitude;
2193                         content_info->media_meta.altitude = altitude;
2194
2195                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
2196                         if (mmf_error != FILEINFO_ERROR_NONE) {
2197                                 cdis_value = 0;
2198                                 SAFE_FREE(err_attr_name);
2199                         }
2200
2201                         media_svc_debug("CDIS : %d", cdis_value);
2202                 }
2203
2204                 mmf_error = mm_file_destroy_tag_attrs(tag);
2205                 if (mmf_error != FILEINFO_ERROR_NONE)
2206                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
2207         }       else {
2208                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
2209                 char *no_tag_title = NULL;
2210                 media_svc_error("no tag information");
2211
2212                 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
2213                 if (no_tag_title) {
2214                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
2215                         if (ret != MS_MEDIA_ERR_NONE)
2216                                 media_svc_error("strcpy error");
2217                         SAFE_FREE(no_tag_title);
2218                 } else {
2219                         media_svc_error("Can't extract title");
2220                 }
2221
2222                 content_info->album_id = album_id;
2223         }
2224
2225         /*Get Content attribute ===========*/
2226         if (cdis_value == 1)
2227                 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
2228         else
2229                 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
2230
2231         if (mmf_error == FILEINFO_ERROR_NONE) {
2232                 /*Common attribute*/
2233                 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
2234                 if (mmf_error != FILEINFO_ERROR_NONE) {
2235                         SAFE_FREE(err_attr_name);
2236                         media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
2237                 } else {
2238                         /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
2239                 }
2240
2241                 /*Sound/Music attribute*/
2242                 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
2243
2244                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
2245                         if (mmf_error != FILEINFO_ERROR_NONE) {
2246                                 SAFE_FREE(err_attr_name);
2247                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2248                         } else {
2249                                 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
2250                         }
2251
2252                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
2253                         if (mmf_error != FILEINFO_ERROR_NONE) {
2254                                 SAFE_FREE(err_attr_name);
2255                                 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
2256                         } else {
2257                                 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
2258                         }
2259
2260                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
2261                         if (mmf_error != FILEINFO_ERROR_NONE) {
2262                                 SAFE_FREE(err_attr_name);
2263                                 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
2264                         } else {
2265                                 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
2266                         }
2267
2268                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
2269                         if (mmf_error != FILEINFO_ERROR_NONE) {
2270                                 SAFE_FREE(err_attr_name);
2271                                 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
2272                         } else {
2273                                 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
2274                         }
2275                 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)      {       /*Video attribute*/
2276                         int audio_bitrate = 0;
2277                         int video_bitrate = 0;
2278
2279                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
2280                         if (mmf_error != FILEINFO_ERROR_NONE) {
2281                                 SAFE_FREE(err_attr_name);
2282                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2283                         } else {
2284                                 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
2285                         }
2286
2287                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
2288                         if (mmf_error != FILEINFO_ERROR_NONE) {
2289                                 SAFE_FREE(err_attr_name);
2290                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2291                         } else {
2292                                 /*media_svc_debug("video bit rate : %d", video_bitrate); */
2293                         }
2294
2295                         content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
2296
2297                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
2298                         if (mmf_error != FILEINFO_ERROR_NONE) {
2299                                 SAFE_FREE(err_attr_name);
2300                                 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
2301                         } else {
2302                                 /*media_svc_debug("width : %d", content_info->media_meta.width); */
2303                         }
2304
2305                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
2306                         if (mmf_error != FILEINFO_ERROR_NONE) {
2307                                 SAFE_FREE(err_attr_name);
2308                                 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
2309                         } else {
2310                                 /*media_svc_debug("height : %d", content_info->media_meta.height); */
2311                         }
2312
2313                 } else {
2314                         media_svc_error("Not support type");
2315                         mmf_error = mm_file_destroy_content_attrs(content);
2316                         if (mmf_error != FILEINFO_ERROR_NONE)
2317                                 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2318
2319                         return MS_MEDIA_ERR_INVALID_PARAMETER;
2320                 }
2321
2322                 mmf_error = mm_file_destroy_content_attrs(content);
2323                 if (mmf_error != FILEINFO_ERROR_NONE)
2324                         media_svc_error("fail to free content attr - err(%x)", mmf_error);
2325         } else {
2326                 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
2327         }
2328
2329         return MS_MEDIA_ERR_NONE;
2330 }
2331
2332 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
2333 {
2334         media_svc_retm_if(content_info == NULL, "content info is NULL");
2335
2336         /* Delete media_svc_content_info_s */
2337         SAFE_FREE(content_info->media_uuid);
2338         SAFE_FREE(content_info->path);
2339         SAFE_FREE(content_info->file_name);
2340         SAFE_FREE(content_info->mime_type);
2341         SAFE_FREE(content_info->folder_uuid);
2342         SAFE_FREE(content_info->thumbnail_path);
2343         SAFE_FREE(content_info->storage_uuid);
2344
2345         /* Delete media_svc_content_meta_s */
2346         SAFE_FREE(content_info->media_meta.title);
2347         SAFE_FREE(content_info->media_meta.album);
2348         SAFE_FREE(content_info->media_meta.artist);
2349         SAFE_FREE(content_info->media_meta.album_artist);
2350         SAFE_FREE(content_info->media_meta.genre);
2351         SAFE_FREE(content_info->media_meta.composer);
2352         SAFE_FREE(content_info->media_meta.year);
2353         SAFE_FREE(content_info->media_meta.recorded_date);
2354         SAFE_FREE(content_info->media_meta.copyright);
2355         SAFE_FREE(content_info->media_meta.track_num);
2356         SAFE_FREE(content_info->media_meta.description);
2357         SAFE_FREE(content_info->media_meta.datetaken);
2358         SAFE_FREE(content_info->media_meta.exposure_time);
2359         SAFE_FREE(content_info->media_meta.model);
2360         SAFE_FREE(content_info->media_meta.weather);
2361         SAFE_FREE(content_info->media_meta.category);
2362         SAFE_FREE(content_info->media_meta.keyword);
2363         SAFE_FREE(content_info->media_meta.location_tag);
2364         SAFE_FREE(content_info->media_meta.content_name);
2365         SAFE_FREE(content_info->media_meta.age_rating);
2366         SAFE_FREE(content_info->media_meta.author);
2367         SAFE_FREE(content_info->media_meta.provider);
2368
2369         SAFE_FREE(content_info->media_meta.title_pinyin);
2370         SAFE_FREE(content_info->media_meta.album_pinyin);
2371         SAFE_FREE(content_info->media_meta.artist_pinyin);
2372         SAFE_FREE(content_info->media_meta.album_artist_pinyin);
2373         SAFE_FREE(content_info->media_meta.genre_pinyin);
2374         SAFE_FREE(content_info->media_meta.composer_pinyin);
2375         SAFE_FREE(content_info->media_meta.copyright_pinyin);
2376         SAFE_FREE(content_info->media_meta.description_pinyin);
2377
2378         return;
2379 }
2380
2381 int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
2382 {
2383         if (STRING_VALID(path)) {
2384                 char *internal_path = _media_svc_get_path(uid);
2385                 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0)) {
2386                         *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
2387                 } else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
2388                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2389                 } else if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
2390                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
2391                 } else if (STRING_VALID(MEDIA_ROOT_PATH_DISC) && (strncmp(path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0)) {
2392                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2393                 } else {
2394                         media_svc_error("Invalid Path");
2395                         SAFE_FREE(internal_path);
2396                         return MS_MEDIA_ERR_INVALID_PARAMETER;
2397                 }
2398                 SAFE_FREE(internal_path);
2399         } else {
2400                 media_svc_error("INVALID parameter");
2401                 return MS_MEDIA_ERR_INVALID_PARAMETER;
2402         }
2403
2404         return MS_MEDIA_ERR_NONE;
2405 }
2406
2407 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2408 {
2409         char *result, *sr;
2410         size_t i, count = 0;
2411         size_t oldlen = strlen(olds);
2412         if (oldlen < 1) return s;
2413         size_t newlen = strlen(news);
2414
2415         if (newlen != oldlen) {
2416                 for (i = 0; s[i] != '\0';) {
2417                         if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
2418                         else i++;
2419                 }
2420         } else i = strlen(s);
2421
2422
2423         result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
2424         if (result == NULL) return NULL;
2425
2426         sr = result;
2427         while (*s) {
2428                 if (memcmp(s, olds, oldlen) == 0) {
2429                         memcpy(sr, news, newlen);
2430                         sr += newlen;
2431                         s += oldlen;
2432                 } else *sr++ = *s++;
2433         }
2434
2435         *sr = '\0';
2436
2437         return result;
2438 }
2439
2440 bool _media_svc_is_drm_file(const char *path)
2441 {
2442         return FALSE;
2443 }
2444
2445 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)
2446 {
2447         int ret = MS_MEDIA_ERR_NONE;
2448
2449         ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
2450
2451         if (ret != MS_MEDIA_ERR_NONE) {
2452                 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2453                 ret = MS_MEDIA_ERR_INTERNAL;
2454         } else {
2455                 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2456         }
2457
2458         return ret;
2459 }
2460
2461 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2462 {
2463         int ret = MS_MEDIA_ERR_NONE;
2464         int size = 0;
2465         pinyin_name_s *pinyinname = NULL;
2466
2467         *pinyin_str = NULL;
2468
2469         if (!STRING_VALID(src_str)) {
2470                 media_svc_debug("String is invalid");
2471                 return ret;
2472         }
2473
2474         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2475         if (ret == MS_MEDIA_ERR_NONE) {
2476                 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
2477                         *pinyin_str = strdup(pinyinname[0].pinyin_name);
2478                 else
2479                         *pinyin_str = strdup(src_str);  /* Return Original Non China Character */
2480         }
2481
2482         _media_svc_pinyin_free(pinyinname, size);
2483
2484         return ret;
2485 }
2486
2487 bool _media_svc_check_pinyin_support(void)
2488 {
2489         /*Check CSC*/
2490         return TRUE;
2491 }
2492
2493 char* _media_svc_get_title_from_path(const char *path)
2494 {
2495         char *filename = NULL;
2496         char *title = NULL;
2497         char    *ext = NULL;
2498         int filename_len = -1;
2499         int new_title_len = -1;
2500
2501         if (!path) {
2502                 media_svc_error("path is NULL");
2503                 return NULL;
2504         }
2505
2506         filename = g_path_get_basename(path);
2507         if (!STRING_VALID(filename)) {
2508                 media_svc_error("wrong file name");
2509                 SAFE_FREE(filename);
2510                 return NULL;
2511         }
2512
2513         filename_len = strlen(filename);
2514
2515         ext = g_strrstr(filename, ".");
2516         if (!ext) {
2517                 media_svc_error("there is no file extention");
2518                 return filename;
2519         }
2520
2521         new_title_len = filename_len - strlen(ext);
2522         if (new_title_len < 1) {
2523                 media_svc_error("title length is zero");
2524                 SAFE_FREE(filename);
2525                 return NULL;
2526         }
2527
2528         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2529
2530         SAFE_FREE(filename);
2531
2532         media_svc_debug("extract title is [%s]", title);
2533
2534         return title;
2535 }
2536
2537 #define BUF_LENGHT 256
2538
2539 void _media_svc_print_stderror(void)
2540 {
2541         char buf[BUF_LENGHT] = {0,};
2542
2543         media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));
2544 }
2545
2546 int _media_svc_get_media_type(const char *path, int *mediatype)
2547 {
2548         int ret = MS_MEDIA_ERR_NONE;
2549         char mime_type[256] = {0};
2550         media_svc_media_type_e media_type =  MEDIA_SVC_MEDIA_TYPE_OTHER;
2551
2552         ret = __media_svc_get_mime_type(path, mime_type);
2553         if (ret == MS_MEDIA_ERR_NONE) {
2554                 __media_svc_get_media_type(path, mime_type, &media_type);
2555         } else {
2556                 media_svc_error("__media_svc_get_mime_type failed");
2557         }
2558
2559         *mediatype = media_type;
2560
2561         return ret;
2562 }
2563