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
|
#include "item_id.h"
#include <unity/storage/provider/Exceptions.h>
using namespace std;
using namespace unity::storage::provider;
static const auto URL_FORMAT = QUrl::FullyEncoded | QUrl::NormalizePathSegments;
QUrl id_to_url(string const& item_id, QUrl const& base_url)
{
QUrl item_url(QString::fromStdString(item_id), QUrl::StrictMode);
if (!item_url.isValid())
{
throw InvalidArgumentException("Invalid item ID: " + item_id);
}
item_url = base_url.resolved(item_url);
QByteArray base_url_bytes = base_url.toEncoded(URL_FORMAT);
QByteArray item_url_bytes = item_url.toEncoded(URL_FORMAT);
if (!item_url_bytes.startsWith(base_url_bytes))
{
throw InvalidArgumentException("Invalid item ID: " + item_id);
}
return item_url;
}
string url_to_id(QUrl const& item_url, QUrl const& base_url)
{
QByteArray base_url_bytes = base_url.toEncoded(URL_FORMAT);
QByteArray item_url_bytes = item_url.toEncoded(URL_FORMAT);
if (!item_url_bytes.startsWith(base_url_bytes))
{
throw RemoteCommsException("Url is outside of base URL: " + item_url_bytes.toStdString());
}
string item_id(item_url_bytes.begin() + base_url_bytes.size(),
item_url_bytes.end());
if (item_id.empty())
{
item_id = ".";
}
return item_id;
}
// Construct a possible ID for a named child of a parent ID
string make_child_id(string const& parent_id, string const& name)
{
if (name == "." || name == "..")
{
throw InvalidArgumentException("Invalid name: " + name);
}
string item_id = parent_id;
if (item_id == ".")
{
item_id.clear();
}
else if (item_id.size() == 0 || item_id[item_id.size()-1] != '/')
{
item_id += '/';
}
item_id += QUrl::toPercentEncoding(QString::fromStdString(name)).toStdString();
return item_id;
}
|