Use new image thumbnail extracting APIs provided by libmedia-thumbnail
[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 <system_info.h>
34 #include <sys/vfs.h>
35 #include <glib/gstdio.h>
36 #include <sys/stat.h>
37 #include <dirent.h>
38 #include <ctype.h>
39 #include <aul/aul.h>
40 #include <mm_file.h>
41 #include <libexif/exif-data.h>
42 #include <media-util.h>
43 #include <uuid/uuid.h>
44 #include <mm_util_magick.h>
45 #include <media-thumbnail.h>
46 #include "media-util-err.h"
47 #include "media-svc-util.h"
48 #include "media-svc-db-utils.h"
49 #include "media-svc-debug.h"
50 #include "media-svc-env.h"
51 #include "media-svc-hash.h"
52 #include "media-svc-album.h"
53 #include "media-svc-localize_ch.h"
54
55 #define MEDIA_SVC_FILE_EXT_LEN_MAX                              6                       /**< Maximum file ext lenth*/
56
57 /* Define data structures for media type and mime type */
58 #define MEDIA_SVC_CATEGORY_UNKNOWN      0x00000000      /**< Default */
59 #define MEDIA_SVC_CATEGORY_ETC          0x00000001      /**< ETC category */
60 #define MEDIA_SVC_CATEGORY_IMAGE        0x00000002      /**< Image category */
61 #define MEDIA_SVC_CATEGORY_VIDEO        0x00000004      /**< Video category */
62 #define MEDIA_SVC_CATEGORY_MUSIC        0x00000008      /**< Music category */
63 #define MEDIA_SVC_CATEGORY_SOUND        0x00000010      /**< Sound category */
64 #define MEDIA_SVC_CATEGORY_PVR  0x00000020      /**< PVR category */
65 #define MEDIA_SVC_CATEGORY_UHD  0x00000040      /**< UHD category */
66 #define MEDIA_SVC_CATEGORY_SCSA 0x00000080      /**< SCSA category */
67
68 #define CONTENT_TYPE_NUM 5
69 #define MUSIC_MIME_NUM 29
70 #define SOUND_MIME_NUM 1
71 #define MIME_TYPE_LENGTH 255
72 #define MIME_LENGTH 50
73 #define _3GP_FILE ".3gp"
74 #define _MP4_FILE ".mp4"
75 #define _ASF_FILE ".asf"
76 #define MEDIA_SVC_ARTWORK_SIZE 2000
77 #define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
78
79 #define MEDIA_SVC_DEFAULT_GPS_VALUE                     -200                    /**< Default GPS Value*/
80
81 typedef struct {
82         char content_type[15];
83         int category_by_mime;
84 } _media_svc_content_table_s;
85
86 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
87         {"audio", MEDIA_SVC_CATEGORY_SOUND},
88         {"image", MEDIA_SVC_CATEGORY_IMAGE},
89         {"video", MEDIA_SVC_CATEGORY_VIDEO},
90         {"application", MEDIA_SVC_CATEGORY_ETC},
91         {"text", MEDIA_SVC_CATEGORY_ETC},
92 };
93
94 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
95         /*known mime types of normal files*/
96         "mpeg",
97         "ogg",
98         "x-ms-wma",
99         "x-flac",
100         "mp4",
101         /* known mime types of drm files*/
102         "mp3",
103         "x-mp3", /*alias of audio/mpeg*/
104         "x-mpeg", /*alias of audio/mpeg*/
105         "3gpp",
106         "x-ogg", /*alias of audio/ogg*/
107         "vnd.ms-playready.media.pya:*.pya", /*playready*/
108         "wma",
109         "aac",
110         "x-m4a", /*alias of audio/mp4*/
111         /* below mimes are rare*/
112         "x-vorbis+ogg",
113         "x-flac+ogg",
114         "x-matroska",
115         "ac3",
116         "mp2",
117         "x-ape",
118         "x-ms-asx",
119         "vnd.rn-realaudio",
120
121         "x-vorbis", /*alias of audio/x-vorbis+ogg*/
122         "vorbis", /*alias of audio/x-vorbis+ogg*/
123         "x-oggflac",
124         "x-mp2", /*alias of audio/mp2*/
125         "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
126         "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
127         "x-wav",
128 };
129
130 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
131         "x-smaf",
132 };
133
134 char *_media_info_generate_uuid(void)
135 {
136         uuid_t uuid_value;
137         char uuid_unparsed[37];
138
139 RETRY_GEN:
140         uuid_generate(uuid_value);
141         uuid_unparse(uuid_value, uuid_unparsed);
142
143         if (strlen(uuid_unparsed) < 36) {
144                 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
145                 goto RETRY_GEN;
146         }
147
148         return g_strdup(uuid_unparsed);
149 }
150
151 static int __media_svc_split_to_double(char *input, double *arr)
152 {
153         char tmp_arr[255] = {0, };
154         int len = 0, idx = 0, arr_idx = 0, str_idx = 0;
155
156         if (!STRING_VALID(input)) {
157                 media_svc_error("Invalid parameter");
158                 return MS_MEDIA_ERR_INVALID_PARAMETER;
159         }
160         memset(tmp_arr, 0x0, sizeof(tmp_arr));
161
162         /*media_svc_debug("input: [%s]", input); */
163
164         len = strlen(input);
165
166         for (idx = 0; idx < (len + 1); idx++) {
167                 if (input[idx] == ' ') {
168                         continue;
169                 } else if ((input[idx] == ',') || (idx == len)) {
170                         arr[arr_idx] = atof(tmp_arr);
171                         arr_idx++;
172                         str_idx = 0;
173                         /*media_svc_debug("idx=[%d] arr_idx=[%d] tmp_attr[%s] atof(tmp_arr)=[%f]", idx, arr_idx, tmp_arr, atof(tmp_arr)); */
174                         memset(tmp_arr, 0x0, sizeof(tmp_arr));
175                 } else {
176                         tmp_arr[str_idx] = input[idx];
177                         str_idx++;
178                 }
179         }
180
181         if (arr_idx != 3) {
182                 media_svc_debug("Error when parsing GPS [%d]", arr_idx);
183                 return MS_MEDIA_ERR_INTERNAL;
184         }
185
186         return MS_MEDIA_ERR_NONE;
187 }
188
189 /* GPS information is not ExifTag member */
190 static int __media_svc_get_exif_gps_double(ExifData *ed, double *value, long tagtype)
191 {
192         int ret = MS_MEDIA_ERR_NONE;
193         ExifEntry *entry;
194         char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
195         double tmp_arr[3] = { 0.0, 0.0, 0.0 };
196
197         media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
198         media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
199
200         entry = exif_data_get_entry(ed, tagtype);
201         if (entry) {
202                 exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
203                 gps_buf[strlen(gps_buf)] = '\0';
204
205                 ret = __media_svc_split_to_double(gps_buf, tmp_arr);
206                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
207
208                 *value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
209         }
210
211         return MS_MEDIA_ERR_NONE;
212 }
213
214 static int __media_svc_get_exif_gps_str(ExifData *ed, char *value, long tagtype)
215 {
216         ExifEntry *entry;
217
218         media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
219         media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
220
221         entry = exif_data_get_entry(ed, tagtype);
222         if (entry) {
223                 exif_entry_get_value(entry, value, MEDIA_SVC_METADATA_LEN_MAX);
224                 value[strlen(value)] = '\0';
225         }
226
227         return MS_MEDIA_ERR_NONE;
228 }
229
230 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, ExifTag tagtype)
231 {
232         ExifEntry *entry;
233         ExifByteOrder mByteOrder;
234         ExifRational mRational;
235         long numerator, denominator;
236
237         media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
238
239         entry = exif_data_get_entry(ed, tagtype);
240         media_svc_retv_if(!entry, MS_MEDIA_ERR_NONE);
241
242         switch (tagtype) {
243         case EXIF_TAG_ORIENTATION:
244         case EXIF_TAG_PIXEL_X_DIMENSION:
245         case EXIF_TAG_PIXEL_Y_DIMENSION:
246         case EXIF_TAG_ISO_SPEED_RATINGS:
247                 media_svc_retvm_if(!i_value, MS_MEDIA_ERR_INVALID_PARAMETER, "i_value is NULL");
248
249                 mByteOrder = exif_data_get_byte_order(ed);
250                 short exif_value = exif_get_short(entry->data, mByteOrder);
251                 *i_value = (int)exif_value;
252                 break;
253         case EXIF_TAG_EXPOSURE_TIME:
254                 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
255
256                 mByteOrder = exif_data_get_byte_order(ed);
257                 mRational = exif_get_rational(entry->data, mByteOrder);
258                 numerator = mRational.numerator;
259                 denominator = mRational.denominator;
260                 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
261                 break;
262         case EXIF_TAG_FNUMBER:
263                 media_svc_retvm_if(!d_value, MS_MEDIA_ERR_INVALID_PARAMETER, "d_value is NULL");
264
265                 mByteOrder = exif_data_get_byte_order(ed);
266                 mRational = exif_get_rational(entry->data, mByteOrder);
267                 numerator = mRational.numerator;
268                 denominator = mRational.denominator;
269
270                 *d_value = ((numerator*1.0)/(denominator*1.0));
271                 break;
272         default:
273                 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
274
275                 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
276                 buf[strlen(buf)] = '\0';
277         }
278
279         return MS_MEDIA_ERR_NONE;
280 }
281
282 static time_t __media_svc_get_timeline_from_str(const char *timstr)
283 {
284         struct tm t;
285         time_t modified_t = 0;
286         time_t rawtime;
287         struct tm timeinfo;
288
289         if (!STRING_VALID(timstr)) {
290                 media_svc_error("Invalid Parameter");
291                 return 0;
292         }
293
294         /*Exif Format : %Y:%m:%d %H:%M:%S
295         Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
296         memset(&t, 0x00, sizeof(struct tm));
297
298         tzset();
299         time(&rawtime);
300         localtime_r(&rawtime, &timeinfo);
301
302         if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
303                 t.tm_isdst = timeinfo.tm_isdst;
304                 if (t.tm_isdst != 0)
305                         media_svc_debug("DST %d", t.tm_isdst);
306
307                 /* If time string has timezone */
308                 if (strptime(timstr, "%Y:%m:%d %H:%M:%S %z", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S %z", &t)) {
309                         GTimeVal timeval;
310                         char tim_tmp_str[255] = { 0, };
311
312                         /* ISO8601 Time string format */
313                         strftime(tim_tmp_str, 255, "%Y-%m-%dT%H:%M:%S%z", &t);
314                         g_time_val_from_iso8601(tim_tmp_str, &timeval);
315                         modified_t = timeval.tv_sec;
316                         media_svc_debug("Calibrated timeval : [%lu][%s]", modified_t, tim_tmp_str);
317                 } else {
318                         /* Just localtime */
319                         modified_t = mktime(&t);
320                 }
321
322                 if (modified_t > 0)
323                         return modified_t;
324                 else
325                         media_svc_debug("Failed to get timeline : [%s] [%d:%d:%d: %d:%d:%d]", timstr, t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
326         } else {
327                 media_svc_error("Failed to get timeline : [%s]", timstr);
328         }
329
330         return 0;
331 }
332
333 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
334 {
335         int idx = 0;
336
337         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is null");
338         media_svc_retvm_if(mimetype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mimetype is null");
339         media_svc_retvm_if(category == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "category is null");
340
341         *category = MEDIA_SVC_CATEGORY_UNKNOWN;
342
343         /*categorize from mimetype */
344         for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
345                 if (strncmp(mimetype, content_category[idx].content_type, strlen(content_category[idx].content_type)) == 0) {
346                         *category = (*category | content_category[idx].category_by_mime);
347                         break;
348                 }
349         }
350
351         /*in application type, exitst sound file ex) x-smafs, asf */
352         if (*category & MEDIA_SVC_CATEGORY_ETC) {
353                 int prefix_len = strlen(content_category[3].content_type) + 1;
354                 char *ext = NULL;
355
356                 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
357                         if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
358                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
359                                 *category |= MEDIA_SVC_CATEGORY_SOUND;
360                                 break;
361                         }
362                 }
363
364                 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
365                         *category ^= MEDIA_SVC_CATEGORY_ETC;
366                         *category |= MEDIA_SVC_CATEGORY_SOUND;
367                 }
368
369                 /*"asf" must check video stream and then categorize in directly. */
370                 ext = strrchr(path, '.');
371                 if (ext != NULL) {
372                         if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
373                                 int audio = 0;
374                                 int video = 0;
375                                 int err = 0;
376
377                                 err = mm_file_get_stream_info(path, &audio, &video);
378                                 if (err == 0) {
379                                         if (audio > 0 && video == 0) {
380                                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
381                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
382                                         } else {
383                                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
384                                                 *category |= MEDIA_SVC_CATEGORY_VIDEO;
385                                         }
386                                 }
387                         }
388                 }
389         }
390
391         /*check music file in sound files. */
392         if (*category & MEDIA_SVC_CATEGORY_SOUND) {
393                 int prefix_len = strlen(content_category[0].content_type) + 1;
394
395                 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
396                         if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
397                                 *category ^= MEDIA_SVC_CATEGORY_SOUND;
398                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
399                                 break;
400                         }
401                 }
402
403                 /*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*/
404                 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
405                         *category ^= MEDIA_SVC_CATEGORY_SOUND;
406                         *category |= MEDIA_SVC_CATEGORY_ETC;
407                 }
408         } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
409                 /*some video files don't have video stream. in this case it is categorize as music. */
410                 char *ext = NULL;
411                 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
412                 ext = strrchr(path, '.');
413                 if (ext != NULL) {
414                         if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
415                                 int audio = 0;
416                                 int video = 0;
417                                 int err = 0;
418
419                                 err = mm_file_get_stream_info(path, &audio, &video);
420                                 if (err == 0) {
421                                         if (audio > 0 && video == 0) {
422                                                 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
423                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
424                                         }
425                                 }
426                                 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
427                         }
428                 }
429         }
430
431         return MS_MEDIA_ERR_NONE;
432 }
433
434 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
435 {
436         int ret = MS_MEDIA_ERR_NONE;
437         int category = 0;
438
439         ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
440         if (ret != MS_MEDIA_ERR_NONE)
441                 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
442
443         switch (category) {
444         case MEDIA_SVC_CATEGORY_SOUND:
445                 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
446                 break;
447         case MEDIA_SVC_CATEGORY_MUSIC:
448                 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
449                 break;
450         case MEDIA_SVC_CATEGORY_IMAGE:
451                 *media_type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
452                 break;
453         case MEDIA_SVC_CATEGORY_VIDEO:
454                 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
455                 break;
456         default:
457                 *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
458         }
459
460         return ret;
461 }
462
463 /*
464 drm_contentifo is not NULL, if the file is OMA DRM.
465 If the file is not OMA DRM, drm_contentinfo must be NULL.
466 */
467 static int __media_svc_get_mime_type(const char *path, char *mimetype)
468 {
469         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
470
471         /*in case of normal files or failure to get mime in drm */
472         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
473                 media_svc_error("aul_get_mime_from_file fail");
474                 return MS_MEDIA_ERR_INTERNAL;
475         }
476
477         return MS_MEDIA_ERR_NONE;
478 }
479
480 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
481 {
482         int i = 0;
483
484         for (i = strlen(file_path); i >= 0; i--) {
485                 if (file_path[i] == '.') {
486                         SAFE_STRLCPY(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
487                         return true;
488                 }
489
490                 if (file_path[i] == '/')
491                         return false;
492         }
493         return false;
494 }
495 #if 0
496 static int __media_svc_resize_artwork(const char *path, const char *img_format)
497 {
498         int ret = MS_MEDIA_ERR_NONE;
499         unsigned int width = 0;
500         unsigned int height = 0;
501         unsigned int resized_width = 0;
502         unsigned int resized_height = 0;
503         mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
504
505         if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
506                 media_svc_debug("type [jpeg]");
507
508                 mm_util_extract_image_info(path, &img_type, &width, &height);
509
510                 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
511                         media_svc_debug("No need resizing");
512                         return MS_MEDIA_ERR_NONE;
513                 }
514
515                 /* resizing */
516                 if (width > height) {
517                         resized_height = MEDIA_SVC_ARTWORK_SIZE;
518                         resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
519                 } else {
520                         resized_width = MEDIA_SVC_ARTWORK_SIZE;
521                         resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
522                 }
523
524                 ret = mm_util_resize_P_P(path, resized_width, resized_height, path);
525
526         } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
527                 media_svc_debug("type [png]");
528         } else {
529                 media_svc_debug("Not proper img format");
530         }
531
532         return ret;
533 }
534 #endif
535 static int __media_svc_safe_atoi(char *buffer, int *si)
536 {
537         char *end = NULL;
538         errno = 0;
539         media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
540
541         const long sl = strtol(buffer, &end, 10);
542
543         media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
544         media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
545         media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
546         media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
547         media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
548
549         *si = (int)sl;
550
551         return MS_MEDIA_ERR_NONE;
552 }
553
554 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
555 {
556         int ret = MS_MEDIA_ERR_NONE;
557
558         media_svc_sec_debug("start save image, path [%s] image size [%d]", image_path, size);
559
560         if (!image) {
561                 media_svc_error("invalid image..");
562                 return MS_MEDIA_ERR_INVALID_PARAMETER;
563         }
564
565         struct statfs fs;
566         char *thumb_path = NULL;
567         ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
568         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path fail");
569
570         if (-1 == statfs(thumb_path, &fs)) {
571                 media_svc_error("error in statfs");
572                 SAFE_FREE(thumb_path);
573                 return MS_MEDIA_ERR_INTERNAL;
574         }
575
576         SAFE_FREE(thumb_path);
577
578         long bsize_kbytes = fs.f_bsize >> 10;
579
580         if ((bsize_kbytes * fs.f_bavail) < 1024) {
581                 media_svc_error("not enought space...");
582                 return MS_MEDIA_ERR_NOT_ENOUGH_SPACE;
583         }
584
585         FILE *fp = NULL;
586         int nwrite = -1;
587         if (image != NULL && size > 0) {
588                 fp = fopen(image_path, "w");
589
590                 if (fp == NULL) {
591                         media_svc_error("failed to open file");
592                         return MS_MEDIA_ERR_INTERNAL;
593                 }
594
595                 nwrite = fwrite(image, 1, size, fp);
596                 if (nwrite != size) {
597                         media_svc_error("failed to write thumbnail");
598                         fclose(fp);
599                         return MS_MEDIA_ERR_INTERNAL;
600                 }
601                 fclose(fp);
602         }
603
604         return MS_MEDIA_ERR_NONE;
605 }
606
607 static char *__media_svc_get_title_from_filepath(const char *path)
608 {
609         char *filename = NULL;
610         char *title = NULL;
611         char    *ext = NULL;
612         int filename_len = -1;
613         int new_title_len = -1;
614
615         if (!path) {
616                 media_svc_error("path is NULL");
617                 return NULL;
618         }
619
620         filename = g_path_get_basename(path);
621         if (!STRING_VALID(filename)) {
622                 media_svc_error("wrong file name");
623                 SAFE_FREE(filename);
624                 return NULL;
625         }
626
627         filename_len = strlen(filename);
628
629         ext = g_strrstr(filename, ".");
630         if (!ext) {
631                 media_svc_error("there is no file extention");
632                 return filename;
633         }
634
635         new_title_len = filename_len - strlen(ext);
636         if (new_title_len < 1) {
637                 media_svc_error("title length is zero");
638                 SAFE_FREE(filename);
639                 return NULL;
640         }
641
642         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
643
644         SAFE_FREE(filename);
645
646         media_svc_debug("extract title is [%s]", title);
647
648         return title;
649 }
650
651 void _media_svc_remove_file(const char *path)
652 {
653         if (!STRING_VALID(path))
654                 return;
655
656         if (remove(path) != 0)
657                 media_svc_stderror("fail to remove file result");
658 }
659
660 int _media_svc_get_thumbnail_path(media_svc_media_type_e media_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
661 {
662         int ret = MS_MEDIA_ERR_NONE;
663         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
664         char hash[255 + 1] = {0, };
665         char *thumb_dir = NULL;
666         char *thumbfile_ext = NULL;
667
668         ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
669         if (!STRING_VALID(thumb_dir)) {
670                 media_svc_error("ms_user_get_root_thumb_store_path failed");
671                 return MS_MEDIA_ERR_INTERNAL;
672         }
673
674         if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
675                 media_svc_error("Wrong path[%s]", thumb_dir);
676                 SAFE_FREE(thumb_dir);
677                 return MS_MEDIA_ERR_INTERNAL;
678         }
679
680         memset(file_ext, 0, sizeof(file_ext));
681         if (!__media_svc_get_file_ext(pathname, file_ext))
682                 media_svc_error("get file ext fail");
683
684         ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
685         if (ret != MS_MEDIA_ERR_NONE) {
686                 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
687                 SAFE_FREE(thumb_dir);
688                 return MS_MEDIA_ERR_INTERNAL;
689         }
690
691         if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
692                 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
693                         thumbfile_ext = (char *)"jpg";
694                 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
695                         thumbfile_ext = (char *)"png";
696                 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
697                         thumbfile_ext = (char *)"gif";
698                 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
699                         thumbfile_ext = (char *)"bmp";
700                 } else {
701                         media_svc_error("Not proper img format");
702                         SAFE_FREE(thumb_dir);
703                         return MS_MEDIA_ERR_INTERNAL;
704                 }
705
706                 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
707         } else {
708                 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
709         }
710
711         SAFE_FREE(thumb_dir);
712
713         return MS_MEDIA_ERR_NONE;
714 }
715
716 int _media_svc_get_file_time(const char *full_path)
717 {
718         struct stat statbuf = { 0, };
719
720         if (stat(full_path, &statbuf) == -1) {
721                 media_svc_stderror("stat fails.");
722                 return 0;
723         }
724
725         return statbuf.st_mtime;
726 }
727
728 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, bool refresh)
729 {
730         int ret = MS_MEDIA_ERR_NONE;
731         bool drm_type = false;
732         char mime_type[256] = {0, };
733         media_svc_media_type_e media_type;
734
735         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
736         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
737
738         content_info->path = g_strdup(path);
739         media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
740
741         struct stat st;
742         memset(&st, 0, sizeof(struct stat));
743         if (stat(path, &st) == 0) {
744                 content_info->modified_time = st.st_mtime;
745                 content_info->timeline = content_info->modified_time;
746                 content_info->size = st.st_size;
747         } else {
748                 media_svc_stderror("stat failed");
749         }
750
751         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
752         if (refresh) {
753                 media_svc_debug("refresh");
754                 return MS_MEDIA_ERR_NONE;
755         }
756
757         content_info->storage_uuid = g_strdup(storage_id);
758         media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
759
760         content_info->storage_type = storage_type;
761         time(&content_info->added_time);
762
763         content_info->media_uuid = _media_info_generate_uuid();
764         media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
765
766         content_info->file_name = g_path_get_basename(path);
767         media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
768
769         /* if the file is DRM file, drm_type value is DRM_TRUE(1).
770         if drm_contentinfo is not NULL, the file is OMA DRM.*/
771         ret = __media_svc_get_mime_type(path, mime_type);
772         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
773
774         media_svc_debug("mime [%s]", mime_type);
775         content_info->is_drm = drm_type;
776
777         ret = __media_svc_get_media_type(path, mime_type, &media_type);
778         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
779
780         content_info->mime_type = g_strdup(mime_type);
781         media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
782
783         media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, media_type);
784
785         content_info->media_type = media_type;
786
787         return MS_MEDIA_ERR_NONE;
788 }
789
790 int image_360_check(char *path)
791 {
792         FILE *fp = NULL;
793         long app1_size = 0;
794         int size = 1;
795         unsigned char exif_header[4] = {0, };
796         unsigned char exif_app1[2] = {0, };
797         unsigned char exif_app1_xmp[2] = {0, };
798         long exif_app1_xmp_size = 0;
799         unsigned char exif_app1_xmp_t[2] = {0, };
800         char *xmp_data = NULL;
801         int size1 = 0;
802         int size2 = 0;
803         int fdata = 0;
804         int temp = 0;
805
806         memset(exif_header, 0x00, sizeof(exif_header));
807         memset(exif_app1, 0x00, sizeof(exif_app1));
808         memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
809         memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
810
811         fp = fopen(path, "rb");
812         if (fp == NULL)
813                 goto ERROR;
814
815         size = fread(exif_header, 1, sizeof(exif_header), fp);
816         if (size <= 0)
817                 goto ERROR;
818
819         if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
820                 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
821                 if (size <= 0)
822                         goto ERROR;
823
824                 size1 = exif_app1[0];
825                 size2 = exif_app1[1];
826
827                 app1_size = size1 * 256 + size2 - 2;
828
829                 if (fseek(fp, app1_size, SEEK_CUR) != 0)
830                         goto ERROR;
831
832                 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
833                 if (size <= 0)
834                         goto ERROR;
835
836                 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
837                         int result = 0;
838                         char *ptr = NULL;
839                         size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
840                         if (size <= 0)
841                                 goto ERROR;
842
843                         size1 = exif_app1_xmp_t[0];
844                         size2 = exif_app1_xmp_t[1];
845
846                         exif_app1_xmp_size = size1 * 256 + size2 - 2;
847
848                         if (exif_app1_xmp_size > 0) {
849                                 xmp_data = (char *)malloc(exif_app1_xmp_size);
850                                 memset(xmp_data, 0x0, exif_app1_xmp_size);
851
852                                 ptr = xmp_data;
853
854                                 while (exif_app1_xmp_size >= 0) {
855                                         exif_app1_xmp_size--;
856                                         fdata = fgetc(fp);
857                                         if (fdata == EOF)
858                                                 continue;
859                                         if (fdata == '\0')
860                                                 continue;
861                                         *ptr = (char)fdata;
862                                         ptr++;
863                                         temp++;
864                                 }
865                                 ptr = ptr - temp;
866
867                                 if (strstr(ptr, "UsePanoramaViewer")
868                                 && strstr(ptr, "True")
869                                 && strstr(ptr, "ProjectionType")
870                                 && strstr(ptr, "equirectangular"))
871                                         result = 1;
872
873                                 SAFE_FREE(xmp_data);
874                         } else {
875                                 media_svc_error("invalid exif_app1_xmp_size [%ld]", exif_app1_xmp_size);
876                         }
877
878                         if (fp) {
879                                 fclose(fp);
880                                 fp = NULL;
881                         }
882                         return result;
883                 } else {
884                         goto ERROR;
885                 }
886         } else {
887                 goto ERROR;
888         }
889 ERROR:
890         if (fp) {
891                 fclose(fp);
892                 fp = NULL;
893         }
894         return 0;
895 }
896
897 static char * __media_svc_get_title(MMHandleType tag, const char *path)
898 {
899         int ret = FILEINFO_ERROR_NONE;
900         char *p = NULL;
901         int size = 0;
902         char *title = NULL;
903
904         if (tag) {
905                 ret = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
906                 if (ret == FILEINFO_ERROR_NONE && size > 0) {
907                         while(p && isspace(*p))
908                                 p++;
909
910                         return g_strdup(p);
911                 }
912         }
913
914         title = __media_svc_get_title_from_filepath(path);
915         if (title)
916                 return title;
917
918         media_svc_error("Can't extract title");
919
920         return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
921 }
922
923 char * _media_svc_get_title_by_path(const char *path)
924 {
925         /* No MMHandleType in media-svc.c */
926         return __media_svc_get_title(NULL, path);
927 }
928
929 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
930 {
931         double value = 0.0;
932         int orient_value = 0;
933         int exif_width = 0;
934         int exif_height = 0;
935         ExifData *ed = NULL;
936         bool has_datetaken = false;
937         double fnumber = 0.0;
938         int iso = 0;
939         char *path = NULL;
940
941         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
942
943         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
944         media_svc_retvm_if(content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid media_type [%d]", content_info->media_type);
945         media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
946
947         path = content_info->path;
948         content_info->media_meta.title = __media_svc_get_title(NULL, path);
949
950         /* Load an ExifData object from an EXIF file */
951         ed = exif_data_new_from_file(path);
952
953         if (!ed) {
954                 media_svc_sec_debug("There is no exif data in [ %s ]", path);
955                 goto GET_WIDTH_HEIGHT;
956         }
957
958         content_info->media_meta.is_360 = image_360_check(path);
959
960         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
961         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
962         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
963
964         /* Not used. But to preserved the behavior, set MEDIA_SVC_TAG_UNKNOWN. */
965         content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
966         content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
967         content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
968         content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
969         content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
970         content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
971         content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
972         content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
973
974         memset(buf, 0x00, sizeof(buf));
975         if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
976                 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
977                         if (!g_strcmp0(buf, "S"))
978                                 value *= -1;
979                         content_info->media_meta.latitude = value;
980                 }
981         }
982
983         memset(buf, 0x00, sizeof(buf));
984         if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
985                 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
986                         if (!g_strcmp0(buf, "W"))
987                                 value *= -1;
988
989                         content_info->media_meta.longitude = value;
990                 }
991         }
992
993         memset(buf, 0x00, sizeof(buf));
994         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
995                 if (strlen(buf) > 0)
996                         content_info->media_meta.description = g_strdup(buf);
997                 else
998                         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
999
1000         }
1001
1002         memset(buf, 0x00, sizeof(buf));
1003         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1004                 if (strlen(buf) > 0) {
1005                         has_datetaken = true;
1006                         content_info->media_meta.datetaken = g_strdup(buf);
1007
1008                         /* This is same as recorded_date */
1009                         content_info->media_meta.recorded_date = g_strdup(buf);
1010                 }
1011         }
1012
1013         memset(buf, 0x00, sizeof(buf));
1014         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1015                 if (strlen(buf) > 0) {
1016                         has_datetaken = true;
1017                         content_info->media_meta.datetaken = g_strdup(buf);
1018
1019                         /* This is same as recorded_date */
1020                         content_info->media_meta.recorded_date =  g_strdup(buf);
1021                 }
1022         }
1023
1024         if (has_datetaken) {
1025                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1026                 if (content_info->timeline == 0)
1027                         content_info->timeline = content_info->modified_time;
1028                 else
1029                         media_svc_debug("Timeline : %ld", content_info->timeline);
1030         }
1031
1032         memset(buf, 0x00, sizeof(buf));
1033         /* Get exposure_time value from exif. */
1034         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1035                 if (strlen(buf) > 0)
1036                         content_info->media_meta.exposure_time = g_strdup(buf);
1037         }
1038
1039         /* Get fnumber value from exif. */
1040         if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE)
1041                 content_info->media_meta.fnumber = fnumber;
1042
1043         /* Get iso value from exif. */
1044         if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE)
1045                 content_info->media_meta.iso = iso;
1046
1047         memset(buf, 0x00, sizeof(buf));
1048         /* Get model value from exif. */
1049         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1050                 if (strlen(buf) > 0)
1051                         content_info->media_meta.model = g_strdup(buf);
1052         }
1053
1054         /* Get orientation value from exif. */
1055         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1056                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1057                         content_info->media_meta.orientation = orient_value;
1058         }
1059
1060         /* Get width value from exif. */
1061         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1062                 if (exif_width > 0)
1063                         content_info->media_meta.width = exif_width;
1064         }
1065
1066         /* Get height value from exif. */
1067         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1068                 if (exif_height > 0)
1069                         content_info->media_meta.height = exif_height;
1070         }
1071
1072         if (ed)
1073                 exif_data_unref(ed);
1074
1075 GET_WIDTH_HEIGHT:
1076
1077         if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
1078                 /*Get image width, height*/
1079                 unsigned int img_width = 0;
1080                 unsigned int img_height = 0;
1081                 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
1082
1083                 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
1084                 if (content_info->media_meta.width == 0)
1085                         content_info->media_meta.width = img_width;
1086
1087                 if (content_info->media_meta.height == 0)
1088                         content_info->media_meta.height = img_height;
1089         }
1090
1091         return MS_MEDIA_ERR_NONE;
1092 }
1093
1094 static char * __media_svc_get_tag_str_value(MMHandleType tag, const char *tag_name)
1095 {
1096         int ret = FILEINFO_ERROR_NONE;
1097         char *p = NULL;
1098         int size = 0;
1099
1100         ret = mm_file_get_attrs(tag, tag_name, &p, &size, NULL);
1101         if (ret == FILEINFO_ERROR_NONE && size > 0)
1102                 return g_strdup(p);
1103
1104         return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1105 }
1106
1107 int _media_svc_extract_music_metadata_for_update(media_svc_content_info_s *content_info, const char *path)
1108 {
1109         MMHandleType tag = 0;
1110         int mmf_error = FILEINFO_ERROR_NONE;
1111
1112         content_info->path = g_strdup(path);
1113
1114         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1115         if (mmf_error == FILEINFO_ERROR_NONE) {
1116                 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
1117                 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
1118                 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
1119                 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
1120                 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
1121                 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
1122                 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
1123                 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
1124         } else {
1125                 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1126         }
1127
1128         mm_file_destroy_tag_attrs(tag);
1129
1130         return MS_MEDIA_ERR_NONE;
1131 }
1132
1133 int _media_svc_extract_media_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
1134 {
1135         MMHandleType content = 0;
1136         MMHandleType tag = 0;
1137         char *p = NULL;
1138         unsigned char *image = NULL;
1139         unsigned int size = 0;
1140         int mmf_error = FILEINFO_ERROR_NONE;
1141         int album_id = 0;
1142         int ret = MS_MEDIA_ERR_NONE;
1143         int convert_value = 0;
1144         int cdis_value = 0;
1145
1146         /*Get Content Tag attribute ===========*/
1147         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1148
1149         if (mmf_error == FILEINFO_ERROR_NONE) {
1150                 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
1151                 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
1152                 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
1153                 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
1154                 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
1155                 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
1156                 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
1157                 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
1158                 content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
1159
1160                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1161                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1162                         if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
1163                                 /*Creation time format is 2013-01-01 00:00:00 +0000. change it to 2013:01:01 00:00:00  +0000 like exif time format*/
1164                                 char *p_value = g_strdelimit(g_strdup(p), "-", ':');
1165                                 content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
1166                                 SAFE_FREE(p_value);
1167                         } else {
1168                                 content_info->media_meta.recorded_date = g_strdup(p);
1169                         }
1170
1171                         if (STRING_VALID(content_info->media_meta.recorded_date)) {
1172                                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1173                                 if (content_info->timeline == 0)
1174                                         content_info->timeline = content_info->modified_time;
1175
1176                                 /* This is same as datetaken */
1177                                 /* Remove compensation string */
1178                                 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
1179                                         content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
1180                                         g_free(content_info->media_meta.recorded_date);
1181                                         content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
1182                                 } else {
1183                                         content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
1184                                 }
1185                         }
1186                 }
1187
1188                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
1189                 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
1190                         if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1191                                 content_info->media_meta.year = g_strdup(p);
1192                 }
1193
1194                 if (!content_info->media_meta.year)
1195                                 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1196
1197                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RATING, &p, &size, NULL);
1198                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1199                         if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1200                                 content_info->media_meta.rating = convert_value;
1201                 } else {
1202                         content_info->media_meta.rating = 0;
1203                 }
1204
1205                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1206
1207                 /*Do not extract artwork for the USB Storage content*/
1208                 if (content_info->storage_type != MS_USER_STORAGE_EXTERNAL_USB) {
1209                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1210                         if (mmf_error != FILEINFO_ERROR_NONE)
1211                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1212
1213                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1214                         if (mmf_error != FILEINFO_ERROR_NONE)
1215                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1216
1217                         if (image != NULL && size > 0) {
1218                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1219                                 int artwork_mime_size = -1;
1220
1221                                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1222                                 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1223                                         ret = _media_svc_get_thumbnail_path(content_info->media_type, thumb_path, content_info->path, p, uid);
1224                                         if (ret != MS_MEDIA_ERR_NONE) {
1225                                                 media_svc_error("Fail to Get Thumbnail Path");
1226                                         } else {
1227                                                 ret = __media_svc_save_image(image, size, thumb_path, uid);
1228                                                 if (ret != MS_MEDIA_ERR_NONE) {
1229                                                         media_svc_error("Fail to Save Image");
1230                                                 } else {
1231                                                         content_info->thumbnail_path = g_strdup(thumb_path);
1232                                                         /* NOTICE : Prevent resize for performance (2020.02.07)
1233                                                         *  In most cases, artwork's format is jpeg and size is under MEDIA_SVC_ARTWORK_SIZE * MEDIA_SVC_ARTWORK_SIZE.
1234                                                         *  So, doing mm_util_extract_image_info to check image size is a time-consuming task.
1235                                                         */
1236 #if 0
1237                                                         /* albumart resizing */
1238                                                         ret = __media_svc_resize_artwork(thumb_path, p);
1239                                                         if (ret != MS_MEDIA_ERR_NONE) {
1240                                                                 media_svc_error("Fail to Make Thumbnail Image");
1241                                                                 _media_svc_remove_file(thumb_path);
1242
1243                                                         } else {
1244                                                                 content_info->thumbnail_path = g_strdup(thumb_path);
1245                                                         }
1246 #endif
1247                                                 }
1248                                         }
1249                                 }
1250                         }
1251                 }
1252
1253                 /*Initialize album_id to 0. below code will set the album_id*/
1254                 content_info->album_id = album_id;
1255                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1256                 if (ret != MS_MEDIA_ERR_NONE) {
1257                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1258                                 media_svc_debug("album does not exist. So start to make album art");
1259                                 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1260                                         (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1261                                         ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1262                                 else
1263                                         ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1264
1265                                 content_info->album_id = album_id;
1266                         }
1267                 } else {
1268                         content_info->album_id = album_id;
1269                 }
1270
1271                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1272                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1273                 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1274
1275                 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1276                         double longitude = 0.0;
1277                         double latitude = 0.0;
1278                         double altitude = 0.0;
1279
1280                         mm_file_get_attrs(tag, MM_FILE_TAG_LONGITUDE, &longitude,
1281                                 MM_FILE_TAG_LATIDUE, &latitude,
1282                                 MM_FILE_TAG_ALTIDUE, &altitude,
1283                                 NULL);
1284
1285                         content_info->media_meta.longitude = (longitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : longitude;
1286                         content_info->media_meta.latitude = (latitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : latitude;
1287                         content_info->media_meta.altitude = (altitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : altitude;
1288
1289                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1290                         if (mmf_error != FILEINFO_ERROR_NONE)
1291                                 cdis_value = 0;
1292
1293                         media_svc_debug("CDIS : %d", cdis_value);
1294
1295                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1296                         if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1297                                 content_info->media_meta.orientation = atoi(p);
1298                         } else {
1299                                 content_info->media_meta.orientation = 0;
1300                                 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1301                         }
1302                 }
1303
1304                 mmf_error = mm_file_destroy_tag_attrs(tag);
1305                 if (mmf_error != FILEINFO_ERROR_NONE)
1306                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1307         }       else {
1308                 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1309                 content_info->album_id = album_id;
1310         }
1311
1312         /*Get Content attribute ===========*/
1313         if (cdis_value == 1)
1314                 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1315         else
1316                 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1317
1318         media_svc_retvm_if(mmf_error != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_NONE, "mm_file_create_content_attrs failed");
1319
1320         if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1321                 int audio_bitrate = 0;
1322                 int video_bitrate = 0;
1323
1324                 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1325                         MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate,
1326                         MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate,
1327                         MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width,
1328                         MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height,
1329                         NULL);
1330
1331                 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1332         } else {
1333                 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1334                         MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate,
1335                         MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate,
1336                         MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel,
1337                         MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample,
1338                         NULL);
1339         }
1340
1341         mm_file_destroy_content_attrs(content);
1342
1343         return MS_MEDIA_ERR_NONE;
1344 }
1345
1346 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1347 {
1348         media_svc_retm_if(content_info == NULL, "content info is NULL");
1349
1350         /* Delete media_svc_content_info_s */
1351         SAFE_FREE(content_info->media_uuid);
1352         SAFE_FREE(content_info->path);
1353         SAFE_FREE(content_info->file_name);
1354         SAFE_FREE(content_info->mime_type);
1355         SAFE_FREE(content_info->folder_uuid);
1356         SAFE_FREE(content_info->thumbnail_path);
1357         SAFE_FREE(content_info->storage_uuid);
1358
1359         /* Delete media_svc_content_meta_s */
1360         SAFE_FREE(content_info->media_meta.title);
1361         SAFE_FREE(content_info->media_meta.album);
1362         SAFE_FREE(content_info->media_meta.artist);
1363         SAFE_FREE(content_info->media_meta.album_artist);
1364         SAFE_FREE(content_info->media_meta.genre);
1365         SAFE_FREE(content_info->media_meta.composer);
1366         SAFE_FREE(content_info->media_meta.year);
1367         SAFE_FREE(content_info->media_meta.recorded_date);
1368         SAFE_FREE(content_info->media_meta.copyright);
1369         SAFE_FREE(content_info->media_meta.track_num);
1370         SAFE_FREE(content_info->media_meta.description);
1371         SAFE_FREE(content_info->media_meta.datetaken);
1372         SAFE_FREE(content_info->media_meta.exposure_time);
1373         SAFE_FREE(content_info->media_meta.model);
1374
1375         SAFE_FREE(content_info->file_name_pinyin);
1376         SAFE_FREE(content_info->media_meta.title_pinyin);
1377         SAFE_FREE(content_info->media_meta.album_pinyin);
1378         SAFE_FREE(content_info->media_meta.artist_pinyin);
1379         SAFE_FREE(content_info->media_meta.album_artist_pinyin);
1380         SAFE_FREE(content_info->media_meta.genre_pinyin);
1381         SAFE_FREE(content_info->media_meta.composer_pinyin);
1382         SAFE_FREE(content_info->media_meta.copyright_pinyin);
1383         SAFE_FREE(content_info->media_meta.description_pinyin);
1384 }
1385
1386 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
1387 {
1388         int ret = MS_MEDIA_ERR_NONE;
1389
1390         media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
1391         media_svc_retvm_if(!thumb_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1392         media_svc_retvm_if(!g_file_test(path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "File doesn't exist[%s]", path);
1393
1394         ms_user_storage_type_e store_type = -1;
1395         ret = ms_user_get_storage_type(uid, path, &store_type);
1396
1397         if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
1398                 media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
1399                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1400         }
1401
1402         media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1403
1404         //1. make hash path
1405         ret = _media_svc_get_thumbnail_path(media_type, thumb_path, path, NULL, uid);
1406         if (ret != MS_MEDIA_ERR_NONE) {
1407                 media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
1408                 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1409                 return ret;
1410         }
1411
1412         //2. save thumbnail
1413         if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1414                 return create_image_thumbnail_to_file(path, CONTENT_THUMB_DEFAULT_WIDTH, CONTENT_THUMB_DEFAULT_HEIGHT, thumb_path);
1415         else
1416                 return create_video_thumbnail_to_file(path, CONTENT_THUMB_DEFAULT_WIDTH, CONTENT_THUMB_DEFAULT_HEIGHT, thumb_path, true);
1417
1418 }
1419
1420 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1421 {
1422         int ret = MS_MEDIA_ERR_NONE;
1423         int size = 0;
1424         pinyin_name_s *pinyinname = NULL;
1425
1426         media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1427         media_svc_retvm_if(pinyin_str == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "pinyin_str is NULL");
1428
1429         *pinyin_str = NULL;
1430
1431         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1432         if (ret == MS_MEDIA_ERR_NONE) {
1433                 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1434                         *pinyin_str = strdup(pinyinname[0].pinyin_name);
1435                 else
1436                         *pinyin_str = strdup(src_str);  /* Return Original Non China Character */
1437         }
1438
1439         _media_svc_pinyin_free(pinyinname, size);
1440
1441         return ret;
1442 }
1443
1444 bool _media_svc_check_pinyin_support(void)
1445 {
1446         int ret = SYSTEM_INFO_ERROR_NONE;
1447         bool is_supported = false;
1448         static int media_svc_pinyin_support = -1;
1449
1450         if (media_svc_pinyin_support == -1) {
1451                 ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
1452                 if (ret != SYSTEM_INFO_ERROR_NONE) {
1453                         media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
1454                         return false;
1455                 }
1456
1457                 media_svc_pinyin_support = is_supported;
1458         }
1459
1460         return media_svc_pinyin_support;
1461 }
1462
1463 int _media_svc_get_media_type(const char *path, int *mediatype)
1464 {
1465         int ret = MS_MEDIA_ERR_NONE;
1466         char mime_type[256] = {0};
1467         media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1468
1469         media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1470
1471         ret = __media_svc_get_mime_type(path, mime_type);
1472         if (ret == MS_MEDIA_ERR_NONE)
1473                 __media_svc_get_media_type(path, mime_type, &media_type);
1474         else
1475                 media_svc_error("__media_svc_get_mime_type failed");
1476
1477         *mediatype = media_type;
1478
1479         return ret;
1480 }
1481
1482 bool _media_svc_is_valid_storage_type(ms_user_storage_type_e storage_type)
1483 {
1484         switch (storage_type) {
1485         case MS_USER_STORAGE_INTERNAL:
1486         case MS_USER_STORAGE_EXTERNAL:
1487         case MS_USER_STORAGE_EXTERNAL_USB:
1488                 return true;
1489         default:
1490                 media_svc_error("storage type is incorrect[%d]", storage_type);
1491                 return false;
1492         }
1493 }