diff --git a/src/components/upload/get.rs b/src/components/upload/get.rs index e4bfa20..984a8f9 100644 --- a/src/components/upload/get.rs +++ b/src/components/upload/get.rs @@ -6,6 +6,8 @@ use diesel::prelude::*; #[cfg(feature = "server")] use httpserver::DB; +#[cfg(feature = "server")] +use httpserver::models::GetFile; #[cfg(feature = "server")] use httpserver::schema; @@ -37,10 +39,10 @@ async fn download_file(id: String) -> Result { use schema::file; let s_config = ServerConfig::load(); - let result = DB.with(|pool| schema::file::table.select(file::filename) + let result = DB.with(|pool| GetFile::query() .filter(file::stored_filename.eq(id.clone())) .filter(file::deleted.eq(false)) - .load::(&mut pool.get().unwrap())); + .load(&mut pool.get().unwrap())); if let Err(_) = result { return HttpError::internal_server_error("Database request failed"); @@ -56,10 +58,12 @@ async fn download_file(id: String) -> Result { let path = Path::new(&file_path); // create a filestream from raw to set the filename to the uploaded filename - let fstream = create_filestream(db_file[0].clone(), path.into()).await; + let fstream = create_filestream(db_file[0].filename.clone(), path.into()).await; match fstream { Ok(stream) => { + DB.with(|pool| diesel::update(&db_file[0]).set(file::downloads.eq(file::downloads + 1)) + .execute(&mut pool.get().unwrap())); Ok(stream) }, Err(err) => { diff --git a/src/components/upload/info.rs b/src/components/upload/info.rs index f0f24b9..a0976d4 100644 --- a/src/components/upload/info.rs +++ b/src/components/upload/info.rs @@ -22,16 +22,17 @@ struct FetchedInfo { filename: String, size: i64, created_at: DateTime, + downloads: i64, + deleted: bool } #[get("/api/upload/info?filename")] async fn get_file_info(filename: String) -> Result { use schema::file; - let result = DB.with(|pool| schema::file::table.select((file::filename, file::file_size, file::created_at)) + let result = DB.with(|pool| schema::file::table.select((file::filename, file::file_size, file::created_at, file::downloads, file::deleted)) .filter(file::stored_filename.eq(filename)) - .filter(file::deleted.eq(false)) - .load::<(String, i64, SystemTime)>(&mut pool.get().unwrap())); + .load::<(String, i64, SystemTime, i64, bool)>(&mut pool.get().unwrap())); if let Err(_) = result { return HttpError::internal_server_error("Database request failed"); @@ -43,7 +44,7 @@ async fn get_file_info(filename: String) -> Result { let db_file = &db_file[0]; - Ok(FetchedInfo { filename: db_file.0.clone(), size: db_file.1, created_at: db_file.2.into()}) + Ok(FetchedInfo { filename: db_file.0.clone(), size: db_file.1, created_at: db_file.2.into(), downloads: db_file.3, deleted: db_file.4}) } fn show_file_info(file_id: String, file_info: &FetchedInfo) -> Element { @@ -55,11 +56,17 @@ fn show_file_info(file_id: String, file_info: &FetchedInfo) -> Element { p { "File name : {file_info.filename}" } p { "This file was uploaded on the {creation_date}" } p { "File size : {byte_to_human_size(file_info.size.try_into().unwrap())}" } - a { - class: "height-20px text-blue-800 underline", - rel: "noopener noreferrer", - href: "/upload/{file_id}/dl", - "click here to download the file" + p { "This file was downloaded {file_info.downloads} times "} + if file_info.deleted { + p { "This was deleted" } + } + else { + a { + class: "height-20px text-blue-800 underline", + rel: "noopener noreferrer", + href: "/upload/{file_id}/dl", + "click here to download the file" + } } } }