Arduino DLNA Server
Loading...
Searching...
No Matches
TreeNode.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <cctype>
9#include <string>
10#include <utility>
11#include <vector>
12
13namespace tiny_dlna {
14
18class TreeNode {
19 public:
20 TreeNode() = default;
21 uint32_t id = 0;
23 std::vector<TreeNode*, AllocatorPSRAM<TreeNode*>> children;
24 TreeNode* parent = nullptr;
25 uint32_t size = 0;
26 bool is_expanded = false;
27 bool is_dir = false;
28
29 static void* operator new(std::size_t size) {
31 return alloc.allocate(1);
32 }
33
34 static void operator delete(void* ptr) {
36 alloc.deallocate(static_cast<TreeNode*>(ptr), 1);
37 }
38
46 static void setMimeRules(
47 const std::vector<std::pair<std::string, std::string>>& rules) {
48 auto& tbl = mimeRulesTable();
49 tbl.clear();
50 tbl.reserve(rules.size());
51 for (auto& r : rules) tbl.emplace_back(normalizeExt(r.first), r.second);
52 }
53
57 static void addMimeRule(const std::string& ext, const std::string& mime) {
58 mimeRulesTable().emplace_back(normalizeExt(ext), mime);
59 }
60
66 std::string getMime() const {
67 if (is_dir) return {};
68 std::string lower;
69 lower.reserve(file_name.size());
70 for (unsigned char c : file_name) lower.push_back((char)std::tolower(c));
71 const auto& rules = mimeRulesTable();
72 for (const auto& r : rules) {
73 const std::string& ext = r.first;
74 const std::string& mime = r.second;
75 if (lower.size() >= ext.size() &&
76 lower.compare(lower.size() - ext.size(), ext.size(), ext) == 0) {
77 return mime;
78 }
79 }
80 return {};
81 }
82
87 std::string path() const {
88 if (parent == nullptr) {
89 std::string result = file_name.c_str();
90 return result;
91 }
92 std::string parentPath = parent->path();
93 if (parentPath.back() != '/') parentPath += '/';
94 parentPath += file_name.c_str();
95 return parentPath;
96 }
97
102 int level() const {
103 int lvl = 0;
104 const TreeNode* p = parent;
105 while (p) {
106 ++lvl;
107 p = p->parent;
108 }
109 return lvl;
110 }
111
112 private:
113 // Accessor for the rules table with defaults populated once.
114 static std::vector<std::pair<std::string, std::string>>& mimeRulesTable() {
115 static std::vector<std::pair<std::string, std::string>> rules = {
116 {".mp3", "audio/mpeg"}, {".aac", "audio/aac"}, {".m4a", "audio/aac"},
117 {".wav", "audio/wav"}, {".flac", "audio/flac"}, {".ogg", "audio/ogg"},
118 };
119 return rules;
120 }
121
122 static std::string normalizeExt(std::string ext) {
123 if (ext.empty()) return ext;
124 // ensure leading dot
125 if (ext[0] != '.') ext.insert(ext.begin(), '.');
126 // lowercase
127 for (auto& ch : ext) ch = (char)std::tolower((unsigned char)ch);
128 return ext;
129 }
130};
131
132} // namespace tiny_dlna
Custom allocator that uses ESP32's PSRAM for memory allocation.
Definition: Allocator.h:24
pointer allocate(size_type n)
Allocate memory from PSRAM.
Definition: Allocator.h:55
void deallocate(pointer p, size_type) noexcept
Deallocate memory.
Definition: Allocator.h:81
Node in the directory tree representing either a file or a directory.
Definition: TreeNode.h:18
std::string getMime() const
Very basic MIME inference based on file extension.
Definition: TreeNode.h:66
static void setMimeRules(const std::vector< std::pair< std::string, std::string > > &rules)
Replace all MIME rules.
Definition: TreeNode.h:46
static void addMimeRule(const std::string &ext, const std::string &mime)
Add one MIME rule mapping extension -> MIME type.
Definition: TreeNode.h:57
uint32_t size
Definition: TreeNode.h:25
std::vector< TreeNode *, AllocatorPSRAM< TreeNode * > > children
Definition: TreeNode.h:23
stringPSRAM file_name
Definition: TreeNode.h:22
bool is_dir
Definition: TreeNode.h:27
bool is_expanded
Definition: TreeNode.h:26
int level() const
Returns the nesting level from the root.
Definition: TreeNode.h:102
std::string path() const
Computes the full path from the root to this node.
Definition: TreeNode.h:87
TreeNode * parent
Definition: TreeNode.h:24
Definition: Allocator.h:13
std::basic_string< char, std::char_traits< char >, AllocatorPSRAM< char > > stringPSRAM
Definition: Allocator.h:106