summaryrefslogtreecommitdiff
path: root/src/util/media-thumb-util.c
blob: 3cccc9e9c5f298cd269a25a95d372fe00a427281 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
 * 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-thumb-util.h"
#include "media-thumb-internal.h"

#include <glib.h>
#include <aul.h>
#include <string.h>
#include <drm_client.h>
#include <sys/stat.h>
#include <grp.h>
#include <pwd.h>
#include <sys/smack.h>

#define GLOBAL_USER	0 //#define 	tzplatform_getenv(TZ_GLOBAL) //TODO

int _media_thumb_get_width(media_thumb_type thumb_type)
{
	if (thumb_type == MEDIA_THUMB_LARGE) {
		return THUMB_LARGE_WIDTH;
	} else if (thumb_type == MEDIA_THUMB_SMALL) {
		return  THUMB_SMALL_WIDTH;
	} else {
		return -1;
	}
}

int _media_thumb_get_height(media_thumb_type thumb_type)
{
	if (thumb_type == MEDIA_THUMB_LARGE) {
		return THUMB_LARGE_HEIGHT;
	} else if (thumb_type == MEDIA_THUMB_SMALL) {
		return  THUMB_SMALL_HEIGHT;
	} else {
		return -1;
	}
}

int _media_thumb_get_file_ext(const char *file_path, char *file_ext, int max_len)
{
	int i = 0;

	for (i = strlen(file_path); i >= 0; i--) {
		if ((file_path[i] == '.') && (i < strlen(file_path))) {
			strncpy(file_ext, &file_path[i + 1], max_len);
			return 0;
		}

		/* meet the dir. no ext */
		if (file_path[i] == '/') {
			return -1;
		}
	}

	return -1;
}

int
_media_thumb_get_file_type(const char *file_full_path)
{
	int ret = 0;
	drm_bool_type_e drm_type;
	drm_file_type_e drm_file_type;
	char mimetype[255];

	if (file_full_path == NULL)
		return MEDIA_THUMB_ERROR_INVALID_PARAMETER;

	ret = drm_is_drm_file(file_full_path, &drm_type);
	if (ret < 0) {
		thumb_err("drm_is_drm_file falied : %d", ret);
		drm_type = DRM_FALSE;
	}

	if (drm_type == DRM_TRUE) {
		thumb_dbg("DRM file : %s", file_full_path);

		ret = drm_get_file_type(file_full_path, &drm_file_type);
		if (ret < 0) {
			thumb_err("drm_get_file_type falied : %d", ret);
			return THUMB_NONE_TYPE;
		}

		if (drm_file_type == DRM_TYPE_UNDEFINED) {
			return THUMB_NONE_TYPE;
		} else {
			drm_content_info_s contentInfo;
			memset(&contentInfo, 0x00, sizeof(drm_content_info_s));

			ret = drm_get_content_info(file_full_path, &contentInfo);
			if (ret != DRM_RETURN_SUCCESS) {
				thumb_err("drm_get_content_info() fails. : %d", ret);
				return THUMB_NONE_TYPE;
			}
			thumb_dbg("DRM mime type: %s", contentInfo.mime_type);

			strncpy(mimetype, contentInfo.mime_type, sizeof(mimetype) - 1);
			mimetype[sizeof(mimetype) - 1] = '\0';
		}
	} else {
		/* get content type and mime type from file. */
		ret =
			aul_get_mime_from_file(file_full_path, mimetype, sizeof(mimetype));
		if (ret < 0) {
			thumb_warn
				("aul_get_mime_from_file fail.. Now trying to get type by extension");
	
			char ext[255] = { 0 };
			int ret = _media_thumb_get_file_ext(file_full_path, ext, sizeof(ext));
			if (ret < 0) {
				thumb_err("_media_thumb_get_file_ext failed");
				return THUMB_NONE_TYPE;
			}
	
			if (strcasecmp(ext, "JPG") == 0 ||
				strcasecmp(ext, "JPEG") == 0 ||
				strcasecmp(ext, "PNG") == 0 ||
				strcasecmp(ext, "GIF") == 0 ||
				strcasecmp(ext, "AGIF") == 0 ||
				strcasecmp(ext, "XWD") == 0 ||
				strcasecmp(ext, "BMP") == 0 ||
				strcasecmp(ext, "WBMP") == 0) {
				return THUMB_IMAGE_TYPE;
			} else if (strcasecmp(ext, "AVI") == 0 ||
				strcasecmp(ext, "MPEG") == 0 ||
				strcasecmp(ext, "MP4") == 0 ||
				strcasecmp(ext, "DCF") == 0 ||
				strcasecmp(ext, "WMV") == 0 ||
				strcasecmp(ext, "3GPP") == 0 ||
				strcasecmp(ext, "3GP") == 0) {
				return THUMB_VIDEO_TYPE;
			} else {
				return THUMB_NONE_TYPE;
			}
		}
	}

	thumb_dbg("mime type : %s", mimetype);

	/* categorize from mimetype */
	if (strstr(mimetype, "image") != NULL) {
		return THUMB_IMAGE_TYPE;
	} else if (strstr(mimetype, "video") != NULL) {
		return THUMB_VIDEO_TYPE;
	}

	return THUMB_NONE_TYPE;
}

