mirror of https://github.com/rwf2/Rocket.git
Use 'tokio::fs::File' in the 'pastebin' example.
This commit is contained in:
parent
2b3d8110f1
commit
114a733d70
|
@ -6,11 +6,10 @@ mod paste_id;
|
|||
#[cfg(test)] mod tests;
|
||||
|
||||
use std::io;
|
||||
use std::fs::File;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use rocket::Data;
|
||||
use rocket::response::{content, Debug};
|
||||
use rocket::response::{content::Plain, Debug};
|
||||
use rocket::tokio::fs::File;
|
||||
|
||||
use crate::paste_id::PasteID;
|
||||
|
||||
|
@ -23,14 +22,14 @@ async fn upload(paste: Data) -> Result<String, Debug<io::Error>> {
|
|||
let filename = format!("upload/{id}", id = id);
|
||||
let url = format!("{host}/{id}\n", host = HOST, id = id);
|
||||
|
||||
paste.stream_to_file(PathBuf::from(filename)).await?;
|
||||
paste.stream_to_file(filename).await?;
|
||||
Ok(url)
|
||||
}
|
||||
|
||||
#[get("/<id>")]
|
||||
fn retrieve(id: PasteID<'_>) -> Option<content::Plain<File>> {
|
||||
async fn retrieve(id: PasteID<'_>) -> Option<Plain<File>> {
|
||||
let filename = format!("upload/{id}", id = id);
|
||||
File::open(&filename).map(|f| content::Plain(f)).ok()
|
||||
File::open(&filename).await.map(Plain).ok()
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
|
|
|
@ -213,9 +213,6 @@ mkdir upload
|
|||
For the `upload` route, we'll need to `use` a few items:
|
||||
|
||||
```rust
|
||||
use std::io;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use rocket::Data;
|
||||
use rocket::http::RawStr;
|
||||
use rocket::response::Debug;
|
||||
|
@ -266,8 +263,6 @@ Here's our version (in `src/main.rs`):
|
|||
# fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { Ok(()) }
|
||||
# }
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use rocket::Data;
|
||||
use rocket::response::Debug;
|
||||
|
||||
|
@ -278,7 +273,7 @@ async fn upload(paste: Data) -> Result<String, Debug<std::io::Error>> {
|
|||
let url = format!("{host}/{id}\n", host = "http://localhost:8000", id = id);
|
||||
|
||||
// Write the paste out to the file and return the URL.
|
||||
paste.stream_to_file(PathBuf::from(filename)).await?;
|
||||
paste.stream_to_file(filename).await?;
|
||||
Ok(url)
|
||||
}
|
||||
```
|
||||
|
@ -335,13 +330,13 @@ paste doesn't exist.
|
|||
```rust
|
||||
# #[macro_use] extern crate rocket;
|
||||
|
||||
use std::fs::File;
|
||||
use rocket::http::RawStr;
|
||||
use rocket::tokio::fs::File;
|
||||
|
||||
#[get("/<id>")]
|
||||
fn retrieve(id: &RawStr) -> Option<File> {
|
||||
async fn retrieve(id: &RawStr) -> Option<File> {
|
||||
let filename = format!("upload/{id}", id = id);
|
||||
File::open(&filename).ok()
|
||||
File::open(&filename).await.ok()
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -420,14 +415,14 @@ the `retrieve` route, preventing attacks on the `retrieve` route:
|
|||
```rust
|
||||
# #[macro_use] extern crate rocket;
|
||||
|
||||
# use std::fs::File;
|
||||
# use rocket::tokio::fs::File;
|
||||
|
||||
# type PasteId = usize;
|
||||
|
||||
#[get("/<id>")]
|
||||
fn retrieve(id: PasteId) -> Option<File> {
|
||||
async fn retrieve(id: PasteId) -> Option<File> {
|
||||
let filename = format!("upload/{id}", id = id);
|
||||
File::open(&filename).ok()
|
||||
File::open(&filename).await.ok()
|
||||
}
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in New Issue