Skip to content

Commit f0deefa

Browse files
authored
Add files via upload
1 parent 9e68cc1 commit f0deefa

2 files changed

Lines changed: 12 additions & 21 deletions

File tree

src/WebResponses.cpp

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -750,10 +750,8 @@ AsyncFileResponse::AsyncFileResponse(FS &fs, const String &path, const char *con
750750
// Extract filename from path and set as download attachment
751751
int filenameStart = path.lastIndexOf('/') + 1;
752752
char buf[26 + path.length() - filenameStart];
753-
strcpy(buf, T_attachment);
754753
char *filename = (char *)path.c_str() + filenameStart;
755-
strcat(buf, filename);
756-
strcat(buf, "\"");
754+
snprintf(buf, sizeof(buf), T_attachment, filename);
757755
addHeader(T_Content_Disposition, buf, false);
758756
} else {
759757
// Serve file inline (display in browser)
@@ -766,11 +764,9 @@ AsyncFileResponse::AsyncFileResponse(FS &fs, const String &path, const char *con
766764
AsyncFileResponse::AsyncFileResponse(File content, const String &path, const char *contentType, bool download, AwsTemplateProcessor callback)
767765
: AsyncAbstractResponse(callback) {
768766
_code = 200;
767+
_path = path;
769768

770-
const char *contentName = content.name();
771-
const size_t lenFilename = strlen(contentName);
772-
if (lenFilename > sizeof(T__gz) &&
773-
memcmp(contentName + lenFilename - (sizeof(T__gz) - 1), T__gz, sizeof(T__gz) - 1) == 0) {
769+
if (String(content.name()).endsWith(T__gz) && !path.endsWith(T__gz)) {
774770
addHeader(T_Content_Encoding, T_gzip, false);
775771
_callback = nullptr; // Unable to process gzipped templates
776772
_sendContentLength = true;
@@ -780,27 +776,22 @@ AsyncFileResponse::AsyncFileResponse(File content, const String &path, const cha
780776
_content = content;
781777
_contentLength = _content.size();
782778

783-
if (*contentType == '\0') {
779+
if (strlen(contentType) == 0) {
784780
_setContentTypeFromPath(path);
785781
} else {
786782
_contentType = contentType;
787783
}
788784

789-
if (path.endsWith(T__jpg))
790-
download = true;
785+
int filenameStart = path.lastIndexOf('/') + 1;
786+
char buf[26 + path.length() - filenameStart];
787+
char *filename = (char *)path.c_str() + filenameStart;
788+
791789
if (download) {
792-
// Extract filename from path and set as download attachment
793-
int filenameStart = path.lastIndexOf('/') + 1;
794-
char buf[26 + path.length() - filenameStart];
795-
strcpy(buf, T_attachment);
796-
char *filename = (char *)path.c_str() + filenameStart;
797-
strcat(buf, filename);
798-
strcat(buf, "\"");
799-
addHeader(T_Content_Disposition, buf, false);
790+
snprintf_P(buf, sizeof(buf), PSTR("attachment; filename=\"%s\""), filename);
800791
} else {
801-
// Serve file inline (display in browser)
802-
addHeader(T_Content_Disposition, T_inline, false);
792+
snprintf_P(buf, sizeof(buf), PSTR("inline"));
803793
}
794+
addHeader(T_Content_Disposition, buf, false);
804795
}
805796

806797
size_t AsyncFileResponse::_fillBuffer(uint8_t *data, size_t len) {

src/literals.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ static constexpr const char *T_100_CONTINUE = "100-continue";
1212
static constexpr const char *T_13 = "13";
1313
static constexpr const char *T_ACCEPT = "Accept";
1414
static constexpr const char *T_Accept_Ranges = "Accept-Ranges";
15-
static constexpr const char *T_attachment = "attachment; filename=\"";
15+
static constexpr const char *T_attachment = "attachment; filename=\"%s\"";
1616
static constexpr const char *T_AUTH = "Authorization";
1717
static constexpr const char *T_auth_nonce = "\", qop=\"auth\", nonce=\"";
1818
static constexpr const char *T_BASIC = "Basic";

0 commit comments

Comments
 (0)