Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions src/libstore/http-binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,27 +134,41 @@ bool HttpBinaryCacheStore::fileExists(const std::string & path)
}
}

void HttpBinaryCacheStore::upsertFile(
const std::string & path, RestartableSource & source, const std::string & mimeType, uint64_t sizeHint)
void HttpBinaryCacheStore::upload(
std::string_view path,
RestartableSource & source,
uint64_t sizeHint,
std::string_view mimeType,
std::optional<std::string_view> contentEncoding)
{
auto req = makeRequest(path);
req.method = HttpMethod::PUT;
auto compressionMethod = getCompressionMethod(path);

std::optional<CompressedSource> compressed;

if (compressionMethod) {
compressed = CompressedSource(source, *compressionMethod);
req.headers.emplace_back("Content-Encoding", *compressionMethod);
req.data = {compressed->size(), *compressed};
} else {
req.data = {sizeHint, source};
if (contentEncoding) {
req.headers.emplace_back("Content-Encoding", *contentEncoding);
}

req.data = {sizeHint, source};
req.mimeType = mimeType;

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 {
getFileTransfer()->upload(req);
if (auto compressionMethod = getCompressionMethod(path)) {
CompressedSource compressed(source, *compressionMethod);
upload(path, compressed, mimeType);
} else {
upload(path, source, sizeHint, mimeType, std::nullopt);
}
} catch (FileTransferError & e) {
UploadToHTTP err(e.message());
err.addTrace({}, "while uploading to HTTP binary cache at '%s'", config->cacheUri.to_string());
Expand Down
31 changes: 31 additions & 0 deletions src/libstore/include/nix/store/http-binary-cache-store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,37 @@ protected:

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, Callback<std::optional<std::string>> callback) noexcept override;
Expand Down
Loading