mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2025-12-18 15:17:42 +03:00
Combine removeDir implementations in Epub
This commit is contained in:
parent
f67dd6a5f1
commit
583fe91fe4
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
|
#include "Epub/FsHelpers.h"
|
||||||
|
|
||||||
bool Epub::findContentOpfFile(const ZipFile& zip, std::string& contentOpfFile) {
|
bool Epub::findContentOpfFile(const ZipFile& zip, std::string& contentOpfFile) {
|
||||||
// open up the meta data to find where the content.opf file lives
|
// open up the meta data to find where the content.opf file lives
|
||||||
size_t s;
|
size_t s;
|
||||||
@ -249,46 +251,13 @@ bool Epub::load() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool removeDir(const char *path) {
|
|
||||||
// 1. Open the directory
|
|
||||||
File dir = SD.open(path);
|
|
||||||
if (!dir) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!dir.isDirectory()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
File file = dir.openNextFile();
|
|
||||||
while (file) {
|
|
||||||
String filePath = path;
|
|
||||||
if (!filePath.endsWith("/")) {
|
|
||||||
filePath += "/";
|
|
||||||
}
|
|
||||||
filePath += file.name();
|
|
||||||
|
|
||||||
if (file.isDirectory()) {
|
|
||||||
if (!removeDir(filePath.c_str())) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!SD.remove(filePath.c_str())) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
file = dir.openNextFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
return SD.rmdir(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Epub::clearCache() const {
|
bool Epub::clearCache() const {
|
||||||
if (!SD.exists(cachePath.c_str())) {
|
if (!SD.exists(cachePath.c_str())) {
|
||||||
Serial.printf("[%lu] [EPB] Cache does not exist, no action needed\n", millis());
|
Serial.printf("[%lu] [EPB] Cache does not exist, no action needed\n", millis());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!removeDir(cachePath.c_str())) {
|
if (!FsHelpers::removeDir(cachePath.c_str())) {
|
||||||
Serial.printf("[%lu] [EPB] Failed to clear cache\n", millis());
|
Serial.printf("[%lu] [EPB] Failed to clear cache\n", millis());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
36
lib/Epub/Epub/FsHelpers.cpp
Normal file
36
lib/Epub/Epub/FsHelpers.cpp
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#include "FsHelpers.h"
|
||||||
|
|
||||||
|
#include <SD.h>
|
||||||
|
|
||||||
|
bool FsHelpers::removeDir(const char* path) {
|
||||||
|
// 1. Open the directory
|
||||||
|
File dir = SD.open(path);
|
||||||
|
if (!dir) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!dir.isDirectory()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = dir.openNextFile();
|
||||||
|
while (file) {
|
||||||
|
String filePath = path;
|
||||||
|
if (!filePath.endsWith("/")) {
|
||||||
|
filePath += "/";
|
||||||
|
}
|
||||||
|
filePath += file.name();
|
||||||
|
|
||||||
|
if (file.isDirectory()) {
|
||||||
|
if (!removeDir(filePath.c_str())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!SD.remove(filePath.c_str())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file = dir.openNextFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
return SD.rmdir(path);
|
||||||
|
}
|
||||||
6
lib/Epub/Epub/FsHelpers.h
Normal file
6
lib/Epub/Epub/FsHelpers.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
class FsHelpers {
|
||||||
|
public:
|
||||||
|
static bool removeDir(const char* path);
|
||||||
|
};
|
||||||
@ -1,12 +1,13 @@
|
|||||||
#include "Section.h"
|
#include "Section.h"
|
||||||
|
|
||||||
#include <SD.h>
|
#include <SD.h>
|
||||||
|
#include <Serialization.h>
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
#include "EpubHtmlParserSlim.h"
|
#include "EpubHtmlParserSlim.h"
|
||||||
|
#include "FsHelpers.h"
|
||||||
#include "Page.h"
|
#include "Page.h"
|
||||||
#include "Serialization.h"
|
|
||||||
|
|
||||||
constexpr uint8_t SECTION_FILE_VERSION = 4;
|
constexpr uint8_t SECTION_FILE_VERSION = 4;
|
||||||
|
|
||||||
@ -89,39 +90,6 @@ void Section::setupCacheDir() const {
|
|||||||
SD.mkdir(cachePath.c_str());
|
SD.mkdir(cachePath.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool removeDir(const char *path) {
|
|
||||||
// 1. Open the directory
|
|
||||||
File dir = SD.open(path);
|
|
||||||
if (!dir) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!dir.isDirectory()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
File file = dir.openNextFile();
|
|
||||||
while (file) {
|
|
||||||
String filePath = path;
|
|
||||||
if (!filePath.endsWith("/")) {
|
|
||||||
filePath += "/";
|
|
||||||
}
|
|
||||||
filePath += file.name();
|
|
||||||
|
|
||||||
if (file.isDirectory()) {
|
|
||||||
if (!removeDir(filePath.c_str())) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!SD.remove(filePath.c_str())) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
file = dir.openNextFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
return SD.rmdir(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Your updated class method (assuming you are using the 'SD' object, which is a wrapper for a specific filesystem)
|
// Your updated class method (assuming you are using the 'SD' object, which is a wrapper for a specific filesystem)
|
||||||
bool Section::clearCache() const {
|
bool Section::clearCache() const {
|
||||||
if (!SD.exists(cachePath.c_str())) {
|
if (!SD.exists(cachePath.c_str())) {
|
||||||
@ -129,7 +97,7 @@ bool Section::clearCache() const {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!removeDir(cachePath.c_str())) {
|
if (!FsHelpers::removeDir(cachePath.c_str())) {
|
||||||
Serial.printf("[%lu] [SCT] Failed to clear cache\n", millis());
|
Serial.printf("[%lu] [SCT] Failed to clear cache\n", millis());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user