Skip to content

Commit

Permalink
Merge branch 'release/0.7.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
s3rius committed Nov 8, 2023
2 parents b0c8f5e + 2112d8f commit a435b59
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 10 deletions.
8 changes: 5 additions & 3 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustus"
version = "0.7.5"
version = "0.7.6"
edition = "2021"
description = "TUS protocol implementation written in Rust."
keywords = ["tus", "server", "actix-web"]
Expand Down 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
2 changes: 1 addition & 1 deletion deploy/helm/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ version: 0.2.0
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.6.1"
appVersion: "0.7.6"


dependencies:
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

0 comments on commit a435b59

Please sign in to comment.