1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-08 19:46:02 +01:00

refactor(libstore): add HttpBinaryCacheStore::upload method

Introduce protected `upload` method overloads in `HttpBinaryCacheStore`
that handle the actual upload after compression has been applied. This
separates compression concerns (in `upsertFile`) from upload mechanics
(in `upload`).

Two overloads are provided:

1. `upload(path, RestartableSource &, sizeHint, mimeType, contentEncoding)`
2. `upload(path, CompressedSource &, mimeType)`
This commit is contained in:
Bernardo Meurer Costa 2025-10-30 02:36:28 +00:00
parent 93fe3354b5
commit d857a4be50
No known key found for this signature in database
2 changed files with 57 additions and 12 deletions

View file

@ -134,27 +134,41 @@ bool HttpBinaryCacheStore::fileExists(const std::string & path)
} }
} }
void HttpBinaryCacheStore::upsertFile( void HttpBinaryCacheStore::upload(
const std::string & path, RestartableSource & source, const std::string & mimeType, uint64_t sizeHint) std::string_view path,
RestartableSource & source,
uint64_t sizeHint,
std::string_view mimeType,
std::optional<std::string_view> contentEncoding)
{ {
auto req = makeRequest(path); auto req = makeRequest(path);
req.method = HttpMethod::PUT; req.method = HttpMethod::PUT;
auto compressionMethod = getCompressionMethod(path);
std::optional<CompressedSource> compressed; if (contentEncoding) {
req.headers.emplace_back("Content-Encoding", *contentEncoding);
if (compressionMethod) {
compressed = CompressedSource(source, *compressionMethod);
req.headers.emplace_back("Content-Encoding", *compressionMethod);
req.data = {compressed->size(), *compressed};
} else {
req.data = {sizeHint, source};
} }
req.data = {sizeHint, source};
req.mimeType = mimeType; req.mimeType = mimeType;
try {
getFileTransfer()->upload(req); getFileTransfer()->upload(req);
}
void HttpBinaryCacheStore::upload(std::string_view path, CompressedSource & source, std::string_view mimeType)
{
upload(path, static_cast<RestartableSource &>(source), source.size(), mimeType, source.getCompressionMethod());
}
void HttpBinaryCacheStore::upsertFile(
const std::string & path, RestartableSource & source, const std::string & mimeType, uint64_t sizeHint)
{
try {
if (auto compressionMethod = getCompressionMethod(path)) {
CompressedSource compressed(source, *compressionMethod);
upload(path, compressed, mimeType);
} else {
upload(path, source, sizeHint, mimeType, std::nullopt);
}
} catch (FileTransferError & e) { } catch (FileTransferError & e) {
UploadToHTTP err(e.message()); UploadToHTTP err(e.message());
err.addTrace({}, "while uploading to HTTP binary cache at '%s'", config->cacheUri.to_string()); err.addTrace({}, "while uploading to HTTP binary cache at '%s'", config->cacheUri.to_string());

View file

@ -85,6 +85,37 @@ protected:
FileTransferRequest makeRequest(std::string_view path); FileTransferRequest makeRequest(std::string_view path);
/**
* Uploads data to the binary cache.
*
* This is a lower-level method that handles the actual upload after
* compression has been applied. It does not handle compression or
* error wrapping - those are the caller's responsibility.
*
* @param path The path in the binary cache to upload to
* @param source The data source (should already be compressed if needed)
* @param sizeHint Size hint for the data
* @param mimeType The MIME type of the content
* @param contentEncoding Optional Content-Encoding header value (e.g., "xz", "br")
*/
void upload(
std::string_view path,
RestartableSource & source,
uint64_t sizeHint,
std::string_view mimeType,
std::optional<std::string_view> contentEncoding);
/**
* Uploads data to the binary cache (CompressedSource overload).
*
* This overload infers both the size and compression method from the CompressedSource.
*
* @param path The path in the binary cache to upload to
* @param source The compressed source (knows size and compression method)
* @param mimeType The MIME type of the content
*/
void upload(std::string_view path, CompressedSource & source, std::string_view mimeType);
void getFile(const std::string & path, Sink & sink) override; void getFile(const std::string & path, Sink & sink) override;
void getFile(const std::string & path, Callback<std::optional<std::string>> callback) noexcept override; void getFile(const std::string & path, Callback<std::optional<std::string>> callback) noexcept override;