#include "filesystem.h"
#include <QFile>
#include <QFileInfo>
#include <QCryptographicHash>
#include <QDebug>

bool FileSystem::fileExists(QString path) {
    QFileInfo check_file(path);
    bool exists = check_file.exists() && check_file.isFile();

    if(exists == false)
        qWarning("%s:%i: %s%s", __FILE__, __LINE__, "Файл не найден: ", path.toStdString().c_str());

    return exists;
}

QString FileSystem::fileHash(const QString &fileName, QCryptographicHash::Algorithm hashAlgorithm) {
    QFile file(fileName);
    if (file.open(QIODevice::ReadOnly)) {
        QByteArray fileData = file.readAll();
        QByteArray hashData = QCryptographicHash::hash(fileData, hashAlgorithm);
        return hashData.toHex();
    }
    return QByteArray();
}

void FileSystem::clearFolder(QDir &dir) {
    //Получаем список файлов
    QStringList lstFiles = dir.entryList(QDir::Files);

    //Удаляем файлы
    foreach (QString entry, lstFiles){
        QString entryAbsPath = dir.absolutePath() + "/" + entry;
        //QFile::setPermissions(entryAbsPath, QFile::ReadOwner | QFile::WriteOwner);
        qDebug() << dir.absolutePath();
        QFile::remove(entryAbsPath);
    }
}
