Skip to content

Commit 6f383df

Browse files
committed
Add checksum_algorithm field to specify details of location checksum
1 parent c7b67a8 commit 6f383df

12 files changed

Lines changed: 113 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88
### Added
9-
- [747](https://github.com/thoth-pub/thoth/pull/747) - Add `Location.checksum` field
9+
- [747](https://github.com/thoth-pub/thoth/pull/747) - Add `checksum` and `checksum_algorithm` fields to `Location`
1010

1111
## [[1.1.1]](https://github.com/thoth-pub/thoth/releases/tag/v1.1.1) - 2026-04-24
1212
### Security
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
ALTER TABLE public.location
2-
DROP COLUMN IF EXISTS checksum;
2+
DROP CONSTRAINT IF EXISTS location_checksum_and_algorithm_all_or_none,
3+
DROP COLUMN IF EXISTS checksum,
4+
DROP COLUMN IF EXISTS checksum_algorithm,
5+
6+
DROP TYPE IF EXISTS public.checksum_algorithm;
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1+
CREATE TYPE public.checksum_algorithm AS ENUM (
2+
'MD5',
3+
'SHA256'
4+
);
5+
16
ALTER TABLE public.location
2-
ADD COLUMN checksum TEXT;
7+
ADD COLUMN checksum TEXT,
8+
ADD COLUMN checksum_algorithm public.checksum_algorithm,
9+
ADD CONSTRAINT location_checksum_and_algorithm_all_or_none CHECK ((checksum IS NULL AND checksum_algorithm IS NULL) OR (checksum IS NOT NULL AND checksum_algorithm IS NOT NULL));

thoth-api/src/graphql/model.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::model::{
2121
contribution::{Contribution, ContributionType},
2222
contributor::Contributor,
2323
endorsement::{Endorsement, EndorsementOrderBy},
24-
file::{File, FileType},
24+
file::{ChecksumAlgorithm, File, FileType},
2525
funding::Funding,
2626
imprint::{Imprint, ImprintField, ImprintOrderBy},
2727
institution::Institution,
@@ -1921,6 +1921,11 @@ impl Location {
19211921
self.checksum.as_ref()
19221922
}
19231923

1924+
#[graphql(description = "Algorithm used to generate the checksum (MD5 or SHA-256)")]
1925+
pub fn checksum_algorithm(&self) -> Option<&ChecksumAlgorithm> {
1926+
self.checksum_algorithm.as_ref()
1927+
}
1928+
19241929
#[graphql(description = "Date and time at which the location record was created")]
19251930
pub fn created_at(&self) -> Timestamp {
19261931
self.created_at

thoth-api/src/graphql/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ fn make_new_location(publication_id: Uuid, canonical: bool) -> NewLocation {
471471
location_platform: LocationPlatform::Other,
472472
canonical,
473473
checksum: None,
474+
checksum_algorithm: None,
474475
}
475476
}
476477

@@ -1106,6 +1107,7 @@ fn patch_location(location: &Location) -> PatchLocation {
11061107
location_platform: location.location_platform,
11071108
canonical: location.canonical,
11081109
checksum: location.checksum.clone(),
1110+
checksum_algorithm: location.checksum_algorithm,
11091111
}
11101112
}
11111113

thoth-api/src/model/file/crud.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use super::FileType;
21
use super::{
3-
upload_request_headers, File, FileCleanupCandidate, FilePolicy, FileUpload, FileUploadResponse,
4-
NewFile, NewFileUpload,
2+
upload_request_headers, ChecksumAlgorithm, File, FileCleanupCandidate, FilePolicy, FileType,
3+
FileUpload, FileUploadResponse, NewFile, NewFileUpload,
54
};
65
use crate::db::PgPool;
76
use crate::model::{
@@ -724,7 +723,7 @@ impl FileUpload {
724723
ctx: &C,
725724
work: &Work,
726725
cdn_url: &str,
727-
cdn_checksum: &str,
726+
cdn_sha256: &str,
728727
featured_video_dimensions: Option<(i32, i32)>,
729728
) -> ThothResult<()> {
730729
match self.file_type {
@@ -742,7 +741,7 @@ impl FileUpload {
742741
publication_id,
743742
work.landing_page.clone(),
744743
cdn_url,
745-
Some(cdn_checksum.to_string()),
744+
Some(cdn_sha256.to_string()),
746745
)?;
747746
}
748747
FileType::AdditionalResource => {
@@ -794,7 +793,7 @@ impl FileUpload {
794793
publication_id: Uuid,
795794
landing_page: Option<String>,
796795
full_text_url: &str,
797-
checksum: Option<String>,
796+
sha256: Option<String>,
798797
) -> ThothResult<()> {
799798
use crate::schema::location::dsl;
800799

@@ -812,7 +811,8 @@ impl FileUpload {
812811
patch.full_text_url = Some(full_text_url.to_string());
813812
patch.landing_page = landing_page;
814813
patch.canonical = true;
815-
patch.checksum = checksum;
814+
patch.checksum = sha256;
815+
patch.checksum_algorithm = Some(ChecksumAlgorithm::Sha256);
816816
if patch.canonical {
817817
patch.canonical_record_complete(ctx.db())?;
818818
}
@@ -834,7 +834,8 @@ impl FileUpload {
834834
full_text_url: Some(full_text_url.to_string()),
835835
location_platform: LocationPlatform::Thoth,
836836
canonical: false,
837-
checksum,
837+
checksum: sha256,
838+
checksum_algorithm: Some(ChecksumAlgorithm::Sha256),
838839
};
839840
let created_location = Location::create(ctx.db(), &new_location)?;
840841
let mut patch = PatchLocation::from(created_location.clone());
@@ -850,7 +851,8 @@ impl FileUpload {
850851
full_text_url: Some(full_text_url.to_string()),
851852
location_platform: LocationPlatform::Thoth,
852853
canonical: true,
853-
checksum,
854+
checksum: sha256,
855+
checksum_algorithm: Some(ChecksumAlgorithm::Sha256),
854856
};
855857
new_location.canonical_record_complete(ctx.db())?;
856858
Location::create(ctx.db(), &new_location)?;

thoth-api/src/model/file/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ pub enum FileType {
5050
WorkFeaturedVideo,
5151
}
5252

53+
#[cfg_attr(
54+
feature = "backend",
55+
derive(diesel_derive_enum::DbEnum, juniper::GraphQLEnum),
56+
graphql(description = "Algorithm used to create file checksum"),
57+
ExistingTypePath = "crate::schema::sql_types::ChecksumAlgorithm"
58+
)]
59+
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, EnumString, Display)]
60+
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
61+
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
62+
pub enum ChecksumAlgorithm {
63+
#[cfg_attr(feature = "backend", db_rename = "MD5")]
64+
Md5,
65+
#[cfg_attr(feature = "backend", db_rename = "SHA256")]
66+
Sha256,
67+
}
68+
5369
#[cfg_attr(feature = "backend", derive(diesel::Queryable))]
5470
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
5571
#[serde(rename_all = "camelCase")]

thoth-api/src/model/location/crud.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ impl Crud for Location {
6767
LocationField::Checksum => {
6868
apply_directional_order!(query, order.direction, order, checksum)
6969
}
70+
LocationField::ChecksumAlgorithm => {
71+
apply_directional_order!(query, order.direction, order, checksum_algorithm)
72+
}
7073
LocationField::CreatedAt => {
7174
apply_directional_order!(query, order.direction, order, created_at)
7275
}

thoth-api/src/model/location/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use strum::EnumString;
44
use uuid::Uuid;
55

66
use crate::graphql::types::inputs::Direction;
7-
use crate::model::Timestamp;
7+
use crate::model::{file::ChecksumAlgorithm, Timestamp};
88
#[cfg(feature = "backend")]
99
use crate::schema::location;
1010
#[cfg(feature = "backend")]
@@ -166,6 +166,7 @@ pub enum LocationField {
166166
LocationPlatform,
167167
Canonical,
168168
Checksum,
169+
ChecksumAlgorithm,
169170
CreatedAt,
170171
UpdatedAt,
171172
}
@@ -181,6 +182,7 @@ pub struct Location {
181182
pub location_platform: LocationPlatform,
182183
pub canonical: bool,
183184
pub checksum: Option<String>,
185+
pub checksum_algorithm: Option<ChecksumAlgorithm>,
184186
pub created_at: Timestamp,
185187
pub updated_at: Timestamp,
186188
}
@@ -198,6 +200,7 @@ pub struct NewLocation {
198200
pub location_platform: LocationPlatform,
199201
pub canonical: bool,
200202
pub checksum: Option<String>,
203+
pub checksum_algorithm: Option<ChecksumAlgorithm>,
201204
}
202205

203206
#[cfg_attr(
@@ -214,6 +217,7 @@ pub struct PatchLocation {
214217
pub location_platform: LocationPlatform,
215218
pub canonical: bool,
216219
pub checksum: Option<String>,
220+
pub checksum_algorithm: Option<ChecksumAlgorithm>,
217221
}
218222

219223
#[cfg_attr(feature = "backend", derive(diesel::Queryable))]
@@ -265,6 +269,7 @@ impl From<Location> for PatchLocation {
265269
location_platform: location.location_platform,
266270
canonical: location.canonical,
267271
checksum: location.checksum,
272+
checksum_algorithm: location.checksum_algorithm,
268273
}
269274
}
270275
}

0 commit comments

Comments
 (0)