| 76 | : _diskCache(std::move(diskCache)), _logger(logger) {} |
| 77 | |
| 78 | Result<BytesView> ResourcesBundleResultTransformer::preprocess(const StringBox& localFilename, |
| 79 | const BytesView& remoteData) const { |
| 80 | if (_diskCache == nullptr) { |
| 81 | return Error("Cannot load resources without a disk cache"); |
| 82 | } |
| 83 | |
| 84 | _diskCache->remove(Path(localFilename)); |
| 85 | |
| 86 | Result<ValdiModuleArchive> decompressedBundleResult; |
| 87 | if (_decompressionDisabled) { |
| 88 | decompressedBundleResult = ValdiModuleArchive::deserialize(remoteData); |
| 89 | } else { |
| 90 | decompressedBundleResult = ValdiModuleArchive::decompress(remoteData.data(), remoteData.size()); |
| 91 | } |
| 92 | |
| 93 | if (!decompressedBundleResult) { |
| 94 | return decompressedBundleResult.moveError(); |
| 95 | } |
| 96 | |
| 97 | auto decompressedBundle = Valdi::makeShared<ValdiModuleArchive>(decompressedBundleResult.moveValue()); |
| 98 | |
| 99 | ValdiArchiveBuilder manifestBuilder; |
| 100 | |
| 101 | Path cacheDirectoryPath(localFilename.toStringView()); |
| 102 | cacheDirectoryPath.removeFileExtension(); |
| 103 | cacheDirectoryPath.appendFileExtension("dir"); |
| 104 | |
| 105 | for (const auto& path : decompressedBundle->getAllEntryPaths()) { |
| 106 | auto entry = decompressedBundle->getEntry(path); |
| 107 | SC_ASSERT(entry.has_value(), "Unable to retrieve module entry"); |
| 108 | |
| 109 | auto bytesView = BytesView(decompressedBundle, entry->data, entry->size); |
| 110 | |
| 111 | auto cachePath = cacheDirectoryPath.appending(path.toStringView()); |
| 112 | |
| 113 | auto storeSuccess = _diskCache->store(cachePath, bytesView); |
| 114 | if (!storeSuccess) { |
| 115 | return storeSuccess.error().rethrow( |
| 116 | STRING_FORMAT("Failed to store resource item '{}' in disk cache", path)); |
| 117 | } |
| 118 | |
| 119 | manifestBuilder.addEntry(ValdiArchiveEntry(path, cachePath.toStringBox())); |
| 120 | } |
| 121 | |
| 122 | auto moduleBytes = manifestBuilder.build(); |
| 123 | return moduleBytes->toBytesView(); |
| 124 | } |
| 125 | |
| 126 | std::string_view ResourcesBundleResultTransformer::getItemTypeDescription() const { |
| 127 | return "assets package"; |
no test coverage detected