Fix wrong error type
[platform/core/api/thumbnail-util.git] / src / thumbnail_util.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <thumbnail_util.h>
18 #include <thumbnail_util_private.h>
19
20 /* For async API */
21 #include <media-thumbnail.h>
22 #include <media-util.h>
23
24 /* For sync API */
25 #include <aul.h>
26
27 static int __thumbnail_util_error_capi(int internal_error)
28 {
29         switch (internal_error) {
30         case MS_MEDIA_ERR_NONE:
31                 return THUMBNAIL_UTIL_ERROR_NONE;
32         case MS_MEDIA_ERR_INVALID_PARAMETER:
33                 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
34         case MS_MEDIA_ERR_OUT_OF_MEMORY:
35                 return THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY;
36         case MS_MEDIA_ERR_PERMISSION_DENIED:
37                 return THUMBNAIL_UTIL_ERROR_PERMISSION_DENIED;
38         case MS_MEDIA_ERR_THUMB_TOO_BIG:
39         case MS_MEDIA_ERR_THUMB_UNSUPPORTED:
40                 return THUMBNAIL_UTIL_ERROR_UNSUPPORTED_CONTENT;
41         default:
42                 return THUMBNAIL_UTIL_ERROR_INVALID_OPERATION;
43         }
44 }
45 // LCOV_EXCL_START
46 int thumbnail_util_create(thumbnail_h *thumb)
47 {
48         thumbnail_util_warn("DEPRECATION WARNING: thumbnail_util_create() is deprecated and will be removed from next release.");
49         return THUMBNAIL_UTIL_ERROR_NONE;
50 }
51
52 int thumbnail_util_extract(thumbnail_h thumb, thumbnail_extracted_cb callback, void *user_data, char **request_id)
53 {
54         thumbnail_util_warn("DEPRECATION WARNING: thumbnail_util_extract() is deprecated and will be removed from next release. Use thumbnail_util_extract_to_file() or thumbnail_util_extract_to_buffer() instead.");
55         return THUMBNAIL_UTIL_ERROR_NONE;
56 }
57
58 int thumbnail_util_set_path(thumbnail_h thumb, const char *path)
59 {
60         thumbnail_util_warn("DEPRECATION WARNING: thumbnail_util_set_path() is deprecated and will be removed from next release.");
61         return THUMBNAIL_UTIL_ERROR_NONE;
62 }
63
64 int thumbnail_util_set_size(thumbnail_h thumb, int width, int height)
65 {
66         thumbnail_util_warn("DEPRECATION WARNING: thumbnail_util_set_size() is deprecated and will be removed from next release.");
67         return THUMBNAIL_UTIL_ERROR_NONE;
68 }
69
70 int thumbnail_util_cancel(thumbnail_h thumb, const char *request_id)
71 {
72         thumbnail_util_warn("DEPRECATION WARNING: thumbnail_util_cancel() is deprecated and will be removed from next release.");
73         return THUMBNAIL_UTIL_ERROR_NONE;
74 }
75
76 int thumbnail_util_destroy(thumbnail_h thumb)
77 {
78         thumbnail_util_warn("DEPRECATION WARNING: thumbnail_util_destroy() is deprecated and will be removed from next release.");
79         return THUMBNAIL_UTIL_ERROR_NONE;
80 }
81 // LCOV_EXCL_STOP
82 static int __thumbnail_util_check_media_type(const char *path, thumbnail_util_media_type_e *type)
83 {
84         int ret = THUMBNAIL_UTIL_ERROR_NONE;
85         char mimetype[255] = {0,};
86         const char *unsupported_type = "image/tiff";
87         const char *supported_type = "application/vnd.ms-asf";
88
89         // Check file is existed
90         thumbnail_util_retv_if(path == NULL, THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
91         if (access(path, R_OK) < 0) {
92                 if (errno == EACCES || errno == EPERM) {
93                         thumbnail_util_error("Fail to open path: Permission Denied [%s]", path);
94                         return  THUMBNAIL_UTIL_ERROR_PERMISSION_DENIED;
95                 } else {
96                         thumbnail_util_error("Fail to open path: Invalid Path [%s]", path);
97                         return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
98                 }
99         }
100
101         // Check media type
102         ret = aul_get_mime_from_file(path, mimetype, sizeof(mimetype));
103         thumbnail_util_retvm_if(ret < 0, THUMBNAIL_UTIL_ERROR_INVALID_OPERATION, "aul_get_mime_from_file failed");
104
105         thumbnail_util_debug("mime type : %s", mimetype);
106
107         /* categorize from mimetype */
108         if (strstr(mimetype, "image") != NULL) {
109                 thumbnail_util_retvm_if(!strcmp(mimetype, unsupported_type), THUMBNAIL_UTIL_ERROR_UNSUPPORTED_CONTENT, "This is unsupport file type");
110                 *type = THUMBNAIL_UTIL_IMAGE;
111                 return THUMBNAIL_UTIL_ERROR_NONE;
112         } else if (strstr(mimetype, "video") != NULL) {
113                 *type = THUMBNAIL_UTIL_VIDEO;
114                 return THUMBNAIL_UTIL_ERROR_NONE;
115         } else if (strstr(mimetype, supported_type) != NULL) {
116                 *type = THUMBNAIL_UTIL_VIDEO;
117                 return THUMBNAIL_UTIL_ERROR_NONE;
118         }
119
120         return THUMBNAIL_UTIL_ERROR_UNSUPPORTED_CONTENT;
121 }
122
123 int thumbnail_util_extract_to_buffer(const char *path, unsigned int width, unsigned int height, unsigned char **thumb_buffer, size_t *thumb_size, unsigned int *thumb_width, unsigned int *thumb_height)
124 {
125         int ret = THUMBNAIL_UTIL_ERROR_NONE;
126         thumbnail_util_media_type_e type;
127
128         /* check media type */
129         ret = __thumbnail_util_check_media_type(path, &type);
130         thumbnail_util_retvm_if(ret != THUMBNAIL_UTIL_ERROR_NONE, ret, "__thumbnail_util_check_media_type failed");
131
132         /* If image, check support format */
133         if (type == THUMBNAIL_UTIL_IMAGE)
134                 ret = create_image_thumbnail_to_buffer(path, width, height, thumb_buffer, thumb_size, thumb_width, thumb_height);
135         else
136                 ret = create_video_thumbnail_to_buffer(path, width, height, thumb_buffer, thumb_size, thumb_width, thumb_height);
137
138         return __thumbnail_util_error_capi(ret);
139 }
140
141 int thumbnail_util_extract_to_file(const char *path, unsigned int width, unsigned int height, const char *thumbnail_path)
142 {
143         int ret = THUMBNAIL_UTIL_ERROR_NONE;
144         thumbnail_util_media_type_e type;
145
146         /* check media type */
147         ret = __thumbnail_util_check_media_type(path, &type);
148         thumbnail_util_retvm_if(ret != THUMBNAIL_UTIL_ERROR_NONE, ret, "__thumbnail_util_check_media_type failed");
149
150         /* If image, check support format */
151         if (type == THUMBNAIL_UTIL_IMAGE) {
152                 ret = create_image_thumbnail_to_file(path, width, height, thumbnail_path, false);
153         } else {
154                 if (!g_regex_match_simple("[^/]\\.jpe?g$", thumbnail_path, G_REGEX_CASELESS, 0)) {
155                         thumbnail_util_error("Unsupported path or extensions [%s]", thumbnail_path);
156                         return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
157                 }
158
159                 ret = create_video_thumbnail_to_file(path, width, height, thumbnail_path, false);
160         }
161
162         return __thumbnail_util_error_capi(ret);
163 }