Skip to content

Commit 49084a7

Browse files
authored
Merge pull request #14421 from lovesegfault/http-upload
refactor(libstore): add `HttpBinaryCacheStore::upload` method
2 parents 6d87184 + d857a4b commit 49084a7

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

src/libstore/http-binary-cache-store.cc

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,27 +134,41 @@ bool HttpBinaryCacheStore::fileExists(const std::string & path)
134134
}
135135
}
136136

137-
void HttpBinaryCacheStore::upsertFile(
138-
const std::string & path, RestartableSource & source, const std::string & mimeType, uint64_t sizeHint)
137+
void HttpBinaryCacheStore::upload(
138+
std::string_view path,
139+
RestartableSource & source,
140+
uint64_t sizeHint,
141+
std::string_view mimeType,
142+
std::optional<std::string_view> contentEncoding)
139143
{
140144
auto req = makeRequest(path);
141145
req.method = HttpMethod::PUT;
142-
auto compressionMethod = getCompressionMethod(path);
143146

144-
std::optional<CompressedSource> compressed;
145-
146-
if (compressionMethod) {
147-
compressed = CompressedSource(source, *compressionMethod);
148-
req.headers.emplace_back("Content-Encoding", *compressionMethod);
149-
req.data = {compressed->size(), *compressed};
150-
} else {
151-
req.data = {sizeHint, source};
147+
if (contentEncoding) {
148+
req.headers.emplace_back("Content-Encoding", *contentEncoding);
152149
}
153150

151+
req.data = {sizeHint, source};
154152
req.mimeType = mimeType;
155153

154+
getFileTransfer()->upload(req);
155+
}
156+
157+
void HttpBinaryCacheStore::upload(std::string_view path, CompressedSource & source, std::string_view mimeType)
158+
{
159+
upload(path, static_cast<RestartableSource &>(source), source.size(), mimeType, source.getCompressionMethod());
160+
}
161+
162+
void HttpBinaryCacheStore::upsertFile(
163+
const std::string & path, RestartableSource & source, const std::string & mimeType, uint64_t sizeHint)
164+
{
156165
try {
157-
getFileTransfer()->upload(req);
166+
if (auto compressionMethod = getCompressionMethod(path)) {
167+
CompressedSource compressed(source, *compressionMethod);
168+
upload(path, compressed, mimeType);
169+
} else {
170+
upload(path, source, sizeHint, mimeType, std::nullopt);
171+
}
158172
} catch (FileTransferError & e) {
159173
UploadToHTTP err(e.message());
160174
err.addTrace({}, "while uploading to HTTP binary cache at '%s'", config->cacheUri.to_string());

src/libstore/include/nix/store/http-binary-cache-store.hh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,37 @@ protected:
8585

8686
FileTransferRequest makeRequest(std::string_view path);
8787

88+
/**
89+
* Uploads data to the binary cache.
90+
*
91+
* This is a lower-level method that handles the actual upload after
92+
* compression has been applied. It does not handle compression or
93+
* error wrapping - those are the caller's responsibility.
94+
*
95+
* @param path The path in the binary cache to upload to
96+
* @param source The data source (should already be compressed if needed)
97+
* @param sizeHint Size hint for the data
98+
* @param mimeType The MIME type of the content
99+
* @param contentEncoding Optional Content-Encoding header value (e.g., "xz", "br")
100+
*/
101+
void upload(
102+
std::string_view path,
103+
RestartableSource & source,
104+
uint64_t sizeHint,
105+
std::string_view mimeType,
106+
std::optional<std::string_view> contentEncoding);
107+
108+
/**
109+
* Uploads data to the binary cache (CompressedSource overload).
110+
*
111+
* This overload infers both the size and compression method from the CompressedSource.
112+
*
113+
* @param path The path in the binary cache to upload to
114+
* @param source The compressed source (knows size and compression method)
115+
* @param mimeType The MIME type of the content
116+
*/
117+
void upload(std::string_view path, CompressedSource & source, std::string_view mimeType);
118+
88119
void getFile(const std::string & path, Sink & sink) override;
89120

90121
void getFile(const std::string & path, Callback<std::optional<std::string>> callback) noexcept override;

0 commit comments

Comments
 (0)