-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurltofilename.cpp
More file actions
46 lines (39 loc) · 1.27 KB
/
Copy pathurltofilename.cpp
File metadata and controls
46 lines (39 loc) · 1.27 KB
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
#include "urltofilename.h"
#include <QCryptographicHash>
#include <QRegularExpression>
QString urltofilename(QString url, QMap<QString,QRegularExpression> *defined_urlmatch_ext)
{
auto reflag = QRegularExpression::MultilineOption | QRegularExpression::ExtendedPatternSyntaxOption | QRegularExpression::CaseInsensitiveOption | QRegularExpression::DotMatchesEverythingOption | QRegularExpression::UseUnicodePropertiesOption;
QByteArray ba = QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md5);
QString md5val = ba.toHex().constData();
QString ext;
if (defined_urlmatch_ext != nullptr)
{
for (auto it = defined_urlmatch_ext->begin(); it != defined_urlmatch_ext->end(); it++)
{
if (it.key() != "")
{
if (it.value().match(url).hasMatch())
{
ext = it.key();
if (ext[0] != '.')ext = "." + ext;
break;
}
}
}
}
if (ext == "")
{
ext = url;
if (ext.lastIndexOf(".") != -1)
ext = ext.mid(ext.lastIndexOf("."));
else
ext = "";
if (ext.indexOf("?") != -1)
ext = ext.mid(0, ext.indexOf("?"));
if (ext == ".php")ext = ".html";
ext = ext.replace(QRegularExpression("[\\?/\\\\:<>\\|*]", reflag), "_").trimmed();
if (ext.size() > 6)ext = ext.mid(0, 6);
}
return md5val + ext;
}