From b80b3e0fbc9536447ee8eb91696f9db9874cc653 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20=C3=96man?= Date: Wed, 2 Mar 2011 23:47:57 +0100 Subject: [PATCH] Use content-disposition to set a filename to be used when downloading recorded files --- src/http.c | 8 ++++++-- src/http.h | 3 ++- src/webui/webui.c | 25 +++++++++++++++++++++---- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/http.c b/src/http.c index 8d94c8bc..694f56ae 100644 --- a/src/http.c +++ b/src/http.c @@ -152,7 +152,8 @@ void http_send_header(http_connection_t *hc, int rc, const char *content, int64_t contentlen, const char *encoding, const char *location, - int maxage, const char *range) + int maxage, const char *range, + const char *disposition) { struct tm tm0, *tm; htsbuf_queue_t hdrs; @@ -212,6 +213,9 @@ http_send_header(http_connection_t *hc, int rc, const char *content, htsbuf_qprintf(&hdrs, "Accept-Ranges: %s\r\n", "bytes"); htsbuf_qprintf(&hdrs, "Content-Range: %s\r\n", range); } + + if(disposition != NULL) + htsbuf_qprintf(&hdrs, "Content-Disposition: %s\r\n", disposition); htsbuf_qprintf(&hdrs, "\r\n"); @@ -228,7 +232,7 @@ http_send_reply(http_connection_t *hc, int rc, const char *content, const char *encoding, const char *location, int maxage) { http_send_header(hc, rc, content, hc->hc_reply.hq_size, - encoding, location, maxage, 0); + encoding, location, maxage, 0, NULL); if(hc->hc_no_output) return; diff --git a/src/http.h b/src/http.h index 86adf1b0..2a29b8a9 100644 --- a/src/http.h +++ b/src/http.h @@ -113,7 +113,8 @@ void http_redirect(http_connection_t *hc, const char *location); void http_send_header(http_connection_t *hc, int rc, const char *content, int64_t contentlen, const char *encoding, - const char *location, int maxage, const char *range); + const char *location, int maxage, const char *range, + const char *disposition); typedef int (http_callback_t)(http_connection_t *hc, const char *remain, void *opaque); diff --git a/src/webui/webui.c b/src/webui/webui.c index 6c721ac6..7a696652 100644 --- a/src/webui/webui.c +++ b/src/webui/webui.c @@ -115,7 +115,7 @@ page_static_file(http_connection_t *hc, const char *remain, void *opaque) return 404; } - http_send_header(hc, 200, content, st.st_size, NULL, NULL, 10, 0); + http_send_header(hc, 200, content, st.st_size, NULL, NULL, 10, 0, NULL); sendfile(hc->hc_fd, fd, NULL, st.st_size); close(fd); return 0; @@ -503,7 +503,8 @@ page_static_bundle(http_connection_t *hc, const char *remain, void *opaque) if(!strcmp(fbe->filename, remain)) { http_send_header(hc, 200, content, fbe->size, - fbe->original_size == -1 ? NULL : "gzip", NULL, 10, 0); + fbe->original_size == -1 ? NULL : "gzip", NULL, 10, 0, + NULL); /* ignore return value */ n = write(hc->hc_fd, fbe->data, fbe->size); return 0; @@ -519,12 +520,13 @@ page_static_bundle(http_connection_t *hc, const char *remain, void *opaque) static int page_dvrfile(http_connection_t *hc, const char *remain, void *opaque) { - int fd; + int fd, i; struct stat st; const char *content = NULL, *postfix, *range; dvr_entry_t *de; char *fname; char range_buf[255]; + char disposition[256]; off_t content_len, file_start, file_end, chunk; ssize_t r; @@ -584,9 +586,24 @@ page_dvrfile(http_connection_t *hc, const char *remain, void *opaque) if(file_start > 0) lseek(fd, file_start, SEEK_SET); + if(de->de_title != NULL) { + snprintf(disposition, sizeof(disposition), + "attachment; filename=%s.mkv", de->de_title); + i = 20; + while(disposition[i]) { + if(disposition[i] == ' ') + disposition[i] = '_'; + i++; + } + + } else { + disposition[0] = 0; + } + http_send_header(hc, range ? HTTP_STATUS_PARTIAL_CONTENT : HTTP_STATUS_OK, content, content_len, NULL, NULL, 10, - range ? range_buf : NULL); + range ? range_buf : NULL, + disposition[0] ? disposition : NULL); if(!hc->hc_no_output) { while(content_len > 0) {