Skip to content

Commit 1f2e2e3

Browse files
committed
refactor(transfer): extract checksum retrieval into ExtractChecksum helper function
1 parent f7f5a36 commit 1f2e2e3

File tree

2 files changed

+24
-38
lines changed

2 files changed

+24
-38
lines changed

src/aws-cpp-sdk-transfer/include/aws/transfer/TransferManager.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,22 @@ namespace Aws
338338
*/
339339
void SetChecksumForAlgorithm(const std::shared_ptr<PartState>& state, Aws::S3::Model::CompletedPart& part);
340340

341+
/**
342+
* Extracts checksum from S3 outcome result.
343+
* @param outcome The S3 outcome containing the result with checksums.
344+
* @return The checksum string, or empty string if no checksum is available.
345+
*/
346+
template<typename OutcomeType>
347+
Aws::String ExtractChecksum(const OutcomeType& outcome) {
348+
const auto& result = outcome.GetResult();
349+
if (!result.GetChecksumCRC32().empty()) return result.GetChecksumCRC32();
350+
if (!result.GetChecksumCRC32C().empty()) return result.GetChecksumCRC32C();
351+
if (!result.GetChecksumSHA256().empty()) return result.GetChecksumSHA256();
352+
if (!result.GetChecksumSHA1().empty()) return result.GetChecksumSHA1();
353+
if (!result.GetChecksumCRC64NVME().empty()) return result.GetChecksumCRC64NVME();
354+
return {};
355+
}
356+
341357
/**
342358
* Combines part checksums to calculate full object checksum for validation.
343359
* @param handle The transfer handle containing completed parts with checksums.

src/aws-cpp-sdk-transfer/source/transfer/TransferManager.cpp

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -944,23 +944,9 @@ namespace Aws
944944
// Validate full object checksum if available
945945
bool checksumValid = true;
946946
if (!handle->GetChecksum().empty()) {
947-
const auto& getResult = getObjectOutcome.GetResult();
948-
Aws::String actualChecksum;
947+
Aws::String actualChecksum = ExtractChecksum(getObjectOutcome);
949948

950-
// Get the actual checksum from GetObject response
951-
if (!getResult.GetChecksumCRC32().empty()) {
952-
actualChecksum = getResult.GetChecksumCRC32();
953-
} else if (!getResult.GetChecksumCRC32C().empty()) {
954-
actualChecksum = getResult.GetChecksumCRC32C();
955-
} else if (!getResult.GetChecksumSHA256().empty()) {
956-
actualChecksum = getResult.GetChecksumSHA256();
957-
} else if (!getResult.GetChecksumSHA1().empty()) {
958-
actualChecksum = getResult.GetChecksumSHA1();
959-
} else if (!getResult.GetChecksumCRC64NVME().empty()) {
960-
actualChecksum = getResult.GetChecksumCRC64NVME();
961-
}
962-
963-
if (!actualChecksum.empty() && actualChecksum != handle->GetChecksum()) {
949+
if (actualChecksum != handle->GetChecksum()) {
964950
checksumValid = false;
965951
Aws::Client::AWSError<Aws::S3::S3Errors> checksumError(
966952
Aws::S3::S3Errors::INTERNAL_FAILURE,
@@ -1069,17 +1055,9 @@ namespace Aws
10691055
handle->SetEtag(headObjectOutcome.GetResult().GetETag());
10701056

10711057
// Store full object checksum from HeadObject for validation
1072-
const auto& headResult = headObjectOutcome.GetResult();
1073-
if (!headResult.GetChecksumCRC32().empty()) {
1074-
handle->SetChecksum(headResult.GetChecksumCRC32());
1075-
} else if (!headResult.GetChecksumCRC32C().empty()) {
1076-
handle->SetChecksum(headResult.GetChecksumCRC32C());
1077-
} else if (!headResult.GetChecksumSHA256().empty()) {
1078-
handle->SetChecksum(headResult.GetChecksumSHA256());
1079-
} else if (!headResult.GetChecksumSHA1().empty()) {
1080-
handle->SetChecksum(headResult.GetChecksumSHA1());
1081-
} else if (!headResult.GetChecksumCRC64NVME().empty()) {
1082-
handle->SetChecksum(headResult.GetChecksumCRC64NVME());
1058+
Aws::String checksum = ExtractChecksum(headObjectOutcome);
1059+
if (!checksum.empty()) {
1060+
handle->SetChecksum(checksum);
10831061
}
10841062

10851063
/* When bucket versioning is suspended, head object will return "null" for unversioned object.
@@ -1287,17 +1265,9 @@ namespace Aws
12871265
Aws::String errMsg{handle->WritePartToDownloadStream(bufferStream, partState->GetRangeBegin())};
12881266
if (errMsg.empty()) {
12891267
// Store part checksum for later validation
1290-
const auto& getResult = outcome.GetResult();
1291-
if (!getResult.GetChecksumCRC32().empty()) {
1292-
partState->SetChecksum(getResult.GetChecksumCRC32());
1293-
} else if (!getResult.GetChecksumCRC32C().empty()) {
1294-
partState->SetChecksum(getResult.GetChecksumCRC32C());
1295-
} else if (!getResult.GetChecksumSHA256().empty()) {
1296-
partState->SetChecksum(getResult.GetChecksumSHA256());
1297-
} else if (!getResult.GetChecksumSHA1().empty()) {
1298-
partState->SetChecksum(getResult.GetChecksumSHA1());
1299-
} else if (!getResult.GetChecksumCRC64NVME().empty()) {
1300-
partState->SetChecksum(getResult.GetChecksumCRC64NVME());
1268+
Aws::String checksum = ExtractChecksum(outcome);
1269+
if (!checksum.empty()) {
1270+
partState->SetChecksum(checksum);
13011271
}
13021272

13031273
handle->ChangePartToCompleted(partState, outcome.GetResult().GetETag());

0 commit comments

Comments
 (0)