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