1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

Implement getters for named file fields (#2689)

Co-authored-by: Janis Goldschmidt <github@aberrat.io>
This commit is contained in:
Rob Ede 2022-03-10 01:29:26 +00:00 committed by GitHub
parent a35804b89f
commit 1fd90f0b10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 6 deletions

View File

@ -1,9 +1,11 @@
# Changes # Changes
## Unreleased - 2021-xx-xx ## Unreleased - 2021-xx-xx
- Add `NamedFile::{modified, metadata, content_type, content_disposition, encoding}()` getters. [#2021]
- Update `tokio-uring` dependency to `0.3`. - Update `tokio-uring` dependency to `0.3`.
- Audio files now use `Content-Disposition: inline` instead of `attachment`. [#2645] - Audio files now use `Content-Disposition: inline` instead of `attachment`. [#2645]
[#2021]: https://github.com/actix/actix-web/pull/2021
[#2645]: https://github.com/actix/actix-web/pull/2645 [#2645]: https://github.com/actix/actix-web/pull/2645

View File

@ -23,6 +23,7 @@ use actix_web::{
use bitflags::bitflags; use bitflags::bitflags;
use derive_more::{Deref, DerefMut}; use derive_more::{Deref, DerefMut};
use futures_core::future::LocalBoxFuture; use futures_core::future::LocalBoxFuture;
use mime::Mime;
use mime_guess::from_path; use mime_guess::from_path;
use crate::{encoding::equiv_utf8_text, range::HttpRange}; use crate::{encoding::equiv_utf8_text, range::HttpRange};
@ -76,8 +77,8 @@ pub struct NamedFile {
pub(crate) md: Metadata, pub(crate) md: Metadata,
pub(crate) flags: Flags, pub(crate) flags: Flags,
pub(crate) status_code: StatusCode, pub(crate) status_code: StatusCode,
pub(crate) content_type: mime::Mime, pub(crate) content_type: Mime,
pub(crate) content_disposition: header::ContentDisposition, pub(crate) content_disposition: ContentDisposition,
pub(crate) encoding: Option<ContentEncoding>, pub(crate) encoding: Option<ContentEncoding>,
} }
@ -238,13 +239,13 @@ impl NamedFile {
Self::from_file(file, path) Self::from_file(file, path)
} }
/// Returns reference to the underlying `File` object. /// Returns reference to the underlying file object.
#[inline] #[inline]
pub fn file(&self) -> &File { pub fn file(&self) -> &File {
&self.file &self.file
} }
/// Retrieve the path of this file. /// Returns the filesystem path to this file.
/// ///
/// # Examples /// # Examples
/// ``` /// ```
@ -262,6 +263,48 @@ impl NamedFile {
self.path.as_path() self.path.as_path()
} }
/// Returns the time the file was last modified.
///
/// Returns `None` only on unsupported platforms; see [`std::fs::Metadata::modified()`].
/// Therefore, it is usually safe to unwrap this.
#[inline]
pub fn modified(&self) -> Option<SystemTime> {
self.modified
}
/// Returns the filesystem metadata associated with this file.
#[inline]
pub fn metadata(&self) -> &Metadata {
&self.md
}
/// Returns the `Content-Type` header that will be used when serving this file.
#[inline]
pub fn content_type(&self) -> &Mime {
&self.content_type
}
/// Returns the `Content-Disposition` that will be used when serving this file.
#[inline]
pub fn content_disposition(&self) -> &ContentDisposition {
&self.content_disposition
}
/// Returns the `Content-Encoding` that will be used when serving this file.
///
/// A return value of `None` indicates that the content is not already using a compressed
/// representation and may be subject to compression downstream.
#[inline]
pub fn content_encoding(&self) -> Option<ContentEncoding> {
self.encoding
}
/// Returns the status code for serving this file.
#[inline]
pub fn status_code(&self) -> &StatusCode {
&self.status_code
}
/// Set response **Status Code** /// Set response **Status Code**
pub fn set_status_code(mut self, status: StatusCode) -> Self { pub fn set_status_code(mut self, status: StatusCode) -> Self {
self.status_code = status; self.status_code = status;
@ -271,7 +314,7 @@ impl NamedFile {
/// Set the MIME Content-Type for serving this file. By default the Content-Type is inferred /// Set the MIME Content-Type for serving this file. By default the Content-Type is inferred
/// from the filename extension. /// from the filename extension.
#[inline] #[inline]
pub fn set_content_type(mut self, mime_type: mime::Mime) -> Self { pub fn set_content_type(mut self, mime_type: Mime) -> Self {
self.content_type = mime_type; self.content_type = mime_type;
self self
} }
@ -284,7 +327,7 @@ impl NamedFile {
/// filename is taken from the path provided in the `open` method after converting it to UTF-8 /// filename is taken from the path provided in the `open` method after converting it to UTF-8
/// (using `to_string_lossy`). /// (using `to_string_lossy`).
#[inline] #[inline]
pub fn set_content_disposition(mut self, cd: header::ContentDisposition) -> Self { pub fn set_content_disposition(mut self, cd: ContentDisposition) -> Self {
self.content_disposition = cd; self.content_disposition = cd;
self.flags.insert(Flags::CONTENT_DISPOSITION); self.flags.insert(Flags::CONTENT_DISPOSITION);
self self