mirror of
https://github.com/NixOS/nix.git
synced 2025-11-08 19:46:02 +01:00
refactor(libstore): extract getCompressionMethod() in HttpBinaryCacheStore
Extract the path-based compression method determination logic into a protected method that returns std::optional<std::string>. 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.
This commit is contained in:
parent
115dea10b2
commit
b047cecf5c
1 changed files with 15 additions and 13 deletions
|
|
@ -97,6 +97,18 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
|
std::optional<std::string> 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()
|
void maybeDisable()
|
||||||
{
|
{
|
||||||
auto state(_state.lock());
|
auto state(_state.lock());
|
||||||
|
|
@ -149,19 +161,9 @@ protected:
|
||||||
|
|
||||||
auto data = StreamToSourceAdapter(istream).drain();
|
auto data = StreamToSourceAdapter(istream).drain();
|
||||||
|
|
||||||
// Determine compression method based on file type
|
if (auto compressionMethod = getCompressionMethod(path)) {
|
||||||
std::string compressionMethod;
|
data = compress(*compressionMethod, data);
|
||||||
if (hasSuffix(path, ".narinfo"))
|
req.headers.emplace_back("Content-Encoding", *compressionMethod);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
req.data = std::move(data);
|
req.data = std::move(data);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue