Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions examples/simple/simple.ino
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
#ifdef ESP32
#if defined(ESP32)
#include <WiFi.h>
#else
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#else
#error Platform not supported
#endif

#include <ESPAsyncWebServer.h>
#include <LittleFS.h>
#include <AsyncWebdav.h>

// LittleFS
#include <LittleFS.h>
#define FILESYSTEM LittleFS

// SPIFFS
// #include <SPIFFS.h>
// #define FILESYSTEM SPIFFS

// FFat
// #include <FFat.h>
// #define FILESYSTEM FFat

const char* ssid = "ssid";
const char* password = "pass";

AsyncWebServer server(80);
AsyncWebdav dav("/dav", LittleFS);

size_t totalBytes() { return FILESYSTEM.totalBytes(); }
size_t usedBytes() { return FILESYSTEM.usedBytes(); }

AsyncWebdav dav("/dav", FILESYSTEM, totalBytes, usedBytes);


void setup(void){
Expand All @@ -31,14 +49,14 @@ void setup(void){
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

// init spiffs
LittleFS.begin();
// init filesystem
FILESYSTEM.begin();

// add websocket handler
// add WebDAV handler
server.addHandler(&dav);

// start webserver
server.serveStatic("/", LittleFS, "/").setDefaultFile("index.html");
server.serveStatic("/", FILESYSTEM, "/www/").setDefaultFile("index.html");
server.begin();
}

Expand Down
6 changes: 6 additions & 0 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
"type": "git",
"url": "https://github.com/rostwolke/ESPAsyncWebdav"
},
"dependencies": [
{
"name":"ESP Async WebServer",
"version":"https://github.com/rostwolke/ESPAsyncWebServer"
}
],
"frameworks": "arduino",
"platforms":"espressif"
}
28 changes: 21 additions & 7 deletions src/AsyncWebdav.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <Arduino.h>
#include <ESPAsyncWebServer.h>
#include <DateTime.h>
// #include <DateTime.h>

#ifdef ESP32
#include <AsyncTCP.h>
Expand All @@ -14,11 +14,13 @@

#include "AsyncWebdav.h"

AsyncWebdav::AsyncWebdav(const String& url, fs::FS &fs) : _fs(fs) {
this->_url = url;
AsyncWebdav::AsyncWebdav(const String& url, fs::FS &fs, quota_function totalBytes, quota_function usedBytes)
: _url(url)
, _fs(fs)
, _totalBytes(totalBytes)
, _usedBytes(usedBytes) {
}


bool AsyncWebdav::canHandle(AsyncWebServerRequest *request){
if(request->url().startsWith(this->_url)){
if(request->method() == HTTP_PROPFIND
Expand Down Expand Up @@ -357,8 +359,9 @@ void AsyncWebdav::sendPropResponse(AsyncResponseStream *response, boolean recurs

// get file modified time
time_t lastWrite = curFile->getLastWrite();
DateTimeClass dt(lastWrite);
String fileTimeStamp = dt.format("%a, %d %b %Y %H:%M:%S GMT");
char fileTimeStamp[64];
struct tm* t = localtime(&lastWrite);
strftime(fileTimeStamp, sizeof(fileTimeStamp), "%a, %d %b %Y %H:%M:%S GMT", t);

// send response
response->print("<d:response>");
Expand All @@ -367,7 +370,18 @@ void AsyncWebdav::sendPropResponse(AsyncResponseStream *response, boolean recurs
response->print("<d:prop>");

// last modified
response->printf("<d:getlastmodified>%s</d:getlastmodified>", fileTimeStamp.c_str());
response->printf("<d:getlastmodified>%s</d:getlastmodified>", fileTimeStamp);

// quota
size_t usedBytes = 0;
size_t availBytes = 0;

if (_totalBytes && _usedBytes) {
usedBytes = _usedBytes();
availBytes = _totalBytes() - usedBytes;
}
response->printf("<d:quota-used-bytes>%d</d:quota-used-bytes>", usedBytes);
response->printf("<d:quota-available-bytes>%d</d:quota-available-bytes>", availBytes);

if(curFile->isDirectory()) {
// resource type
Expand Down
9 changes: 7 additions & 2 deletions src/AsyncWebdav.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#include <Arduino.h>
#include <FS.h>

#include <ESPAsyncWebServer.h>

enum DavResourceType { DAV_RESOURCE_NONE, DAV_RESOURCE_FILE, DAV_RESOURCE_DIR };
enum DavDepthType { DAV_DEPTH_NONE, DAV_DEPTH_CHILD, DAV_DEPTH_ALL };


class AsyncWebdav: public AsyncWebHandler {
public:
AsyncWebdav(const String& url, fs::FS &fs);
typedef size_t (*quota_function)(void);

public:
AsyncWebdav(const String& url, fs::FS &fs, quota_function totalBytes = nullptr, quota_function usedBytes = nullptr);

virtual bool canHandle(AsyncWebServerRequest *request) override final;
virtual void handleRequest(AsyncWebServerRequest *request) override final;
Expand All @@ -20,6 +23,8 @@ class AsyncWebdav: public AsyncWebHandler {
private:
String _url;
fs::FS _fs;
quota_function _totalBytes;
quota_function _usedBytes;
void handlePropfind(const String& path, DavResourceType resource, AsyncWebServerRequest * request);
void handleGet(const String& path, DavResourceType resource, AsyncWebServerRequest * request);
void handlePut(const String& path, DavResourceType resource, AsyncWebServerRequest * request, unsigned char *data, size_t len, size_t index, size_t total);
Expand Down