#include "Web_Server.h" #include #include #include "SD_Card.h" #include "Display_PNG.h" #include "Audio_Player.h" WebServer server(80); File uploadFile; bool uploadSuccess = false; String uploadedFilename = ""; const char* uploadPage = R"rawliteral( Photo Frame Upload

Photo Frame

Upload media to the SD card wirelessly

%TOAST%
📁
Drop a file here or browse
No file selected
Uploading...

Accepted: .png (172x320)  ·  .mp3  ·  .wav

)rawliteral"; static bool isAllowedExtension(const String& filename) { String lower = filename; lower.toLowerCase(); return lower.endsWith(".png") || lower.endsWith(".mp3") || lower.endsWith(".wav"); } void handleUpload() { HTTPUpload& upload = server.upload(); if (upload.status == UPLOAD_FILE_START) { String filename = upload.filename; if (!filename.startsWith("/")) filename = "/" + filename; // Reject files that are not PNG, MP3, or WAV if (!isAllowedExtension(filename)) { printf("WebUpload: Rejected non-media file: %s\r\n", filename.c_str()); uploadSuccess = false; return; } printf("WebUpload: Starting upload of %s\r\n", filename.c_str()); uploadedFilename = filename; uploadFile = SD_MMC.open(filename, "w"); uploadSuccess = (bool)uploadFile; } else if (upload.status == UPLOAD_FILE_WRITE) { if (uploadFile) { uploadFile.write(upload.buf, upload.currentSize); } } else if (upload.status == UPLOAD_FILE_END) { if (uploadFile) { uploadFile.close(); printf("WebUpload: Successfully uploaded %s (%d bytes)\r\n", upload.filename.c_str(), upload.totalSize); uploadSuccess = true; // Re-scan SD card to index the new file Search_Images("/"); Search_Audio("/"); } else { uploadSuccess = false; } } } static String buildPage(const String& status, const String& file) { String toast = ""; if (status == "ok") { String safeName = file; safeName.replace("<", "<"); safeName.replace(">", ">"); toast = "
✅ \"" + safeName + "\" uploaded successfully!
"; } else if (status == "fail") { toast = "
❌ Upload failed. Only .png, .mp3 and .wav files are accepted.
"; } String page = String(uploadPage); page.replace("%TOAST%", toast); return page; } void initWebServer() { WiFi.softAP("love", "love1234"); IPAddress IP = WiFi.softAPIP(); printf("WiFi AP Started. SSID: 'love' Password: 'love1234'\r\n"); printf("Web Server IP: %s\r\n", IP.toString().c_str()); server.on("/", HTTP_GET, []() { String status = server.hasArg("status") ? server.arg("status") : ""; String file = server.hasArg("file") ? server.arg("file") : ""; server.send(200, "text/html", buildPage(status, file)); }); server.on("/upload", HTTP_POST, []() { String loc = uploadSuccess ? "/?status=ok&file=" + uploadedFilename : "/?status=fail"; server.sendHeader("Location", loc); server.send(303); }, handleUpload); server.begin(); } void handleWebServer() { server.handleClient(); }