-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathbackup.cpp
More file actions
56 lines (46 loc) · 1.43 KB
/
backup.cpp
File metadata and controls
56 lines (46 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "esphome/core/log.h"
#include "esphome/core/application.h"
#include "esphome/core/util.h"
#include <cstdlib>
#include "backup.h"
namespace esphome {
namespace backup {
static const char *const TAG = "backup";
static const char *const URL = "/config.yaml";
void Backup::setup() {
ESP_LOGCONFIG(TAG, "Setting up backup handler...");
this->base_->init();
this->base_->add_handler(this);
}
void Backup::dump_config() {
ESP_LOGCONFIG(TAG, "Backup:");
ESP_LOGCONFIG(TAG, " URL path is %s", URL);
#ifdef USE_WEBSERVER_AUTH
if (this->using_auth()) {
ESP_LOGCONFIG(TAG, " Basic authentication enabled");
}
#endif
}
bool Backup::canHandle(AsyncWebServerRequest *request)
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2025, 7, 0)
const
#endif
{
return request->method() == HTTP_GET && request->url() == URL;
}
void Backup::handleRequest(AsyncWebServerRequest *request) {
#ifdef USE_WEBSERVER_AUTH
if (this->using_auth() && !request->authenticate(this->username_, this->password_)) {
return request->requestAuthentication();
}
#endif
#ifndef USE_ESP8266
auto *response = request->beginResponse(200, "plain/text;charset=UTF-8", ESPHOME_BACKUP_DATA, ESPHOME_BACKUP_SIZE);
#else
auto *response = request->beginResponse_P(200, "plain/text;charset=UTF-8", ESPHOME_BACKUP_DATA, ESPHOME_BACKUP_SIZE);
#endif
response->addHeader("Content-Encoding", "gzip");
request->send(response);
}
} // namespace backup
} // namespace esphome