MCPcopy Create free account
hub / github.com/TactilityProject/Tactility / receiveFile

Function receiveFile

Tactility/Source/network/HttpdReq.cpp:183–214  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

181}
182
183size_t receiveFile(httpd_req_t* request, size_t length, const std::string& filePath) {
184 constexpr auto BUFFER_SIZE = 512;
185 char buffer[BUFFER_SIZE];
186 size_t bytes_received = 0;
187
188 auto lock = file::getLock(filePath)->asScopedLock();
189 lock.lock();
190
191 auto* file = fopen(filePath.c_str(), "wb");
192 if (file == nullptr) {
193 LOGGER.error("Failed to open file for writing: {}", filePath);
194 return 0;
195 }
196
197 while (bytes_received < length) {
198 auto expected_chunk_size = std::min<size_t>(BUFFER_SIZE, length - bytes_received);
199 size_t receive_chunk_size = httpd_req_recv(request, buffer, expected_chunk_size);
200 if (receive_chunk_size <= 0) {
201 LOGGER.error("Receive failed");
202 break;
203 }
204 if (fwrite(buffer, 1, receive_chunk_size, file) != (size_t)receive_chunk_size) {
205 LOGGER.error("Failed to write all bytes");
206 break;
207 }
208 bytes_received += receive_chunk_size;
209 }
210
211 // Write file
212 fclose(file);
213 return bytes_received;
214}
215
216}
217

Callers 2

handleAppInstallMethod · 0.85
handleApiAppsInstallMethod · 0.85

Calls 4

getLockFunction · 0.85
asScopedLockMethod · 0.80
errorMethod · 0.80
lockMethod · 0.45

Tested by

no test coverage detected