From 313da6b583377c3f544418046a2a073c27dc4ad7 Mon Sep 17 00:00:00 2001 From: neri Date: Sun, 4 Apr 2021 14:33:37 +0200 Subject: [PATCH] Always display content as text if user uploaded it as such Fixes #8 --- src/download.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/download.rs b/src/download.rs index 0812669..4087617 100644 --- a/src/download.rs +++ b/src/download.rs @@ -12,8 +12,8 @@ use sqlx::{ Row, }; -use crate::config::Config; use crate::deleter; +use crate::{config::Config, file_kind::FileKind}; const VIEW_HTML: &str = include_str!("../template/view.html"); @@ -23,10 +23,11 @@ pub async fn download( config: web::Data, ) -> Result { let id = req.match_info().query("id"); - let mut rows = - sqlx::query("SELECT file_id, file_name, delete_on_download from files WHERE file_id = $1") - .bind(id) - .fetch(db.as_ref()); + let mut rows = sqlx::query( + "SELECT file_id, file_name, kind, delete_on_download from files WHERE file_id = $1", + ) + .bind(id) + .fetch(db.as_ref()); let row: PgRow = rows .try_next() .await @@ -35,13 +36,15 @@ pub async fn download( let file_id: String = row.get("file_id"); let file_name: String = row.get("file_name"); + let file_kind: String = row.get("kind"); let delete_on_download: bool = row.get("delete_on_download"); let mut path = config.files_dir.clone(); path.push(&file_id); let download = req.query_string().contains("dl"); let (content_type, mut content_disposition) = get_content_types(&path, &file_name); - let response = if content_type.type_() == mime::TEXT && !download { + let is_text = file_kind == FileKind::Text.to_string() || content_type.type_() == mime::TEXT; + let response = if is_text && !download { let content = fs::read_to_string(path).await.map_err(|_| { error::ErrorInternalServerError("this file should be here but could not be found") })?;