mirror of
https://github.com/QRouland/UTPass.git
synced 2026-01-10 19:36:57 +00:00
Refactor unzip password store
This commit is contained in:
@@ -1,67 +1,47 @@
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
#include <QUrl>
|
||||
#include <QtCore/QStandardPaths>
|
||||
#include <quazip5/JlCompress.h>
|
||||
#include <QSemaphore>
|
||||
|
||||
#include "jobs/unzipjob.h"
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
|
||||
Utils::Utils():
|
||||
m_sem(std::unique_ptr<QSemaphore>(new QSemaphore(1)))
|
||||
{}
|
||||
|
||||
bool Utils::unzip(QUrl zip_url, QString dir_out_path)
|
||||
{
|
||||
auto tmp_dir_path = QStandardPaths::writableLocation(
|
||||
QStandardPaths::CacheLocation).append("/unzip");
|
||||
|
||||
QDir tmp_dir(tmp_dir_path);
|
||||
tmp_dir.removeRecursively();
|
||||
tmp_dir.mkpath(".");
|
||||
|
||||
qDebug() << "Temp dir path is " << tmp_dir_path;
|
||||
auto status = !JlCompress::extractDir(
|
||||
zip_url.toLocalFile(),
|
||||
tmp_dir_path
|
||||
).isEmpty();
|
||||
|
||||
if (!status) {
|
||||
tmp_dir.removeRecursively();
|
||||
if (!this->m_sem->tryAcquire(1, 500)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
qDebug() << "Guessing if it should remove a single root folder";
|
||||
QStringList files_in_tmp_dir = tmp_dir.entryList(QDir::AllEntries | QDir::Hidden |
|
||||
QDir::NoDotAndDotDot);
|
||||
|
||||
auto dir_import_path =
|
||||
files_in_tmp_dir.length() == 1 ?
|
||||
tmp_dir_path.append("/" + files_in_tmp_dir.first()) : tmp_dir_path;
|
||||
qDebug() << "Final imported tmp path dir is " << dir_import_path;
|
||||
|
||||
qDebug() << "Removing destination";
|
||||
QDir dir_out(dir_out_path);
|
||||
dir_out.removeRecursively();
|
||||
|
||||
qDebug() << "Moving zip content to destination";
|
||||
QDir dir;
|
||||
qDebug() << dir_import_path << " to " << dir_out_path;
|
||||
auto ret = dir.rename(dir_import_path, dir_out_path);
|
||||
tmp_dir.removeRecursively();;
|
||||
return ret;
|
||||
qInfo() << "Unzip path " << zip_url << " to " << dir_out_path;
|
||||
auto job = new UnzipJob(zip_url, QDir(dir_out_path));
|
||||
connect(job, &UnzipJob::resultReady, this, &Utils::unzipResult);
|
||||
connect(job, &UnzipJob::finished, job, &QObject::deleteLater);
|
||||
job->start();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Utils::rmFile(QUrl file_url)
|
||||
void Utils::unzipResult(bool err)
|
||||
{
|
||||
return QFile::remove(file_url.toLocalFile());
|
||||
|
||||
qDebug() << "Unzip Result";
|
||||
if (err) {
|
||||
qInfo() << "Unzip Failed";
|
||||
emit unzipFailed("failed to unzip archive");
|
||||
|
||||
} else {
|
||||
qInfo() << "Unzip Succeed";
|
||||
emit unzipSucceed();
|
||||
}
|
||||
this->m_sem->release(1);
|
||||
}
|
||||
|
||||
bool Utils::rmDir(QUrl dir_url)
|
||||
{
|
||||
QDir dir(dir_url.toLocalFile());
|
||||
return dir.removeRecursively();
|
||||
}
|
||||
|
||||
QString Utils::manifestPath()
|
||||
{
|
||||
auto path = QDir(QDir::currentPath()).filePath("manifest_.json");
|
||||
qDebug() << "Manifest path : " << path;
|
||||
qInfo() << "Manifest path : " << path;
|
||||
return path;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user