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