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