summaryrefslogtreecommitdiff
path: root/src/media-thumbnail.c
blob: d481eed63a5c032016f0bba5d2b2a71a4cd051cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * libmedia-thumbnail
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include "media-thumbnail.h"
#include "media-thumb-debug.h"
#include "media-thumb-util.h"
#include "media-thumb-internal.h"
#include "media-thumb-ipc.h"

#include <glib.h>

int thumbnail_request_from_db(const char *origin_path, char *thumb_path, int max_length, uid_t uid)
{
	int err = MS_MEDIA_ERR_NONE;

	if (origin_path == NULL || thumb_path == NULL) {
		thumb_err("Invalid parameter");
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
			thumb_err("Original path(%s) doesn't exist.", origin_path);
			return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	if (max_length <= 0) {
		thumb_err("Length is invalid");
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	ms_user_storage_type_t store_type = -1;
	err = ms_user_get_storage_type(uid, origin_path, &store_type);

	if((err != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
		thumb_err_slog("origin path(%s) is invalid. err : [%d] store_type [%d]", origin_path, err, store_type);
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	thumb_dbg_slog("Path : %s", origin_path);

	/* Request for thumb file to the daemon "Thumbnail generator" */
	err = _media_thumb_request(THUMB_REQUEST_DB_INSERT, origin_path, thumb_path, max_length, uid);
	if (err != MS_MEDIA_ERR_NONE) {
		thumb_err("_media_thumb_request failed : %d", err);
		return err;
	}

	return MS_MEDIA_ERR_NONE;
}

int thumbnail_request_from_db_async(unsigned int request_id, const char *origin_path, ThumbFunc func, void *user_data, uid_t uid)
{
	int err = MS_MEDIA_ERR_NONE;

	if (origin_path == NULL) {
		thumb_err("Invalid parameter");
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
			thumb_err("Original path(%s) doesn't exist.", origin_path);
			return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	ms_user_storage_type_t store_type = -1;
	err = ms_user_get_storage_type(uid, origin_path, &store_type);

	if((err != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
		thumb_err_slog("origin path(%s) is invalid. err : [%d] store_type [%d]", origin_path, err, store_type);
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	thumb_dbg_slog("Path : %s", origin_path);

	thumbUserData *userData = (thumbUserData*)malloc(sizeof(thumbUserData));
	if (userData == NULL) {
		thumb_err("memory allocation failed");
		return MS_MEDIA_ERR_OUT_OF_MEMORY;
	}
	userData->func = (ThumbFunc)func;
	userData->user_data = user_data;

	/* Request for thumb file to the daemon "Thumbnail generator" */
	err = _media_thumb_request_async(THUMB_REQUEST_DB_INSERT, request_id, origin_path, userData, uid);
	if (err != MS_MEDIA_ERR_NONE) {
		thumb_err("_media_thumb_request failed : %d", err);
		SAFE_FREE(userData);
		return err;
	}

	return MS_MEDIA_ERR_NONE;
}

int thumbnail_request_extract_raw_data_async(int request_id, const char *origin_path, int width, int height, ThumbRawFunc func, void *user_data, uid_t uid)
{
	int err = MS_MEDIA_ERR_NONE;
	int exist = 0;

	if (origin_path == NULL || request_id == 0) {
		thumb_err("original path is NULL. Or there is an error in request_id.");
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	/* check the file exits actually */
	exist = open(origin_path, O_RDONLY);
	if (exist < 0) {
		thumb_err("Fail to open original_path[%s]", origin_path);
		if (errno == EACCES || errno == EPERM)
			return  MS_MEDIA_ERR_PERMISSION_DENIED;
		else
			return MS_MEDIA_ERR_INVALID_PARAMETER;
	}
	close(exist);

	thumb_dbg_slog("Path : %s", origin_path);

	thumbRawUserData *userData = (thumbRawUserData*)malloc(sizeof(thumbRawUserData));
	if (userData == NULL) {
		thumb_err("userData malloc failed : %d", err);
		return MS_MEDIA_ERR_OUT_OF_MEMORY;
	}
	userData->func = func;
	userData->user_data = user_data;

	err = _media_thumb_request_raw_data_async(THUMB_REQUEST_RAW_DATA, request_id, origin_path, width, height, userData, uid);
	if (err != MS_MEDIA_ERR_NONE) {
		thumb_err("_media_raw_thumb_request failed : %d", err);
		SAFE_FREE(userData);
		return err;
	}

	return MS_MEDIA_ERR_NONE;
}

int thumbnail_request_cancel_media(unsigned int request_id)
{
	int err = MS_MEDIA_ERR_NONE;

	err = _media_thumb_request_async(THUMB_REQUEST_CANCEL_MEDIA, request_id, NULL, NULL, 0);
	if (err != MS_MEDIA_ERR_NONE) {
		thumb_err("_media_thumb_request failed : %d", err);
		return err;
	}

	return MS_MEDIA_ERR_NONE;
}

int thumbnail_request_cancel_raw_data(int request_id)
{
	int err = MS_MEDIA_ERR_NONE;

	if (request_id == 0) {
		thumb_err("Invalid parameter");
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	err = _media_thumb_request_raw_data_async(THUMB_REQUEST_CANCEL_RAW_DATA, request_id, NULL, 0, 0, NULL, 0);
	if (err != MS_MEDIA_ERR_NONE) {
		thumb_err("_media_thumb_request failed : %d", err);
		return err;
	}

	return MS_MEDIA_ERR_NONE;
}

int thumbnail_request_cancel_all()
{
	return _media_thumb_request_cancel_all();
}