int _media_thumb_get_store_type_by_path(const char *full_path)
{
	if (full_path != NULL) {
		if (strncmp
		    (full_path, THUMB_PATH_PHONE,
		     strlen(THUMB_PATH_PHONE)) == 0) {
			return THUMB_PHONE;
		} else
		    if (strncmp
			(full_path, THUMB_PATH_MMC,
			 strlen(THUMB_PATH_MMC)) == 0) {
			return THUMB_MMC;
		}
	}

	return -1;
}

int _media_thumb_remove_file(const char *path)
{
	int result = -1;

	result = remove(path);
	if (result == 0) {
		thumb_dbg("success to remove file");
		return TRUE;
	} else {
		thumb_err("fail to remove file[%s] result errno = %s", path, strerror(errno));
		return FALSE;
	}
}

static int _mkdir(const char *dir, mode_t mode) {
        char tmp[256];
        char *p = NULL;
        size_t len;

        snprintf(tmp, sizeof(tmp),"%s",dir);
        len = strlen(tmp);
        if(tmp[len - 1] == '/')
                tmp[len - 1] = 0;
        for(p = tmp + 1; *p; p++)
                if(*p == '/') {
                        *p = 0;
                        mkdir(tmp, mode);
                        *p = '/';
                }
        return mkdir(tmp, mode);
}

static char* _media_thumb_mmc_get_path(uid_t uid)
{
	char *result_psswd = NULL;
	struct group *grpinfo = NULL;
	if(uid == getuid())
	{
		result_psswd = strdup(THUMB_MMC_PATH);
		grpinfo = getgrnam("users");
		if(grpinfo == NULL) {
			thumb_err("getgrnam(users) returns NULL !");
			return NULL;
		}
	}
	else
	{
		struct passwd *userinfo = getpwuid(uid);
		if(userinfo == NULL) {
			thumb_err("getpwuid(%d) returns NULL !", uid);
			return NULL;
		}
		grpinfo = getgrnam("users");
		if(grpinfo == NULL) {
			thumb_err("getgrnam(users) returns NULL !");
			return NULL;
		}
		// Compare git_t type and not group name
		if (grpinfo->gr_gid != userinfo->pw_gid) {
			thumb_err("UID [%d] does not belong to 'users' group!", uid);
			return NULL;
		}
		asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/mmc", userinfo->pw_dir);
	}
	
	_mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);

	return result_psswd;
}

static char* _media_thumb_phone_get_path(uid_t uid)
{
	char *result_psswd = NULL;
	struct group *grpinfo = NULL;
	if(uid == getuid())
	{
		result_psswd = strdup(THUMB_PHONE_PATH);
		grpinfo = getgrnam("users");
		if(grpinfo == NULL) {
			thumb_err("getgrnam(users) returns NULL !");
			return NULL;
		}
	}
	else
	{
		struct passwd *userinfo = getpwuid(uid);
		if(userinfo == NULL) {
			thumb_err("getpwuid(%d) returns NULL !", uid);
			return NULL;
		}
		grpinfo = getgrnam("users");
		if(grpinfo == NULL) {
			thumb_err("getgrnam(users) returns NULL !");
			return NULL;
		}
		// Compare git_t type and not group name
		if (grpinfo->gr_gid != userinfo->pw_gid) {
			thumb_err("UID [%d] does not belong to 'users' group!", uid);
			return NULL;
		}
		asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/phone", userinfo->pw_dir);
	}

	_mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);

	return result_psswd;
}

