MCPcopy Create free account
hub / github.com/OpenEPaperLink/OpenEPaperLink / downloadAndWriteBinary

Function downloadAndWriteBinary

ESP32_AP-Flasher/src/espflasher.cpp:129–204  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

127}
128
129bool downloadAndWriteBinary(String &filename, const char *url) {
130 HTTPClient binaryHttp;
131 bool Ret = false;
132 bool bHaveFsMutex = false;
133
134 LOG("downloadAndWriteBinary: url %s\n",url);
135 binaryHttp.begin(url);
136 binaryHttp.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
137 do {
138 int binaryResponseCode = binaryHttp.GET();
139 if(binaryResponseCode != HTTP_CODE_OK) {
140 wsSerial("http error " + String(binaryResponseCode) + " fetching " + String(url));
141 break;
142 }
143 int contentLength = binaryHttp.getSize();
144 LOG("contentLength %d\r\n",contentLength);
145 if(contentLength < 0) {
146 wsSerial("Couldn't get contentLength");
147 break;
148 }
149 xSemaphoreTake(fsMutex, portMAX_DELAY);
150 bHaveFsMutex = true;
151 File file = contentFS->open(filename, "wb");
152 if(!file) {
153 wsSerial("file open error " + String(filename));
154 break;
155 }
156 wsSerial("downloading " + String(filename));
157 WiFiClient *stream = binaryHttp.getStreamPtr();
158 uint8_t buffer[1024];
159 size_t totalBytesRead = 0;
160 // timeout if we don't average at least 1k bytes/second
161 unsigned long timeOut = millis() + contentLength;
162 while(stream->connected() && totalBytesRead < contentLength) {
163 size_t bytesRead;
164 size_t bytesToRead;
165 if(stream->available()) {
166 bytesToRead = min(sizeof(buffer), (size_t) stream->available());
167 bytesRead = stream->readBytes(buffer, bytesToRead);
168 if(bytesRead == 0 || millis() > timeOut) {
169 wsSerial("Download time out");
170 break;
171 }
172 file.write(buffer, bytesRead);
173 totalBytesRead += bytesRead;
174 vTaskDelay(1 / portTICK_PERIOD_MS);
175 } else {
176 vTaskDelay(10 / portTICK_PERIOD_MS);
177 }
178 }
179 file.close();
180
181 if(!stream->connected()) {
182 wsSerial("Connection dropped during transfer");
183 break;
184 }
185 file = contentFS->open(filename, "r");
186 if(!file) {

Callers 1

FlashC6_H2Function · 0.85

Calls 8

wsSerialFunction · 0.85
openMethod · 0.80
closeMethod · 0.80
beginMethod · 0.45
availableMethod · 0.45
writeMethod · 0.45
sizeMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected