Permalink
Browse files

change variables video_filename, video_second, image_width, image_hei…

…ght to directives to be optional
  • Loading branch information...
1 parent fe9f810 commit 0d6ff9faf87ceea0013c04d209664d185beaf799 @wandenberg committed Nov 30, 2011
View
16 include/ngx_http_video_thumbextractor_module.h
@@ -7,10 +7,10 @@
#include <video_hash.h>
typedef struct {
- ngx_int_t index_video_filename;
- ngx_int_t index_video_second;
- ngx_int_t index_image_width;
- ngx_int_t index_image_height;
+ ngx_http_complex_value_t *video_filename;
+ ngx_http_complex_value_t *video_second;
+ ngx_http_complex_value_t *image_width;
+ ngx_http_complex_value_t *image_height;
ngx_uint_t jpeg_baseline;
ngx_uint_t jpeg_progressive_mode;
@@ -27,9 +27,9 @@ static ngx_int_t ngx_http_video_thumbextractor_handler(ngx_http_request_t *r);
static ngx_str_t NGX_HTTP_VIDEO_THUMBEXTRACTOR_CONTENT_TYPE = ngx_string("image/jpeg");
#define NGX_HTTP_VIDEO_THUMBEXTRACTOR_VARIABLE_REQUIRED(variable, log, msg) \
- if ((variable == NULL) || variable->not_found || (variable->len == 0)) { \
- ngx_log_error(NGX_LOG_ERR, log, 0, "video thumb extractor module: %s", msg); \
- return NGX_HTTP_BAD_REQUEST; \
- }
+ if (variable.len == 0) { \
+ ngx_log_error(NGX_LOG_ERR, log, 0, "video thumb extractor module: %s", msg); \
+ return NGX_HTTP_BAD_REQUEST; \
+ }
#endif /* NGX_HTTP_VIDEO_THUMBEXTRACTOR_MODULE_H_ */
View
8 nginx.conf
@@ -24,10 +24,10 @@ http {
location / {
video_thumbextractor;
- set $video_thumbextractor_video_filename $uri;
- set $video_thumbextractor_video_second $arg_second;
- set $video_thumbextractor_image_width $arg_width;
- set $video_thumbextractor_image_height $arg_height;
+ video_thumbextractor_video_filename $uri;
+ video_thumbextractor_video_second $arg_second;
+ video_thumbextractor_image_width $arg_width;
+ video_thumbextractor_image_height $arg_height;
}
}
}
View
31 src/ngx_http_video_thumbextractor_module.c
@@ -6,10 +6,10 @@ static ngx_int_t
ngx_http_video_thumbextractor_handler(ngx_http_request_t *r)
{
ngx_http_video_thumbextractor_loc_conf_t *vtlcf;
- ngx_http_variable_value_t *vv_filename, *vv_second, *vv_width, *vv_height;
+ ngx_str_t vv_filename = ngx_null_string, vv_second = ngx_null_string;
+ ngx_str_t vv_width = ngx_null_string, vv_height = ngx_null_string;
ngx_str_t *filename;
- ngx_uint_t second = 0, width = 0, height = 0;
- ngx_int_t rc;
+ ngx_int_t rc, second = 0, width = 0, height = 0;
caddr_t out_buffer = 0;
size_t out_len = 0;
ngx_buf_t *b;
@@ -20,30 +20,31 @@ ngx_http_video_thumbextractor_handler(ngx_http_request_t *r)
ngx_http_core_loc_conf_t *clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
// check if received a filename
- vv_filename = ngx_http_get_indexed_variable(r, vtlcf->index_video_filename);
+ ngx_http_complex_value(r, vtlcf->video_filename, &vv_filename);
NGX_HTTP_VIDEO_THUMBEXTRACTOR_VARIABLE_REQUIRED(vv_filename, r->connection->log, "filename variable is empty");
- vv_second = ngx_http_get_indexed_variable(r, vtlcf->index_video_second);
+ ngx_http_complex_value(r, vtlcf->video_second, &vv_second);
NGX_HTTP_VIDEO_THUMBEXTRACTOR_VARIABLE_REQUIRED(vv_second, r->connection->log, "second variable is empty");
- vv_width = ngx_http_get_indexed_variable(r, vtlcf->index_image_width);
- if ((vv_width != NULL) && !vv_width->not_found && (vv_width->len > 0)) {
- width = ngx_atoi(vv_width->data, vv_width->len);
+ if (vtlcf->image_width != NULL) {
+ ngx_http_complex_value(r, vtlcf->image_width, &vv_width);
+ width = ngx_atoi(vv_width.data, vv_width.len);
+ width = (width != NGX_ERROR) ? width : 0;
}
- vv_height = ngx_http_get_indexed_variable(r, vtlcf->index_image_height);
- if ((vv_height != NULL) && !vv_height->not_found && (vv_height->len > 0)) {
- height = ngx_atoi(vv_height->data, vv_height->len);
+ if (vtlcf->image_height != NULL) {
+ ngx_http_complex_value(r, vtlcf->image_height, &vv_height);
+ height = ngx_atoi(vv_height.data, vv_height.len);
+ height = (height != NGX_ERROR) ? height : 0;
}
- if ((filename = ngx_http_video_thumbextractor_create_str(r->pool, clcf->root.len + vv_filename->len)) == NULL) {
+ if ((filename = ngx_http_video_thumbextractor_create_str(r->pool, clcf->root.len + vv_filename.len)) == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "video thumb extractor module: unable to allocate memory to store full filename");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
- ngx_memcpy(ngx_copy(filename->data, clcf->root.data, clcf->root.len), vv_filename->data, vv_filename->len);
-
- second = ngx_atoi(vv_second->data, vv_second->len);
+ ngx_memcpy(ngx_copy(filename->data, clcf->root.data, clcf->root.len), vv_filename.data, vv_filename.len);
+ second = ngx_atoi(vv_second.data, vv_second.len);
if ((rc = ngx_http_video_thumbextractor_get_thumb(vtlcf, (char *)filename->data, second, width, height, &out_buffer, &out_len, r->pool, r->connection->log)) != NGX_OK) {
if (rc == NGX_ERROR) {
View
173 src/ngx_http_video_thumbextractor_module_setup.c
@@ -8,60 +8,79 @@ static ngx_int_t ngx_http_video_thumbextractor_init_worker(ngx_cycle_t *cycle);
static char *ngx_http_video_thumbextractor(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
-static ngx_str_t ngx_http_video_thumbextractor_video_filename = ngx_string("video_thumbextractor_video_filename");
-static ngx_str_t ngx_http_video_thumbextractor_video_second = ngx_string("video_thumbextractor_video_second");
-static ngx_str_t ngx_http_video_thumbextractor_image_width = ngx_string("video_thumbextractor_image_width");
-static ngx_str_t ngx_http_video_thumbextractor_image_height = ngx_string("video_thumbextractor_image_height");
-
static ngx_command_t ngx_http_video_thumbextractor_commands[] = {
{ ngx_string("video_thumbextractor"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_video_thumbextractor,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
+ { ngx_string("video_thumbextractor_video_filename"),
+ NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_http_set_complex_value_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_video_thumbextractor_loc_conf_t, video_filename),
+ NULL },
+ { ngx_string("video_thumbextractor_video_second"),
+ NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_http_set_complex_value_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_video_thumbextractor_loc_conf_t, video_second),
+ NULL },
+ { ngx_string("video_thumbextractor_image_width"),
+ NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_http_set_complex_value_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_video_thumbextractor_loc_conf_t, image_width),
+ NULL },
+ { ngx_string("video_thumbextractor_image_height"),
+ NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_http_set_complex_value_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_video_thumbextractor_loc_conf_t, image_height),
+ NULL },
{ ngx_string("video_thumbextractor_jpeg_baseline"),
- NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_num_slot,
- NGX_HTTP_LOC_CONF_OFFSET,
- offsetof(ngx_http_video_thumbextractor_loc_conf_t, jpeg_baseline),
- NULL },
- { ngx_string("video_thumbextractor_jpeg_progressive_mode"),
- NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_num_slot,
- NGX_HTTP_LOC_CONF_OFFSET,
- offsetof(ngx_http_video_thumbextractor_loc_conf_t, jpeg_progressive_mode),
- NULL },
- { ngx_string("video_thumbextractor_jpeg_optimize"),
- NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_num_slot,
- NGX_HTTP_LOC_CONF_OFFSET,
- offsetof(ngx_http_video_thumbextractor_loc_conf_t, jpeg_optimize),
- NULL },
- { ngx_string("video_thumbextractor_jpeg_optimize"),
- NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_num_slot,
- NGX_HTTP_LOC_CONF_OFFSET,
- offsetof(ngx_http_video_thumbextractor_loc_conf_t, jpeg_optimize),
- NULL },
- { ngx_string("video_thumbextractor_jpeg_smooth"),
- NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_num_slot,
- NGX_HTTP_LOC_CONF_OFFSET,
- offsetof(ngx_http_video_thumbextractor_loc_conf_t, jpeg_smooth),
- NULL },
- { ngx_string("video_thumbextractor_jpeg_quality"),
- NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_num_slot,
- NGX_HTTP_LOC_CONF_OFFSET,
- offsetof(ngx_http_video_thumbextractor_loc_conf_t, jpeg_quality),
- NULL },
- { ngx_string("video_thumbextractor_jpeg_dpi"),
- NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_num_slot,
- NGX_HTTP_LOC_CONF_OFFSET,
- offsetof(ngx_http_video_thumbextractor_loc_conf_t, jpeg_dpi),
- NULL },
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_num_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_video_thumbextractor_loc_conf_t, jpeg_baseline),
+ NULL },
+ { ngx_string("video_thumbextractor_jpeg_progressive_mode"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_num_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_video_thumbextractor_loc_conf_t, jpeg_progressive_mode),
+ NULL },
+ { ngx_string("video_thumbextractor_jpeg_optimize"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_num_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_video_thumbextractor_loc_conf_t, jpeg_optimize),
+ NULL },
+ { ngx_string("video_thumbextractor_jpeg_optimize"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_num_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_video_thumbextractor_loc_conf_t, jpeg_optimize),
+ NULL },
+ { ngx_string("video_thumbextractor_jpeg_smooth"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_num_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_video_thumbextractor_loc_conf_t, jpeg_smooth),
+ NULL },
+ { ngx_string("video_thumbextractor_jpeg_quality"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_num_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_video_thumbextractor_loc_conf_t, jpeg_quality),
+ NULL },
+ { ngx_string("video_thumbextractor_jpeg_dpi"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_num_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_video_thumbextractor_loc_conf_t, jpeg_dpi),
+ NULL },
ngx_null_command
};
@@ -107,6 +126,10 @@ ngx_http_video_thumbextractor_create_loc_conf(ngx_conf_t *cf)
}
conf->enabled = NGX_CONF_UNSET;
+ conf->video_filename = NULL;
+ conf->video_second = NULL;
+ conf->image_width = NULL;
+ conf->image_height = NULL;
conf->jpeg_baseline = NGX_CONF_UNSET_UINT;
conf->jpeg_progressive_mode = NGX_CONF_UNSET_UINT;
conf->jpeg_optimize = NGX_CONF_UNSET_UINT;
@@ -131,6 +154,22 @@ ngx_http_video_thumbextractor_merge_loc_conf(ngx_conf_t *cf, void *parent, void
return NGX_CONF_OK;
}
+ if (conf->video_filename == NULL) {
+ conf->video_filename = prev->video_filename;
+ }
+
+ if (conf->video_second == NULL) {
+ conf->video_second = prev->video_second;
+ }
+
+ if (conf->image_width == NULL) {
+ conf->image_width = prev->image_width;
+ }
+
+ if (conf->image_height == NULL) {
+ conf->image_height = prev->image_height;
+ }
+
ngx_conf_merge_uint_value(conf->jpeg_baseline, prev->jpeg_baseline, 1);
ngx_conf_merge_uint_value(conf->jpeg_progressive_mode, prev->jpeg_progressive_mode, 0);
ngx_conf_merge_uint_value(conf->jpeg_optimize, prev->jpeg_optimize, 100);
@@ -140,13 +179,23 @@ ngx_http_video_thumbextractor_merge_loc_conf(ngx_conf_t *cf, void *parent, void
// sanity checks
+ if (conf->video_filename == NULL) {
+ ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "video thumbextractor module: video_thumbextractor_video_filename must be defined");
+ return NGX_CONF_ERROR;
+ }
+
+ if (conf->video_second == NULL) {
+ ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "video thumbextractor module: video_thumbextractor_video_second must be defined");
+ return NGX_CONF_ERROR;
+ }
+
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_video_thumbextractor_init_worker(ngx_cycle_t *cycle)
{
- ngx_http_video_thumbextractor_init_libraries();
+ ngx_http_video_thumbextractor_init_libraries();
return NGX_OK;
}
@@ -157,33 +206,9 @@ ngx_http_video_thumbextractor(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
ngx_http_core_loc_conf_t *clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
ngx_http_video_thumbextractor_loc_conf_t *vtlcf = conf;
- clcf->handler = ngx_http_video_thumbextractor_handler;
-
- vtlcf->enabled = 1;
-
- vtlcf->index_video_filename = ngx_http_get_variable_index(cf, &ngx_http_video_thumbextractor_video_filename);
- if (vtlcf->index_video_filename == NGX_ERROR) {
- ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "video thumbextractor module: video_thumbextractor_video_filename variable could not be defined");
- return NGX_CONF_ERROR;
- }
-
- vtlcf->index_video_second = ngx_http_get_variable_index(cf, &ngx_http_video_thumbextractor_video_second);
- if (vtlcf->index_video_second == NGX_ERROR) {
- ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "video thumbextractor module: ngx_http_video_thumbextractor_video_second variable could not be defined");
- return NGX_CONF_ERROR;
- }
-
- vtlcf->index_image_width = ngx_http_get_variable_index(cf, &ngx_http_video_thumbextractor_image_width);
- if (vtlcf->index_image_width == NGX_ERROR) {
- ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "video thumbextractor module: ngx_http_video_thumbextractor_image_width variable could not be defined");
- return NGX_CONF_ERROR;
- }
-
- vtlcf->index_image_height = ngx_http_get_variable_index(cf, &ngx_http_video_thumbextractor_image_height);
- if (vtlcf->index_image_height == NGX_ERROR) {
- ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "video thumbextractor module: ngx_http_video_thumbextractor_image_height variable could not be defined");
- return NGX_CONF_ERROR;
- }
+ clcf->handler = ngx_http_video_thumbextractor_handler;
+
+ vtlcf->enabled = 1;
return NGX_CONF_OK;
}

0 comments on commit 0d6ff9f

Please sign in to comment.