Skip to content

Commit

Permalink
Add Content-Disposition
Browse files Browse the repository at this point in the history
  • Loading branch information
james58899 committed Oct 30, 2023
1 parent 2efe7a3 commit f1b6568
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/route/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{io::SeekFrom, ops::RangeInclusive, sync::Arc, time::Duration};
use actix_files::NamedFile;
use actix_web::{
body::SizedStream,
http::header::{ContentType, TryIntoHeaderPair},
http::header::{ContentDisposition, ContentType, DispositionParam, DispositionType::Inline, TryIntoHeaderPair},
route,
web::{BytesMut, Data},
HttpRequest, HttpResponse, Responder,
Expand Down Expand Up @@ -34,14 +34,17 @@ static TTL: RangeInclusive<i64> = -900..=900; // Token TTL 15 minutes
#[route("/h/{fileid}/{additional}/{filename:.*}", method = "GET", method = "HEAD")]
async fn hath(
req: HttpRequest,
Path((file_id, additional, _file_name)): Path<(String, String, String)>,
Path((file_id, additional, file_name)): Path<(String, String, String)>,
data: Data<AppState>,
) -> impl Responder {
let additional = parse_additional(&additional);

let mut keystamp = additional.get("keystamp").map(String::as_str).unwrap_or_default().split('-');
let file_index = additional.get("fileindex").map(String::as_str).unwrap_or_default();
let xres = additional.get("xres").map(String::as_str).unwrap_or_default();
let content_disposition = ContentDisposition {
disposition: Inline,
parameters: vec![DispositionParam::Filename(file_name)],
};

// keystamp check
let time = keystamp.next().unwrap_or_default();
Expand All @@ -68,7 +71,7 @@ async fn hath(
.unwrap();
let mut res = file
.use_etag(false)
.disable_content_disposition()
.set_content_disposition(content_disposition)
.set_content_type(info.mime_type())
.into_response(&req);
res.headers_mut().insert(cache_header.0, cache_header.1);
Expand Down Expand Up @@ -194,6 +197,7 @@ async fn hath(
let mut builder = HttpResponse::Ok();
builder.insert_header(ContentType(info.mime_type()));
builder.insert_header(CacheControl(vec![CacheDirective::Public, CacheDirective::MaxAge(31536000)]));
builder.insert_header(content_disposition);
builder.body(SizedStream::new(
file_size,
stream! {
Expand Down

0 comments on commit f1b6568

Please sign in to comment.