-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.cpp
135 lines (106 loc) · 3.4 KB
/
storage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <QDir>
#include <QDebug>
#include "types.h"
#include "storage.h"
const QString storage_t::tmpsuffix = ".davqt-tmp";
struct storage_t::Pimpl {
//Pimpl(const QString& r, QString p, bool k) : root(k ? r + QDir::separator() + p : r), path(p.replace(QRegExp("^[/]*"), "")), keep(k) {}
Pimpl(const QString& r, QString p, bool k) : root(r), path(p.replace(QRegExp("^[/]*"), "")), keep(k) {}
const QDir root;
const QString path;
const bool keep;
};
storage_t::storage_t(const QString& root, const QString& path, bool keep_structure)
: p_(new Pimpl(root, path, keep_structure))
{
if (!p_->root.exists()) {
if (!p_->root.mkpath(".")) {
throw qt_exception_t(QString("Can't use root dir [%1]").arg(p_->root.absolutePath()));
}
}
// if (!p_->path_dir.exists()) {
// if (!p_->path_dir.mkpath(".")) {
// throw qt_exception_t(QString("Can't use path dir [%1]").arg(p_->path_dir.absolutePath()));
// }
// }
//
// p_->path = p_->root.relativeFilePath(p_->path_dir.absolutePath());
qDebug() << QString("Storage created at [%1]/[%2]").arg(p_->root.absolutePath()).arg(p_->path);
}
storage_t::~storage_t()
{
}
QString storage_t::root() const
{
return p_->root.absolutePath();
}
QString storage_t::path() const
{
return p_->path;
}
QString storage_t::prefix() const
{
if (p_->keep) {
return root() + QDir::separator() + path();
}
else {
return root();
}
}
QString storage_t::key(QString path) const
{
if (p_->keep) {
return QString(this->path() + QDir::separator() + file_path(path)).replace(QRegExp("/$"), "").replace(QRegExp("^[/]*"), "").replace("//", "/");
}
else {
return QString(file_path(path)).replace(QRegExp("/$"), "").replace(QRegExp("^[/]*"), "").replace("//", "/");
}
}
bool storage_t::keep_structure() const
{
return p_->keep;
}
QFileInfo storage_t::info(QString raw_key) const
{
const QString key = raw_key.replace(QRegExp("^[/]*" + path()), "");
return QFileInfo(prefix() + QDir::separator() + key);
}
QString storage_t::file(const QString& key) const
{
return info(key).fileName();
}
QString storage_t::folder(const QString& key) const
{
QString dirname = info(key).dir().absolutePath() + "/";
return dirname.replace(prefix(), "").replace(QRegExp("^[/]*"), "").replace("//", "/");
}
QString storage_t::file_path(const QString& key) const
{
QString fullpath = info(key).absoluteFilePath();
return fullpath.replace(prefix(), "").replace(QRegExp("^[/]*"), "").replace("//", "/");
}
QString storage_t::absolute_file_path(const QString& key) const
{
return info(key).absoluteFilePath();
}
QString storage_t::remote_file_path(const QString& key) const
{
return QString("/" + p_->path + "/" + file_path(key)).replace("//", "/");
}
QFileInfoList storage_t::entries(QString raw_key) const
{
const QString key = raw_key.replace(QRegExp("^[/]*" + path()), "");
qDebug() << "Storage::entries: " << key;
QDir dir(absolute_file_path(key));
qDebug() << "mkapth:" << dir.absolutePath();
dir.mkpath(".");
if (!dir.exists()) throw qt_exception_t("Folder " + key + " does not exists");
QFileInfoList result = dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::System | QDir::Hidden, QDir::DirsFirst);
result.erase(
std::remove_if( std::begin(result), std::end(result), [] (const QFileInfo& info) {
return info.fileName().endsWith(storage_t::tmpsuffix);
}),
std::end(result)
);
return result;
}