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 "451"
45 #define THUMB_COMM_SOCK_PATH tzplatform_mkpath(TZ_SYS_RUN, "media-server/media_ipc_thumbcomm.socket")
46 #define THUMB_EMPTY_STR ""
48 static __thread char **arr_path;
49 static __thread uid_t *arr_uid;
50 static __thread int g_idx = 0;
51 static __thread int g_cur_idx = 0;
53 GMainLoop *g_thumb_server_mainloop; // defined in thumb-server.c as extern
55 static gboolean __thumb_server_send_msg_to_agent(int msg_type);
56 static void __thumb_daemon_stop_job(void);
57 static int __thumb_daemon_all_extract(uid_t uid);
58 int _thumb_daemon_process_queue_jobs(gpointer data);
59 static gboolean _thumb_server_send_deny_message(int sockfd);
61 gboolean _thumb_daemon_start_jobs(gpointer data)
64 /* Initialize ecore-evas to use evas library */
67 __thumb_server_send_msg_to_agent(MS_MSG_THUMB_SERVER_READY);
72 void _thumb_daemon_finish_jobs(void)
74 sqlite3 *sqlite_db_handle = _media_thumb_db_get_handle();
76 if (sqlite_db_handle != NULL) {
77 _media_thumb_db_disconnect();
78 thumb_dbg("sqlite3 handle is alive. So disconnect to sqlite3");
81 /* Shutdown ecore-evas */
82 ecore_evas_shutdown();
83 g_main_loop_quit(g_thumb_server_mainloop);
88 int _thumb_daemon_mmc_status(void)
93 err = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status);
96 } else if (err == -1) {
97 thumb_err("vconf_get_int failed : %d", err);
99 thumb_err("vconf_get_int Unexpected error code: %d", err);
105 void _thumb_daemon_mmc_eject_vconf_cb(void *data)
110 thumb_warn("_thumb_daemon_vconf_cb called");
112 err = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status);
114 if (status == VCONFKEY_SYSMAN_MMC_REMOVED || status == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED) {
115 thumb_warn("SD card is ejected or not mounted. So media-thumbnail-server stops jobs to extract all thumbnails");
117 __thumb_daemon_stop_job();
119 } else if (err == -1) {
120 thumb_err("vconf_get_int failed : %d", err);
122 thumb_err("vconf_get_int Unexpected error code: %d", err);
128 void _thumb_daemon_vconf_cb(void *data)
133 thumb_warn("_thumb_daemon_vconf_cb called");
135 err = vconf_get_int(VCONFKEY_SYSMAN_MMC_FORMAT, &status);
137 if (status == VCONFKEY_SYSMAN_MMC_FORMAT_COMPLETED) {
138 thumb_warn("SD card format is completed. So media-thumbnail-server stops jobs to extract all thumbnails");
140 __thumb_daemon_stop_job();
142 thumb_dbg("not completed");
144 } else if (err == -1) {
145 thumb_err("vconf_get_int failed : %d", err);
147 thumb_err("vconf_get_int Unexpected error code: %d", err);
153 static void __thumb_daemon_stop_job()
158 thumb_warn("There are %d jobs in the queue. But all jobs will be stopped", g_idx - g_cur_idx);
160 for (i = g_cur_idx; i < g_idx; i++) {
161 path = arr_path[g_cur_idx++];
168 int _thumb_daemon_process_job(thumbMsg *req_msg, thumbMsg *res_msg)
170 int err = MS_MEDIA_ERR_NONE;
172 err = _media_thumb_process(req_msg, res_msg);
173 if (err != MS_MEDIA_ERR_NONE) {
174 if (req_msg->msg_type == THUMB_REQUEST_SAVE_FILE) {
175 thumb_err("_media_thumb_process is failed: %d", err);
176 res_msg->status = THUMB_FAIL;
178 if (err != MS_MEDIA_ERR_FILE_NOT_EXIST) {
179 thumb_warn("_media_thumb_process is failed: %d, So use default thumb", err);
180 res_msg->status = THUMB_SUCCESS;
182 thumb_warn("_media_thumb_process is failed: %d, (file not exist) ", err);
183 res_msg->status = THUMB_FAIL;
187 res_msg->status = THUMB_SUCCESS;
193 static int __thumb_daemon_process_job_raw(thumbMsg *req_msg, thumbMsg *res_msg)
195 int err = MS_MEDIA_ERR_NONE;
197 err = _media_thumb_process_raw(req_msg, res_msg);
198 if (err != MS_MEDIA_ERR_NONE) {
199 thumb_warn("_media_thumb_process is failed: %d", err);
200 res_msg->status = THUMB_FAIL;
202 res_msg->status = THUMB_SUCCESS;
208 static int __thumb_daemon_all_extract(uid_t uid)
210 int err = MS_MEDIA_ERR_NONE;
211 char query_string[MAX_PATH_SIZE + 1] = { 0, };
212 char path[MAX_PATH_SIZE + 1] = { 0, };
213 sqlite3 *sqlite_db_handle = NULL;
214 sqlite3_stmt *sqlite_stmt = NULL;
216 err = _media_thumb_db_connect(uid);
217 if (err != MS_MEDIA_ERR_NONE) {
218 thumb_err("_media_thumb_db_connect failed: %d", err);
222 sqlite_db_handle = _media_thumb_db_get_handle();
223 if (sqlite_db_handle == NULL) {
224 thumb_err("sqlite handle is NULL");
225 return MS_MEDIA_ERR_INTERNAL;
228 if (_thumb_daemon_mmc_status() == VCONFKEY_SYSMAN_MMC_MOUNTED) {
229 snprintf(query_string, sizeof(query_string), SELECT_PATH_FROM_UNEXTRACTED_THUMB_MEDIA);
231 snprintf(query_string, sizeof(query_string), SELECT_PATH_FROM_UNEXTRACTED_THUMB_INTERNAL_MEDIA);
234 thumb_warn("Query: %s", query_string);
236 err = sqlite3_prepare_v2(sqlite_db_handle, query_string, strlen(query_string), &sqlite_stmt, NULL);
237 if (SQLITE_OK != err) {
238 thumb_err("prepare error [%s]", sqlite3_errmsg(sqlite_db_handle));
239 _media_thumb_db_disconnect();
240 return MS_MEDIA_ERR_INTERNAL;
244 err = sqlite3_step(sqlite_stmt);
245 if (err != SQLITE_ROW) {
246 thumb_dbg("end of row [%s]", sqlite3_errmsg(sqlite_db_handle));
250 strncpy(path, (const char *)sqlite3_column_text(sqlite_stmt, 0), sizeof(path));
251 path[sizeof(path) - 1] = '\0';
253 thumb_dbg_slog("Path : %s", path);
256 arr_path = (char**)malloc(sizeof(char*));
257 arr_uid = (uid_t*)malloc(sizeof(uid_t));
259 arr_path = (char**)realloc(arr_path, (g_idx + 1) * sizeof(char*));
260 arr_uid = (uid_t*)realloc(arr_uid, (g_idx + 1) * sizeof(uid_t));
262 arr_uid[g_idx] = uid;
263 arr_path[g_idx++] = strdup(path);
266 sqlite3_finalize(sqlite_stmt);
267 _media_thumb_db_disconnect();
269 return MS_MEDIA_ERR_NONE;
272 int _thumb_daemon_process_queue_jobs(gpointer data)
274 int err = MS_MEDIA_ERR_NONE;
278 if (g_cur_idx < g_idx) {
279 thumb_warn("There are %d jobs in the queue", g_idx - g_cur_idx);
280 thumb_dbg("Current idx : [%d]", g_cur_idx);
281 uid = arr_uid[g_cur_idx];
282 path = arr_path[g_cur_idx++];
284 thumbMsg recv_msg, res_msg;
285 memset(&recv_msg, 0x00, sizeof(thumbMsg));
286 memset(&res_msg, 0x00, sizeof(thumbMsg));
288 recv_msg.msg_type = THUMB_REQUEST_DB_INSERT;
290 strncpy(recv_msg.org_path, path, sizeof(recv_msg.org_path));
291 recv_msg.org_path[sizeof(recv_msg.org_path) - 1] = '\0';
293 err = _thumb_daemon_process_job(&recv_msg, &res_msg);
294 if (err == MS_MEDIA_ERR_FILE_NOT_EXIST) {
295 thumb_err("Thumbnail processing is failed : %d", err);
297 if (res_msg.status == THUMB_SUCCESS) {
299 err = _media_thumb_db_connect(uid);
300 if (err != MS_MEDIA_ERR_NONE) {
301 thumb_err("_media_thumb_mb_svc_connect failed: %d", err);
305 /* Need to update DB once generating thumb is done */
306 err = _media_thumb_update_db(recv_msg.org_path,
308 res_msg.origin_width,
309 res_msg.origin_height,
311 if (err != MS_MEDIA_ERR_NONE) {
312 thumb_err("_media_thumb_update_db failed : %d", err);
315 _media_thumb_db_disconnect();
323 thumb_warn("Deleting array");
326 //_media_thumb_db_disconnect();
328 __thumb_server_send_msg_to_agent(MS_MSG_THUMB_EXTRACT_ALL_DONE); // MS_MSG_THUMB_EXTRACT_ALL_DONE
336 gboolean _thumb_server_read_socket(GIOChannel *src,
337 GIOCondition condition,
340 struct sockaddr_un client_addr;
341 unsigned int client_addr_len;
344 ms_peer_credentials credentials;
347 int client_sock = -1;
349 memset((void *)&recv_msg, 0, sizeof(recv_msg));
350 memset((void *)&res_msg, 0, sizeof(res_msg));
351 memset((void *)&credentials, 0, sizeof(credentials));
353 sock = g_io_channel_unix_get_fd(src);
355 thumb_err("sock fd is invalid!");
359 client_addr_len = sizeof(client_addr);
361 if ((client_sock = accept(sock, (struct sockaddr*)&client_addr, &client_addr_len)) < 0) {
362 thumb_stderror("accept failed : %s");
366 if (ms_cynara_receive_untrusted_message_thumb(client_sock, &recv_msg, &credentials) != MS_MEDIA_ERR_NONE) {
367 thumb_err("ms_cynara_receive_untrusted_message_thumb failed");
372 if (recv_msg.msg_type == THUMB_REQUEST_KILL_SERVER && strncmp(credentials.uid, THUMB_ROOT_UID, strlen(THUMB_ROOT_UID)) != 0) {
373 thumb_err("Only root can send THUMB_REQUEST_KILL_SERVER request");
374 _thumb_server_send_deny_message(client_sock);
379 if (recv_msg.msg_type != THUMB_REQUEST_KILL_SERVER) {
380 if (ms_cynara_check(&credentials, MEDIA_STORAGE_PRIVILEGE) != MS_MEDIA_ERR_NONE) {
381 thumb_err("Cynara denied access to process request");
382 _thumb_server_send_deny_message(client_sock);
388 SAFE_FREE(credentials.smack);
389 SAFE_FREE(credentials.uid);
391 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);
393 if (recv_msg.msg_type == THUMB_REQUEST_ALL_MEDIA) {
394 thumb_dbg("All thumbnails are being extracted now");
395 __thumb_daemon_all_extract(recv_msg.uid);
396 g_idle_add(_thumb_daemon_process_queue_jobs, NULL);
397 } else if (recv_msg.msg_type == THUMB_REQUEST_RAW_DATA) {
398 __thumb_daemon_process_job_raw(&recv_msg, &res_msg);
399 } else if (recv_msg.msg_type == THUMB_REQUEST_KILL_SERVER) {
400 thumb_warn("received KILL msg from thumbnail agent.");
402 _thumb_daemon_process_job(&recv_msg, &res_msg);
405 if (res_msg.msg_type == 0)
406 res_msg.msg_type = recv_msg.msg_type;
407 res_msg.request_id = recv_msg.request_id;
408 strncpy(res_msg.org_path, recv_msg.org_path, recv_msg.origin_path_size);
409 res_msg.origin_path_size = recv_msg.origin_path_size;
410 if (res_msg.msg_type != THUMB_RESPONSE_RAW_DATA) {
411 res_msg.dest_path_size = strlen(res_msg.dst_path)+1;
412 res_msg.thumb_size = 0;
414 res_msg.dest_path_size = 1;
415 res_msg.dst_path[0] = '\0';
419 int sending_block = 0;
420 int block_size = sizeof(res_msg) - MAX_FILEPATH_LEN*2 - sizeof(unsigned char *);
421 unsigned char *buf = NULL;
422 _media_thumb_set_buffer(&res_msg, &buf, &buf_size);
424 while (buf_size > 0) {
425 if (buf_size < THUMB_BLOCK_SIZE) {
426 block_size = buf_size;
428 if (send(client_sock, buf+sending_block, block_size, 0) != block_size) {
429 thumb_stderror("sendto failed : %s");
431 sending_block += block_size;
432 buf_size -= block_size;
433 if (block_size < THUMB_BLOCK_SIZE) {
434 block_size = THUMB_BLOCK_SIZE;
438 thumb_dbg_slog("Sent data(%d) from %s", res_msg.thumb_size, res_msg.org_path);
441 SAFE_FREE(res_msg.thumb_data);
443 if (recv_msg.msg_type == THUMB_REQUEST_KILL_SERVER) {
444 thumb_warn("Shutting down...");
445 g_main_loop_quit(g_thumb_server_mainloop);
453 static gboolean __thumb_server_send_msg_to_agent(int msg_type)
456 ms_sock_info_s sock_info;
457 struct sockaddr_un serv_addr;
458 ms_thumb_server_msg send_msg;
459 sock_info.port = MS_THUMB_COMM_PORT;
461 if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info) < 0) {
462 thumb_err("ms_ipc_create_server_socket failed");
466 memset(&serv_addr, 0, sizeof(serv_addr));
468 sock = sock_info.sock_fd;
469 serv_addr.sun_family = AF_UNIX;
470 strncpy(serv_addr.sun_path, THUMB_COMM_SOCK_PATH, strlen(THUMB_COMM_SOCK_PATH));
473 /* Connecting to the thumbnail server */
474 if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
475 thumb_stderror("connect");
476 ms_ipc_delete_client_socket(&sock_info);
477 return MS_MEDIA_ERR_SOCKET_CONN;
480 send_msg.msg_type = msg_type;
482 if (send(sock, &send_msg, sizeof(ms_thumb_server_msg), 0) != sizeof(ms_thumb_server_msg)) {
483 thumb_stderror("sendto failed");
484 ms_ipc_delete_client_socket(&sock_info);
488 thumb_dbg("Sending msg to thumbnail agent[%d] is successful", send_msg.msg_type);
490 ms_ipc_delete_client_socket(&sock_info);
495 static gboolean _thumb_server_send_deny_message(int sockfd)
498 int bytes_to_send = sizeof(msg) - sizeof(msg.org_path) - sizeof(msg.dst_path);
500 msg.msg_type = THUMB_RESPONSE;
501 msg.status = THUMB_FAIL;
503 if (send(sockfd, &msg, bytes_to_send, 0) != bytes_to_send) {
504 thumb_stderror("send failed");
511 gboolean _thumb_server_prepare_socket(int *sock_fd)
514 unsigned short serv_port;
519 memset((void *)&recv_msg, 0, sizeof(recv_msg));
520 memset((void *)&res_msg, 0, sizeof(res_msg));
521 serv_port = MS_THUMB_DAEMON_PORT;
523 if (ms_ipc_create_server_socket(MS_PROTOCOL_TCP, serv_port, &sock) < 0) {
524 thumb_err("ms_ipc_create_server_socket failed");
528 if (ms_cynara_enable_credentials_passing(sock) != MS_MEDIA_ERR_NONE) {
529 thumb_err("ms_cynara_enable_credentials_passing failed");
539 int _thumbnail_get_data(const char *origin_path,
540 media_thumb_format format,
542 unsigned char **data,
551 int err = MS_MEDIA_ERR_NONE;
552 int thumb_width = -1;
553 int thumb_height = -1;
555 if (origin_path == NULL || size == NULL
556 || width == NULL || height == NULL) {
557 thumb_err("Invalid parameter");
558 return MS_MEDIA_ERR_INVALID_PARAMETER;
561 if (format < MEDIA_THUMB_BGRA || format > MEDIA_THUMB_RGB888) {
562 thumb_err("parameter format is invalid");
563 return MS_MEDIA_ERR_INVALID_PARAMETER;
567 (origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
568 thumb_err("Original path (%s) does not exist", origin_path);
569 return MS_MEDIA_ERR_INVALID_PARAMETER;
572 thumb_dbg("Origin path : %s", origin_path);
574 int file_type = THUMB_NONE_TYPE;
575 media_thumb_info thumb_info = {0,};
576 file_type = _media_thumb_get_file_type(origin_path);
577 thumb_width = *width;
578 thumb_height = *height;
579 if (thumb_width == 0 || thumb_height == 0) {
580 thumb_width = THUMB_DEFAULT_WIDTH;
581 thumb_height = THUMB_DEFAULT_HEIGHT;
584 thumb_info.is_raw = FALSE;
586 if (file_type == THUMB_IMAGE_TYPE) {
587 err = _media_thumb_image(origin_path, thumb_path, thumb_width, thumb_height, format, &thumb_info);
588 if (err != MS_MEDIA_ERR_NONE) {
589 thumb_err("_media_thumb_image failed");
592 } else if (file_type == THUMB_VIDEO_TYPE) {
593 err = _media_thumb_video(origin_path, thumb_width, thumb_height, format, &thumb_info);
594 if (err != MS_MEDIA_ERR_NONE) {
595 thumb_err("_media_thumb_image failed");
599 thumb_err("invalid file type");
600 return MS_MEDIA_ERR_INVALID_PARAMETER;
603 if (size) *size = thumb_info.size;
604 if (width) *width = thumb_info.width;
605 if (height) *height = thumb_info.height;
606 *data = thumb_info.data;
607 if (origin_width) *origin_width = thumb_info.origin_width;
608 if (origin_height) *origin_height = thumb_info.origin_height;
609 if (alpha) *alpha = thumb_info.alpha;
610 if (is_saved) *is_saved = thumb_info.is_saved;
612 thumb_dbg("Thumb data is generated successfully (Size:%d, W:%d, H:%d) 0x%x", *size, *width, *height, *data);
614 return MS_MEDIA_ERR_NONE;
617 int _thumbnail_get_raw_data(const char *origin_path,
618 media_thumb_format format,
621 unsigned char **data,
624 int err = MS_MEDIA_ERR_NONE;
625 int thumb_width = -1;
626 int thumb_height = -1;
627 const char * thumb_path = NULL;
629 if (origin_path == NULL || *width <= 0 || *height <= 0) {
630 thumb_err("Invalid parameter");
631 return MS_MEDIA_ERR_INVALID_PARAMETER;
634 if (format < MEDIA_THUMB_BGRA || format > MEDIA_THUMB_RGB888) {
635 thumb_err("parameter format is invalid");
636 return MS_MEDIA_ERR_INVALID_PARAMETER;
639 if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
640 thumb_err("Original path (%s) does not exist", origin_path);
641 return MS_MEDIA_ERR_INVALID_PARAMETER;
644 int file_type = THUMB_NONE_TYPE;
645 media_thumb_info thumb_info = {0,};
646 file_type = _media_thumb_get_file_type(origin_path);
647 thumb_width = *width;
648 thumb_height = *height;
649 thumb_info.is_raw = TRUE;
651 if (file_type == THUMB_IMAGE_TYPE) {
652 err = _media_thumb_image(origin_path, thumb_path, thumb_width, thumb_height, format, &thumb_info);
653 if (err != MS_MEDIA_ERR_NONE) {
654 thumb_err("_media_thumb_image failed");
657 } else if (file_type == THUMB_VIDEO_TYPE) {
658 err = _media_thumb_video(origin_path, thumb_width, thumb_height, format, &thumb_info);
659 if (err != MS_MEDIA_ERR_NONE) {
660 thumb_err("_media_thumb_image failed");
664 thumb_err("invalid file type");
665 return MS_MEDIA_ERR_INVALID_PARAMETER;
668 if (size) *size = thumb_info.size;
669 *data = thumb_info.data;
670 *width = thumb_info.width;
671 *height = thumb_info.height;
673 return MS_MEDIA_ERR_NONE;
676 int _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg)
678 int err = MS_MEDIA_ERR_NONE;
679 unsigned char *data = NULL;
686 char *thumb_path = NULL;
687 int need_update_db = 0;
689 bool is_saved = FALSE;
691 if (req_msg == NULL || res_msg == NULL) {
692 thumb_err("Invalid msg!");
693 return MS_MEDIA_ERR_INVALID_PARAMETER;
696 int msg_type = req_msg->msg_type;
697 const char *origin_path = req_msg->org_path;
699 media_thumb_format thumb_format = MEDIA_THUMB_BGRA;
700 thumb_w = req_msg->thumb_width;
701 thumb_h = req_msg->thumb_height;
702 thumb_path = res_msg->dst_path;
703 thumb_path[0] = '\0';
704 max_length = sizeof(res_msg->dst_path) -1;
706 if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
707 thumb_err("origin_path does not exist in file system.");
708 return MS_MEDIA_ERR_FILE_NOT_EXIST;
711 err = _media_thumb_db_connect(req_msg->uid);
712 if (err != MS_MEDIA_ERR_NONE) {
713 thumb_err("_media_thumb_mb_svc_connect failed: %d", err);
717 if (msg_type == THUMB_REQUEST_DB_INSERT) {
718 err = _media_thumb_get_thumb_from_db_with_size(origin_path, thumb_path, max_length, &need_update_db, &origin_w, &origin_h);
719 if (err == MS_MEDIA_ERR_NONE) {
720 res_msg->origin_width = origin_w;
721 res_msg->origin_height = origin_h;
722 _media_thumb_db_disconnect();
723 return MS_MEDIA_ERR_NONE;
725 if (strlen(thumb_path) == 0) {
726 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length, req_msg->uid);
727 if (err != MS_MEDIA_ERR_NONE) {
728 thumb_err("_media_thumb_get_hash_name failed - %d", err);
729 strncpy(thumb_path, THUMB_EMPTY_STR, max_length);
730 _media_thumb_db_disconnect();
734 thumb_path[strlen(thumb_path)] = '\0';
738 } else if (msg_type == THUMB_REQUEST_SAVE_FILE) {
739 strncpy(thumb_path, req_msg->dst_path, max_length);
741 } else if (msg_type == THUMB_REQUEST_ALL_MEDIA) {
742 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length, req_msg->uid);
743 if (err != MS_MEDIA_ERR_NONE) {
744 thumb_err("_media_thumb_get_hash_name failed - %d", err);
745 strncpy(thumb_path, THUMB_EMPTY_STR, max_length);
746 _media_thumb_db_disconnect();
750 thumb_path[strlen(thumb_path)] = '\0';
753 thumb_dbg_slog("Thumb path : %s", thumb_path);
755 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
756 thumb_warn("thumb path already exists in file system.. remove the existed file");
757 _media_thumb_remove_file(thumb_path);
760 err = _thumbnail_get_data(origin_path, thumb_format, thumb_path, &data, &thumb_size, &thumb_w, &thumb_h, &origin_w, &origin_h, &alpha, &is_saved);
761 if (err != MS_MEDIA_ERR_NONE) {
762 thumb_err("_thumbnail_get_data failed - %d", err);
764 strncpy(thumb_path, THUMB_EMPTY_STR, max_length);
769 res_msg->msg_type = THUMB_RESPONSE;
770 res_msg->thumb_size = thumb_size;
771 res_msg->thumb_width = thumb_w;
772 res_msg->thumb_height = thumb_h;
773 res_msg->origin_width = origin_w;
774 res_msg->origin_height = origin_h;
776 /* If the image is transparent PNG format, make png file as thumbnail of this image */
779 err = _media_thumb_get_file_ext(origin_path, file_ext, sizeof(file_ext));
780 if (strncasecmp(file_ext, "png", 3) == 0) {
781 int len = strlen(thumb_path);
782 thumb_path[len - 3] = 'p';
783 thumb_path[len - 2] = 'n';
784 thumb_path[len - 1] = 'g';
786 thumb_dbg_slog("Thumb path is changed : %s", thumb_path);
789 if (is_saved == FALSE && data != NULL) {
790 err = _media_thumb_save_to_file_with_evas(data, thumb_w, thumb_h, alpha, thumb_path);
791 if (err != MS_MEDIA_ERR_NONE) {
792 thumb_err("save_to_file_with_evas failed - %d", err);
795 if (msg_type == THUMB_REQUEST_DB_INSERT || msg_type == THUMB_REQUEST_ALL_MEDIA) {
796 strncpy(thumb_path, THUMB_EMPTY_STR, max_length);
798 _media_thumb_db_disconnect();
801 thumb_dbg("file save success");
804 thumb_dbg("file is already saved");
809 fd = open(thumb_path, O_WRONLY);
811 thumb_warn("open failed");
815 thumb_warn("fsync failed");
824 /* DB update if needed */
825 if (need_update_db == 1) {
826 err = _media_thumb_update_db(origin_path, thumb_path, res_msg->origin_width, res_msg->origin_height, req_msg->uid);
827 if (err != MS_MEDIA_ERR_NONE) {
828 thumb_err("_media_thumb_update_db failed : %d", err);
832 _media_thumb_db_disconnect();
834 return MS_MEDIA_ERR_NONE;
838 _media_thumb_process_raw(thumbMsg *req_msg, thumbMsg *res_msg)
840 int err = MS_MEDIA_ERR_NONE;
841 unsigned char *data = NULL;
846 if (req_msg == NULL || res_msg == NULL) {
847 thumb_err("Invalid msg!");
848 return MS_MEDIA_ERR_INVALID_PARAMETER;
851 const char *origin_path = req_msg->org_path;
853 media_thumb_format thumb_format = MEDIA_THUMB_BGRA;
854 thumb_w = req_msg->thumb_width;
855 thumb_h = req_msg->thumb_height;
857 err = _thumbnail_get_raw_data(origin_path, thumb_format, &thumb_w, &thumb_h, &data, &thumb_size);
859 if (err != MS_MEDIA_ERR_NONE) {
860 thumb_err("_thumbnail_get_data failed - %d", err);
864 res_msg->msg_type = THUMB_RESPONSE_RAW_DATA;
865 res_msg->thumb_width = thumb_w;
866 res_msg->thumb_height = thumb_h;
867 res_msg->thumb_size = thumb_size;
868 res_msg->thumb_data = malloc(thumb_size * sizeof(unsigned char));
869 memcpy(res_msg->thumb_data, data, thumb_size);
873 return MS_MEDIA_ERR_NONE;