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 char * __media_svc_get_exif_datetaken(ExifData *ed)
134 char tmp[MEDIA_SVC_METADATA_LEN_MAX + 1] = { 0, };
136 media_svc_retv_if(!ed, NULL);
138 entry = exif_data_get_entry(ed, EXIF_TAG_DATE_TIME_ORIGINAL);
140 exif_entry_get_value(entry, tmp, MEDIA_SVC_METADATA_LEN_MAX);
142 return g_strdup(tmp);
145 entry = exif_data_get_entry(ed, EXIF_TAG_DATE_TIME);
147 exif_entry_get_value(entry, tmp, MEDIA_SVC_METADATA_LEN_MAX);
149 return g_strdup(tmp);
155 static bool __media_svc_get_exif_int(ExifData *ed, ExifTag tagtype, int *value)
159 media_svc_retv_if(!ed, false);
160 media_svc_retvm_if(!value, false, "value is NULL");
162 entry = exif_data_get_entry(ed, tagtype);
163 media_svc_retv_if(!entry, false);
165 short exif_value = exif_get_short(entry->data, exif_data_get_byte_order(ed));
166 media_svc_retv_if(exif_value < 0, false);
167 *value = (int)exif_value;
172 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
178 media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "path is null");
179 media_svc_retvm_if(!mime_type, MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is null");
180 media_svc_retvm_if(!media_type, MS_MEDIA_ERR_INVALID_PARAMETER, "media_type is null");
183 if (strncmp(mime_type, IMAGE_PREFIX, PREFIX_LEN) == 0) {
184 *media_type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
185 return MS_MEDIA_ERR_NONE;
189 if (strncmp(mime_type, AUDIO_PREFIX, PREFIX_LEN) == 0) {
190 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
192 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
193 if (strcmp(mime_type + PREFIX_LEN, music_mime_table[idx]) == 0) {
194 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
199 /* audio/x-mpegurl : .m3u file (playlist file) */
200 if (strcmp(mime_type + PREFIX_LEN, "x-mpegurl") == 0)
201 *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
203 return MS_MEDIA_ERR_NONE;
207 if (strncmp(mime_type, VIDEO_PREFIX, PREFIX_LEN) == 0) {
208 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
210 /*some video files don't have video stream. in this case it is categorize as music. */
211 if (strcmp(mime_type + PREFIX_LEN, "3gpp") == 0 ||
212 strcmp(mime_type + PREFIX_LEN, "mp4") == 0) {
213 if (mm_file_get_stream_info(path, &audio, &video) == FILEINFO_ERROR_NONE) {
214 if (audio > 0 && video == 0)
215 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
219 return MS_MEDIA_ERR_NONE;
223 *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
225 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
226 if (strcmp(mime_type, sound_mime_table[idx]) == 0) {
227 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
228 return MS_MEDIA_ERR_NONE;
232 /*"asf" must check video stream and then categorize in directly. */
233 if (strcmp(mime_type, "application/vnd.ms-asf") == 0) {
234 if (mm_file_get_stream_info(path, &audio, &video) == FILEINFO_ERROR_NONE) {
235 if (audio > 0 && video == 0)
236 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
238 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
241 return MS_MEDIA_ERR_NONE;
244 if (strcmp(mime_type, "application/epub+zip") == 0 || strcmp(mime_type, "application/pdf") == 0)
245 *media_type = MEDIA_SVC_MEDIA_TYPE_BOOK;
247 return MS_MEDIA_ERR_NONE;
250 static int __media_svc_get_mime_type(const char *path, char *mimetype)
252 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
254 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
255 media_svc_error("aul_get_mime_from_file fail");
256 return MS_MEDIA_ERR_INTERNAL;
259 return MS_MEDIA_ERR_NONE;
262 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
266 for (i = strlen(file_path); i >= 0; i--) {
267 if (file_path[i] == '.') {
268 g_strlcpy(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
272 if (file_path[i] == '/')
278 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
280 int ret = MS_MEDIA_ERR_NONE;
282 char *thumb_path = NULL;
283 long bsize_kbytes = 0;
284 GError *error = NULL;
286 media_svc_retvm_if(!image || size == 0, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid image");
287 media_svc_sec_debug("start save image, path [%s] image size [%d]", image_path, size);
289 ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
290 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path error");
292 ret = statfs(thumb_path, &fs);
294 media_svc_retvm_if(ret == -1, MS_MEDIA_ERR_INTERNAL, "statfs failed");
296 bsize_kbytes = fs.f_bsize >> 10;
297 media_svc_retvm_if((bsize_kbytes * fs.f_bavail) < 1024, MS_MEDIA_ERR_NOT_ENOUGH_SPACE, "Not enough space");
299 if (!g_file_set_contents(image_path, (const gchar *)image, (gssize)size, &error)) {
300 media_svc_error("g_file_set_contents faild:%s", error->message);
302 return MS_MEDIA_ERR_INTERNAL;
305 return MS_MEDIA_ERR_NONE;
308 static char *__media_svc_get_title_from_filename(const char *filename)
311 char *last_dot = NULL;
313 media_svc_retvm_if(!STRING_VALID(filename), g_strdup(MEDIA_SVC_TAG_UNKNOWN), "Invalid path");
315 last_dot = strrchr(filename, '.');
317 title = g_strndup(filename, last_dot - filename);
319 title = g_strdup(filename);
322 media_svc_debug("extract title is [%s]", title);
327 void _media_svc_remove_file(const char *path)
329 if (!STRING_VALID(path))
332 if (remove(path) != 0)
333 media_svc_stderror("fail to remove file result");
336 static int __media_svc_get_thumbnail_path(char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
338 int ret = MS_MEDIA_ERR_NONE;
339 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
340 g_autofree gchar *hash = NULL;
341 g_autofree gchar *thumb_dir = NULL;
343 ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
344 media_svc_retvm_if(!STRING_VALID(thumb_dir), ret, "ms_user_get_root_thumb_store_path failed");
345 media_svc_retvm_if(!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR), MS_MEDIA_ERR_INTERNAL, "Not a directory");
347 memset(file_ext, 0, sizeof(file_ext));
348 if (!__media_svc_get_file_ext(pathname, file_ext))
349 media_svc_error("get file ext fail");
351 hash = g_compute_checksum_for_string(G_CHECKSUM_MD5, pathname, -1);
352 media_svc_retvm_if(!hash, MS_MEDIA_ERR_INTERNAL, "Failed to create hashname");
355 /* 'img_format' is mime-type */
356 if (!g_str_has_prefix(img_format, IMAGE_PREFIX) || strlen(img_format) == PREFIX_LEN) {
357 media_svc_error("Not proper img format");
358 return MS_MEDIA_ERR_INTERNAL;
361 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, img_format + PREFIX_LEN);
363 if (strcasecmp(file_ext, "PNG") == 0)
364 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
366 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
369 return MS_MEDIA_ERR_NONE;
372 int _media_svc_get_file_time(const char *full_path)
374 struct stat statbuf = { 0, };
376 if (stat(full_path, &statbuf) == -1) {
377 media_svc_stderror("stat fails.");
381 return statbuf.st_mtime;
384 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, const char *path, bool refresh)
386 int ret = MS_MEDIA_ERR_NONE;
387 char mime_type[256] = {0, };
388 media_svc_media_type_e media_type;
389 struct stat st = { 0, };
391 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
393 content_info->path = g_strdup(path);
394 content_info->file_name = g_path_get_basename(path);
396 if (stat(path, &st) == 0) {
397 content_info->modified_time = st.st_mtime;
398 content_info->size = st.st_size;
400 media_svc_stderror("stat failed");
403 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
405 media_svc_debug("refresh");
406 return MS_MEDIA_ERR_NONE;
409 content_info->storage_uuid = g_strdup(storage_id);
410 media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
412 content_info->media_uuid = __media_info_generate_uuid();
413 media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
415 ret = __media_svc_get_mime_type(path, mime_type);
416 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
418 media_svc_debug("mime [%s]", mime_type);
420 ret = __media_svc_get_media_type(path, mime_type, &media_type);
421 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
423 content_info->mime_type = g_strdup(mime_type);
424 media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
426 media_svc_sec_debug("path[%s], media_type[%d]", content_info->path, media_type);
428 content_info->media_type = media_type;
430 return MS_MEDIA_ERR_NONE;
433 static char * __media_svc_get_title(MMHandleType tag, const char *filename)
435 int ret = FILEINFO_ERROR_NONE;
440 ret = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
441 if (ret == FILEINFO_ERROR_NONE && size > 0) {
442 while(p && isspace(*p))
449 return __media_svc_get_title_from_filename(filename);
452 char * _media_svc_get_title_from_filename(const char *filename)
454 return __media_svc_get_title_from_filename(filename);
457 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
459 int orient_value = 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");
466 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
468 content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
470 /* Load an ExifData object from an EXIF file */
471 ed = exif_data_new_from_file(content_info->path);
473 media_svc_sec_debug("There is no exif data in [ %s ]", content_info->path);
474 goto GET_WIDTH_HEIGHT;
477 content_info->media_meta.datetaken = __media_svc_get_exif_datetaken(ed);
479 if (__media_svc_get_exif_int(ed, EXIF_TAG_ORIENTATION, &orient_value)) {
480 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
481 content_info->media_meta.orientation = orient_value;
484 if (__media_svc_get_exif_int(ed, EXIF_TAG_PIXEL_X_DIMENSION, &exif_width))
485 content_info->media_meta.width = exif_width;
487 if (__media_svc_get_exif_int(ed, EXIF_TAG_PIXEL_Y_DIMENSION, &exif_height))
488 content_info->media_meta.height = exif_height;
493 if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
494 /*Get image width, height*/
495 unsigned int img_width = 0;
496 unsigned int img_height = 0;
498 if (get_image_info(content_info->path, &img_width, &img_height) != THUMB_OK)
499 return MS_MEDIA_ERR_NONE;
501 if (content_info->media_meta.width == 0)
502 content_info->media_meta.width = img_width;
504 if (content_info->media_meta.height == 0)
505 content_info->media_meta.height = img_height;
508 return MS_MEDIA_ERR_NONE;
511 static char * __media_svc_get_tag_str_value(MMHandleType tag, const char *tag_name)
513 int ret = FILEINFO_ERROR_NONE;
517 ret = mm_file_get_attrs(tag, tag_name, &p, &size, NULL);
518 if (ret == FILEINFO_ERROR_NONE && size > 0)
521 return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
524 static char * __media_svc_extract_albumart(MMHandleType tag, const char *path, uid_t uid)
526 int ret = FILEINFO_ERROR_NONE;
527 unsigned char *image = NULL;
528 unsigned int size = 0;
529 char *mimetype = NULL;
530 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = { 0, };
531 unsigned int mime_size = 0;
533 ret = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
534 media_svc_retvm_if(ret != FILEINFO_ERROR_NONE, NULL, "Failed to get tag artwork[%d]", ret);
535 media_svc_retvm_if(!image || size == 0, NULL, "Invalid artwork");
537 ret = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &mimetype, &mime_size, NULL);
538 media_svc_retvm_if(ret != FILEINFO_ERROR_NONE, NULL, "Failed to get tag mime[%d]", ret);
539 media_svc_retvm_if(mime_size == 0, NULL, "Invalid mimetype");
541 ret = __media_svc_get_thumbnail_path(thumb_path, path, mimetype, uid);
542 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, NULL, "Failed to get thumbnail path");
544 ret = __media_svc_save_image(image, size, thumb_path, uid);
545 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, NULL, "Fail to save thumbnail");
547 return g_strdup(thumb_path);
550 void _media_svc_extract_audio_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
552 MMHandleType tag = 0;
554 unsigned int size = 0;
555 int mmf_error = FILEINFO_ERROR_NONE;
557 int ret = MS_MEDIA_ERR_NONE;
558 bool support_albumart = ms_user_thumb_support(uid, content_info->path);
560 /*Get Content Tag attribute ===========*/
561 if (support_albumart)
562 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
564 mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
566 if (mmf_error != FILEINFO_ERROR_NONE) {
567 content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
568 content_info->album_id = 0;
572 content_info->media_meta.title = __media_svc_get_title(tag, content_info->file_name);
573 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
574 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
575 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
576 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
577 content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
579 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
580 if (mmf_error == FILEINFO_ERROR_NONE && size == 4)
581 content_info->media_meta.year = g_strdup(p);
583 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
585 /*Do not extract artwork for the USB Storage content*/
586 if (support_albumart)
587 content_info->thumbnail_path = __media_svc_extract_albumart(tag, content_info->path, uid);
589 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
590 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
591 media_svc_debug("album does not exist. So start to make album art");
592 if (strlen(content_info->media_meta.album) > 0 && strlen(content_info->media_meta.artist) > 0)
593 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);
595 ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
597 content_info->album_id = album_id;
599 if (mm_file_destroy_tag_attrs(tag) != FILEINFO_ERROR_NONE)
600 media_svc_error("destroy failed");
603 void _media_svc_extract_video_metadata(media_svc_content_info_s *content_info)
605 /* All metadata fields must be empty strings until media_video is deleted */
606 content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
607 content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
608 content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
609 content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
610 content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
611 content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
612 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
613 content_info->album_id = 0;
616 static gchar * __media_svc_get_zipfile_string(zip_t *z, const char *fname)
619 zip_int64_t index_num = 0;
620 zip_file_t *file = NULL;
621 zip_stat_t sb = {0, };
624 media_svc_retvm_if(!z, NULL, "z is NULL");
625 media_svc_retvm_if(!fname, NULL, "fname is NULL");
627 index_num = zip_name_locate(z, fname, ZIP_FL_NOCASE);
628 media_svc_retvm_if(index_num == -1, NULL, "fname is not exists [%s]", fname);
630 err = zip_stat_index(z, index_num, ZIP_STAT_SIZE, &sb);
631 media_svc_retvm_if(err == -1, NULL, "zip_stat_index failed");
633 file = zip_fopen_index(z, index_num, ZIP_FL_UNCHANGED);
634 media_svc_retvm_if(!file, NULL, "zip_fopen_index failed");
636 buf = g_malloc0(sb.size + 1);
638 err = zip_fread(file, buf, sb.size);
649 static xmlNodePtr __media_svc_find_node(xmlNodePtr node, const char *key)
651 xmlNodePtr tmp = NULL;
653 media_svc_retvm_if(!node, NULL, "node is NULL");
654 media_svc_retvm_if(!key, NULL, "key is NULL");
656 for (tmp = node->children; tmp; tmp = tmp->next) {
657 if (xmlIsBlankNode(tmp))
660 if (g_str_has_suffix((gchar *)tmp->name, key))
667 static char * __media_svc_remove_escape_c(const char *value)
673 media_svc_retv_if(!value, NULL);
677 for (i = 0; i < len; i++) {
678 if (value[i] != 10 && value[i] != 32) { // 10='\n' 32=' '
686 end = end - start + 1;
688 return g_strndup(value + start, end);
691 static char * __media_svc_find_and_get_value(xmlNodePtr node, const char *key)
693 xmlNodePtr tmp = NULL;
694 char *tmp_res = NULL;
697 media_svc_retvm_if(!node, NULL, "node is NULL");
698 media_svc_retvm_if(!key, NULL, "key is NULL");
700 for (tmp = node->children; tmp; tmp = tmp->next) {
701 if (xmlIsBlankNode(tmp))
705 tmp_res = __media_svc_find_and_get_value(tmp, key);
707 res = __media_svc_remove_escape_c(tmp_res);
713 if (g_str_has_suffix((gchar *)tmp->name, key))
714 return (char *)xmlNodeGetContent(tmp);
720 static gboolean __media_svc_get_epub_root_file(zip_t *z, char **opf_file)
723 gchar *tmp_buf = NULL;
724 xmlDocPtr doc = NULL;
725 xmlNodePtr node = NULL;
727 media_svc_retvm_if(!z, FALSE, "z is NULL");
728 media_svc_retvm_if(!opf_file, FALSE, "opf_file is NULL");
730 buf = __media_svc_get_zipfile_string(z, "META-INF/container.xml");
731 media_svc_retvm_if(!buf, FALSE, "buf is NULL");
733 tmp_buf = g_strrstr(buf, ">");
735 *(tmp_buf + 1) = '\0';
737 doc = xmlParseDoc((const xmlChar *)buf);
739 media_svc_retvm_if(!doc, FALSE, "doc is NULL");
741 node = xmlDocGetRootElement(doc);
742 node = __media_svc_find_node(node, "rootfiles");
743 node = __media_svc_find_node(node, "rootfile");
745 *opf_file = (char *)xmlGetProp(node, (const xmlChar *)"full-path");
746 media_svc_sec_debug("OPF [%s]", *opf_file);
752 static gboolean __media_svc_get_xml_metadata(const xmlChar *buffer, gboolean is_pdf, media_svc_content_info_s *content_info)
754 xmlDocPtr doc = NULL;
755 xmlNodePtr root = NULL;
757 media_svc_retvm_if(!buffer, FALSE, "buffer is NULL");
758 media_svc_retvm_if(!content_info, FALSE, "content_info is NULL");
760 doc = xmlParseDoc(buffer);
761 media_svc_retv_if(!doc, FALSE);
763 root = xmlDocGetRootElement(doc);
769 content_info->media_meta.title = __media_svc_find_and_get_value(root, "title");
770 /* In the case of PDF, if there is no title, it may not be a metadata block. Search for the next xml block*/
771 if (is_pdf && !content_info->media_meta.title) {
776 content_info->media_meta.artist = __media_svc_find_and_get_value(root, "creator");
777 if (!content_info->media_meta.artist)
778 content_info->media_meta.artist = __media_svc_find_and_get_value(root, "author");
779 content_info->media_meta.genre = __media_svc_find_and_get_value(root, "subject");
786 static int __media_svc_get_epub_metadata(media_svc_content_info_s *content_info)
791 char *opf_path = NULL;
793 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
796 z = zip_open(content_info->path, ZIP_RDONLY, &err);
797 media_svc_retvm_if(err == -1, MS_MEDIA_ERR_INTERNAL, "zip_open failed");
799 //2. find and read opf file
800 if (!__media_svc_get_epub_root_file(z, &opf_path)) {
801 media_svc_error("__media_svc_get_epub_root_file failed");
803 return MS_MEDIA_ERR_INTERNAL;
807 buf = __media_svc_get_zipfile_string(z, opf_path);
810 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INTERNAL, "__media_svc_get_zipfile_string failed");
812 if (!__media_svc_get_xml_metadata((const xmlChar *)buf, FALSE, content_info))
813 media_svc_error("__media_svc_get_xml_metadata failed");
817 return MS_MEDIA_ERR_NONE;
820 static int __media_svc_get_pdf_metadata(media_svc_content_info_s *content_info)
826 int search_limit = 0;
827 char tmp[MEDIA_SVC_PDF_BUF_SIZE + 1] = {0, };
828 gchar *meta_buf = NULL;
831 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
832 media_svc_retvm_if(content_info->size < 256, MS_MEDIA_ERR_INTERNAL, "open failed");
834 fd = open(content_info->path, O_RDONLY);
835 media_svc_retvm_if(fd < 0, MS_MEDIA_ERR_INTERNAL, "open failed");
837 search_limit = content_info->size - MEDIA_SVC_PDF_TAG_TAIL_LEN;
839 while (cur_pos <= search_limit) {
840 if (lseek(fd, cur_pos, SEEK_SET) == -1)
843 memset(&tmp, 0x00, MEDIA_SVC_PDF_BUF_SIZE + 1);
845 if (read(fd, &tmp, MEDIA_SVC_PDF_BUF_SIZE) != MEDIA_SVC_PDF_BUF_SIZE) {
846 media_svc_error("read failed");
850 //1.Find <x:xmpmeta .. </x:xmpmeta> block
851 if (start_pos == 0 && (found = strstr(tmp, "<x:xmpmeta"))) {
852 start_pos = cur_pos + (found - tmp);
853 // media_svc_error("FIND START_POS[%d]", start_pos);
858 if (start_pos != 0 && (found = strstr(tmp, "</x:xmpmeta>"))) {
859 end_pos = cur_pos + (found - tmp) + MEDIA_SVC_PDF_TAG_TAIL_LEN;
860 // media_svc_error("FIND END_POS[%d]", end_pos);
864 //2.get metadata using xml parser
865 if (start_pos && end_pos) {
866 if (lseek(fd, start_pos, SEEK_SET) == -1)
869 meta_buf = g_malloc0(end_pos - start_pos + 1);
871 if (read(fd, meta_buf, end_pos - start_pos) == end_pos - start_pos) {
872 if (__media_svc_get_xml_metadata((const xmlChar *)meta_buf, TRUE, content_info)) {
890 return MS_MEDIA_ERR_NONE;
893 int _media_svc_extract_book_metadata(media_svc_content_info_s *content_info)
895 int ret = MS_MEDIA_ERR_NONE;
897 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content info is NULL");
899 if (g_str_has_suffix(content_info->mime_type, "epub+zip"))
900 ret = __media_svc_get_epub_metadata(content_info);
902 ret = __media_svc_get_pdf_metadata(content_info);
904 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "failed to extract metadata");
905 if (!content_info->media_meta.title || strlen(content_info->media_meta.title) == 0) {
906 g_free(content_info->media_meta.title);
907 content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
910 return MS_MEDIA_ERR_NONE;
913 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
915 media_svc_retm_if(!content_info, "content info is NULL");
917 /* Delete media_svc_content_info_s */
918 g_free(content_info->media_uuid);
919 g_free(content_info->path);
920 g_free(content_info->file_name);
921 g_free(content_info->mime_type);
922 g_free(content_info->thumbnail_path);
923 g_free(content_info->storage_uuid);
925 /* Delete media_svc_content_meta_s */
926 g_free(content_info->media_meta.title);
927 g_free(content_info->media_meta.album);
928 g_free(content_info->media_meta.artist);
929 g_free(content_info->media_meta.album_artist);
930 g_free(content_info->media_meta.genre);
931 g_free(content_info->media_meta.year);
932 g_free(content_info->media_meta.track_num);
933 g_free(content_info->media_meta.datetaken);
936 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
938 int ret = MS_MEDIA_ERR_NONE;
940 media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
941 media_svc_retvm_if(!thumb_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
942 media_svc_retvm_if(!g_file_test(path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "File doesn't exist[%s]", path);
944 if (!ms_user_thumb_support(uid, path)) {
945 media_svc_sec_error("origin path(%s) is invalid", path);
946 return MS_MEDIA_ERR_INVALID_PARAMETER;
949 media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
952 ret = __media_svc_get_thumbnail_path(thumb_path, path, NULL, uid);
953 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to create thumbnail path[%d]", ret);
956 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
957 ret = create_image_thumbnail_to_file(path, MEDIA_SVC_THUMB_WIDTH, MEDIA_SVC_THUMB_HEIGHT, thumb_path, true);
959 ret = create_video_thumbnail_to_file(path, MEDIA_SVC_THUMB_WIDTH, MEDIA_SVC_THUMB_HEIGHT, thumb_path, true);
961 return (ret == THUMB_OK) ? MS_MEDIA_ERR_NONE : MS_MEDIA_ERR_INTERNAL;
964 int _media_svc_get_media_type(const char *path, int *mediatype)
966 int ret = MS_MEDIA_ERR_NONE;
967 char mime_type[256] = {0};
968 media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
970 media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
972 ret = __media_svc_get_mime_type(path, mime_type);
973 if (ret == MS_MEDIA_ERR_NONE)
974 __media_svc_get_media_type(path, mime_type, &media_type);
976 media_svc_error("__media_svc_get_mime_type failed");
978 *mediatype = media_type;
983 bool _media_svc_is_keyword_included(const char *path, const char *keyword)
987 bool (*svc_search) (const char *, const char *);
989 media_svc_retvm_if(!path, false, "Invalid path");
990 media_svc_retvm_if(!keyword, false, "Invalid keyword");
992 handle = dlopen(PATH_PLUGIN_LIB, RTLD_LAZY);
993 media_svc_retvm_if(!handle, false, "dlopen failed");
995 if (g_str_has_suffix(path, "epub") || g_str_has_suffix(path, "EPUB"))
996 svc_search = dlsym(handle, "media_svc_epub_is_keyword_included");
998 svc_search = dlsym(handle, "media_svc_pdf_is_keyword_included");
1001 media_svc_error("dlsym failed - %s", dlerror());
1006 ret = svc_search(path, keyword);
1012 static int __media_svc_create_wordbook_db(const char *path, sqlite3 **handle)
1014 int ret = SQLITE_OK;
1015 sqlite3 *db_handle = NULL;
1018 ret = sqlite3_open_v2(path, &db_handle, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
1019 media_svc_retvm_if(ret != SQLITE_OK, ret, "sqlite3_open_v2 failed : %d", ret);
1021 ret = sqlite3_exec(db_handle, "PRAGMA journal_mode = OFF;", NULL, NULL, &err);
1022 if (ret != SQLITE_OK)
1025 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);
1026 if (ret != SQLITE_OK)
1029 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);
1030 if (ret != SQLITE_OK)
1033 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);
1034 if (ret != SQLITE_OK)
1037 *handle = db_handle;
1042 media_svc_error("sqlite3_exec failed : %s", err);
1043 SQLITE3_SAFE_FREE(err);
1044 sqlite3_close_v2(db_handle);
1049 static bool __media_svc_get_wordbook_handle(uid_t uid, sqlite3 **handle)
1051 int ret = SQLITE_OK;
1052 char *db_path = NULL;
1054 ms_user_get_wordbook_db_path(uid, &db_path);
1058 ret = sqlite3_open_v2(db_path, handle, SQLITE_OPEN_READWRITE, NULL);
1059 if (ret != SQLITE_OK) {
1060 ret = __media_svc_create_wordbook_db(db_path, handle);
1062 media_svc_retvm_if(ret != SQLITE_OK, false, "__media_svc_create_wordbook_db failed : %d", ret);
1064 ret = sqlite3_exec(*handle, "PRAGMA journal_mode = OFF;", NULL, NULL, NULL);
1065 if (ret != SQLITE_OK)
1066 media_svc_error("Failed to change journal mode [%d]", ret);
1072 static bool __media_svc_is_exist_in_wordbook(sqlite3 *db_handle, const char *path)
1074 int ret = SQLITE_OK;
1078 query = sqlite3_mprintf("UPDATE files SET validity=1 WHERE path = %Q", path);
1080 ret = sqlite3_exec(db_handle, query, NULL, NULL, &err);
1081 SQLITE3_SAFE_FREE(query);
1082 if (ret != SQLITE_OK) {
1083 media_svc_error("Query failed. [%s]", err);
1084 SQLITE3_SAFE_FREE(err);
1088 return sqlite3_changes(db_handle) > 0 ? true : false;
1091 static void __media_svc_insert_to_wordbook(sqlite3 *db_handle, const char *path)
1093 void *handle = NULL;
1094 void (*svc_update) (sqlite3 *, const char *);
1097 query = sqlite3_mprintf("INSERT INTO files(path) VALUES(%Q);", path);
1098 sqlite3_exec(db_handle, query, NULL, NULL, NULL);
1099 sqlite3_free(query);
1101 handle = dlopen(PATH_PLUGIN_LIB, RTLD_LAZY);
1103 media_svc_error("dlopen failed");
1107 if (g_str_has_suffix(path, "epub") || g_str_has_suffix(path, "EPUB"))
1108 svc_update = dlsym(handle, "media_svc_epub_insert_to_db");
1110 svc_update = dlsym(handle, "media_svc_pdf_insert_to_db");
1113 media_svc_error("dlsym failed - %s", dlerror());
1118 svc_update(db_handle, path);
1122 void _media_svc_update_wordbook(const char *path, uid_t uid)
1124 sqlite3 *db_handle = NULL;
1127 media_svc_error("Invalid path");
1132 if (!__media_svc_get_wordbook_handle(uid, &db_handle))
1135 if (__media_svc_is_exist_in_wordbook(db_handle, path)) {
1136 sqlite3_close_v2(db_handle);
1140 // if no item, insert to db..
1141 __media_svc_insert_to_wordbook(db_handle, path);
1142 sqlite3_close_v2(db_handle);
1145 void _media_svc_clean_wordbook(uid_t uid)
1147 sqlite3 *db_handle = NULL;
1149 if (!__media_svc_get_wordbook_handle(uid, &db_handle))
1152 sqlite3_exec(db_handle, "DELETE FROM files where validity = 0;", NULL, NULL, NULL);
1153 sqlite3_exec(db_handle, "UPDATE files SET validity = 0;", NULL, NULL, NULL);
1154 sqlite3_close_v2(db_handle);
1157 bool _media_svc_get_matched_list(const char *keyword, uid_t uid, GList **list)
1159 int ret = SQLITE_OK;
1160 sqlite3 *handle = NULL;
1161 sqlite3_stmt *stmt = NULL;
1164 media_svc_retvm_if(!list, false, "list is NULL");
1165 media_svc_retvm_if(!keyword, false, "keyword is NULL");
1166 media_svc_retvm_if(!__media_svc_get_wordbook_handle(uid, &handle), false, "Failed to get handle");
1168 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);
1169 ret = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
1170 SQLITE3_SAFE_FREE(query);
1172 if (ret != SQLITE_OK) {
1173 media_svc_error("Query failed[%d]", ret);
1174 sqlite3_close_v2(handle);
1178 while (sqlite3_step(stmt) == SQLITE_ROW)
1179 *list = g_list_append(*list, g_strdup((char *)sqlite3_column_text(stmt, 0)));
1181 sqlite3_finalize(stmt);
1182 sqlite3_close_v2(handle);