Skip to content

Commit

Permalink
fix: file/dir name codec error (easymodo#354)
Browse files Browse the repository at this point in the history
current method to resolve path is not corret due to encoding issues,
simply using QDir/QFileInfo to replace std::filesystem and std::string.
  • Loading branch information
zymelaii committed Aug 29, 2023
1 parent 4c12677 commit 6beccb0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 72 deletions.
91 changes: 37 additions & 54 deletions qimgv/components/directorymanager/directorymanager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "directorymanager.h"

#include <QDir>
#include <QFileInfo>

namespace fs = std::filesystem;

DirectoryManager::DirectoryManager() :
Expand Down Expand Up @@ -320,64 +323,45 @@ void DirectoryManager::loadEntryList(QString directoryPath, bool recursive) {

// both directories & files
void DirectoryManager::addEntriesFromDirectory(std::vector<FSEntry> &entryVec, QString directoryPath) {
QRegularExpressionMatch match;
for(const auto & entry : fs::directory_iterator(toStdString(directoryPath))) {
QString name = QString::fromStdString(entry.path().filename().generic_string());
QDir root(directoryPath);
root.setFilter(QDir::Dirs | QDir::Files
#ifndef Q_OS_WIN32
// ignore hidden files
if(name.startsWith("."))
continue;
| QDir::NoDot
#endif
QString path = QString::fromStdString(entry.path().generic_string());
match = regex.match(name);
if(entry.is_directory()) { // this can still throw std::bad_alloc ..
FSEntry newEntry;
try {
newEntry.name = name;
newEntry.path = path;
newEntry.isDirectory = true;
//newEntry.size = entry.file_size();
//newEntry.modifyTime = entry.last_write_time();
} catch (const std::filesystem::filesystem_error &err) {
qDebug() << "[DirectoryManager]" << err.what();
continue;
}
);

QRegularExpressionMatch match{};
for (const auto &entry : root.entryInfoList()) {
match = regex.match(entry.absoluteFilePath());
if (!entry.isDir() && !match.hasMatch()) { continue; }
FSEntry newEntry{};
newEntry.name = entry.fileName();
newEntry.path = entry.absoluteFilePath();
newEntry.isDirectory = entry.isDir();
if (newEntry.isDirectory) {
dirEntryVec.emplace_back(newEntry);
} else if (match.hasMatch()) {
FSEntry newEntry;
try {
newEntry.name = name;
newEntry.path = path;
newEntry.isDirectory = false;
newEntry.size = entry.file_size();
newEntry.modifyTime = entry.last_write_time();
} catch (const std::filesystem::filesystem_error &err) {
qDebug() << "[DirectoryManager]" << err.what();
continue;
}
} else {
newEntry.size = entry.size();
newEntry.modifyTime = entry.lastModified();
entryVec.emplace_back(newEntry);
}
}
}

void DirectoryManager::addEntriesFromDirectoryRecursive(std::vector<FSEntry> &entryVec, QString directoryPath) {
QRegularExpressionMatch match;
for(const auto & entry : fs::recursive_directory_iterator(toStdString(directoryPath))) {
QString name = QString::fromStdString(entry.path().filename().generic_string());
QString path = QString::fromStdString(entry.path().generic_string());
match = regex.match(name);
if(!entry.is_directory() && match.hasMatch()) {
FSEntry newEntry;
try {
newEntry.name = name;
newEntry.path = path;
newEntry.isDirectory = false;
newEntry.size = entry.file_size();
newEntry.modifyTime = entry.last_write_time();
} catch (const std::filesystem::filesystem_error &err) {
qDebug() << "[DirectoryManager]" << err.what();
continue;
}
QDir root(directoryPath);
root.setFilter(QDir::Files);

QRegularExpressionMatch match{};
for (const auto &entry : root.entryInfoList()) {
match = regex.match(entry.absoluteFilePath());
if (match.hasMatch()) {
FSEntry newEntry{};
newEntry.name = entry.fileName();
newEntry.path = entry.absoluteFilePath();
newEntry.isDirectory = entry.isDir();
newEntry.size = entry.size();
newEntry.modifyTime = entry.lastModified();
entryVec.emplace_back(newEntry);
}
}
Expand Down Expand Up @@ -417,9 +401,8 @@ bool DirectoryManager::insertFileEntry(const QString &filePath) {
bool DirectoryManager::forceInsertFileEntry(const QString &filePath) {
if(!this->isFile(filePath) || containsFile(filePath))
return false;
std::filesystem::directory_entry stdEntry(toStdString(filePath));
QString fileName = QString::fromStdString(stdEntry.path().filename().generic_string()); // isn't it beautiful
FSEntry FSEntry(filePath, fileName, stdEntry.file_size(), stdEntry.last_write_time(), stdEntry.is_directory());
QFileInfo file(filePath);
FSEntry FSEntry(file.absoluteFilePath(), file.fileName(), file.size(), file.lastModified(), file.isDir());
insert_sorted(fileEntryVec, FSEntry, std::bind(compareFunction(), this, std::placeholders::_1, std::placeholders::_2));
if(!directoryPath().isEmpty()) {
qDebug() << "fileIns" << filePath << directoryPath();
Expand Down Expand Up @@ -471,8 +454,8 @@ void DirectoryManager::renameFileEntry(const QString &oldFilePath, const QString
int oldIndex = indexOfFile(oldFilePath);
fileEntryVec.erase(fileEntryVec.begin() + oldIndex);
// insert
std::filesystem::directory_entry stdEntry(toStdString(newFilePath));
FSEntry FSEntry(newFilePath, newFileName, stdEntry.file_size(), stdEntry.last_write_time(), stdEntry.is_directory());
QFileInfo file(newFilePath);
FSEntry FSEntry(file.absoluteFilePath(), file.fileName(), file.size(), file.lastModified(), file.isDir());
insert_sorted(fileEntryVec, FSEntry, std::bind(compareFunction(), this, std::placeholders::_1, std::placeholders::_2));
qDebug() << "fileRen" << oldFilePath << newFilePath;
emit fileRenamed(oldFilePath, oldIndex, newFilePath, indexOfFile(newFilePath));
Expand Down
1 change: 1 addition & 0 deletions qimgv/components/loader/loaderrunnable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LoaderRunnable::LoaderRunnable(QString _path) : path(_path) {
void LoaderRunnable::run() {
//QElapsedTimer t;
//t.start();
qDebug() << "Loading " << path;
auto image = ImageFactory::createImage(path);
//qDebug() << "L: " << t.elapsed();
emit finished(image, path);
Expand Down
32 changes: 16 additions & 16 deletions qimgv/sourcecontainers/fsentry.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
#include "fsentry.h"

#include <QDir>

FSEntry::FSEntry() {
}

FSEntry::FSEntry(const QString &path) {
std::filesystem::directory_entry stdEntry(toStdString(path));
QString name = QString::fromStdString(stdEntry.path().filename().generic_string());
if(stdEntry.is_directory()) {
try {
this->name = name;
this->path = path;
this->isDirectory = true;
} catch (const std::filesystem::filesystem_error &err) { }
QFileInfo file(path);
if(file.isDir()) {
this->name = file.fileName();
this->path = file.absoluteFilePath();
this->isDirectory = true;
} else {
try {
this->name = name;
this->path = path;
this->isDirectory = false;
this->size = stdEntry.file_size();
this->modifyTime = stdEntry.last_write_time();
} catch (const std::filesystem::filesystem_error &err) { }
this->name = file.fileName();
this->path = file.absoluteFilePath();
this->isDirectory = false;
this->size = file.size();
this->modifyTime = file.lastModified();
}
}

FSEntry::FSEntry( QString _path, QString _name, std::uintmax_t _size, std::filesystem::file_time_type _modifyTime, bool _isDirectory)
FSEntry::FSEntry( QString _path, QString _name, std::uintmax_t _size, QDateTime _modifyTime, bool _isDirectory)
: path(_path),
name(_name),
size(_size),
modifyTime(_modifyTime),
isDirectory(_isDirectory)
{
}

FSEntry::FSEntry( QString _path, QString _name, std::uintmax_t _size, bool _isDirectory)
: path(_path),
name(_name),
size(_size),
isDirectory(_isDirectory)
{
}

FSEntry::FSEntry( QString _path, QString _name, bool _isDirectory)
: path(_path),
name(_name),
isDirectory(_isDirectory)
{
}

bool FSEntry::operator==(const QString &anotherPath) const {
return this->path == anotherPath;
}
5 changes: 3 additions & 2 deletions qimgv/sourcecontainers/fsentry.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#pragma once
#include <QString>
#include <QDateTime>
#include <filesystem>
#include "utils/stuff.h"

class FSEntry {
public:
FSEntry();
FSEntry( const QString &filePath);
FSEntry( QString _path, QString _name, std::uintmax_t _size, std::filesystem::file_time_type _modifyTime, bool _isDirectory);
FSEntry( QString _path, QString _name, std::uintmax_t _size, QDateTime _modifyTime, bool _isDirectory);
FSEntry( QString _path, QString _name, std::uintmax_t _size, bool _isDirectory);
FSEntry( QString _path, QString _name, bool _isDirectory);
bool operator==(const QString &anotherPath) const;

QString path, name;
std::uintmax_t size;
std::filesystem::file_time_type modifyTime;
QDateTime modifyTime;
bool isDirectory;
};

0 comments on commit 6beccb0

Please sign in to comment.