Use media type instead of category
[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 /*For ebook*/
55 #include <zip.h>
56 #include <libxml/xmlmemory.h>
57 #include <libxml/parser.h>
58 #include <libxml/HTMLparser.h>
59
60 #define MEDIA_SVC_FILE_EXT_LEN_MAX                              6                       /**< Maximum file ext lenth*/
61
62 #define CONTENT_TYPE_NUM 5
63 #define MUSIC_MIME_NUM 29
64 #define SOUND_MIME_NUM 1
65 #define MIME_TYPE_LENGTH 255
66 #define THUMB_HASH_LEN 256
67 #define MIME_LENGTH 50
68 #define _3GP_FILE ".3gp"
69 #define _MP4_FILE ".mp4"
70 #define _ASF_FILE ".asf"
71 #define MEDIA_SVC_ARTWORK_SIZE 2000
72 #define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
73 #define IMAGE_PREFIX "image/"
74 #define IMAGE_PREFIX_LEN 6
75
76 #define MEDIA_SVC_DEFAULT_GPS_VALUE                     -200                    /**< Default GPS Value*/
77
78 #define MEDIA_SVC_PDF_TAG_TAIL_LEN 12
79 #define MEDIA_SVC_PDF_BUF_SIZE 256
80
81 typedef struct {
82         char content_type[15];
83         media_svc_media_type_e media_type;
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_MEDIA_TYPE_SOUND},
100         {"image", MEDIA_SVC_MEDIA_TYPE_IMAGE},
101         {"video", MEDIA_SVC_MEDIA_TYPE_VIDEO},
102         {"application", MEDIA_SVC_MEDIA_TYPE_OTHER},
103         {"text", MEDIA_SVC_MEDIA_TYPE_OTHER},
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_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
319 {
320         int idx = 0;
321
322         media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "path is null");
323         media_svc_retvm_if(!mime_type, MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is null");
324         media_svc_retvm_if(!media_type, MS_MEDIA_ERR_INVALID_PARAMETER, "media_type is null");
325
326         *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
327
328         /*categorize from mime_type */
329         for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
330                 if (strncmp(mime_type, content_category[idx].content_type, strlen(content_category[idx].content_type)) == 0) {
331                         *media_type = content_category[idx].media_type;
332                         break;
333                 }
334         }
335
336         /*in application type, exitst sound file ex) x-smafs, asf */
337         if (*media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
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(mime_type + prefix_len, sound_mime_table[idx]) != NULL) {
343                                 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
344                                 break;
345                         }
346                 }
347
348                 if (strncasecmp(mime_type, "text/x-iMelody", strlen("text/x-iMelody")) == 0)
349                         *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
350
351                 /*"asf" must check video stream and then categorize in directly. */
352                 ext = strrchr(path, '.');
353                 if (ext != NULL) {
354                         if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
355                                 int audio = 0;
356                                 int video = 0;
357                                 int err = 0;
358
359                                 err = mm_file_get_stream_info(path, &audio, &video);
360                                 if (err == 0) {
361                                         if (audio > 0 && video == 0)
362                                                 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
363                                         else
364                                                 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
365                                 }
366                         }
367                 }
368
369                 if (g_str_has_suffix(mime_type, "epub+zip") || g_str_has_suffix(mime_type, "pdf"))
370                         *media_type = MEDIA_SVC_MEDIA_TYPE_BOOK;
371         }
372
373         /*check music file in sound files. */
374         if (*media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) {
375                 int prefix_len = strlen(content_category[0].content_type) + 1;
376
377                 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
378                         if (strcmp(mime_type + prefix_len, music_mime_table[idx]) == 0) {
379                                 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
380                                 break;
381                         }
382                 }
383
384                 /*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*/
385                 if (strncasecmp(mime_type, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0)
386                         *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
387
388         } else if (*media_type == MEDIA_SVC_VIDEO_VIDEO) {
389                 /*some video files don't have video stream. in this case it is categorize as music. */
390                 char *ext = NULL;
391                 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
392                 ext = strrchr(path, '.');
393                 if (ext != NULL) {
394                         if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
395                                 int audio = 0;
396                                 int video = 0;
397                                 int err = 0;
398
399                                 err = mm_file_get_stream_info(path, &audio, &video);
400                                 if (err == 0) {
401                                         if (audio > 0 && video == 0) {
402                                                 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
403                                         }
404                                 }
405                         }
406                 }
407         }
408
409         return MS_MEDIA_ERR_NONE;
410 }
411
412 /*
413 drm_contentifo is not NULL, if the file is OMA DRM.
414 If the file is not OMA DRM, drm_contentinfo must be NULL.
415 */
416 static int __media_svc_get_mime_type(const char *path, char *mimetype)
417 {
418         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
419
420         /*in case of normal files or failure to get mime in drm */
421         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
422                 media_svc_error("aul_get_mime_from_file fail");
423                 return MS_MEDIA_ERR_INTERNAL;
424         }
425
426         return MS_MEDIA_ERR_NONE;
427 }
428
429 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
430 {
431         int i = 0;
432
433         for (i = strlen(file_path); i >= 0; i--) {
434                 if (file_path[i] == '.') {
435                         SAFE_STRLCPY(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
436                         return true;
437                 }
438
439                 if (file_path[i] == '/')
440                         return false;
441         }
442         return false;
443 }
444
445 static int __media_svc_safe_atoi(char *buffer, int *si)
446 {
447         char *end = NULL;
448         errno = 0;
449         media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
450
451         const long sl = strtol(buffer, &end, 10);
452
453         media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
454         media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
455         media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
456         media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
457         media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
458
459         *si = (int)sl;
460
461         return MS_MEDIA_ERR_NONE;
462 }
463
464 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
465 {
466         int ret = MS_MEDIA_ERR_NONE;
467         struct statfs fs;
468         char *thumb_path = NULL;
469         long bsize_kbytes = 0;
470         GError *error = NULL;
471
472         media_svc_retvm_if(!image || size == 0, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid image");
473         media_svc_sec_debug("start save image, path [%s] image size [%d]", image_path, size);
474
475         ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
476         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path error");
477
478         ret = statfs(thumb_path, &fs);
479         g_free(thumb_path);
480         media_svc_retvm_if(ret == -1, MS_MEDIA_ERR_INTERNAL, "statfs failed");
481
482         bsize_kbytes = fs.f_bsize >> 10;
483         media_svc_retvm_if((bsize_kbytes * fs.f_bavail) < 1024, MS_MEDIA_ERR_NOT_ENOUGH_SPACE, "Not enough space");
484
485         if (!g_file_set_contents(image_path, (const gchar *)image, (gssize)size, &error)) {
486                 media_svc_error("g_file_set_contents faild:%s", error->message);
487                 g_error_free(error);
488                 return MS_MEDIA_ERR_INTERNAL;
489         }
490
491         return MS_MEDIA_ERR_NONE;
492 }
493
494 static char *__media_svc_get_title_from_filepath(const char *path)
495 {
496         char *filename = NULL;
497         char *title = NULL;
498         char *last_dot = NULL;
499
500         media_svc_retvm_if(!STRING_VALID(path), NULL, "Invalid path");
501
502         filename = g_path_get_basename(path);
503
504         last_dot = strrchr(filename, '.');
505         if (last_dot) {
506                 title = g_strndup(filename, last_dot - filename);
507                 g_free(filename);
508         } else {
509                 title = filename;
510         }
511
512         media_svc_debug("extract title is [%s]", title);
513
514         return title;
515 }
516
517 void _media_svc_remove_file(const char *path)
518 {
519         if (!STRING_VALID(path))
520                 return;
521
522         if (remove(path) != 0)
523                 media_svc_stderror("fail to remove file result");
524 }
525
526 int _media_svc_get_thumbnail_path(char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
527 {
528         int ret = MS_MEDIA_ERR_NONE;
529         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
530         char hash[THUMB_HASH_LEN] = {0, };
531         char *thumb_dir = NULL;
532
533         ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
534         media_svc_retvm_if(!STRING_VALID(thumb_dir), MS_MEDIA_ERR_INTERNAL, "ms_user_get_root_thumb_store_path failed");
535
536         if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
537                 media_svc_error("Wrong path[%s]", thumb_dir);
538                 g_free(thumb_dir);
539                 return MS_MEDIA_ERR_INTERNAL;
540         }
541
542         memset(file_ext, 0, sizeof(file_ext));
543         if (!__media_svc_get_file_ext(pathname, file_ext))
544                 media_svc_error("get file ext fail");
545
546         ret = mb_svc_generate_hash_code(pathname, hash, THUMB_HASH_LEN);
547         if (ret != MS_MEDIA_ERR_NONE) {
548                 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
549                 g_free(thumb_dir);
550                 return MS_MEDIA_ERR_INTERNAL;
551         }
552
553         if (img_format) {
554                 /* 'img_format' is mime-type */
555                 if (!g_str_has_prefix(img_format, IMAGE_PREFIX) || strlen(img_format) == IMAGE_PREFIX_LEN) {
556                         media_svc_error("Not proper img format");
557                         g_free(thumb_dir);
558                         return MS_MEDIA_ERR_INTERNAL;
559                 }
560
561                 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, img_format + IMAGE_PREFIX_LEN);
562         } else {
563                 if (strcasecmp(file_ext, "PNG") == 0)
564                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
565                 else
566                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
567         }
568
569         g_free(thumb_dir);
570
571         return MS_MEDIA_ERR_NONE;
572 }
573
574 int _media_svc_get_file_time(const char *full_path)
575 {
576         struct stat statbuf = { 0, };
577
578         if (stat(full_path, &statbuf) == -1) {
579                 media_svc_stderror("stat fails.");
580                 return 0;
581         }
582
583         return statbuf.st_mtime;
584 }
585
586 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)
587 {
588         int ret = MS_MEDIA_ERR_NONE;
589         bool drm_type = false;
590         char mime_type[256] = {0, };
591         media_svc_media_type_e media_type;
592
593         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
594         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
595
596         content_info->path = g_strdup(path);
597         media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
598
599         struct stat st;
600         memset(&st, 0, sizeof(struct stat));
601         if (stat(path, &st) == 0) {
602                 content_info->modified_time = st.st_mtime;
603                 content_info->timeline = content_info->modified_time;
604                 content_info->size = st.st_size;
605         } else {
606                 media_svc_stderror("stat failed");
607         }
608
609         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
610         if (refresh) {
611                 media_svc_debug("refresh");
612                 return MS_MEDIA_ERR_NONE;
613         }
614
615         content_info->storage_uuid = g_strdup(storage_id);
616         media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
617
618         content_info->storage_type = storage_type;
619         time(&content_info->added_time);
620
621         content_info->media_uuid = _media_info_generate_uuid();
622         media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
623
624         content_info->file_name = g_path_get_basename(path);
625         media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
626
627         /* if the file is DRM file, drm_type value is DRM_TRUE(1).
628         if drm_contentinfo is not NULL, the file is OMA DRM.*/
629         ret = __media_svc_get_mime_type(path, mime_type);
630         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
631
632         media_svc_debug("mime [%s]", mime_type);
633         content_info->is_drm = drm_type;
634
635         ret = __media_svc_get_media_type(path, mime_type, &media_type);
636         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
637
638         content_info->mime_type = g_strdup(mime_type);
639         media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
640
641         media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, media_type);
642
643         content_info->media_type = media_type;
644
645         return MS_MEDIA_ERR_NONE;
646 }
647
648 static int __image_360_check(const char *path)
649 {
650         FILE *fp = NULL;
651         size_t size = 0, app1_size = 0, exif_app1_xmp_size = 0;
652         unsigned char exif_header[4] = {0, };
653         unsigned char exif_app1[2] = {0, };
654         unsigned char exif_app1_xmp[2] = {0, };
655         unsigned char exif_app1_xmp_t[2] = {0, };
656         GString *xmp_data = NULL;
657         int fdata = 0;
658         int result = 0;
659
660         memset(exif_header, 0x00, sizeof(exif_header));
661         memset(exif_app1, 0x00, sizeof(exif_app1));
662         memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
663         memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
664
665         fp = fopen(path, "rb");
666         if (fp == NULL)
667                 goto ERROR;
668
669         size = fread(exif_header, 1, sizeof(exif_header), fp);
670         if (size != sizeof(exif_header))
671                 goto ERROR;
672
673         if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
674                 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
675                 if (size != sizeof(exif_app1))
676                         goto ERROR;
677
678                 if ((size_t)((exif_app1[0] << 8) | (exif_app1[1])) <= 2)
679                         goto ERROR;
680
681                 app1_size = (size_t)((exif_app1[0] << 8) | (exif_app1[1])) - 2 ;
682                 if (fseek(fp, app1_size, SEEK_CUR) != 0)
683                         goto ERROR;
684
685                 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
686                 if (size != sizeof(exif_app1_xmp))
687                         goto ERROR;
688
689                 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
690                         size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
691                         if (size != sizeof(exif_app1_xmp_t))
692                                 goto ERROR;
693
694                         if ((size_t)((exif_app1_xmp_t[0] << 8) | (exif_app1_xmp_t[1])) <= 2)
695                                 goto ERROR;
696
697                         exif_app1_xmp_size = (size_t)((exif_app1_xmp_t[0] << 8) | (exif_app1_xmp_t[1])) - 2;
698
699                         xmp_data = g_string_sized_new(exif_app1_xmp_size);
700
701                         do {
702                                 exif_app1_xmp_size--;
703                                 fdata = fgetc(fp);
704                                 if (fdata == EOF)
705                                         continue;
706                                 if (fdata == '\0')
707                                         continue;
708
709                                 xmp_data = g_string_append_c(xmp_data, (gchar)fdata);
710                         } while (exif_app1_xmp_size > 0);
711
712                         if (strstr(xmp_data->str, "UsePanoramaViewer") &&
713                                 strstr(xmp_data->str, "True") &&
714                                 strstr(xmp_data->str, "ProjectionType") &&
715                                 strstr(xmp_data->str, "equirectangular"))
716                                 result = 1;
717
718                         g_string_free(xmp_data, TRUE);
719                 }
720         }
721
722 ERROR:
723         if (fp) {
724                 fclose(fp);
725                 fp = NULL;
726         }
727
728         return result;
729 }
730
731 static char * __media_svc_get_title(MMHandleType tag, const char *path)
732 {
733         int ret = FILEINFO_ERROR_NONE;
734         char *p = NULL;
735         int size = 0;
736         char *title = NULL;
737
738         if (tag) {
739                 ret = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
740                 if (ret == FILEINFO_ERROR_NONE && size > 0) {
741                         while(p && isspace(*p))
742                                 p++;
743
744                         return g_strdup(p);
745                 }
746         }
747
748         title = __media_svc_get_title_from_filepath(path);
749         if (title)
750                 return title;
751
752         media_svc_error("Can't extract title");
753
754         return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
755 }
756
757 char * _media_svc_get_title_by_path(const char *path)
758 {
759         /* No MMHandleType in media-svc.c */
760         return __media_svc_get_title(NULL, path);
761 }
762
763 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
764 {
765         double value = 0.0;
766         int orient_value = 0;
767         int exif_width = 0;
768         int exif_height = 0;
769         ExifData *ed = NULL;
770         bool has_datetaken = false;
771         double fnumber = 0.0;
772         int iso = 0;
773         char *path = NULL;
774
775         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
776
777         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
778         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);
779         media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
780
781         path = content_info->path;
782         content_info->media_meta.title = __media_svc_get_title(NULL, path);
783
784         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
785         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
786         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
787
788         /* Not used. But to preserved the behavior, set MEDIA_SVC_TAG_UNKNOWN. */
789         content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
790         content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
791         content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
792         content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
793         content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
794         content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
795         content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
796         content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
797
798         /* Load an ExifData object from an EXIF file */
799         ed = exif_data_new_from_file(path);
800         if (!ed) {
801                 media_svc_sec_debug("There is no exif data in [ %s ]", path);
802                 goto GET_WIDTH_HEIGHT;
803         }
804
805         content_info->media_meta.is_360 = __image_360_check(path);
806
807         memset(buf, 0x00, sizeof(buf));
808         if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
809                 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
810                         if (!g_strcmp0(buf, "S"))
811                                 value *= -1;
812                         content_info->media_meta.latitude = value;
813                 }
814         }
815
816         memset(buf, 0x00, sizeof(buf));
817         if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
818                 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
819                         if (!g_strcmp0(buf, "W"))
820                                 value *= -1;
821
822                         content_info->media_meta.longitude = value;
823                 }
824         }
825
826         memset(buf, 0x00, sizeof(buf));
827         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
828                 if (strlen(buf) > 0)
829                         content_info->media_meta.description = g_strdup(buf);
830                 else
831                         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
832         }
833
834         memset(buf, 0x00, sizeof(buf));
835         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
836                 if (strlen(buf) > 0) {
837                         has_datetaken = true;
838                         content_info->media_meta.datetaken = g_strdup(buf);
839
840                         /* This is same as recorded_date */
841                         content_info->media_meta.recorded_date = g_strdup(buf);
842                 }
843         }
844
845         memset(buf, 0x00, sizeof(buf));
846         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
847                 if (strlen(buf) > 0) {
848                         has_datetaken = true;
849                         content_info->media_meta.datetaken = g_strdup(buf);
850
851                         /* This is same as recorded_date */
852                         content_info->media_meta.recorded_date = g_strdup(buf);
853                 }
854         }
855
856         if (has_datetaken) {
857                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
858                 if (content_info->timeline == 0)
859                         content_info->timeline = content_info->modified_time;
860                 else
861                         media_svc_debug("Timeline : %ld", content_info->timeline);
862         }
863
864         memset(buf, 0x00, sizeof(buf));
865         /* Get exposure_time value from exif. */
866         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
867                 if (strlen(buf) > 0)
868                         content_info->media_meta.exposure_time = g_strdup(buf);
869         }
870
871         /* Get fnumber value from exif. */
872         if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE)
873                 content_info->media_meta.fnumber = fnumber;
874
875         /* Get iso value from exif. */
876         if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE)
877                 content_info->media_meta.iso = iso;
878
879         memset(buf, 0x00, sizeof(buf));
880         /* Get model value from exif. */
881         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
882                 if (strlen(buf) > 0)
883                         content_info->media_meta.model = g_strdup(buf);
884         }
885
886         /* Get orientation value from exif. */
887         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
888                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
889                         content_info->media_meta.orientation = orient_value;
890         }
891
892         /* Get width value from exif. */
893         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
894                 if (exif_width > 0)
895                         content_info->media_meta.width = exif_width;
896         }
897
898         /* Get height value from exif. */
899         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
900                 if (exif_height > 0)
901                         content_info->media_meta.height = exif_height;
902         }
903
904         if (ed)
905                 exif_data_unref(ed);
906
907 GET_WIDTH_HEIGHT:
908
909         if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
910                 /*Get image width, height*/
911                 unsigned int img_width = 0;
912                 unsigned int img_height = 0;
913                 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
914
915                 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
916                 if (content_info->media_meta.width == 0)
917                         content_info->media_meta.width = img_width;
918
919                 if (content_info->media_meta.height == 0)
920                         content_info->media_meta.height = img_height;
921         }
922
923         return MS_MEDIA_ERR_NONE;
924 }
925
926 static char * __media_svc_get_tag_str_value(MMHandleType tag, const char *tag_name)
927 {
928         int ret = FILEINFO_ERROR_NONE;
929         char *p = NULL;
930         int size = 0;
931
932         ret = mm_file_get_attrs(tag, tag_name, &p, &size, NULL);
933         if (ret == FILEINFO_ERROR_NONE && size > 0)
934                 return g_strdup(p);
935
936         return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
937 }
938
939 int _media_svc_extract_music_metadata_for_update(media_svc_content_info_s *content_info, const char *path)
940 {
941         MMHandleType tag = 0;
942         int mmf_error = FILEINFO_ERROR_NONE;
943
944         content_info->path = g_strdup(path);
945
946         mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
947         if (mmf_error == FILEINFO_ERROR_NONE) {
948                 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
949                 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
950                 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
951                 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
952                 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
953                 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
954                 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
955                 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
956         } else {
957                 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
958         }
959
960         mm_file_destroy_tag_attrs(tag);
961
962         return MS_MEDIA_ERR_NONE;
963 }
964
965 int _media_svc_extract_media_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
966 {
967         MMHandleType content = 0;
968         MMHandleType tag = 0;
969         char *p = NULL;
970         unsigned char *image = NULL;
971         unsigned int size = 0;
972         int mmf_error = FILEINFO_ERROR_NONE;
973         int album_id = 0;
974         int ret = MS_MEDIA_ERR_NONE;
975         int convert_value = 0;
976         int cdis_value = 0;
977
978         /*Get Content Tag attribute ===========*/
979         if (content_info->storage_type == MS_USER_STORAGE_EXTERNAL_USB)
980                 mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
981         else
982                 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
983
984         if (mmf_error == FILEINFO_ERROR_NONE) {
985                 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
986                 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
987                 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
988                 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
989                 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
990                 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
991                 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
992                 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
993                 content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
994
995                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
996                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
997                         if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
998                                 /*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*/
999                                 char *p_value = g_strdelimit(g_strdup(p), "-", ':');
1000                                 content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
1001                                 g_free(p_value);
1002                         } else {
1003                                 content_info->media_meta.recorded_date = g_strdup(p);
1004                         }
1005
1006                         if (STRING_VALID(content_info->media_meta.recorded_date)) {
1007                                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1008                                 if (content_info->timeline == 0)
1009                                         content_info->timeline = content_info->modified_time;
1010
1011                                 /* This is same as datetaken */
1012                                 /* Remove compensation string */
1013                                 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
1014                                         content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
1015                                         g_free(content_info->media_meta.recorded_date);
1016                                         content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
1017                                 } else {
1018                                         content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
1019                                 }
1020                         }
1021                 }
1022
1023                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
1024                 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
1025                         if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1026                                 content_info->media_meta.year = g_strdup(p);
1027                 }
1028
1029                 if (!content_info->media_meta.year)
1030                                 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1031
1032                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RATING, &p, &size, NULL);
1033                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1034                         if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1035                                 content_info->media_meta.rating = convert_value;
1036                 } else {
1037                         content_info->media_meta.rating = 0;
1038                 }
1039
1040                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1041
1042                 /*Do not extract artwork for the USB Storage content*/
1043                 if (content_info->storage_type != MS_USER_STORAGE_EXTERNAL_USB) {
1044                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1045                         if (mmf_error != FILEINFO_ERROR_NONE)
1046                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1047
1048                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1049                         if (mmf_error != FILEINFO_ERROR_NONE)
1050                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1051
1052                         if (image != NULL && size > 0) {
1053                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1054                                 int artwork_mime_size = -1;
1055
1056                                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1057                                 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1058                                         ret = _media_svc_get_thumbnail_path(thumb_path, content_info->path, p, uid);
1059                                         if (ret != MS_MEDIA_ERR_NONE) {
1060                                                 media_svc_error("Fail to Get Thumbnail Path");
1061                                         } else {
1062                                                 ret = __media_svc_save_image(image, size, thumb_path, uid);
1063                                                 if (ret != MS_MEDIA_ERR_NONE) {
1064                                                         media_svc_error("Fail to Save Image");
1065                                                 } else {
1066                                                         content_info->thumbnail_path = g_strdup(thumb_path);
1067                                                 }
1068                                         }
1069                                 }
1070                         }
1071                 }
1072
1073                 /*Initialize album_id to 0. below code will set the album_id*/
1074                 content_info->album_id = album_id;
1075                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1076                 if (ret != MS_MEDIA_ERR_NONE) {
1077                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1078                                 media_svc_debug("album does not exist. So start to make album art");
1079                                 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1080                                         (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1081                                         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);
1082                                 else
1083                                         ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1084
1085                                 content_info->album_id = album_id;
1086                         }
1087                 } else {
1088                         content_info->album_id = album_id;
1089                 }
1090
1091                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1092                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1093                 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1094
1095                 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1096                         double longitude = 0.0;
1097                         double latitude = 0.0;
1098                         double altitude = 0.0;
1099
1100                         mm_file_get_attrs(tag, MM_FILE_TAG_LONGITUDE, &longitude,
1101                                 MM_FILE_TAG_LATIDUE, &latitude,
1102                                 MM_FILE_TAG_ALTIDUE, &altitude,
1103                                 NULL);
1104
1105                         content_info->media_meta.longitude = (longitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : longitude;
1106                         content_info->media_meta.latitude = (latitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : latitude;
1107                         content_info->media_meta.altitude = (altitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : altitude;
1108
1109                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1110                         if (mmf_error != FILEINFO_ERROR_NONE)
1111                                 cdis_value = 0;
1112
1113                         media_svc_debug("CDIS : %d", cdis_value);
1114
1115                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1116                         if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1117                                 content_info->media_meta.orientation = atoi(p);
1118                         } else {
1119                                 content_info->media_meta.orientation = 0;
1120                                 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1121                         }
1122                 }
1123
1124                 mmf_error = mm_file_destroy_tag_attrs(tag);
1125                 if (mmf_error != FILEINFO_ERROR_NONE)
1126                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1127         }       else {
1128                 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1129                 content_info->album_id = album_id;
1130         }
1131
1132         /*Get Content attribute ===========*/
1133         if (cdis_value == 1)
1134                 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1135         else
1136                 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1137
1138         media_svc_retvm_if(mmf_error != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_NONE, "mm_file_create_content_attrs failed");
1139
1140         if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1141                 int audio_bitrate = 0;
1142                 int video_bitrate = 0;
1143
1144                 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1145                         MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate,
1146                         MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate,
1147                         MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width,
1148                         MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height,
1149                         NULL);
1150
1151                 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1152         } else {
1153                 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1154                         MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate,
1155                         MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate,
1156                         MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel,
1157                         MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample,
1158                         NULL);
1159         }
1160
1161         mm_file_destroy_content_attrs(content);
1162
1163         return MS_MEDIA_ERR_NONE;
1164 }
1165
1166 static gchar * __media_svc_get_zipfile_data(zip_t *z, const char *fname)
1167 {
1168         int err = 0;
1169         zip_int64_t index_num = 0;
1170         zip_file_t *file = NULL;
1171         zip_stat_t sb = {0, };
1172         gchar *buf = NULL;
1173
1174         media_svc_retvm_if(!z, NULL, "z is NULL");
1175         media_svc_retvm_if(!fname, NULL, "fname is NULL");
1176
1177         index_num = zip_name_locate(z, fname, ZIP_FL_NOCASE);
1178         media_svc_retvm_if(index_num == -1, NULL, "fname is not exists [%s]", fname);
1179
1180         err = zip_stat_index(z, index_num, ZIP_STAT_SIZE, &sb);
1181         media_svc_retvm_if(err == -1, NULL, "zip_stat_index failed");
1182
1183         file = zip_fopen_index(z, index_num, ZIP_FL_UNCHANGED);
1184         media_svc_retvm_if(!file, NULL, "zip_fopen_index failed");
1185
1186         buf = g_malloc0(sb.size);
1187
1188         err = zip_fread(file, buf, sb.size);
1189         zip_fclose(file);
1190
1191         if (err == -1) {
1192                 g_free(buf);
1193                 buf = NULL;
1194         }
1195
1196         return buf;
1197 }
1198
1199 static xmlNodePtr __media_svc_find_node(xmlNodePtr node, const char *key)
1200 {
1201         xmlNodePtr tmp = NULL;
1202
1203         media_svc_retvm_if(!node, NULL, "node is NULL");
1204         media_svc_retvm_if(!key, NULL, "key is NULL");
1205
1206         for (tmp = node->children; tmp; tmp = tmp->next) {
1207                 if (xmlIsBlankNode(tmp))
1208                         continue;
1209
1210                 if (g_str_has_suffix((gchar *)tmp->name, key))
1211                         return tmp;
1212         }
1213
1214         return NULL;
1215 }
1216
1217 static char * __media_svc_remove_escape_c(const char *value)
1218 {
1219         int start = -1;
1220         int end = 0;
1221         int len, i;
1222
1223         media_svc_retv_if(!value, NULL);
1224
1225         len = strlen(value);
1226
1227         for (i = 0; i < len; i++) {
1228                 if (value[i] != 10 && value[i] != 32) { // 10='\n' 32=' '
1229                         if (start == -1)
1230                                 start = i;
1231
1232                         end = i;
1233                 }
1234         }
1235
1236         end = end - start + 1;
1237
1238         return g_strndup(value + start, end);
1239 }
1240
1241 static char * __media_svc_find_and_get_value(xmlNodePtr node, const char *key)
1242 {
1243         xmlNodePtr tmp = NULL;
1244         char *tmp_res = NULL;
1245         char *res = NULL;
1246
1247         media_svc_retvm_if(!node, NULL, "node is NULL");
1248         media_svc_retvm_if(!key, NULL, "key is NULL");
1249
1250         for (tmp = node->children; tmp; tmp = tmp->next) {
1251                 if (xmlIsBlankNode(tmp))
1252                         continue;
1253
1254                 if (tmp->children) {
1255                         tmp_res = __media_svc_find_and_get_value(tmp, key);
1256                         if (tmp_res) {
1257                                 res = __media_svc_remove_escape_c(tmp_res);
1258                                 xmlFree(tmp_res);
1259                                 return res;
1260                         }
1261                 }
1262
1263                 if (g_str_has_suffix((gchar *)tmp->name, key))
1264                         return (char *)xmlNodeGetContent(tmp);
1265         }
1266
1267         return NULL;
1268 }
1269
1270 static gboolean __media_svc_get_epub_root_file(zip_t *z, char **opf_file)
1271 {
1272         gchar *buf = NULL;
1273         xmlDocPtr doc = NULL;
1274         xmlNodePtr node = NULL;
1275
1276         media_svc_retvm_if(!z, FALSE, "z is NULL");
1277         media_svc_retvm_if(!opf_file, FALSE, "opf_file is NULL");
1278
1279         buf = __media_svc_get_zipfile_data(z, "META-INF/container.xml");
1280         media_svc_retvm_if(!buf, FALSE, "buf is NULL");
1281
1282         doc = xmlParseDoc((const xmlChar *)buf);
1283         g_free(buf);
1284         media_svc_retvm_if(!doc, FALSE, "doc is NULL");
1285
1286         node = xmlDocGetRootElement(doc);
1287         node = __media_svc_find_node(node, "rootfiles");
1288         node = __media_svc_find_node(node, "rootfile");
1289
1290         *opf_file = (char *)xmlGetProp(node, (const xmlChar *)"full-path");
1291         media_svc_sec_debug("OPF [%s]", *opf_file);
1292         xmlFreeDoc(doc);
1293
1294         return TRUE;
1295 }
1296
1297 static gboolean __media_svc_get_xml_metadata(const xmlChar *buffer, gboolean is_pdf, media_svc_content_info_s *content_info)
1298 {
1299         xmlDocPtr doc = NULL;
1300         xmlNodePtr root = NULL;
1301
1302         media_svc_retvm_if(!buffer, FALSE, "buffer is NULL");
1303         media_svc_retvm_if(!content_info, FALSE, "content_info is NULL");
1304
1305         doc = xmlParseDoc(buffer);
1306         media_svc_retv_if(!doc, FALSE);
1307
1308         root = xmlDocGetRootElement(doc);
1309         if (!root) {
1310                 xmlFreeDoc(doc);
1311                 return FALSE;
1312         }
1313
1314         content_info->media_meta.title = __media_svc_find_and_get_value(root, "title");
1315         if (is_pdf && !content_info->media_meta.title) {
1316                 xmlFreeDoc(doc);
1317                 return FALSE;
1318         }
1319
1320         content_info->media_meta.composer = __media_svc_find_and_get_value(root, "creator");
1321         if (!content_info->media_meta.composer)
1322                 content_info->media_meta.composer = __media_svc_find_and_get_value(root, "author");
1323
1324         content_info->media_meta.copyright = __media_svc_find_and_get_value(root, "publisher");
1325         content_info->media_meta.recorded_date = __media_svc_find_and_get_value(root, "date");
1326         content_info->media_meta.genre = __media_svc_find_and_get_value(root, "subject");
1327         content_info->media_meta.description = __media_svc_find_and_get_value(root, "description");
1328
1329         xmlFreeDoc(doc);
1330
1331         return TRUE;
1332 }
1333
1334 static int __media_svc_get_epub_metadata(media_svc_content_info_s *content_info)
1335 {
1336         int err = 0;
1337         zip_t *z = NULL;
1338         gchar *buf = NULL;
1339         char *opf_path = NULL;
1340
1341         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1342
1343         //1. open epub
1344         z = zip_open(content_info->path, ZIP_RDONLY, &err);
1345         media_svc_retvm_if(err == -1, MS_MEDIA_ERR_INTERNAL, "zip_open failed");
1346
1347         //2. find and read opf file
1348         if (!__media_svc_get_epub_root_file(z, &opf_path)) {
1349                 media_svc_error("__media_svc_get_epub_root_file failed");
1350                 zip_close(z);
1351                 return MS_MEDIA_ERR_INTERNAL;
1352         }
1353
1354         //3. get metadata
1355         buf = __media_svc_get_zipfile_data(z, opf_path);
1356         xmlFree(opf_path);
1357         zip_close(z);
1358         media_svc_retvm_if(!buf, MS_MEDIA_ERR_INTERNAL, "__media_svc_get_zipfile_data failed");
1359
1360         if (!__media_svc_get_xml_metadata((const xmlChar *)buf, FALSE, content_info))
1361                 media_svc_error("__media_svc_get_xml_metadata failed");
1362
1363         g_free(buf);
1364
1365         return MS_MEDIA_ERR_NONE;
1366 }
1367
1368 static int __media_svc_get_pdf_metadata(media_svc_content_info_s *content_info)
1369 {
1370     int fd = 0;
1371     int start_pos = 0;
1372     int end_pos = 0;
1373     int cur_pos = 0;
1374     int search_limit = 0;
1375     char tmp[MEDIA_SVC_PDF_BUF_SIZE + 1] = {0, };
1376     gchar *meta_buf = NULL;
1377     char *found = NULL;
1378
1379         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1380         media_svc_retvm_if(content_info->size < 256, MS_MEDIA_ERR_INTERNAL, "open failed");
1381
1382         fd = open(content_info->path, O_RDONLY);
1383         media_svc_retvm_if(fd < 0, MS_MEDIA_ERR_INTERNAL, "open failed");
1384
1385         search_limit = content_info->size - MEDIA_SVC_PDF_TAG_TAIL_LEN;
1386
1387         while (cur_pos <= search_limit) {
1388                 lseek(fd, cur_pos, SEEK_SET);
1389
1390                 if (read(fd, &tmp, MEDIA_SVC_PDF_BUF_SIZE) != MEDIA_SVC_PDF_BUF_SIZE) {
1391                         media_svc_error("read failed");
1392                         break;
1393                 }
1394
1395                 //1.Find <x:xmpmeta .. </x:xmpmeta> block
1396                 if (start_pos == 0 && (found = strstr(tmp, "<x:xmpmeta"))) {
1397                         start_pos = cur_pos + (found - tmp);
1398 //                      media_svc_error("FIND START_POS[%d]", start_pos);
1399                         found = NULL;
1400                 }
1401
1402
1403                 if (start_pos != 0 && (found = strstr(tmp, "</x:xmpmeta>"))) {
1404                         end_pos = cur_pos + (found - tmp) + MEDIA_SVC_PDF_TAG_TAIL_LEN;
1405 //                      media_svc_error("FIND END_POS[%d]", end_pos);
1406                         found = NULL;
1407                 }
1408
1409                 //2.get metadata using xml parser
1410                 if (start_pos && end_pos) {
1411                         meta_buf = g_malloc0(end_pos - start_pos + 1);
1412
1413                         lseek(fd, start_pos, SEEK_SET);
1414                         if (read(fd, meta_buf, end_pos - start_pos) != end_pos - start_pos)
1415                                 goto NEXT;
1416
1417                         if (__media_svc_get_xml_metadata((const xmlChar *)meta_buf, TRUE, content_info)) {
1418                                 g_free(meta_buf);
1419                                 break;
1420                         }
1421
1422                         g_free(meta_buf);
1423                         meta_buf = NULL;
1424 NEXT:
1425                         start_pos = 0;
1426                         end_pos = 0;
1427                 }
1428
1429                 cur_pos += 240;
1430
1431         }
1432
1433         close(fd);
1434
1435         return MS_MEDIA_ERR_NONE;
1436 }
1437
1438 int _media_svc_extract_book_metadata(media_svc_content_info_s *content_info)
1439 {
1440         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content info is NULL");
1441
1442         if (g_str_has_suffix(content_info->mime_type, "epub+zip"))
1443                 return __media_svc_get_epub_metadata(content_info);
1444         else
1445                 return __media_svc_get_pdf_metadata(content_info);
1446 }
1447
1448 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1449 {
1450         media_svc_retm_if(!content_info, "content info is NULL");
1451
1452         /* Delete media_svc_content_info_s */
1453         g_free(content_info->media_uuid);
1454         g_free(content_info->path);
1455         g_free(content_info->file_name);
1456         g_free(content_info->mime_type);
1457         g_free(content_info->folder_uuid);
1458         g_free(content_info->thumbnail_path);
1459         g_free(content_info->storage_uuid);
1460
1461         /* Delete media_svc_content_meta_s */
1462         g_free(content_info->media_meta.title);
1463         g_free(content_info->media_meta.album);
1464         g_free(content_info->media_meta.artist);
1465         g_free(content_info->media_meta.album_artist);
1466         g_free(content_info->media_meta.genre);
1467         g_free(content_info->media_meta.composer);
1468         g_free(content_info->media_meta.year);
1469         g_free(content_info->media_meta.recorded_date);
1470         g_free(content_info->media_meta.copyright);
1471         g_free(content_info->media_meta.track_num);
1472         g_free(content_info->media_meta.description);
1473         g_free(content_info->media_meta.datetaken);
1474         g_free(content_info->media_meta.exposure_time);
1475         g_free(content_info->media_meta.model);
1476
1477         g_free(content_info->file_name_pinyin);
1478         g_free(content_info->media_meta.title_pinyin);
1479         g_free(content_info->media_meta.album_pinyin);
1480         g_free(content_info->media_meta.artist_pinyin);
1481         g_free(content_info->media_meta.album_artist_pinyin);
1482         g_free(content_info->media_meta.genre_pinyin);
1483         g_free(content_info->media_meta.composer_pinyin);
1484         g_free(content_info->media_meta.copyright_pinyin);
1485         g_free(content_info->media_meta.description_pinyin);
1486 }
1487
1488 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
1489 {
1490         int ret = MS_MEDIA_ERR_NONE;
1491
1492         media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
1493         media_svc_retvm_if(!thumb_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1494         media_svc_retvm_if(!g_file_test(path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "File doesn't exist[%s]", path);
1495
1496         ms_user_storage_type_e store_type = -1;
1497         ret = ms_user_get_storage_type(uid, path, &store_type);
1498
1499         if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
1500                 media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
1501                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1502         }
1503
1504         media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1505
1506         //1. make hash path
1507         ret = _media_svc_get_thumbnail_path(thumb_path, path, NULL, uid);
1508         if (ret != MS_MEDIA_ERR_NONE) {
1509                 media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
1510                 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1511                 return ret;
1512         }
1513
1514         //2. save thumbnail
1515         if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1516                 return create_image_thumbnail_to_file(path, CONTENT_THUMB_DEFAULT_WIDTH, CONTENT_THUMB_DEFAULT_HEIGHT, thumb_path, true);
1517         else
1518                 return create_video_thumbnail_to_file(path, CONTENT_THUMB_DEFAULT_WIDTH, CONTENT_THUMB_DEFAULT_HEIGHT, thumb_path, true);
1519
1520 }
1521
1522 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1523 {
1524         int ret = MS_MEDIA_ERR_NONE;
1525         int size = 0;
1526         pinyin_name_s *pinyinname = NULL;
1527
1528         media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1529         media_svc_retvm_if(pinyin_str == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "pinyin_str is NULL");
1530
1531         *pinyin_str = NULL;
1532
1533         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1534         if (ret == MS_MEDIA_ERR_NONE) {
1535                 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1536                         *pinyin_str = g_strdup(pinyinname[0].pinyin_name);
1537                 else
1538                         *pinyin_str = g_strdup(src_str);        /* Return Original Non China Character */
1539         }
1540
1541         _media_svc_pinyin_free(pinyinname, size);
1542
1543         return ret;
1544 }
1545
1546 bool _media_svc_check_pinyin_support(void)
1547 {
1548         int ret = SYSTEM_INFO_ERROR_NONE;
1549         bool is_supported = false;
1550         static int media_svc_pinyin_support = -1;
1551
1552         if (media_svc_pinyin_support == -1) {
1553                 ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
1554                 if (ret != SYSTEM_INFO_ERROR_NONE) {
1555                         media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
1556                         return false;
1557                 }
1558
1559                 media_svc_pinyin_support = is_supported;
1560         }
1561
1562         return media_svc_pinyin_support;
1563 }
1564
1565 int _media_svc_get_media_type(const char *path, int *mediatype)
1566 {
1567         int ret = MS_MEDIA_ERR_NONE;
1568         char mime_type[256] = {0};
1569         media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1570
1571         media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1572
1573         ret = __media_svc_get_mime_type(path, mime_type);
1574         if (ret == MS_MEDIA_ERR_NONE)
1575                 __media_svc_get_media_type(path, mime_type, &media_type);
1576         else
1577                 media_svc_error("__media_svc_get_mime_type failed");
1578
1579         *mediatype = media_type;
1580
1581         return ret;
1582 }
1583
1584 bool _media_svc_is_valid_storage_type(ms_user_storage_type_e storage_type)
1585 {
1586         switch (storage_type) {
1587         case MS_USER_STORAGE_INTERNAL:
1588         case MS_USER_STORAGE_EXTERNAL:
1589         case MS_USER_STORAGE_EXTERNAL_USB:
1590                 return true;
1591         default:
1592                 media_svc_error("storage type is incorrect[%d]", storage_type);
1593                 return false;
1594         }
1595 }