Fix wrongfully parsed hashes for the version_files route(s) (#353)

* Fix wrongfully parsed hashes for the version_files route(s)

* Remove the hex dependency

* Remove unwraps

.

.

Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
This commit is contained in:
4JX
2022-05-27 01:32:32 +00:00
committed by GitHub
parent d29d910ac6
commit d3b578fe8f
3 changed files with 37 additions and 16 deletions

1
Cargo.lock generated
View File

@@ -1416,7 +1416,6 @@ dependencies = [
"futures", "futures",
"futures-timer", "futures-timer",
"gumdrop", "gumdrop",
"hex",
"itertools", "itertools",
"lazy_static", "lazy_static",
"log", "log",

View File

@@ -61,4 +61,3 @@ sqlx = { version = "0.5.11", features = ["runtime-actix-rustls", "postgres", "ti
bytes = "1.1.0" bytes = "1.1.0"
dashmap = "5.2.0" dashmap = "5.2.0"
hex = "0.4.3"

View File

@@ -1,5 +1,5 @@
use super::ApiError; use super::ApiError;
use crate::database::models::version_item::QueryVersion; use crate::database::models::{version_item::QueryVersion, DatabaseError};
use crate::file_hosting::FileHost; use crate::file_hosting::FileHost;
use crate::models::projects::{GameVersion, Loader, Version}; use crate::models::projects::{GameVersion, Loader, Version};
use crate::models::teams::Permissions; use crate::models::teams::Permissions;
@@ -326,7 +326,7 @@ pub async fn get_versions_from_hashes(
) )
.await?; .await?;
let response: Vec<_> = result let response: Result<HashMap<String, Version>, ApiError> = result
.into_iter() .into_iter()
.filter_map(|row| { .filter_map(|row| {
versions_data versions_data
@@ -334,14 +334,21 @@ pub async fn get_versions_from_hashes(
.into_iter() .into_iter()
.find(|x| x.id.0 == row.version_id) .find(|x| x.id.0 == row.version_id)
.map(|v| { .map(|v| {
( if let Ok(parsed_hash) = String::from_utf8(row.hash) {
hex::encode(row.hash), Ok((
crate::models::projects::Version::from(v), parsed_hash,
) crate::models::projects::Version::from(v),
))
} else {
Err(ApiError::Database(DatabaseError::Other(format!(
"Could not parse hash for version {}",
row.version_id
))))
}
}) })
}) })
.collect(); .collect();
Ok(HttpResponse::Ok().json(response)) Ok(HttpResponse::Ok().json(response?))
} }
#[post("download")] #[post("download")]
@@ -372,10 +379,19 @@ pub async fn download_files(
let response = result let response = result
.into_iter() .into_iter()
.map(|row| (hex::encode(row.hash), row.url)) .map(|row| {
.collect::<HashMap<String, String>>(); if let Ok(parsed_hash) = String::from_utf8(row.hash) {
Ok((parsed_hash, row.url))
} else {
Err(ApiError::Database(DatabaseError::Other(format!(
"Could not parse hash for version {}",
row.version_id
))))
}
})
.collect::<Result<HashMap<String, String>, ApiError>>();
Ok(HttpResponse::Ok().json(response)) Ok(HttpResponse::Ok().json(response?))
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@@ -451,10 +467,17 @@ pub async fn update_files(
if let Some(version) = if let Some(version) =
versions.iter().find(|x| x.id.0 == row.version_id) versions.iter().find(|x| x.id.0 == row.version_id)
{ {
response.insert( if let Ok(parsed_hash) = String::from_utf8(row.hash.clone()) {
hex::encode(&row.hash), response.insert(
models::projects::Version::from(version.clone()), parsed_hash,
); models::projects::Version::from(version.clone()),
);
} else {
return Err(ApiError::Database(DatabaseError::Other(format!(
"Could not parse hash for version {}",
row.version_id
))));
}
} }
} }