int
_media_thumb_get_hash_name(const char *file_full_path,
				 char *thumb_hash_path, size_t max_thumb_path, uid_t uid)
{
	char *hash_name;
	char *thumb_dir = NULL;
	char file_ext[255] = { 0 };
	media_thumb_store_type store_type = -1;

	if (file_full_path == NULL || thumb_hash_path == NULL
	    || max_thumb_path <= 0) {
		thumb_err
		    ("file_full_path==NULL || thumb_hash_path == NULL || max_thumb_path <= 0");
		return -1;
	}

	_media_thumb_get_file_ext(file_full_path, file_ext, sizeof(file_ext));

	store_type = _media_thumb_get_store_type_by_path(file_full_path);
	if (store_type == THUMB_PHONE) {
		thumb_dir = _media_thumb_phone_get_path(uid);
	} else if (store_type == THUMB_MMC) {
		thumb_dir = _media_thumb_mmc_get_path(uid);
	} else {
		thumb_dir = _media_thumb_phone_get_path(uid);
	}

	hash_name = _media_thumb_generate_hash_name(file_full_path);

	int ret_len;
	ret_len =
	    snprintf(thumb_hash_path, max_thumb_path - 1, "%s/.%s-%s.jpg",
		     thumb_dir, file_ext, hash_name);
	if (ret_len < 0) {
		thumb_err("Error when snprintf");
		return -1;
	} else if (ret_len > max_thumb_path) {
		thumb_err("Error for the length of thumb pathname");
		return -1;
	}

	//thumb_dbg("thumb hash : %s", thumb_hash_path);

	return 0;
}

int _media_thumb_save_to_file_with_gdk(GdkPixbuf *data, 
											int w,
											int h,
											gboolean alpha,
											char *thumb_path)
{	
	GError *error = NULL;
	
	gdk_pixbuf_save(data,thumb_path,"jpeg", &error, NULL);
	if (error) {
		thumb_dbg ("Error saving image file %s", thumb_path);
		g_error_free (error);
		return -1;
	}

	if(smack_setlabel(thumb_path, "User", SMACK_LABEL_ACCESS)){
		thumb_dbg("failed chsmack -a \"User\" %s", thumb_path);
		return -1;
	} else {
		thumb_dbg("chsmack -a \"User\" %s", thumb_path);
	}

	return 0;
}

int _thumbnail_get_data(const char *origin_path, 
						media_thumb_type thumb_type, 
						media_thumb_format format, 
						unsigned char **data,
						int *size,
						int *width,
						int *height,
						int *origin_width,
						int *origin_height,
						int *alpha,
						uid_t uid)
{
	int err = -1;
	int thumb_width = -1;
	int thumb_height = -1;

	if (origin_path == NULL || size == NULL 
			|| width == NULL || height == NULL) {
		thumb_err("Invalid parameter");
		return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
	}

	if (format < MEDIA_THUMB_BGRA || format > MEDIA_THUMB_RGB888) {
		thumb_err("parameter format is invalid");
		return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
	}

	if (!g_file_test
	    (origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
			thumb_err("Original path (%s) does not exist", origin_path);
			return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
	}

	thumb_width = _media_thumb_get_width(thumb_type);
	if (thumb_width < 0) {
		thumb_err("media_thumb_type is invalid");
		return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
	}

	thumb_height = _media_thumb_get_height(thumb_type);
	if (thumb_height < 0) {
		thumb_err("media_thumb_type is invalid");
		return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
	}

	thumb_dbg("Origin path : %s", origin_path);

	int file_type = THUMB_NONE_TYPE;
	media_thumb_info thumb_info = {0,};
	file_type = _media_thumb_get_file_type(origin_path);

	if (file_type == THUMB_IMAGE_TYPE) {
		err = _media_thumb_image(origin_path, thumb_width, thumb_height, format, &thumb_info, uid);
		if (err < 0) {
			thumb_err("_media_thumb_image failed");
			return err;
		}

	} else if (file_type == THUMB_VIDEO_TYPE) {
		err = _media_thumb_video(origin_path, thumb_width, thumb_height, format, &thumb_info,uid);
		if (err < 0) {
			thumb_err("_media_thumb_image failed");
			return err;
		}
	}

	if (size) *size = thumb_info.size;
	if (width) *width = thumb_info.width;
	if (height) *height = thumb_info.height;
	*data = thumb_info.data;
	if (origin_width) *origin_width = thumb_info.origin_width;
	if (origin_height) *origin_height = thumb_info.origin_height;
	if (alpha) *alpha = thumb_info.alpha;

	thumb_dbg("Thumb data is generated successfully (Size:%d, W:%d, H:%d) 0x%x",
				*size, *width, *height, *data);

	return MEDIA_THUMB_ERROR_NONE;
}