From b047cecf5c9d1cd7b9ad6c1b9e69c66f34f3c603 Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Costa Date: Tue, 21 Oct 2025 08:32:32 +0000 Subject: [PATCH] refactor(libstore): extract getCompressionMethod() in HttpBinaryCacheStore Extract the path-based compression method determination logic into a protected method that returns std::optional. This allows subclasses to reuse the logic and makes the semantics clearer (nullopt means no compression, not empty string). This prepares for S3BinaryCacheStore to apply the same compression rules when implementing multipart uploads. --- src/libstore/http-binary-cache-store.cc | 28 +++++++++++++------------ 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/libstore/http-binary-cache-store.cc b/src/libstore/http-binary-cache-store.cc index 8d5f427af..9567aec2f 100644 --- a/src/libstore/http-binary-cache-store.cc +++ b/src/libstore/http-binary-cache-store.cc @@ -97,6 +97,18 @@ public: protected: + std::optional getCompressionMethod(const std::string & path) + { + if (hasSuffix(path, ".narinfo") && !config->narinfoCompression.get().empty()) + return config->narinfoCompression; + else if (hasSuffix(path, ".ls") && !config->lsCompression.get().empty()) + return config->lsCompression; + else if (hasPrefix(path, "log/") && !config->logCompression.get().empty()) + return config->logCompression; + else + return std::nullopt; + } + void maybeDisable() { auto state(_state.lock()); @@ -149,19 +161,9 @@ protected: auto data = StreamToSourceAdapter(istream).drain(); - // Determine compression method based on file type - std::string compressionMethod; - if (hasSuffix(path, ".narinfo")) - compressionMethod = config->narinfoCompression; - else if (hasSuffix(path, ".ls")) - compressionMethod = config->lsCompression; - else if (hasPrefix(path, "log/")) - compressionMethod = config->logCompression; - - // Apply compression if configured - if (!compressionMethod.empty()) { - data = compress(compressionMethod, data); - req.headers.emplace_back("Content-Encoding", compressionMethod); + if (auto compressionMethod = getCompressionMethod(path)) { + data = compress(*compressionMethod, data); + req.headers.emplace_back("Content-Encoding", *compressionMethod); } req.data = std::move(data);