2 * media-thumbnail-server
4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "thumb-server-internal.h"
23 #include "media-thumb-util.h"
24 #include "media-thumb-debug.h"
31 #include <Ecore_Evas.h>
40 #define LOG_TAG "MEDIA_THUMBNAIL_SERVER"
41 #define THUMB_DEFAULT_WIDTH 320
42 #define THUMB_DEFAULT_HEIGHT 240
43 #define THUMB_BLOCK_SIZE 512
44 #define THUMB_ROOT_UID "0"
46 static __thread char **arr_path;
47 static __thread uid_t *arr_uid;
48 static __thread int g_idx = 0;
49 static __thread int g_cur_idx = 0;
51 GMainLoop *g_thumb_server_mainloop; // defined in thumb-server.c as extern
53 static gboolean __thumb_server_send_msg_to_agent(int msg_type);
54 static void __thumb_daemon_stop_job(void);
55 static int __thumb_daemon_all_extract(uid_t uid);
56 int _thumb_daemon_process_queue_jobs(gpointer data);
57 static gboolean _thumb_server_send_deny_message(int sockfd);
59 gboolean _thumb_daemon_start_jobs(gpointer data)
62 /* Initialize ecore-evas to use evas library */
65 __thumb_server_send_msg_to_agent(MS_MSG_THUMB_SERVER_READY);
70 void _thumb_daemon_finish_jobs(void)
72 sqlite3 *sqlite_db_handle = _media_thumb_db_get_handle();
74 if (sqlite_db_handle != NULL) {
75 _media_thumb_db_disconnect();
76 thumb_dbg("sqlite3 handle is alive. So disconnect to sqlite3");
79 /* Shutdown ecore-evas */
80 ecore_evas_shutdown();
81 g_main_loop_quit(g_thumb_server_mainloop);
86 int _thumb_daemon_mmc_status(void)
91 err = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status);
94 } else if (err == -1) {
95 thumb_err("vconf_get_int failed : %d", err);
97 thumb_err("vconf_get_int Unexpected error code: %d", err);
103 void _thumb_daemon_mmc_eject_vconf_cb(void *data)
108 thumb_warn("_thumb_daemon_vconf_cb called");
110 err = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status);
112 if (status == VCONFKEY_SYSMAN_MMC_REMOVED || status == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED) {
113 thumb_warn("SD card is ejected or not mounted. So media-thumbnail-server stops jobs to extract all thumbnails");
115 __thumb_daemon_stop_job();
117 } else if (err == -1) {
118 thumb_err("vconf_get_int failed : %d", err);
120 thumb_err("vconf_get_int Unexpected error code: %d", err);
126 void _thumb_daemon_vconf_cb(void *data)
131 thumb_warn("_thumb_daemon_vconf_cb called");
133 err = vconf_get_int(VCONFKEY_SYSMAN_MMC_FORMAT, &status);
135 if (status == VCONFKEY_SYSMAN_MMC_FORMAT_COMPLETED) {
136 thumb_warn("SD card format is completed. So media-thumbnail-server stops jobs to extract all thumbnails");
138 __thumb_daemon_stop_job();
140 thumb_dbg("not completed");
142 } else if (err == -1) {
143 thumb_err("vconf_get_int failed : %d", err);
145 thumb_err("vconf_get_int Unexpected error code: %d", err);
151 static void __thumb_daemon_stop_job()
156 thumb_warn("There are %d jobs in the queue. But all jobs will be stopped", g_idx - g_cur_idx);
158 for (i = g_cur_idx; i < g_idx; i++) {
159 path = arr_path[g_cur_idx++];
166 int _thumb_daemon_process_job(thumbMsg *req_msg, thumbMsg *res_msg, uid_t uid)
168 int err = MS_MEDIA_ERR_NONE;
170 err = _media_thumb_process(req_msg, res_msg, uid);
171 if (err != MS_MEDIA_ERR_NONE) {
172 if (req_msg->msg_type == THUMB_REQUEST_SAVE_FILE) {
173 thumb_err("_media_thumb_process is failed: %d", err);
174 res_msg->status = THUMB_FAIL;
176 thumb_warn("_media_thumb_process is failed: %d, So use default thumb", err);
177 res_msg->status = THUMB_SUCCESS;
180 res_msg->status = THUMB_SUCCESS;
186 static int __thumb_daemon_process_job_raw(thumbMsg *req_msg, thumbMsg *res_msg)
188 int err = MS_MEDIA_ERR_NONE;
190 err = _media_thumb_process_raw(req_msg, res_msg);
191 if (err != MS_MEDIA_ERR_NONE) {
192 if (err != MS_MEDIA_ERR_FILE_NOT_EXIST) {
193 thumb_warn("_media_thumb_process is failed: %d, So use default thumb", err);
194 res_msg->status = THUMB_SUCCESS;
196 thumb_warn("_media_thumb_process is failed: %d, (file not exist) ", err);
197 res_msg->status = THUMB_FAIL;
200 res_msg->status = THUMB_SUCCESS;
206 static int __thumb_daemon_all_extract(uid_t uid)
208 int err = MS_MEDIA_ERR_NONE;
209 char query_string[MAX_PATH_SIZE + 1] = { 0, };
210 char path[MAX_PATH_SIZE + 1] = { 0, };
211 sqlite3 *sqlite_db_handle = NULL;
212 sqlite3_stmt *sqlite_stmt = NULL;
214 err = _media_thumb_db_connect(uid);
215 if (err != MS_MEDIA_ERR_NONE) {
216 thumb_err("_media_thumb_db_connect failed: %d", err);
220 sqlite_db_handle = _media_thumb_db_get_handle();
221 if (sqlite_db_handle == NULL) {
222 thumb_err("sqlite handle is NULL");
223 return MS_MEDIA_ERR_INTERNAL;
226 if (_thumb_daemon_mmc_status() == VCONFKEY_SYSMAN_MMC_MOUNTED) {
227 snprintf(query_string, sizeof(query_string), SELECT_PATH_FROM_UNEXTRACTED_THUMB_MEDIA);
229 snprintf(query_string, sizeof(query_string), SELECT_PATH_FROM_UNEXTRACTED_THUMB_INTERNAL_MEDIA);
232 thumb_warn("Query: %s", query_string);
234 err = sqlite3_prepare_v2(sqlite_db_handle, query_string, strlen(query_string), &sqlite_stmt, NULL);
235 if (SQLITE_OK != err) {
236 thumb_err("prepare error [%s]", sqlite3_errmsg(sqlite_db_handle));
237 _media_thumb_db_disconnect();
238 return MS_MEDIA_ERR_INTERNAL;
242 err = sqlite3_step(sqlite_stmt);
243 if (err != SQLITE_ROW) {
244 thumb_dbg("end of row [%s]", sqlite3_errmsg(sqlite_db_handle));
248 strncpy(path, (const char *)sqlite3_column_text(sqlite_stmt, 0), sizeof(path));
249 path[sizeof(path) - 1] = '\0';
251 thumb_dbg_slog("Path : %s", path);
254 arr_path = (char**)malloc(sizeof(char*));
255 arr_uid = (uid_t*)malloc(sizeof(uid_t));
257 arr_path = (char**)realloc(arr_path, (g_idx + 1) * sizeof(char*));
258 arr_uid = (uid_t*)realloc(arr_uid, (g_idx + 1) * sizeof(uid_t));
260 arr_uid[g_idx] = uid;
261 arr_path[g_idx++] = strdup(path);
264 sqlite3_finalize(sqlite_stmt);
265 _media_thumb_db_disconnect();
267 return MS_MEDIA_ERR_NONE;
270 int _thumb_daemon_process_queue_jobs(gpointer data)
272 int err = MS_MEDIA_ERR_NONE;
276 if (g_cur_idx < g_idx) {
277 thumb_warn("There are %d jobs in the queue", g_idx - g_cur_idx);
278 thumb_dbg("Current idx : [%d]", g_cur_idx);
279 uid = arr_uid[g_cur_idx];
280 path = arr_path[g_cur_idx++];
282 thumbMsg recv_msg, res_msg;
283 memset(&recv_msg, 0x00, sizeof(thumbMsg));
284 memset(&res_msg, 0x00, sizeof(thumbMsg));
286 recv_msg.msg_type = THUMB_REQUEST_DB_INSERT;
287 strncpy(recv_msg.org_path, path, sizeof(recv_msg.org_path));
288 recv_msg.org_path[sizeof(recv_msg.org_path) - 1] = '\0';
290 err = _thumb_daemon_process_job(&recv_msg, &res_msg,uid );
291 if (err == MS_MEDIA_ERR_FILE_NOT_EXIST) {
292 thumb_err("Thumbnail processing is failed : %d", err);
294 if (res_msg.status == THUMB_SUCCESS) {
296 err = _media_thumb_db_connect(uid);
297 if (err != MS_MEDIA_ERR_NONE) {
298 thumb_err("_media_thumb_mb_svc_connect failed: %d", err);
302 /* Need to update DB once generating thumb is done */
303 err = _media_thumb_update_db(recv_msg.org_path,
305 res_msg.origin_width,
306 res_msg.origin_height,
308 if (err != MS_MEDIA_ERR_NONE) {
309 thumb_err("_media_thumb_update_db failed : %d", err);
312 _media_thumb_db_disconnect();
320 thumb_warn("Deleting array");
323 //_media_thumb_db_disconnect();
325 __thumb_server_send_msg_to_agent(MS_MSG_THUMB_EXTRACT_ALL_DONE); // MS_MSG_THUMB_EXTRACT_ALL_DONE
333 gboolean _thumb_server_read_socket(GIOChannel *src,
334 GIOCondition condition,
337 struct sockaddr_un client_addr;
338 unsigned int client_addr_len;
341 ms_peer_credentials credentials;
344 int client_sock = -1;
346 memset((void *)&recv_msg, 0, sizeof(recv_msg));
347 memset((void *)&res_msg, 0, sizeof(res_msg));
348 memset((void *)&credentials, 0, sizeof(credentials));
350 sock = g_io_channel_unix_get_fd(src);
352 thumb_err("sock fd is invalid!");
356 client_addr_len = sizeof(client_addr);
358 if ((client_sock = accept(sock, (struct sockaddr*)&client_addr, &client_addr_len)) < 0) {
359 thumb_stderror("accept failed : %s");
363 if (ms_cynara_receive_untrusted_message_thumb(client_sock, &recv_msg, &credentials) != MS_MEDIA_ERR_NONE) {
364 thumb_err("ms_cynara_receive_untrusted_message_thumb failed");
369 if (recv_msg.msg_type == THUMB_REQUEST_KILL_SERVER && strncmp(credentials.uid, THUMB_ROOT_UID, strlen(THUMB_ROOT_UID)) != 0) {
370 thumb_err("Only root can send THUMB_REQUEST_KILL_SERVER request");
371 _thumb_server_send_deny_message(client_sock);
376 if(recv_msg.msg_type != THUMB_REQUEST_KILL_SERVER) {
377 if (ms_cynara_check(&credentials, MEDIA_STORAGE_PRIVILEGE) != MS_MEDIA_ERR_NONE) {
378 thumb_err("Cynara denied access to process request");
379 _thumb_server_send_deny_message(client_sock);
385 SAFE_FREE(credentials.smack);
386 SAFE_FREE(credentials.uid);
388 thumb_warn_slog("Received [%d] %s(%d) from PID(%d)", recv_msg.msg_type, recv_msg.org_path, strlen(recv_msg.org_path), recv_msg.pid);
390 if (recv_msg.msg_type == THUMB_REQUEST_ALL_MEDIA) {
391 thumb_dbg("All thumbnails are being extracted now");
392 __thumb_daemon_all_extract(recv_msg.uid);
393 g_idle_add(_thumb_daemon_process_queue_jobs, NULL);
394 } else if(recv_msg.msg_type == THUMB_REQUEST_RAW_DATA) {
395 __thumb_daemon_process_job_raw(&recv_msg, &res_msg);
396 } else if(recv_msg.msg_type == THUMB_REQUEST_KILL_SERVER) {
397 thumb_warn("received KILL msg from thumbnail agent.");
399 _thumb_daemon_process_job(&recv_msg, &res_msg,recv_msg.uid);
402 if(res_msg.msg_type == 0)
403 res_msg.msg_type = recv_msg.msg_type;
404 res_msg.request_id = recv_msg.request_id;
405 strncpy(res_msg.org_path, recv_msg.org_path, recv_msg.origin_path_size);
406 res_msg.origin_path_size = recv_msg.origin_path_size;
407 if(res_msg.msg_type != THUMB_RESPONSE_RAW_DATA) {
408 res_msg.dest_path_size = strlen(res_msg.dst_path)+1;
409 res_msg.thumb_size = 0;
411 res_msg.dest_path_size = 1;
412 res_msg.dst_path[0] = '\0';
416 int sending_block = 0;
417 int block_size = sizeof(res_msg) - MAX_FILEPATH_LEN*2 - sizeof(unsigned char *);
418 unsigned char *buf = NULL;
419 _media_thumb_set_buffer(&res_msg, &buf, &buf_size);
421 while(buf_size > 0) {
422 if(buf_size < THUMB_BLOCK_SIZE) {
423 block_size = buf_size;
425 if (send(client_sock, buf+sending_block, block_size, 0) != block_size) {
426 thumb_stderror("sendto failed : %s");
428 sending_block += block_size;
429 buf_size -= block_size;
430 if(block_size < THUMB_BLOCK_SIZE) {
431 block_size = THUMB_BLOCK_SIZE;
435 thumb_dbg_slog("Sent data(%d) from %s", res_msg.thumb_size, res_msg.org_path);
439 if(recv_msg.msg_type == THUMB_REQUEST_KILL_SERVER) {
440 thumb_warn("Shutting down...");
441 g_main_loop_quit(g_thumb_server_mainloop);
449 static gboolean __thumb_server_send_msg_to_agent(int msg_type)
452 ms_sock_info_s sock_info;
453 struct sockaddr_un serv_addr;
454 ms_thumb_server_msg send_msg;
455 sock_info.port = MS_THUMB_COMM_PORT;
457 if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info) < 0) {
458 thumb_err("ms_ipc_create_server_socket failed");
462 memset(&serv_addr, 0, sizeof(serv_addr));
464 sock = sock_info.sock_fd;
465 serv_addr.sun_family = AF_UNIX;
466 strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcomm.socket");
469 /* Connecting to the thumbnail server */
470 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
471 thumb_stderror("connect");
472 ms_ipc_delete_client_socket(&sock_info);
473 return MS_MEDIA_ERR_SOCKET_CONN;
476 send_msg.msg_type = msg_type;
478 if (send(sock, &send_msg, sizeof(ms_thumb_server_msg), 0) != sizeof(ms_thumb_server_msg)) {
479 thumb_stderror("sendto failed");
480 ms_ipc_delete_client_socket(&sock_info);
484 thumb_dbg("Sending msg to thumbnail agent[%d] is successful", send_msg.msg_type);
486 ms_ipc_delete_client_socket(&sock_info);
491 static gboolean _thumb_server_send_deny_message(int sockfd)
494 int bytes_to_send = sizeof(msg) - sizeof(msg.org_path) - sizeof(msg.dst_path);
496 msg.msg_type = THUMB_RESPONSE;
497 msg.status = THUMB_FAIL;
499 if (send(sockfd, &msg, bytes_to_send, 0) != bytes_to_send) {
500 thumb_stderror("send failed");
507 gboolean _thumb_server_prepare_socket(int *sock_fd)
510 unsigned short serv_port;
515 memset((void *)&recv_msg, 0, sizeof(recv_msg));
516 memset((void *)&res_msg, 0, sizeof(res_msg));
517 serv_port = MS_THUMB_DAEMON_PORT;
519 if (ms_ipc_create_server_socket(MS_PROTOCOL_TCP, serv_port, &sock) < 0) {
520 thumb_err("ms_ipc_create_server_socket failed");
524 if (ms_cynara_enable_credentials_passing(sock) != MS_MEDIA_ERR_NONE) {
525 thumb_err("ms_cynara_enable_credentials_passing failed");
534 static char* _media_thumb_get_default_path(uid_t uid)
536 char *result_psswd = NULL;
537 struct group *grpinfo = NULL;
540 result_psswd = strdup(THUMB_DEFAULT_PATH);
541 grpinfo = getgrnam("users");
542 if(grpinfo == NULL) {
543 thumb_err("getgrnam(users) returns NULL !");
551 struct passwd *userinfo = getpwuid(uid);
552 if(userinfo == NULL) {
553 thumb_err("getpwuid(%d) returns NULL !", uid);
556 grpinfo = getgrnam("users");
557 if(grpinfo == NULL) {
558 thumb_err("getgrnam(users) returns NULL !");
561 // Compare git_t type and not group name
562 if (grpinfo->gr_gid != userinfo->pw_gid) {
563 thumb_err("UID [%d] does not belong to 'users' group!", uid);
566 asprintf(&result_psswd, "%s/share/media/.thumb/phone", userinfo->pw_dir);
572 int _thumbnail_get_data(const char *origin_path,
573 media_thumb_format format,
575 unsigned char **data,
584 int err = MS_MEDIA_ERR_NONE;
585 int thumb_width = -1;
586 int thumb_height = -1;
588 if (origin_path == NULL || size == NULL
589 || width == NULL || height == NULL) {
590 thumb_err("Invalid parameter");
591 return MS_MEDIA_ERR_INVALID_PARAMETER;
594 if (format < MEDIA_THUMB_BGRA || format > MEDIA_THUMB_RGB888) {
595 thumb_err("parameter format is invalid");
596 return MS_MEDIA_ERR_INVALID_PARAMETER;
600 (origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
601 thumb_err("Original path (%s) does not exist", origin_path);
602 return MS_MEDIA_ERR_INVALID_PARAMETER;
605 thumb_dbg("Origin path : %s", origin_path);
607 int file_type = THUMB_NONE_TYPE;
608 media_thumb_info thumb_info = {0,};
609 file_type = _media_thumb_get_file_type(origin_path);
610 thumb_width = *width;
611 thumb_height = *height;
612 if(thumb_width == 0) {
613 thumb_width = THUMB_DEFAULT_WIDTH;
614 thumb_height = THUMB_DEFAULT_HEIGHT;
617 if (file_type == THUMB_IMAGE_TYPE) {
618 err = _media_thumb_image(origin_path, thumb_path, thumb_width, thumb_height, format, &thumb_info, FALSE);
619 if (err != MS_MEDIA_ERR_NONE) {
620 thumb_err("_media_thumb_image failed");
623 } else if (file_type == THUMB_VIDEO_TYPE) {
624 err = _media_thumb_video(origin_path, thumb_width, thumb_height, format, &thumb_info);
625 if (err != MS_MEDIA_ERR_NONE) {
626 thumb_err("_media_thumb_image failed");
630 thumb_err("invalid file type");
631 return MS_MEDIA_ERR_INVALID_PARAMETER;
634 if (size) *size = thumb_info.size;
635 if (width) *width = thumb_info.width;
636 if (height) *height = thumb_info.height;
637 *data = thumb_info.data;
638 if (origin_width) *origin_width = thumb_info.origin_width;
639 if (origin_height) *origin_height = thumb_info.origin_height;
640 if (alpha) *alpha = thumb_info.alpha;
641 if (is_saved) *is_saved= thumb_info.is_saved;
643 thumb_dbg("Thumb data is generated successfully (Size:%d, W:%d, H:%d) 0x%x",
644 *size, *width, *height, *data);
646 return MS_MEDIA_ERR_NONE;
649 int _thumbnail_get_raw_data(const char *origin_path,
650 media_thumb_format format,
653 unsigned char **data,
656 int err = MS_MEDIA_ERR_NONE;
657 int thumb_width = -1;
658 int thumb_height = -1;
659 const char * thumb_path = NULL;
661 if (origin_path == NULL || *width <= 0 || *height <= 0) {
662 thumb_err("Invalid parameter");
663 return MS_MEDIA_ERR_INVALID_PARAMETER;
666 if (format < MEDIA_THUMB_BGRA || format > MEDIA_THUMB_RGB888) {
667 thumb_err("parameter format is invalid");
668 return MS_MEDIA_ERR_INVALID_PARAMETER;
672 (origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
673 thumb_err("Original path (%s) does not exist", origin_path);
674 return MS_MEDIA_ERR_INVALID_PARAMETER;
677 // thumb_dbg_slog("Origin path : %s", origin_path);
679 int file_type = THUMB_NONE_TYPE;
680 media_thumb_info thumb_info = {0,};
681 file_type = _media_thumb_get_file_type(origin_path);
682 thumb_width = *width;
683 thumb_height = *height;
685 if (file_type == THUMB_IMAGE_TYPE) {
686 err = _media_thumb_image(origin_path, thumb_path, thumb_width, thumb_height, format, &thumb_info, TRUE);
687 if (err != MS_MEDIA_ERR_NONE) {
688 thumb_err("_media_thumb_image failed");
691 } else if (file_type == THUMB_VIDEO_TYPE) {
692 err = _media_thumb_video(origin_path, thumb_width, thumb_height, format, &thumb_info);
693 if (err != MS_MEDIA_ERR_NONE) {
694 thumb_err("_media_thumb_image failed");
698 thumb_err("invalid file type");
699 return MS_MEDIA_ERR_INVALID_PARAMETER;
702 if (size) *size = thumb_info.size;
703 *data = thumb_info.data;
704 *width = thumb_info.width;
705 *height = thumb_info.height;
707 //thumb_dbg("Thumb data is generated successfully (Size:%d, W:%d, H:%d) 0x%x", *size, *width, *height, *data);
709 return MS_MEDIA_ERR_NONE;
712 int _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg, uid_t uid)
714 int err = MS_MEDIA_ERR_NONE;
715 unsigned char *data = NULL;
722 char *thumb_path = NULL;
723 int need_update_db = 0;
725 bool is_saved = FALSE;
727 if (req_msg == NULL || res_msg == NULL) {
728 thumb_err("Invalid msg!");
729 return MS_MEDIA_ERR_INVALID_PARAMETER;
732 int msg_type = req_msg->msg_type;
733 const char *origin_path = req_msg->org_path;
735 media_thumb_format thumb_format = MEDIA_THUMB_BGRA;
736 thumb_w = req_msg->thumb_width;
737 thumb_h = req_msg->thumb_height;
738 thumb_path = res_msg->dst_path;
739 thumb_path[0] = '\0';
740 max_length = sizeof(res_msg->dst_path);
742 if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
743 thumb_err("origin_path does not exist in file system.");
744 return MS_MEDIA_ERR_FILE_NOT_EXIST;
747 err = _media_thumb_db_connect(uid);
748 if (err != MS_MEDIA_ERR_NONE) {
749 thumb_err("_media_thumb_mb_svc_connect failed: %d", err);
753 if (msg_type == THUMB_REQUEST_DB_INSERT) {
754 err = _media_thumb_get_thumb_from_db_with_size(origin_path, thumb_path, max_length, &need_update_db, &origin_w, &origin_h);
755 if (err == MS_MEDIA_ERR_NONE) {
756 res_msg->origin_width = origin_w;
757 res_msg->origin_height = origin_h;
758 _media_thumb_db_disconnect();
759 return MS_MEDIA_ERR_NONE;
761 if (strlen(thumb_path) == 0) {
762 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length,uid);
763 if (err != MS_MEDIA_ERR_NONE) {
764 char *default_path = _media_thumb_get_default_path(uid);
766 thumb_err("_media_thumb_get_hash_name failed - %d", err);
767 strncpy(thumb_path, default_path, max_length);
770 _media_thumb_db_disconnect();
774 thumb_path[strlen(thumb_path)] = '\0';
778 } else if (msg_type == THUMB_REQUEST_SAVE_FILE) {
779 strncpy(thumb_path, req_msg->dst_path, max_length);
781 } else if (msg_type == THUMB_REQUEST_ALL_MEDIA) {
782 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length,uid);
783 if (err != MS_MEDIA_ERR_NONE) {
784 char *default_path = _media_thumb_get_default_path(uid);
786 thumb_err("_media_thumb_get_hash_name failed - %d", err);
787 strncpy(thumb_path, default_path, max_length);
790 _media_thumb_db_disconnect();
794 thumb_path[strlen(thumb_path)] = '\0';
797 thumb_dbg_slog("Thumb path : %s", thumb_path);
799 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
800 thumb_warn("thumb path already exists in file system.. remove the existed file");
801 _media_thumb_remove_file(thumb_path);
804 err = _thumbnail_get_data(origin_path, thumb_format, thumb_path, &data, &thumb_size, &thumb_w, &thumb_h, &origin_w, &origin_h, &alpha, &is_saved);
805 if (err != MS_MEDIA_ERR_NONE) {
806 char *default_path = _media_thumb_get_default_path(uid);
807 thumb_err("_thumbnail_get_data failed - %d", err);
811 strncpy(thumb_path, default_path, max_length);
815 // _media_thumb_db_disconnect();
819 //thumb_dbg("Size : %d, W:%d, H:%d", thumb_size, thumb_w, thumb_h);
820 //thumb_dbg("Origin W:%d, Origin H:%d\n", origin_w, origin_h);
821 //thumb_dbg("Thumb : %s", thumb_path);
823 res_msg->msg_type = THUMB_RESPONSE;
824 res_msg->thumb_size = thumb_size;
825 res_msg->thumb_width = thumb_w;
826 res_msg->thumb_height = thumb_h;
827 res_msg->origin_width = origin_w;
828 res_msg->origin_height = origin_h;
830 /* If the image is transparent PNG format, make png file as thumbnail of this image */
833 err = _media_thumb_get_file_ext(origin_path, file_ext, sizeof(file_ext));
834 if (strncasecmp(file_ext, "png", 3) == 0) {
835 int len = strlen(thumb_path);
836 thumb_path[len - 3] = 'p';
837 thumb_path[len - 2] = 'n';
838 thumb_path[len - 1] = 'g';
840 thumb_dbg_slog("Thumb path is changed : %s", thumb_path);
843 if (is_saved == FALSE && data != NULL) {
844 err = _media_thumb_save_to_file_with_evas(data, thumb_w, thumb_h, alpha, thumb_path);
845 if (err != MS_MEDIA_ERR_NONE) {
846 char *default_path = _media_thumb_get_default_path(uid);
847 thumb_err("save_to_file_with_evas failed - %d", err);
850 if (msg_type == THUMB_REQUEST_DB_INSERT || msg_type == THUMB_REQUEST_ALL_MEDIA) {
852 strncpy(thumb_path, default_path, max_length);
856 _media_thumb_db_disconnect();
859 thumb_dbg("file save success");
862 thumb_dbg("file is already saved");
867 fd = open(thumb_path, O_WRONLY);
869 thumb_warn("open failed");
873 thumb_warn("fsync failed");
882 /* DB update if needed */
883 if (need_update_db == 1) {
884 err = _media_thumb_update_db(origin_path, thumb_path, res_msg->origin_width, res_msg->origin_height, uid);
885 if (err != MS_MEDIA_ERR_NONE) {
886 thumb_err("_media_thumb_update_db failed : %d", err);
890 _media_thumb_db_disconnect();
892 return MS_MEDIA_ERR_NONE;
896 _media_thumb_process_raw(thumbMsg *req_msg, thumbMsg *res_msg)
898 int err = MS_MEDIA_ERR_NONE;
899 unsigned char *data = NULL;
904 if (req_msg == NULL || res_msg == NULL) {
905 thumb_err("Invalid msg!");
906 return MS_MEDIA_ERR_INVALID_PARAMETER;
909 const char *origin_path = req_msg->org_path;
911 media_thumb_format thumb_format = MEDIA_THUMB_BGRA;
912 thumb_w = req_msg->thumb_width;
913 thumb_h = req_msg->thumb_height;
915 //thumb_dbg("origin_path : %s, thumb_w : %d, thumb_h : %d ", origin_path, thumb_w, thumb_h);
917 if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
918 thumb_err("origin_path does not exist in file system.");
919 return MS_MEDIA_ERR_FILE_NOT_EXIST;
922 err = _thumbnail_get_raw_data(origin_path, thumb_format, &thumb_w, &thumb_h, &data, &thumb_size);
924 if (err != MS_MEDIA_ERR_NONE) {
925 thumb_err("_thumbnail_get_data failed - %d", err);
929 res_msg->msg_type = THUMB_RESPONSE_RAW_DATA;
930 res_msg->thumb_width = thumb_w;
931 res_msg->thumb_height = thumb_h;
932 res_msg->thumb_size = thumb_size;
933 res_msg->thumb_data = malloc(thumb_size * sizeof(unsigned char));
934 memcpy(res_msg->thumb_data, data, thumb_size);
936 //thumb_dbg("Size : %d, W:%d, H:%d", thumb_size, thumb_w, thumb_h);
939 return MS_MEDIA_ERR_NONE;