22 SDIndex(SDT &sd) { p_sd = &sd; };
23 void begin(
const char *startDir,
const char *extension,
24 const char *file_name_pattern,
bool setupIndex =
true) {
26 this->start_dir = startDir;
27 this->ext = extension;
28 this->file_name_pattern = file_name_pattern;
29 idx_path = filePathString(startDir,
"idx.txt");
30 idx_defpath = filePathString(startDir,
"idx-def.txt");
31 int idx_file_size = indexFileTSize();
32 LOGI(
"Index file size: %d", idx_file_size);
34 String(startDir) +
"|" + extension +
"|" + file_name_pattern;
35 String keyOld = getIndexDef();
36 if (setupIndex && (keyNew != keyOld || idx_file_size == 0)) {
37 FileT idxfile = p_sd->open(idx_path.c_str(), FILE_WRITE);
38 LOGW(
"Creating index file");
40 LOGI(
"Indexing completed");
47 void ls(
Print &p,
const char *startDir,
const char *extension,
48 const char *file_name_pattern =
"*") {
50 this->ext = extension;
51 this->file_name_pattern = file_name_pattern;
53 file_path_stack.clear();
54 file_path_str.clear();
60 if (max_idx >= 0 && idx > max_idx) {
61 LOGE(
"idx %d > size %d", idx, max_idx);
65 FileT idxfile = p_sd->open(idx_path.c_str());
68 if (idxfile.available() == 0) {
69 LOGE(
"Index file is empty");
73 while (idxfile.available() > 0 && !found) {
74 result = idxfile.readStringUntil(
'\n');
77 char *c_str = (
char *)result.c_str();
79 int lastPos = result.length() - 1;
80 if (c_str[lastPos] == 13) {
84 LOGD(
"%d -> %s", count, c_str);
95 return found ? result.c_str() :
nullptr;
100 if (filename ==
nullptr) {
101 LOGE(
"filename is null");
105 FileT idxfile = p_sd->open(idx_path.c_str());
106 if (idxfile.available() == 0) {
107 LOGE(
"Index file is empty");
113 String searchName(filename);
115 while (idxfile.available() > 0) {
116 result = idxfile.readStringUntil(
'\n');
119 char *c_str = (
char *)result.c_str();
121 int lastPos = result.length() - 1;
122 if (lastPos >= 0 && c_str[lastPos] == 13) {
126 LOGD(
"Comparing %d: '%s' with '%s'", count, c_str, filename);
129 String currentFile(c_str);
130 if (currentFile == searchName ||
131 currentFile.endsWith(
"/" + searchName) ||
132 currentFile.endsWith(searchName)) {
133 LOGD(
"Found '%s' at index %d", filename, count);
147 LOGD(
"File '%s' not found in index", filename);
153 FileT idxfile = p_sd->open(idx_path.c_str());
156 while (idxfile.available() > 0) {
157 result = idxfile.readStringUntil(
'\n');
159 char *c_str = (
char *)result.c_str();
173 List<String> file_path_stack;
174 String file_path_str;
175 const char *start_dir;
177 const char *ext =
nullptr;
178 const char *file_name_pattern =
nullptr;
181 String filePathString(
const char *name,
const char *suffix) {
182 String result = name;
183 return result.endsWith(
"/") ? result + suffix : result +
"/" + suffix;
188 LOGD(
"listDir: %s", dirname);
189 FileT root = open(dirname);
191 LOGE(
"Open failed: %s", dirname);
195 if (!isDirectory(root)) {
196 LOGD(
"Is not directory: %s", dirname);
200 if (
StrView(dirname).startsWith(
".")) {
201 LOGD(
"Invalid file: %s", dirname);
207 FileT file = openNext(root);
209 if (isDirectory(file)) {
211 LOGD(
"name: %s", name.c_str());
213 listDir(idxfile, name.c_str());
217 LOGD(
"Adding file to index: %s", fn);
220 LOGD(
"Ignoring %s", fn);
223 file = openNext(root);
228 bool isDirectory(FileT &f) {
233 result = f.isDirectory();
235 LOGD(
"isDirectory %s: %d",
fileName(f), result);
239 FileT openNext(FileT &dir) {
243 if (!result.openNext(&dir, O_READ)) {
244 LOGD(
"No next file");
248 return dir.openNextFile();
252 void pushPath(
const char *name) {
253 LOGD(
"pushPath: %s", name);
254 LOGD(
"pushPath: %s", name);
255 String nameStr(name);
256 file_path_stack.push_back(nameStr);
262 file_path_stack.pop_back(str);
263 LOGD(
"popPath: %s", str.c_str());
269 const char *file_name =
fileName(file);
270 if (file.isDirectory()) {
271 LOGD(
"-> isValidAudioFile: '%s': %d", file_name,
false);
274 StrView strFileTName(file_name);
276 strFileTName.
matches(file_name_pattern) && !isHidden(file);
277 LOGD(
"-> isValidAudioFile: '%s': %d", file_name, result);
281 String getIndexDef() {
282 FileT idxdef = p_sd->open(idx_defpath.c_str());
283 String key1 = idxdef.readString();
287 void saveIndexDef(String keyNew) {
288 FileT idxdef = p_sd->open(idx_defpath.c_str(), FILE_WRITE);
289 idxdef.write((
const uint8_t *)keyNew.c_str(), keyNew.length());
293 size_t indexFileTSize() {
294 FileT idxfile = p_sd->open(idx_path.c_str());
295 size_t result = idxfile.size();
300 void rewind(FileT &f) {
313 static char name[MAX_FILE_LEN];
314 file.getName(name, MAX_FILE_LEN);
323 return file.name() + pos;
329#if defined(USE_SDFAT) || ESP_IDF_VERSION_MAJOR >= 4
330 LOGD(
"-> fileNamePath: %s",
fileName(file));
331 file_path_str = start_dir;
332 if (!file_path_str.endsWith(
"/")) {
333 file_path_str +=
"/";
335 for (
int j = 0; j < file_path_stack.size(); j++) {
336 file_path_str += file_path_stack[j] +
"/";
339 static char name[MAX_FILE_LEN];
340 strncpy(name,
fileName(file), MAX_FILE_LEN);
341 file_path_str += name;
342 const char *result = file_path_str.c_str();
343 LOGD(
"<- fileNamePath: %s", result);
350 bool isHidden(FileT &f) {
358 FileT open(
const char *name) {
362 if (!result.open(name)) {
363 LOGE(
"FileT open error: %s", name);
367 return p_sd->open(name,
"r");