Skip to content

Commit 1fd90f0

Browse files
Implement getters for named file fields (#2689)
Co-authored-by: Janis Goldschmidt <[email protected]>
1 parent a35804b commit 1fd90f0

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

actix-files/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Changes
22

33
## Unreleased - 2021-xx-xx
4+
- Add `NamedFile::{modified, metadata, content_type, content_disposition, encoding}()` getters. [#2021]
45
- Update `tokio-uring` dependency to `0.3`.
56
- Audio files now use `Content-Disposition: inline` instead of `attachment`. [#2645]
67

8+
[#2021]: https://github.com/actix/actix-web/pull/2021
79
[#2645]: https://github.com/actix/actix-web/pull/2645
810

911

actix-files/src/named.rs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use actix_web::{
2323
use bitflags::bitflags;
2424
use derive_more::{Deref, DerefMut};
2525
use futures_core::future::LocalBoxFuture;
26+
use mime::Mime;
2627
use mime_guess::from_path;
2728

2829
use crate::{encoding::equiv_utf8_text, range::HttpRange};
@@ -76,8 +77,8 @@ pub struct NamedFile {
7677
pub(crate) md: Metadata,
7778
pub(crate) flags: Flags,
7879
pub(crate) status_code: StatusCode,
79-
pub(crate) content_type: mime::Mime,
80-
pub(crate) content_disposition: header::ContentDisposition,
80+
pub(crate) content_type: Mime,
81+
pub(crate) content_disposition: ContentDisposition,
8182
pub(crate) encoding: Option<ContentEncoding>,
8283
}
8384

@@ -238,13 +239,13 @@ impl NamedFile {
238239
Self::from_file(file, path)
239240
}
240241

241-
/// Returns reference to the underlying `File` object.
242+
/// Returns reference to the underlying file object.
242243
#[inline]
243244
pub fn file(&self) -> &File {
244245
&self.file
245246
}
246247

247-
/// Retrieve the path of this file.
248+
/// Returns the filesystem path to this file.
248249
///
249250
/// # Examples
250251
/// ```
@@ -262,6 +263,48 @@ impl NamedFile {
262263
self.path.as_path()
263264
}
264265

266+
/// Returns the time the file was last modified.
267+
///
268+
/// Returns `None` only on unsupported platforms; see [`std::fs::Metadata::modified()`].
269+
/// Therefore, it is usually safe to unwrap this.
270+
#[inline]
271+
pub fn modified(&self) -> Option<SystemTime> {
272+
self.modified
273+
}
274+
275+
/// Returns the filesystem metadata associated with this file.
276+
#[inline]
277+
pub fn metadata(&self) -> &Metadata {
278+
&self.md
279+
}
280+
281+
/// Returns the `Content-Type` header that will be used when serving this file.
282+
#[inline]
283+
pub fn content_type(&self) -> &Mime {
284+
&self.content_type
285+
}
286+
287+
/// Returns the `Content-Disposition` that will be used when serving this file.
288+
#[inline]
289+
pub fn content_disposition(&self) -> &ContentDisposition {
290+
&self.content_disposition
291+
}
292+
293+
/// Returns the `Content-Encoding` that will be used when serving this file.
294+
///
295+
/// A return value of `None` indicates that the content is not already using a compressed
296+
/// representation and may be subject to compression downstream.
297+
#[inline]
298+
pub fn content_encoding(&self) -> Option<ContentEncoding> {
299+
self.encoding
300+
}
301+
302+
/// Returns the status code for serving this file.
303+
#[inline]
304+
pub fn status_code(&self) -> &StatusCode {
305+
&self.status_code
306+
}
307+
265308
/// Set response **Status Code**
266309
pub fn set_status_code(mut self, status: StatusCode) -> Self {
267310
self.status_code = status;
@@ -271,7 +314,7 @@ impl NamedFile {
271314
/// Set the MIME Content-Type for serving this file. By default the Content-Type is inferred
272315
/// from the filename extension.
273316
#[inline]
274-
pub fn set_content_type(mut self, mime_type: mime::Mime) -> Self {
317+
pub fn set_content_type(mut self, mime_type: Mime) -> Self {
275318
self.content_type = mime_type;
276319
self
277320
}
@@ -284,7 +327,7 @@ impl NamedFile {
284327
/// filename is taken from the path provided in the `open` method after converting it to UTF-8
285328
/// (using `to_string_lossy`).
286329
#[inline]
287-
pub fn set_content_disposition(mut self, cd: header::ContentDisposition) -> Self {
330+
pub fn set_content_disposition(mut self, cd: ContentDisposition) -> Self {
288331
self.content_disposition = cd;
289332
self.flags.insert(Flags::CONTENT_DISPOSITION);
290333
self

0 commit comments

Comments
 (0)