Permalink
Browse files

Add a cleanup for updating state file in case of uploading interupt

  • Loading branch information...
1 parent ccbe8f1 commit 2fa4d2153910a8a5d470a3e61238f565e5ca4506 @bigplum committed Apr 25, 2011
Showing with 64 additions and 3 deletions.
  1. +64 −3 ngx_http_upload_module.c
View
@@ -120,6 +120,7 @@ typedef struct {
typedef struct ngx_http_upload_cleanup_s {
ngx_fd_t fd;
u_char *filename;
+ u_char *state_filename;
ngx_http_headers_out_t *headers_out;
ngx_array_t *cleanup_statuses;
ngx_log_t *log;
@@ -385,6 +386,9 @@ static ngx_int_t upload_parse_request_headers(ngx_http_upload_ctx_t *upload_ctx,
static ngx_int_t upload_process_buf(ngx_http_upload_ctx_t *upload_ctx, u_char *start, u_char *end);
static ngx_int_t upload_process_raw_buf(ngx_http_upload_ctx_t *upload_ctx, u_char *start, u_char *end);
+static void
+ngx_http_upload_cleanup_part(void *data);
+
static ngx_command_t ngx_http_upload_commands[] = { /* {{{ */
/*
@@ -716,13 +720,55 @@ static ngx_str_t ngx_upload_field_part2 = { /* {{{ */
(u_char*)"\"" CRLF CRLF
}; /* }}} */
+static void
+ngx_http_upload_cleanup_part(void *data)
+{
+ ngx_http_request_t *r = data;
+ ngx_http_upload_ctx_t *u;
+ ngx_int_t rc;
+ ngx_http_upload_range_t content_range_n;
+
+ u = ngx_http_get_module_ctx(r, ngx_http_upload_module);
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+ "cleanup http upload request, out offset: %d", u->output_file.offset);
+
+ if(u->output_file.offset == u->content_range_n.end + 1){
+ return;
+ }
+
+ content_range_n.start = u->content_range_n.start;
+ content_range_n.end = u->content_range_n.start + u->output_file.offset - 1;
+ content_range_n.total = u->content_range_n.total;
+
+ rc = ngx_http_upload_merge_ranges(u, &content_range_n);
+
+ if(rc == NGX_ERROR) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0
+ , "upload cleanup: error merging ranges"
+ );
+
+ return;
+ }
+}
+
+
static ngx_int_t /* {{{ ngx_http_upload_handler */
ngx_http_upload_handler(ngx_http_request_t *r)
{
ngx_http_upload_loc_conf_t *ulcf;
ngx_http_upload_ctx_t *u;
ngx_int_t rc;
+ ngx_http_cleanup_t *cln;
+ cln = ngx_http_cleanup_add(r, 0);
+ if (cln == NULL) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ cln->handler = ngx_http_upload_cleanup_part;
+ cln->data = r;
+
+
if(r->method & NGX_HTTP_OPTIONS)
return ngx_http_upload_options_handler(r);
@@ -1163,6 +1209,7 @@ static ngx_int_t ngx_http_upload_start_handler(ngx_http_upload_ctx_t *u) { /* {{
ucln = u->cln->data;
ucln->fd = file->fd;
ucln->filename = file->name.data;
+ ucln->state_filename = u->state_file.name.data;
ucln->log = r->connection->log;
ucln->headers_out = &r->headers_out;
ucln->cleanup_statuses = ulcf->cleanup_statuses;
@@ -3844,18 +3891,32 @@ ngx_upload_cleanup_handler(void *data)
}
if(do_cleanup) {
- if(ngx_delete_file(cln->filename) == NGX_FILE_ERROR) {
+ if(ngx_delete_file(cln->filename) == NGX_FILE_ERROR) {
+ ngx_log_error(NGX_LOG_ERR, cln->log, ngx_errno
+ , "failed to remove destination file \"%s\" after http status %l"
+ , cln->filename
+ , cln->headers_out->status
+ );
+ }else
+ ngx_log_error(NGX_LOG_INFO, cln->log, 0
+ , "finished cleanup of file \"%s\" after http status %l"
+ , cln->filename
+ , cln->headers_out->status
+ );
+ if(cln->state_filename){
+ if(ngx_delete_file(cln->state_filename) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ERR, cln->log, ngx_errno
, "failed to remove destination file \"%s\" after http status %l"
- , cln->filename
+ , cln->state_filename
, cln->headers_out->status
);
}else
ngx_log_error(NGX_LOG_INFO, cln->log, 0
, "finished cleanup of file \"%s\" after http status %l"
- , cln->filename
+ , cln->state_filename
, cln->headers_out->status
);
+ }
}
}
} /* }}} */

0 comments on commit 2fa4d21

Please sign in to comment.