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 "media-thumb-util.h"
23 #include "media-thumb-internal.h"
24 #include "media-thumb-debug.h"
32 #include <sys/smack.h>
34 #define GLOBAL_USER 0 //#define tzplatform_getenv(TZ_GLOBAL) //TODO
36 int _media_thumb_get_width(media_thumb_type thumb_type)
38 if (thumb_type == MEDIA_THUMB_LARGE) {
39 return THUMB_LARGE_WIDTH;
40 } else if (thumb_type == MEDIA_THUMB_SMALL) {
41 return THUMB_SMALL_WIDTH;
47 int _media_thumb_get_height(media_thumb_type thumb_type)
49 if (thumb_type == MEDIA_THUMB_LARGE) {
50 return THUMB_LARGE_HEIGHT;
51 } else if (thumb_type == MEDIA_THUMB_SMALL) {
52 return THUMB_SMALL_HEIGHT;
58 int _media_thumb_get_file_ext(const char *file_path, char *file_ext, int max_len)
62 for (i = strlen(file_path); i >= 0; i--) {
63 if ((file_path[i] == '.') && (i < strlen(file_path))) {
64 strncpy(file_ext, &file_path[i + 1], max_len);
68 /* meet the dir. no ext */
69 if (file_path[i] == '/') {
78 _media_thumb_get_file_type(const char *file_full_path)
81 char mimetype[255] = {0,};
83 if (file_full_path == NULL)
84 return MS_MEDIA_ERR_INVALID_PARAMETER;
86 /* get content type and mime type from file. */
87 ret = aul_get_mime_from_file(file_full_path, mimetype, sizeof(mimetype));
90 ("aul_get_mime_from_file fail.. Now trying to get type by extension");
92 char ext[255] = { 0 };
93 int ret = _media_thumb_get_file_ext(file_full_path, ext, sizeof(ext));
95 thumb_err("_media_thumb_get_file_ext failed");
96 return THUMB_NONE_TYPE;
99 if (strcasecmp(ext, "JPG") == 0 ||
100 strcasecmp(ext, "JPEG") == 0 ||
101 strcasecmp(ext, "PNG") == 0 ||
102 strcasecmp(ext, "GIF") == 0 ||
103 strcasecmp(ext, "AGIF") == 0 ||
104 strcasecmp(ext, "XWD") == 0 ||
105 strcasecmp(ext, "BMP") == 0 ||
106 strcasecmp(ext, "WBMP") == 0) {
107 return THUMB_IMAGE_TYPE;
108 } else if (strcasecmp(ext, "AVI") == 0 ||
109 strcasecmp(ext, "MPEG") == 0 ||
110 strcasecmp(ext, "MP4") == 0 ||
111 strcasecmp(ext, "DCF") == 0 ||
112 strcasecmp(ext, "WMV") == 0 ||
113 strcasecmp(ext, "3GPP") == 0 ||
114 strcasecmp(ext, "3GP") == 0) {
115 return THUMB_VIDEO_TYPE;
117 return THUMB_NONE_TYPE;
121 thumb_dbg("mime type : %s", mimetype);
123 /* categorize from mimetype */
124 if (strstr(mimetype, "image") != NULL) {
125 return THUMB_IMAGE_TYPE;
126 } else if (strstr(mimetype, "video") != NULL) {
127 return THUMB_VIDEO_TYPE;
130 return THUMB_NONE_TYPE;
133 int _media_thumb_get_store_type_by_path(const char *full_path)
135 if (full_path != NULL) {
136 if (strncmp(full_path, THUMB_PATH_PHONE, strlen(THUMB_PATH_PHONE)) == 0) {
138 } else if (strncmp(full_path, THUMB_PATH_MMC, strlen(THUMB_PATH_MMC)) == 0) {
146 int _media_thumb_remove_file(const char *path)
150 result = remove(path);
152 thumb_dbg("success to remove file");
155 thumb_stderror("fail to remove file[%s] result");
160 static int _mkdir(const char *dir, mode_t mode) {
165 snprintf(tmp, sizeof(tmp),"%s",dir);
167 if(tmp[len - 1] == '/')
169 for(p = tmp + 1; *p; p++)
175 return mkdir(tmp, mode);
178 static char* _media_thumb_mmc_get_path(uid_t uid)
180 char *result_psswd = NULL;
181 struct group *grpinfo = NULL;
184 result_psswd = strdup(THUMB_MMC_PATH);
185 grpinfo = getgrnam("users");
186 if(grpinfo == NULL) {
187 thumb_err("getgrnam(users) returns NULL !");
193 struct passwd *userinfo = getpwuid(uid);
194 if(userinfo == NULL) {
195 thumb_err("getpwuid(%d) returns NULL !", uid);
198 grpinfo = getgrnam("users");
199 if(grpinfo == NULL) {
200 thumb_err("getgrnam(users) returns NULL !");
203 // Compare git_t type and not group name
204 if (grpinfo->gr_gid != userinfo->pw_gid) {
205 thumb_err("UID [%d] does not belong to 'users' group!", uid);
208 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/mmc", userinfo->pw_dir);
211 _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);
216 static char* _media_thumb_phone_get_path(uid_t uid)
218 char *result_psswd = NULL;
219 struct group *grpinfo = NULL;
222 result_psswd = strdup(THUMB_PHONE_PATH);
223 grpinfo = getgrnam("users");
224 if(grpinfo == NULL) {
225 thumb_err("getgrnam(users) returns NULL !");
231 struct passwd *userinfo = getpwuid(uid);
232 if(userinfo == NULL) {
233 thumb_err("getpwuid(%d) returns NULL !", uid);
236 grpinfo = getgrnam("users");
237 if(grpinfo == NULL) {
238 thumb_err("getgrnam(users) returns NULL !");
241 // Compare git_t type and not group name
242 if (grpinfo->gr_gid != userinfo->pw_gid) {
243 thumb_err("UID [%d] does not belong to 'users' group!", uid);
246 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/phone", userinfo->pw_dir);
249 _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);
254 int _media_thumb_get_hash_name(const char *file_full_path,
255 char *thumb_hash_path, size_t max_thumb_path, uid_t uid)
258 char *thumb_dir = NULL;
259 char file_ext[255] = { 0 };
260 media_thumb_store_type store_type = -1;
262 if (file_full_path == NULL || thumb_hash_path == NULL
263 || max_thumb_path <= 0) {
265 ("file_full_path==NULL || thumb_hash_path == NULL || max_thumb_path <= 0");
269 _media_thumb_get_file_ext(file_full_path, file_ext, sizeof(file_ext));
271 store_type = _media_thumb_get_store_type_by_path(file_full_path);
272 if (store_type == THUMB_PHONE) {
273 thumb_dir = _media_thumb_phone_get_path(uid);
274 } else if (store_type == THUMB_MMC) {
275 thumb_dir = _media_thumb_mmc_get_path(uid);
277 thumb_dir = _media_thumb_phone_get_path(uid);
280 hash_name = _media_thumb_generate_hash_name(file_full_path);
284 snprintf(thumb_hash_path, max_thumb_path - 1, "%s/.%s-%s.jpg",
285 thumb_dir, file_ext, hash_name);
287 thumb_err("Error when snprintf");
289 } else if (ret_len > max_thumb_path) {
290 thumb_err("Error for the length of thumb pathname");
294 //thumb_dbg("thumb hash : %s", thumb_hash_path);
299 int _media_thumb_save_to_file_with_gdk(GdkPixbuf *data,
305 GError *error = NULL;
307 gdk_pixbuf_save(data,thumb_path,"jpeg", &error, NULL);
309 thumb_dbg ("Error saving image file %s", thumb_path);
310 g_error_free (error);
314 if(smack_setlabel(thumb_path, "User", SMACK_LABEL_ACCESS)){
315 thumb_dbg("failed chsmack -a \"User\" %s", thumb_path);
318 thumb_dbg("chsmack -a \"User\" %s", thumb_path);
324 int _thumbnail_get_data(const char *origin_path,
325 media_thumb_type thumb_type,
326 media_thumb_format format,
327 unsigned char **data,
337 int thumb_width = -1;
338 int thumb_height = -1;
340 if (origin_path == NULL || size == NULL
341 || width == NULL || height == NULL) {
342 thumb_err("Invalid parameter");
343 return MS_MEDIA_ERR_INVALID_PARAMETER;
346 if (format < MEDIA_THUMB_BGRA || format > MEDIA_THUMB_RGB888) {
347 thumb_err("parameter format is invalid");
348 return MS_MEDIA_ERR_INVALID_PARAMETER;
352 (origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
353 thumb_err("Original path (%s) does not exist", origin_path);
354 return MS_MEDIA_ERR_INVALID_PARAMETER;
357 thumb_width = _media_thumb_get_width(thumb_type);
358 if (thumb_width < 0) {
359 thumb_err("media_thumb_type is invalid");
360 return MS_MEDIA_ERR_INVALID_PARAMETER;
363 thumb_height = _media_thumb_get_height(thumb_type);
364 if (thumb_height < 0) {
365 thumb_err("media_thumb_type is invalid");
366 return MS_MEDIA_ERR_INVALID_PARAMETER;
369 thumb_dbg("Origin path : %s", origin_path);
371 int file_type = THUMB_NONE_TYPE;
372 media_thumb_info thumb_info = {0,};
373 file_type = _media_thumb_get_file_type(origin_path);
375 if (file_type == THUMB_IMAGE_TYPE) {
376 err = _media_thumb_image(origin_path, thumb_width, thumb_height, format, &thumb_info, false, uid);
378 thumb_err("_media_thumb_image failed");
382 } else if (file_type == THUMB_VIDEO_TYPE) {
383 err = _media_thumb_video(origin_path, thumb_width, thumb_height, format, &thumb_info,uid);
385 thumb_err("_media_thumb_image failed");
390 if (size) *size = thumb_info.size;
391 if (width) *width = thumb_info.width;
392 if (height) *height = thumb_info.height;
393 *data = thumb_info.gdkdata;
394 if (origin_width) *origin_width = thumb_info.origin_width;
395 if (origin_height) *origin_height = thumb_info.origin_height;
396 if (alpha) *alpha = thumb_info.alpha;
398 thumb_dbg("Thumb data is generated successfully (Size:%d, W:%d, H:%d) 0x%x",
399 *size, *width, *height, *data);
401 return MS_MEDIA_ERR_NONE;
404 int _thumbnail_get_raw_data(const char *origin_path,
405 media_thumb_format format,
406 unsigned char **data,
413 int thumb_width = -1;
414 int thumb_height = -1;
416 if (origin_path == NULL || * width <= 0 || *height <= 0) {
417 thumb_err("Invalid parameter");
418 return MS_MEDIA_ERR_INVALID_PARAMETER;
421 if (format < MEDIA_THUMB_BGRA || format > MEDIA_THUMB_RGB888) {
422 thumb_err("parameter format is invalid");
423 return MS_MEDIA_ERR_INVALID_PARAMETER;
427 (origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
428 thumb_err("Original path (%s) does not exist", origin_path);
429 return MS_MEDIA_ERR_INVALID_PARAMETER;
432 thumb_dbg("Origin path : %s", origin_path);
434 int file_type = THUMB_NONE_TYPE;
435 media_thumb_info thumb_info = {0,};
436 file_type = _media_thumb_get_file_type(origin_path);
437 thumb_width = *width;
438 thumb_height = *height;
440 if (file_type == THUMB_IMAGE_TYPE) {
441 err = _media_thumb_image(origin_path, thumb_width, thumb_height, format, &thumb_info, true, uid);
443 thumb_err("_media_thumb_image failed");
447 } else if (file_type == THUMB_VIDEO_TYPE) {
448 err = _media_thumb_video(origin_path, thumb_width, thumb_height, format, &thumb_info,uid);
450 thumb_err("_media_thumb_image failed");
455 if (size) *size = thumb_info.size;
456 if (width) *width = thumb_info.width;
457 if (height) *height = thumb_info.height;
458 *data = thumb_info.gdkdata;
460 thumb_dbg("Thumb data is generated successfully (Size:%d, W:%d, H:%d) 0x%x",
461 *size, *width, *height, *data);
463 return MS_MEDIA_ERR_NONE;