4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
29 #include <libexif/exif-data.h>
30 #include <uuid/uuid.h>
31 #include <media-thumbnail.h>
32 #include <media-util-user.h>
33 #include "media-svc-util.h"
34 #include "media-svc-db-utils.h"
35 #include "media-svc-debug.h"
36 #include "media-svc-env.h"
37 #include "media-svc-album.h"
38 /*For ebook metadata */
40 #include <libxml/xmlmemory.h>
41 #include <libxml/parser.h>
42 #include <libxml/HTMLparser.h>
45 #define MEDIA_SVC_FILE_EXT_LEN_MAX 6
47 #define MUSIC_MIME_NUM 29
48 #define SOUND_MIME_NUM 2
49 #define MIME_LENGTH 50
50 #define IMAGE_PREFIX "image/"
51 #define AUDIO_PREFIX "audio/"
52 #define VIDEO_PREFIX "video/"
55 #define MEDIA_SVC_PDF_TAG_TAIL_LEN 12
56 #define MEDIA_SVC_PDF_BUF_SIZE 256
58 #define MEDIA_SVC_THUMB_WIDTH 320
59 #define MEDIA_SVC_THUMB_HEIGHT 240
61 #define PATH_PLUGIN_LIB PATH_LIBDIR"/libmedia-ebook-plugin.so"
63 enum Exif_Orientation {
75 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
76 /*known mime types of normal files*/
83 "x-mp3", /*alias of audio/mpeg*/
84 "x-mpeg", /*alias of audio/mpeg*/
86 "x-ogg", /*alias of audio/ogg*/
87 "vnd.ms-playready.media.pya:*.pya", /*playready*/
90 "x-m4a", /*alias of audio/mp4*/
91 /* below mimes are rare*/
100 "x-vorbis", /*alias of audio/x-vorbis+ogg*/
101 "vorbis", /*alias of audio/x-vorbis+ogg*/
103 "x-mp2", /*alias of audio/mp2*/
104 "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
105 "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
109 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
110 "application/x-smaf",
114 static char *__media_info_generate_uuid(void)
117 char uuid_unparsed[37];
120 uuid_generate(uuid_value);
121 uuid_unparse(uuid_value, uuid_unparsed);
123 if (strlen(uuid_unparsed) < 36) {
124 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
128 return g_strdup(uuid_unparsed);
131 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, ExifTag tagtype)
134 ExifByteOrder mByteOrder;
136 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
138 entry = exif_data_get_entry(ed, tagtype);
139 media_svc_retv_if(!entry, MS_MEDIA_ERR_NONE);
142 case EXIF_TAG_ORIENTATION:
143 case EXIF_TAG_PIXEL_X_DIMENSION:
144 case EXIF_TAG_PIXEL_Y_DIMENSION:
145 media_svc_retvm_if(!i_value, MS_MEDIA_ERR_INVALID_PARAMETER, "i_value is NULL");
147 mByteOrder = exif_data_get_byte_order(ed);
148 short exif_value = exif_get_short(entry->data, mByteOrder);
149 *i_value = (int)exif_value;
152 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
154 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
155 buf[strlen(buf)] = '\0';
158 return MS_MEDIA_ERR_NONE;
161 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
167 media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "path is null");
168 media_svc_retvm_if(!mime_type, MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is null");
169 media_svc_retvm_if(!media_type, MS_MEDIA_ERR_INVALID_PARAMETER, "media_type is null");
172 if (strncmp(mime_type, IMAGE_PREFIX, PREFIX_LEN) == 0) {
173 *media_type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
174 return MS_MEDIA_ERR_NONE;
178 if (strncmp(mime_type, AUDIO_PREFIX, PREFIX_LEN) == 0) {
179 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
181 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
182 if (strcmp(mime_type + PREFIX_LEN, music_mime_table[idx]) == 0) {
183 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
188 /* audio/x-mpegurl : .m3u file (playlist file) */
189 if (strcmp(mime_type + PREFIX_LEN, "x-mpegurl") == 0)
190 *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
192 return MS_MEDIA_ERR_NONE;
196 if (strncmp(mime_type, VIDEO_PREFIX, PREFIX_LEN) == 0) {
197 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
199 /*some video files don't have video stream. in this case it is categorize as music. */
200 if (strcmp(mime_type + PREFIX_LEN, "3gpp") == 0 ||
201 strcmp(mime_type + PREFIX_LEN, "mp4") == 0) {
202 if (mm_file_get_stream_info(path, &audio, &video) == FILEINFO_ERROR_NONE) {
203 if (audio > 0 && video == 0)
204 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
208 return MS_MEDIA_ERR_NONE;
212 *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
214 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
215 if (strcmp(mime_type, sound_mime_table[idx]) == 0) {
216 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
217 return MS_MEDIA_ERR_NONE;
221 /*"asf" must check video stream and then categorize in directly. */
222 if (strcmp(mime_type, "application/vnd.ms-asf") == 0) {
223 if (mm_file_get_stream_info(path, &audio, &video) == FILEINFO_ERROR_NONE) {
224 if (audio > 0 && video == 0)
225 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
227 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
230 return MS_MEDIA_ERR_NONE;
233 if (strcmp(mime_type, "application/epub+zip") == 0 || strcmp(mime_type, "application/pdf") == 0)
234 *media_type = MEDIA_SVC_MEDIA_TYPE_BOOK;
236 return MS_MEDIA_ERR_NONE;
239 static int __media_svc_get_mime_type(const char *path, char *mimetype)
241 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
243 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
244 media_svc_error("aul_get_mime_from_file fail");
245 return MS_MEDIA_ERR_INTERNAL;
248 return MS_MEDIA_ERR_NONE;
251 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
255 for (i = strlen(file_path); i >= 0; i--) {
256 if (file_path[i] == '.') {
257 g_strlcpy(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
261 if (file_path[i] == '/')
267 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
269 int ret = MS_MEDIA_ERR_NONE;
271 char *thumb_path = NULL;
272 long bsize_kbytes = 0;
273 GError *error = NULL;
275 media_svc_retvm_if(!image || size == 0, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid image");
276 media_svc_sec_debug("start save image, path [%s] image size [%d]", image_path, size);
278 ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
279 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path error");
281 ret = statfs(thumb_path, &fs);
283 media_svc_retvm_if(ret == -1, MS_MEDIA_ERR_INTERNAL, "statfs failed");
285 bsize_kbytes = fs.f_bsize >> 10;
286 media_svc_retvm_if((bsize_kbytes * fs.f_bavail) < 1024, MS_MEDIA_ERR_NOT_ENOUGH_SPACE, "Not enough space");
288 if (!g_file_set_contents(image_path, (const gchar *)image, (gssize)size, &error)) {
289 media_svc_error("g_file_set_contents faild:%s", error->message);
291 return MS_MEDIA_ERR_INTERNAL;
294 return MS_MEDIA_ERR_NONE;
297 static char *__media_svc_get_title_from_filename(const char *filename)
300 char *last_dot = NULL;
302 media_svc_retvm_if(!STRING_VALID(filename), g_strdup(MEDIA_SVC_TAG_UNKNOWN), "Invalid path");
304 last_dot = strrchr(filename, '.');
306 title = g_strndup(filename, last_dot - filename);
308 title = g_strdup(filename);
311 media_svc_debug("extract title is [%s]", title);
316 void _media_svc_remove_file(const char *path)
318 if (!STRING_VALID(path))
321 if (remove(path) != 0)
322 media_svc_stderror("fail to remove file result");
325 static int __media_svc_get_thumbnail_path(char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
327 int ret = MS_MEDIA_ERR_NONE;
328 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
329 g_autofree gchar *hash = NULL;
330 g_autofree gchar *thumb_dir = NULL;
332 ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
333 media_svc_retvm_if(!STRING_VALID(thumb_dir), ret, "ms_user_get_root_thumb_store_path failed");
334 media_svc_retvm_if(!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR), MS_MEDIA_ERR_INTERNAL, "Not a directory");
336 memset(file_ext, 0, sizeof(file_ext));
337 if (!__media_svc_get_file_ext(pathname, file_ext))
338 media_svc_error("get file ext fail");
340 hash = g_compute_checksum_for_string(G_CHECKSUM_MD5, pathname, -1);
341 media_svc_retvm_if(!hash, MS_MEDIA_ERR_INTERNAL, "Failed to create hashname");
344 /* 'img_format' is mime-type */
345 if (!g_str_has_prefix(img_format, IMAGE_PREFIX) || strlen(img_format) == PREFIX_LEN) {
346 media_svc_error("Not proper img format");
347 return MS_MEDIA_ERR_INTERNAL;
350 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, img_format + PREFIX_LEN);
352 if (strcasecmp(file_ext, "PNG") == 0)
353 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
355 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
358 return MS_MEDIA_ERR_NONE;
361 int _media_svc_get_file_time(const char *full_path)
363 struct stat statbuf = { 0, };
365 if (stat(full_path, &statbuf) == -1) {
366 media_svc_stderror("stat fails.");
370 return statbuf.st_mtime;
373 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, const char *path, bool refresh)
375 int ret = MS_MEDIA_ERR_NONE;
376 char mime_type[256] = {0, };
377 media_svc_media_type_e media_type;
379 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
381 content_info->path = g_strdup(path);
382 media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
385 memset(&st, 0, sizeof(struct stat));
386 if (stat(path, &st) == 0) {
387 content_info->modified_time = st.st_mtime;
388 content_info->timeline = content_info->modified_time;
389 content_info->size = st.st_size;
391 media_svc_stderror("stat failed");
394 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
396 media_svc_debug("refresh");
397 return MS_MEDIA_ERR_NONE;
400 content_info->storage_uuid = g_strdup(storage_id);
401 media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
403 time(&content_info->added_time);
405 content_info->media_uuid = __media_info_generate_uuid();
406 media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
408 content_info->file_name = g_path_get_basename(path);
409 media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
411 ret = __media_svc_get_mime_type(path, mime_type);
412 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
414 media_svc_debug("mime [%s]", mime_type);
416 ret = __media_svc_get_media_type(path, mime_type, &media_type);
417 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
419 content_info->mime_type = g_strdup(mime_type);
420 media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
422 media_svc_sec_debug("path[%s], media_type[%d]", path, media_type);
424 content_info->media_type = media_type;
426 return MS_MEDIA_ERR_NONE;
429 static char * __media_svc_get_title(MMHandleType tag, const char *filename)
431 int ret = FILEINFO_ERROR_NONE;
436 ret = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
437 if (ret == FILEINFO_ERROR_NONE && size > 0) {
438 while(p && isspace(*p))
445 return __media_svc_get_title_from_filename(filename);
448 char * _media_svc_get_title_from_filename(const char *filename)
450 return __media_svc_get_title_from_filename(filename);
453 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
455 int orient_value = 0;
459 bool has_datetaken = false;
462 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
464 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
465 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);
466 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
468 path = content_info->path;
469 content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
471 /* Not used. But to preserved the behavior, set MEDIA_SVC_TAG_UNKNOWN. */
472 content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
473 content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
474 content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
475 content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
476 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
477 content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
479 /* Load an ExifData object from an EXIF file */
480 ed = exif_data_new_from_file(path);
482 media_svc_sec_debug("There is no exif data in [ %s ]", path);
483 goto GET_WIDTH_HEIGHT;
486 memset(buf, 0x00, sizeof(buf));
487 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
488 if (strlen(buf) > 0) {
489 has_datetaken = true;
490 content_info->media_meta.datetaken = g_strdup(buf);
492 /* This is same as recorded_date */
493 content_info->media_meta.recorded_date = g_strdup(buf);
497 memset(buf, 0x00, sizeof(buf));
498 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
499 if (strlen(buf) > 0) {
500 has_datetaken = true;
501 content_info->media_meta.datetaken = g_strdup(buf);
503 /* This is same as recorded_date */
504 content_info->media_meta.recorded_date = g_strdup(buf);
508 if (content_info->media_meta.recorded_date == NULL)
509 content_info->media_meta.recorded_date = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
511 /* Get orientation value from exif. */
512 if (__media_svc_get_exif_info(ed, NULL, &orient_value, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
513 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
514 content_info->media_meta.orientation = orient_value;
517 /* Get width value from exif. */
518 if (__media_svc_get_exif_info(ed, NULL, &exif_width, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
520 content_info->media_meta.width = exif_width;
523 /* Get height value from exif. */
524 if (__media_svc_get_exif_info(ed, NULL, &exif_height, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
526 content_info->media_meta.height = exif_height;
533 if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
534 /*Get image width, height*/
535 unsigned int img_width = 0;
536 unsigned int img_height = 0;
538 if (get_image_info(path, &img_width, &img_height) != THUMB_OK)
539 return MS_MEDIA_ERR_NONE;
541 if (content_info->media_meta.width == 0)
542 content_info->media_meta.width = img_width;
544 if (content_info->media_meta.height == 0)
545 content_info->media_meta.height = img_height;
548 return MS_MEDIA_ERR_NONE;
551 static char * __media_svc_get_tag_str_value(MMHandleType tag, const char *tag_name)
553 int ret = FILEINFO_ERROR_NONE;
557 ret = mm_file_get_attrs(tag, tag_name, &p, &size, NULL);
558 if (ret == FILEINFO_ERROR_NONE && size > 0)
561 return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
564 static char * __media_svc_extract_albumart(MMHandleType tag, const char *path, uid_t uid)
566 int ret = FILEINFO_ERROR_NONE;
567 unsigned char *image = NULL;
568 unsigned int size = 0;
569 char *mimetype = NULL;
570 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = { 0, };
571 unsigned int mime_size = 0;
573 ret = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
574 media_svc_retvm_if(ret != FILEINFO_ERROR_NONE, NULL, "Failed to get tag artwork[%d]", ret);
575 media_svc_retvm_if(!image || size == 0, NULL, "Invalid artwork");
577 ret = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &mimetype, &mime_size, NULL);
578 media_svc_retvm_if(ret != FILEINFO_ERROR_NONE, NULL, "Failed to get tag mime[%d]", ret);
579 media_svc_retvm_if(mime_size == 0, NULL, "Invalid mimetype");
581 ret = __media_svc_get_thumbnail_path(thumb_path, path, mimetype, uid);
582 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, NULL, "Failed to get thumbnail path");
584 ret = __media_svc_save_image(image, size, thumb_path, uid);
585 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, NULL, "Fail to save thumbnail");
587 return g_strdup(thumb_path);
590 void _media_svc_extract_audio_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
592 MMHandleType tag = 0;
594 unsigned int size = 0;
595 int mmf_error = FILEINFO_ERROR_NONE;
597 int ret = MS_MEDIA_ERR_NONE;
598 bool support_albumart = ms_user_thumb_support(uid, content_info->path);
600 /*Get Content Tag attribute ===========*/
601 if (support_albumart)
602 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
604 mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
606 if (mmf_error != FILEINFO_ERROR_NONE) {
607 content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
608 content_info->album_id = 0;
612 content_info->media_meta.title = __media_svc_get_title(tag, content_info->file_name);
613 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
614 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
615 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
616 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
617 content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
618 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
620 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
621 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
622 if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
623 /*Creation time format is 20130101 00:00:00 +0000. change it to 2013:01:01 00:00:00 +0000 like exif time format*/
624 char *p_value = g_strdelimit(g_strdup(p), "-", ':');
625 content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
628 content_info->media_meta.recorded_date = g_strdup(p);
632 if (content_info->media_meta.recorded_date == NULL)
633 content_info->media_meta.recorded_date = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
635 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
636 if (mmf_error == FILEINFO_ERROR_NONE && size == 4)
637 content_info->media_meta.year = g_strdup(p);
639 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
641 /*Do not extract artwork for the USB Storage content*/
642 if (support_albumart)
643 content_info->thumbnail_path = __media_svc_extract_albumart(tag, content_info->path, uid);
645 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
646 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
647 media_svc_debug("album does not exist. So start to make album art");
648 if (strlen(content_info->media_meta.album) > 0 && strlen(content_info->media_meta.artist) > 0)
649 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);
651 ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
653 content_info->album_id = album_id;
655 if (mm_file_destroy_tag_attrs(tag) != FILEINFO_ERROR_NONE)
656 media_svc_error("destroy failed");
659 int _media_svc_extract_video_metadata(media_svc_content_info_s *content_info)
661 int mmf_error = FILEINFO_ERROR_NONE;
662 MMHandleType tag = 0;
663 MMHandleType content = 0;
665 unsigned int size = 0;
667 mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
669 if (mmf_error == FILEINFO_ERROR_NONE) {
670 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
671 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
672 if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
673 /*Creation time format is 20130101 00:00:00 +0000. change it to 2013:01:01 00:00:00 +0000 like exif time format*/
674 content_info->media_meta.recorded_date = g_strdelimit(g_strdup(p), "-", ':');
676 content_info->media_meta.recorded_date = g_strdup(p);
680 if (content_info->media_meta.recorded_date == NULL)
681 content_info->media_meta.recorded_date = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
682 content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
684 mmf_error = mm_file_destroy_tag_attrs(tag);
685 if (mmf_error != FILEINFO_ERROR_NONE)
686 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
688 /*Get Content attribute ===========*/
689 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
690 media_svc_retvm_if(mmf_error != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_NONE, "mm_file_create_content_attrs failed");
692 mm_file_get_attrs(content, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width,
693 MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height,
696 mm_file_destroy_content_attrs(content);
698 content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
699 content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
700 content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
701 content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
702 content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
703 content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
704 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
705 content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
706 content_info->album_id = 0;
708 return MS_MEDIA_ERR_NONE;
711 static gchar * __media_svc_get_zipfile_string(zip_t *z, const char *fname)
714 zip_int64_t index_num = 0;
715 zip_file_t *file = NULL;
716 zip_stat_t sb = {0, };
719 media_svc_retvm_if(!z, NULL, "z is NULL");
720 media_svc_retvm_if(!fname, NULL, "fname is NULL");
722 index_num = zip_name_locate(z, fname, ZIP_FL_NOCASE);
723 media_svc_retvm_if(index_num == -1, NULL, "fname is not exists [%s]", fname);
725 err = zip_stat_index(z, index_num, ZIP_STAT_SIZE, &sb);
726 media_svc_retvm_if(err == -1, NULL, "zip_stat_index failed");
728 file = zip_fopen_index(z, index_num, ZIP_FL_UNCHANGED);
729 media_svc_retvm_if(!file, NULL, "zip_fopen_index failed");
731 buf = g_malloc0(sb.size + 1);
733 err = zip_fread(file, buf, sb.size);
744 static xmlNodePtr __media_svc_find_node(xmlNodePtr node, const char *key)
746 xmlNodePtr tmp = NULL;
748 media_svc_retvm_if(!node, NULL, "node is NULL");
749 media_svc_retvm_if(!key, NULL, "key is NULL");
751 for (tmp = node->children; tmp; tmp = tmp->next) {
752 if (xmlIsBlankNode(tmp))
755 if (g_str_has_suffix((gchar *)tmp->name, key))
762 static char * __media_svc_remove_escape_c(const char *value)
768 media_svc_retv_if(!value, NULL);
772 for (i = 0; i < len; i++) {
773 if (value[i] != 10 && value[i] != 32) { // 10='\n' 32=' '
781 end = end - start + 1;
783 return g_strndup(value + start, end);
786 static char * __media_svc_find_and_get_value(xmlNodePtr node, const char *key)
788 xmlNodePtr tmp = NULL;
789 char *tmp_res = NULL;
792 media_svc_retvm_if(!node, NULL, "node is NULL");
793 media_svc_retvm_if(!key, NULL, "key is NULL");
795 for (tmp = node->children; tmp; tmp = tmp->next) {
796 if (xmlIsBlankNode(tmp))
800 tmp_res = __media_svc_find_and_get_value(tmp, key);
802 res = __media_svc_remove_escape_c(tmp_res);
808 if (g_str_has_suffix((gchar *)tmp->name, key))
809 return (char *)xmlNodeGetContent(tmp);
815 static gboolean __media_svc_get_epub_root_file(zip_t *z, char **opf_file)
818 gchar *tmp_buf = NULL;
819 xmlDocPtr doc = NULL;
820 xmlNodePtr node = NULL;
822 media_svc_retvm_if(!z, FALSE, "z is NULL");
823 media_svc_retvm_if(!opf_file, FALSE, "opf_file is NULL");
825 buf = __media_svc_get_zipfile_string(z, "META-INF/container.xml");
826 media_svc_retvm_if(!buf, FALSE, "buf is NULL");
828 tmp_buf = g_strrstr(buf, ">");
830 *(tmp_buf + 1) = '\0';
832 doc = xmlParseDoc((const xmlChar *)buf);
834 media_svc_retvm_if(!doc, FALSE, "doc is NULL");
836 node = xmlDocGetRootElement(doc);
837 node = __media_svc_find_node(node, "rootfiles");
838 node = __media_svc_find_node(node, "rootfile");
840 *opf_file = (char *)xmlGetProp(node, (const xmlChar *)"full-path");
841 media_svc_sec_debug("OPF [%s]", *opf_file);
847 static gboolean __media_svc_get_xml_metadata(const xmlChar *buffer, gboolean is_pdf, media_svc_content_info_s *content_info)
849 xmlDocPtr doc = NULL;
850 xmlNodePtr root = NULL;
852 media_svc_retvm_if(!buffer, FALSE, "buffer is NULL");
853 media_svc_retvm_if(!content_info, FALSE, "content_info is NULL");
855 doc = xmlParseDoc(buffer);
856 media_svc_retv_if(!doc, FALSE);
858 root = xmlDocGetRootElement(doc);
864 content_info->media_meta.title = __media_svc_find_and_get_value(root, "title");
865 /* In the case of PDF, if there is no title, it may not be a metadata block. Search for the next xml block*/
866 if (is_pdf && !content_info->media_meta.title) {
871 content_info->media_meta.artist = __media_svc_find_and_get_value(root, "creator");
872 if (!content_info->media_meta.artist)
873 content_info->media_meta.artist = __media_svc_find_and_get_value(root, "author");
874 content_info->media_meta.genre = __media_svc_find_and_get_value(root, "subject");
875 content_info->media_meta.copyright = __media_svc_find_and_get_value(root, "publisher");
876 content_info->media_meta.recorded_date = __media_svc_find_and_get_value(root, "date");
883 static int __media_svc_get_epub_metadata(media_svc_content_info_s *content_info)
888 char *opf_path = NULL;
890 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
893 z = zip_open(content_info->path, ZIP_RDONLY, &err);
894 media_svc_retvm_if(err == -1, MS_MEDIA_ERR_INTERNAL, "zip_open failed");
896 //2. find and read opf file
897 if (!__media_svc_get_epub_root_file(z, &opf_path)) {
898 media_svc_error("__media_svc_get_epub_root_file failed");
900 return MS_MEDIA_ERR_INTERNAL;
904 buf = __media_svc_get_zipfile_string(z, opf_path);
907 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INTERNAL, "__media_svc_get_zipfile_string failed");
909 if (!__media_svc_get_xml_metadata((const xmlChar *)buf, FALSE, content_info))
910 media_svc_error("__media_svc_get_xml_metadata failed");
914 return MS_MEDIA_ERR_NONE;
917 static int __media_svc_get_pdf_metadata(media_svc_content_info_s *content_info)
923 int search_limit = 0;
924 char tmp[MEDIA_SVC_PDF_BUF_SIZE + 1] = {0, };
925 gchar *meta_buf = NULL;
928 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
929 media_svc_retvm_if(content_info->size < 256, MS_MEDIA_ERR_INTERNAL, "open failed");
931 fd = open(content_info->path, O_RDONLY);
932 media_svc_retvm_if(fd < 0, MS_MEDIA_ERR_INTERNAL, "open failed");
934 search_limit = content_info->size - MEDIA_SVC_PDF_TAG_TAIL_LEN;
936 while (cur_pos <= search_limit) {
937 if (lseek(fd, cur_pos, SEEK_SET) == -1)
940 memset(&tmp, 0x00, MEDIA_SVC_PDF_BUF_SIZE + 1);
942 if (read(fd, &tmp, MEDIA_SVC_PDF_BUF_SIZE) != MEDIA_SVC_PDF_BUF_SIZE) {
943 media_svc_error("read failed");
947 //1.Find <x:xmpmeta .. </x:xmpmeta> block
948 if (start_pos == 0 && (found = strstr(tmp, "<x:xmpmeta"))) {
949 start_pos = cur_pos + (found - tmp);
950 // media_svc_error("FIND START_POS[%d]", start_pos);
955 if (start_pos != 0 && (found = strstr(tmp, "</x:xmpmeta>"))) {
956 end_pos = cur_pos + (found - tmp) + MEDIA_SVC_PDF_TAG_TAIL_LEN;
957 // media_svc_error("FIND END_POS[%d]", end_pos);
961 //2.get metadata using xml parser
962 if (start_pos && end_pos) {
963 if (lseek(fd, start_pos, SEEK_SET) == -1)
966 meta_buf = g_malloc0(end_pos - start_pos + 1);
968 if (read(fd, meta_buf, end_pos - start_pos) == end_pos - start_pos) {
969 if (__media_svc_get_xml_metadata((const xmlChar *)meta_buf, TRUE, content_info)) {
987 return MS_MEDIA_ERR_NONE;
990 int _media_svc_extract_book_metadata(media_svc_content_info_s *content_info)
992 int ret = MS_MEDIA_ERR_NONE;
994 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content info is NULL");
996 if (g_str_has_suffix(content_info->mime_type, "epub+zip"))
997 ret = __media_svc_get_epub_metadata(content_info);
999 ret = __media_svc_get_pdf_metadata(content_info);
1001 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "failed to extract metadata");
1002 if (!content_info->media_meta.title || strlen(content_info->media_meta.title) == 0) {
1003 g_free(content_info->media_meta.title);
1004 content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
1007 return MS_MEDIA_ERR_NONE;
1010 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1012 media_svc_retm_if(!content_info, "content info is NULL");
1014 /* Delete media_svc_content_info_s */
1015 g_free(content_info->media_uuid);
1016 g_free(content_info->path);
1017 g_free(content_info->file_name);
1018 g_free(content_info->mime_type);
1019 g_free(content_info->thumbnail_path);
1020 g_free(content_info->storage_uuid);
1022 /* Delete media_svc_content_meta_s */
1023 g_free(content_info->media_meta.title);
1024 g_free(content_info->media_meta.album);
1025 g_free(content_info->media_meta.artist);
1026 g_free(content_info->media_meta.album_artist);
1027 g_free(content_info->media_meta.genre);
1028 g_free(content_info->media_meta.year);
1029 g_free(content_info->media_meta.recorded_date);
1030 g_free(content_info->media_meta.copyright);
1031 g_free(content_info->media_meta.track_num);
1032 g_free(content_info->media_meta.datetaken);
1035 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
1037 int ret = MS_MEDIA_ERR_NONE;
1039 media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
1040 media_svc_retvm_if(!thumb_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1041 media_svc_retvm_if(!g_file_test(path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "File doesn't exist[%s]", path);
1043 if (!ms_user_thumb_support(uid, path)) {
1044 media_svc_sec_error("origin path(%s) is invalid", path);
1045 return MS_MEDIA_ERR_INVALID_PARAMETER;
1048 media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1050 //1. make thumb path
1051 ret = __media_svc_get_thumbnail_path(thumb_path, path, NULL, uid);
1052 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to create thumbnail path[%d]", ret);
1055 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1056 ret = create_image_thumbnail_to_file(path, MEDIA_SVC_THUMB_WIDTH, MEDIA_SVC_THUMB_HEIGHT, thumb_path, true);
1058 ret = create_video_thumbnail_to_file(path, MEDIA_SVC_THUMB_WIDTH, MEDIA_SVC_THUMB_HEIGHT, thumb_path, true);
1060 return (ret == THUMB_OK) ? MS_MEDIA_ERR_NONE : MS_MEDIA_ERR_INTERNAL;
1063 int _media_svc_get_media_type(const char *path, int *mediatype)
1065 int ret = MS_MEDIA_ERR_NONE;
1066 char mime_type[256] = {0};
1067 media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1069 media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1071 ret = __media_svc_get_mime_type(path, mime_type);
1072 if (ret == MS_MEDIA_ERR_NONE)
1073 __media_svc_get_media_type(path, mime_type, &media_type);
1075 media_svc_error("__media_svc_get_mime_type failed");
1077 *mediatype = media_type;
1082 bool _media_svc_is_keyword_included(const char *path, const char *keyword)
1085 void *handle = NULL;
1086 bool (*svc_search) (const char *, const char *);
1088 media_svc_retvm_if(!path, false, "Invalid path");
1089 media_svc_retvm_if(!keyword, false, "Invalid keyword");
1091 handle = dlopen(PATH_PLUGIN_LIB, RTLD_LAZY);
1092 media_svc_retvm_if(!handle, false, "dlopen failed");
1094 if (g_str_has_suffix(path, "epub") || g_str_has_suffix(path, "EPUB"))
1095 svc_search = dlsym(handle, "media_svc_epub_is_keyword_included");
1097 svc_search = dlsym(handle, "media_svc_pdf_is_keyword_included");
1100 media_svc_error("dlsym failed - %s", dlerror());
1105 ret = svc_search(path, keyword);
1111 static int __media_svc_create_wordbook_db(const char *path, sqlite3 **handle)
1113 int ret = SQLITE_OK;
1114 sqlite3 *db_handle = NULL;
1117 ret = sqlite3_open_v2(path, &db_handle, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
1118 media_svc_retvm_if(ret != SQLITE_OK, ret, "sqlite3_open_v2 failed : %d", ret);
1120 ret = sqlite3_exec(db_handle, "PRAGMA journal_mode = OFF;", NULL, NULL, &err);
1121 if (ret != SQLITE_OK)
1124 ret = sqlite3_exec(db_handle, "CREATE TABLE IF NOT EXISTS files(id integer primary key autoincrement, path text unique, validity integer default 1);", NULL, NULL, &err);
1125 if (ret != SQLITE_OK)
1128 ret = sqlite3_exec(db_handle, "CREATE TABLE IF NOT EXISTS words(file_id integer, word text, frequency integer default 1, unique(file_id, word));", NULL, NULL, &err);
1129 if (ret != SQLITE_OK)
1132 ret = sqlite3_exec(db_handle, "CREATE TRIGGER IF NOT EXISTS TR_files_words DELETE ON files BEGIN DELETE FROM words WHERE file_id = old.id;END;", NULL, NULL, &err);
1133 if (ret != SQLITE_OK)
1136 *handle = db_handle;
1141 media_svc_error("sqlite3_exec failed : %s", err);
1142 SQLITE3_SAFE_FREE(err);
1143 sqlite3_close_v2(db_handle);
1148 static bool __media_svc_get_wordbook_handle(uid_t uid, sqlite3 **handle)
1150 int ret = SQLITE_OK;
1151 char *db_path = NULL;
1153 ms_user_get_wordbook_db_path(uid, &db_path);
1157 ret = sqlite3_open_v2(db_path, handle, SQLITE_OPEN_READWRITE, NULL);
1158 if (ret != SQLITE_OK) {
1159 ret = __media_svc_create_wordbook_db(db_path, handle);
1161 media_svc_retvm_if(ret != SQLITE_OK, false, "__media_svc_create_wordbook_db failed : %d", ret);
1163 ret = sqlite3_exec(*handle, "PRAGMA journal_mode = OFF;", NULL, NULL, NULL);
1164 if (ret != SQLITE_OK)
1165 media_svc_error("Failed to change journal mode [%d]", ret);
1171 static bool __media_svc_is_exist_in_wordbook(sqlite3 *db_handle, const char *path)
1173 int ret = SQLITE_OK;
1177 query = sqlite3_mprintf("UPDATE files SET validity=1 WHERE path = %Q", path);
1179 ret = sqlite3_exec(db_handle, query, NULL, NULL, &err);
1180 SQLITE3_SAFE_FREE(query);
1181 if (ret != SQLITE_OK) {
1182 media_svc_error("Query failed. [%s]", err);
1183 SQLITE3_SAFE_FREE(err);
1187 return sqlite3_changes(db_handle) > 0 ? true : false;
1190 static void __media_svc_insert_to_wordbook(sqlite3 *db_handle, const char *path)
1192 void *handle = NULL;
1193 void (*svc_update) (sqlite3 *, const char *);
1196 query = sqlite3_mprintf("INSERT INTO files(path) VALUES(%Q);", path);
1197 sqlite3_exec(db_handle, query, NULL, NULL, NULL);
1198 sqlite3_free(query);
1200 handle = dlopen(PATH_PLUGIN_LIB, RTLD_LAZY);
1202 media_svc_error("dlopen failed");
1206 if (g_str_has_suffix(path, "epub") || g_str_has_suffix(path, "EPUB"))
1207 svc_update = dlsym(handle, "media_svc_epub_insert_to_db");
1209 svc_update = dlsym(handle, "media_svc_pdf_insert_to_db");
1212 media_svc_error("dlsym failed - %s", dlerror());
1217 svc_update(db_handle, path);
1221 void _media_svc_update_wordbook(const char *path, uid_t uid)
1223 sqlite3 *db_handle = NULL;
1226 media_svc_error("Invalid path");
1231 if (!__media_svc_get_wordbook_handle(uid, &db_handle))
1234 if (__media_svc_is_exist_in_wordbook(db_handle, path)) {
1235 sqlite3_close_v2(db_handle);
1239 // if no item, insert to db..
1240 __media_svc_insert_to_wordbook(db_handle, path);
1241 sqlite3_close_v2(db_handle);
1244 void _media_svc_clean_wordbook(uid_t uid)
1246 sqlite3 *db_handle = NULL;
1248 if (!__media_svc_get_wordbook_handle(uid, &db_handle))
1251 sqlite3_exec(db_handle, "DELETE FROM files where validity = 0;", NULL, NULL, NULL);
1252 sqlite3_exec(db_handle, "UPDATE files SET validity = 0;", NULL, NULL, NULL);
1253 sqlite3_close_v2(db_handle);
1256 bool _media_svc_get_matched_list(const char *keyword, uid_t uid, GList **list)
1258 int ret = SQLITE_OK;
1259 sqlite3 *handle = NULL;
1260 sqlite3_stmt *stmt = NULL;
1263 media_svc_retvm_if(!list, false, "list is NULL");
1264 media_svc_retvm_if(!keyword, false, "keyword is NULL");
1265 media_svc_retvm_if(!__media_svc_get_wordbook_handle(uid, &handle), false, "Failed to get handle");
1267 query = sqlite3_mprintf("SELECT files.path FROM files JOIN (SELECT file_id, sum(frequency) AS freq_sum FROM words WHERE word LIKE '%q%%' GROUP BY file_id ORDER BY freq_sum DESC) w ON files.id = w.file_id;", keyword);
1268 ret = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
1269 SQLITE3_SAFE_FREE(query);
1271 if (ret != SQLITE_OK) {
1272 media_svc_error("Query failed[%d]", ret);
1273 sqlite3_close_v2(handle);
1277 while (sqlite3_step(stmt) == SQLITE_ROW)
1278 *list = g_list_append(*list, g_strdup((char *)sqlite3_column_text(stmt, 0)));
1280 sqlite3_finalize(stmt);
1281 sqlite3_close_v2(handle);