mirror of
https://github.com/NixOS/nix.git
synced 2025-11-19 00:39:37 +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:
parent
93fe3354b5
commit
d857a4be50
2 changed files with 57 additions and 12 deletions
|
|
@ -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());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue