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