rocket_community/fs/
named_file.rs

1use std::io;
2use std::ops::{Deref, DerefMut};
3use std::path::{Path, PathBuf};
4
5use tokio::fs::{File, OpenOptions};
6
7use crate::http::ContentType;
8use crate::request::Request;
9use crate::response::{self, Responder};
10
11/// A [`Responder`] that sends file data with a Content-Type based on its
12/// file extension.
13///
14/// # Example
15///
16/// A simple static file server mimicking [`FileServer`]:
17///
18/// ```rust
19/// # extern crate rocket_community as rocket;
20/// # use rocket::get;
21/// use std::path::{PathBuf, Path};
22///
23/// use rocket::fs::{NamedFile, relative};
24///
25/// #[get("/file/<path..>")]
26/// pub async fn second(path: PathBuf) -> Option<NamedFile> {
27///     let mut path = Path::new(relative!("static")).join(path);
28///     if path.is_dir() {
29///         path.push("index.html");
30///     }
31///
32///     NamedFile::open(path).await.ok()
33/// }
34/// ```
35///
36/// Always prefer to use [`FileServer`] which has more functionality and a
37/// pithier API.
38///
39/// [`FileServer`]: crate::fs::FileServer
40#[derive(Debug)]
41pub struct NamedFile(PathBuf, File);
42
43impl NamedFile {
44    /// Attempts to open a file in read-only mode.
45    ///
46    /// # Errors
47    ///
48    /// This function will return an error if path does not already exist. Other
49    /// errors may also be returned according to
50    /// [`OpenOptions::open()`](std::fs::OpenOptions::open()).
51    ///
52    /// # Example
53    ///
54    /// ```rust
55    /// # extern crate rocket_community as rocket;
56    /// # use rocket::get;
57    /// use rocket::fs::NamedFile;
58    ///
59    /// #[get("/")]
60    /// async fn index() -> Option<NamedFile> {
61    ///     NamedFile::open("index.html").await.ok()
62    /// }
63    /// ```
64    pub async fn open<P: AsRef<Path>>(path: P) -> io::Result<NamedFile> {
65        // TODO: Grab the file size here and prohibit `seek`ing later (or else
66        // the file's effective size may change), to save on the cost of doing
67        // all of those `seek`s to determine the file size. But, what happens if
68        // the file gets changed between now and then?
69        let file = File::open(path.as_ref()).await?;
70        Ok(NamedFile(path.as_ref().to_path_buf(), file))
71    }
72
73    pub async fn open_with<P: AsRef<Path>>(path: P, opts: &OpenOptions) -> io::Result<NamedFile> {
74        let file = opts.open(path.as_ref()).await?;
75        Ok(NamedFile(path.as_ref().to_path_buf(), file))
76    }
77
78    /// Retrieve the underlying `File`.
79    ///
80    /// # Example
81    ///
82    /// ```rust
83    /// # extern crate rocket_community as rocket;
84    /// use rocket::fs::NamedFile;
85    ///
86    /// # async fn f() -> std::io::Result<()> {
87    /// let named_file = NamedFile::open("index.html").await?;
88    /// let file = named_file.file();
89    /// # Ok(())
90    /// # }
91    /// ```
92    #[inline(always)]
93    pub fn file(&self) -> &File {
94        &self.1
95    }
96
97    /// Retrieve a mutable borrow to the underlying `File`.
98    ///
99    /// # Example
100    ///
101    /// ```rust
102    /// # extern crate rocket_community as rocket;
103    /// use rocket::fs::NamedFile;
104    ///
105    /// # async fn f() -> std::io::Result<()> {
106    /// let mut named_file = NamedFile::open("index.html").await?;
107    /// let file = named_file.file_mut();
108    /// # Ok(())
109    /// # }
110    /// ```
111    #[inline(always)]
112    pub fn file_mut(&mut self) -> &mut File {
113        &mut self.1
114    }
115
116    /// Take the underlying `File`.
117    ///
118    /// # Example
119    ///
120    /// ```rust
121    /// # extern crate rocket_community as rocket;
122    /// use rocket::fs::NamedFile;
123    ///
124    /// # async fn f() -> std::io::Result<()> {
125    /// let named_file = NamedFile::open("index.html").await?;
126    /// let file = named_file.take_file();
127    /// # Ok(())
128    /// # }
129    /// ```
130    #[inline(always)]
131    pub fn take_file(self) -> File {
132        self.1
133    }
134
135    /// Retrieve the path of this file.
136    ///
137    /// # Examples
138    ///
139    /// ```rust
140    /// # extern crate rocket_community as rocket;
141    /// use rocket::fs::NamedFile;
142    ///
143    /// # async fn demo_path() -> std::io::Result<()> {
144    /// let file = NamedFile::open("foo.txt").await?;
145    /// assert_eq!(file.path().as_os_str(), "foo.txt");
146    /// # Ok(())
147    /// # }
148    /// ```
149    #[inline(always)]
150    pub fn path(&self) -> &Path {
151        self.0.as_path()
152    }
153}
154
155/// Streams the named file to the client. Sets or overrides the Content-Type in
156/// the response according to the file's extension if the extension is
157/// recognized. See [`ContentType::from_extension()`] for more information. If
158/// you would like to stream a file with a different Content-Type than that
159/// implied by its extension, use a [`File`] directly.
160impl<'r> Responder<'r, 'static> for NamedFile {
161    fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> {
162        let mut response = self.1.respond_to(req)?;
163        if let Some(ext) = self.0.extension() {
164            if let Some(ct) = ContentType::from_extension(&ext.to_string_lossy()) {
165                response.set_header(ct);
166            }
167        }
168
169        Ok(response)
170    }
171}
172
173impl Deref for NamedFile {
174    type Target = File;
175
176    fn deref(&self) -> &File {
177        &self.1
178    }
179}
180
181impl DerefMut for NamedFile {
182    fn deref_mut(&mut self) -> &mut File {
183        &mut self.1
184    }
185}