Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

downloading files #9

Merged
merged 2 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 89 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ futures = "0.3.4"

# for errors
thiserror = "1.0.18"
actix-files = "0.2.1"
6 changes: 6 additions & 0 deletions http/download.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
GET http://127.0.0.1:8000/download/file/Cargo.toml
Content-Type: application/json

###
GET http://127.0.0.1:8000/download/directory
Content-Type: application/json
33 changes: 33 additions & 0 deletions src/downloads/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::utils::errors;
use actix_files::{ NamedFile, Files };
use actix_web::http::header::{ContentDisposition, DispositionType};
use actix_web::{
web::{get, scope, Path as ReqPath},
Error, HttpResponse, Scope,
};
use serde::Deserialize;
use std::path::Path;

#[derive(Deserialize)]
struct Info {
file_name: String,
}

async fn download_file(info: ReqPath<Info>) -> Result<NamedFile, Error> {
let path = Path::new(&info.file_name);
if path.exists() {
let file = NamedFile::open(&info.file_name)?;
Ok(file.set_content_disposition(ContentDisposition {
disposition: DispositionType::Attachment,
parameters: vec![],
}))
} else {
Err(errors::CustomError::NoFileFound(format!("unable to find {}", &info.file_name)).into())
}
}

pub fn register_download_routes() -> Scope {
scope("/download")
.route("/file/{file_name}", get().to(download_file))
.service(Files::new("/directory", "http").show_files_listing())
}
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ mod middleware;

// errors example
mod errors;

// downloads
mod downloads;

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
// loading .env file
Expand All @@ -43,6 +47,7 @@ async fn main() -> std::io::Result<()> {
.service(auth::auth_routes())
.service(Json::json_routes())
.service(errors::register_error_handlers())
.service(downloads::register_download_routes())
.service(private::register_private().wrap(middleware::private::CheckToken))
})
.bind("127.0.0.1:8000")?
Expand Down