Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added content-disposition from metadata. #144

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ clap = { version = "4.1.8", features = ["derive", "env"] }
dotenvy = { version = "0.15.6", features = ["clap"] }
sentry = "0.30.0"
sentry-actix = "0.30.0"
mime = "0.3.17"
mime_guess = "2.0.4"

[dependencies.sha1]
version = "^0.10.1"
Expand Down
4 changes: 4 additions & 0 deletions src/info_storages/models/file_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ impl FileInfo {
}
}

pub fn get_filename(&self) -> &str {
self.metadata.get("filename").unwrap_or(&self.id)
}

pub async fn json(&self) -> RustusResult<String> {
let info_clone = self.clone();
tokio::task::spawn_blocking(move || {
Expand Down
9 changes: 6 additions & 3 deletions src/storages/file_storage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io::Write, path::PathBuf};
use std::{fs::File, io::Write, path::PathBuf};

use actix_files::NamedFile;
use actix_web::{HttpRequest, HttpResponse};
Expand Down Expand Up @@ -76,8 +76,11 @@ impl Storage for FileStorage {
request: &HttpRequest,
) -> RustusResult<HttpResponse> {
if let Some(path) = &file_info.path {
Ok(NamedFile::open_async(path)
.await
let file = File::open(path).map_err(|err| {
error!("{:?}", err);
RustusError::FileNotFound
})?;
Ok(NamedFile::from_file(file, file_info.get_filename())
.map_err(|err| {
error!("{:?}", err);
RustusError::FileNotFound
Expand Down
6 changes: 5 additions & 1 deletion src/storages/s3_hybrid_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use std::{collections::HashMap, path::PathBuf};
use crate::{
errors::{RustusError, RustusResult},
info_storages::FileInfo,
utils::headers::generate_disposition,
};

use super::Storage;
use crate::{storages::file_storage::FileStorage, utils::dir_struct::substr_time};

use actix_web::{HttpRequest, HttpResponse, HttpResponseBuilder};
use async_trait::async_trait;
use bytes::Bytes;
Expand Down Expand Up @@ -136,7 +138,9 @@ impl Storage for S3HybridStorage {
let s3_request = Reqwest::new(&self.bucket, &key, command);
let s3_response = s3_request.response().await?;
let mut response = HttpResponseBuilder::new(actix_web::http::StatusCode::OK);
Ok(response.streaming(s3_response.bytes_stream()))
Ok(response
.insert_header(generate_disposition(file_info.get_filename()))
.streaming(s3_response.bytes_stream()))
}

async fn add_bytes(&self, file_info: &FileInfo, bytes: Bytes) -> RustusResult<()> {
Expand Down
25 changes: 24 additions & 1 deletion src/utils/headers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::str::FromStr;

use actix_web::HttpRequest;
use actix_web::{
http::header::{ContentDisposition, DispositionParam, DispositionType},
HttpRequest,
};

/// Parse header's value.
///
Expand Down Expand Up @@ -42,6 +45,26 @@ pub fn check_header(request: &HttpRequest, header_name: &str, expr: fn(&str) ->
.unwrap_or(false)
}

/// This function generates content disposition
/// based on file name.
pub fn generate_disposition(filename: &str) -> ContentDisposition {
let mime_type = mime_guess::from_path(filename).first_or_octet_stream();
let disposition = match mime_type.type_() {
mime::IMAGE | mime::TEXT | mime::AUDIO | mime::VIDEO => DispositionType::Inline,
mime::APPLICATION => match mime_type.subtype() {
mime::JAVASCRIPT | mime::JSON => DispositionType::Inline,
name if name == "wasm" => DispositionType::Inline,
_ => DispositionType::Attachment,
},
_ => DispositionType::Attachment,
};

ContentDisposition {
disposition,
parameters: vec![DispositionParam::Filename(String::from(filename))],
}
}

#[cfg(test)]
mod tests {
use super::{check_header, parse_header};
Expand Down
Loading