transitioned urlstorage to LMDBAL, made it possible to build against latest qxmpp
This commit is contained in:
parent
81cf0f8d34
commit
5fbb03fc46
10 changed files with 363 additions and 599 deletions
|
@ -35,13 +35,11 @@ Core::NetworkAccess::NetworkAccess(QObject* parent):
|
|||
currentPath = settings.value("downloadsPath").toString();
|
||||
}
|
||||
|
||||
Core::NetworkAccess::~NetworkAccess()
|
||||
{
|
||||
Core::NetworkAccess::~NetworkAccess() {
|
||||
stop();
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::downladFile(const QString& url)
|
||||
{
|
||||
void Core::NetworkAccess::downladFile(const QString& url) {
|
||||
std::map<QString, Transfer*>::iterator itr = downloads.find(url);
|
||||
if (itr != downloads.end()) {
|
||||
qDebug() << "NetworkAccess received a request to download a file" << url << ", but the file is currently downloading, skipping";
|
||||
|
@ -50,27 +48,25 @@ void Core::NetworkAccess::downladFile(const QString& url)
|
|||
std::pair<QString, std::list<Shared::MessageInfo>> p = storage.getPath(url);
|
||||
if (p.first.size() > 0) {
|
||||
QFileInfo info(p.first);
|
||||
if (info.exists() && info.isFile()) {
|
||||
if (info.exists() && info.isFile())
|
||||
emit downloadFileComplete(p.second, p.first);
|
||||
} else {
|
||||
else
|
||||
startDownload(p.second, url);
|
||||
}
|
||||
} else {
|
||||
startDownload(p.second, url);
|
||||
}
|
||||
} catch (const Archive::NotFound& e) {
|
||||
} catch (const LMDBAL::NotFound& e) {
|
||||
qDebug() << "NetworkAccess received a request to download a file" << url << ", but there is now record of which message uses that file, downloading anyway";
|
||||
storage.addFile(url);
|
||||
startDownload(std::list<Shared::MessageInfo>(), url);
|
||||
} catch (const Archive::Unknown& e) {
|
||||
} catch (const LMDBAL::Unknown& e) {
|
||||
qDebug() << "Error requesting file path:" << e.what();
|
||||
emit loadFileError(std::list<Shared::MessageInfo>(), QString("Database error: ") + e.what(), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::start()
|
||||
{
|
||||
void Core::NetworkAccess::start() {
|
||||
if (!running) {
|
||||
manager = new QNetworkAccessManager();
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
|
||||
|
@ -81,8 +77,7 @@ void Core::NetworkAccess::start()
|
|||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::stop()
|
||||
{
|
||||
void Core::NetworkAccess::stop() {
|
||||
if (running) {
|
||||
storage.close();
|
||||
manager->deleteLater();
|
||||
|
@ -96,8 +91,7 @@ void Core::NetworkAccess::stop()
|
|||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
||||
{
|
||||
void Core::NetworkAccess::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
|
||||
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
||||
QString url = rpl->url().toString();
|
||||
std::map<QString, Transfer*>::const_iterator itr = downloads.find(url);
|
||||
|
@ -115,8 +109,7 @@ void Core::NetworkAccess::onDownloadProgress(qint64 bytesReceived, qint64 bytesT
|
|||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::onDownloadError(QNetworkReply::NetworkError code)
|
||||
{
|
||||
void Core::NetworkAccess::onDownloadError(QNetworkReply::NetworkError code) {
|
||||
qDebug() << "DEBUG: DOWNLOAD ERROR";
|
||||
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
||||
qDebug() << rpl->errorString();
|
||||
|
@ -134,8 +127,7 @@ void Core::NetworkAccess::onDownloadError(QNetworkReply::NetworkError code)
|
|||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::onDownloadSSLError(const QList<QSslError>& errors)
|
||||
{
|
||||
void Core::NetworkAccess::onDownloadSSLError(const QList<QSslError>& errors) {
|
||||
qDebug() << "DEBUG: DOWNLOAD SSL ERRORS";
|
||||
for (const QSslError& err : errors) {
|
||||
qDebug() << err.errorString();
|
||||
|
@ -154,9 +146,7 @@ void Core::NetworkAccess::onDownloadSSLError(const QList<QSslError>& errors)
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
QString Core::NetworkAccess::getErrorText(QNetworkReply::NetworkError code)
|
||||
{
|
||||
QString Core::NetworkAccess::getErrorText(QNetworkReply::NetworkError code) {
|
||||
QString errorText("");
|
||||
switch (code) {
|
||||
case QNetworkReply::NoError:
|
||||
|
@ -280,8 +270,7 @@ QString Core::NetworkAccess::getErrorText(QNetworkReply::NetworkError code)
|
|||
}
|
||||
|
||||
|
||||
void Core::NetworkAccess::onDownloadFinished()
|
||||
{
|
||||
void Core::NetworkAccess::onDownloadFinished() {
|
||||
qDebug() << "DEBUG: DOWNLOAD FINISHED";
|
||||
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
||||
QString url = rpl->url().toString();
|
||||
|
@ -296,11 +285,11 @@ void Core::NetworkAccess::onDownloadFinished()
|
|||
QStringList hops = url.split("/");
|
||||
QString fileName = hops.back();
|
||||
QString jid;
|
||||
if (dwn->messages.size() > 0) {
|
||||
if (dwn->messages.size() > 0)
|
||||
jid = dwn->messages.front().jid;
|
||||
} else {
|
||||
else
|
||||
qDebug() << "An attempt to save the file but it doesn't seem to belong to any message, download is definately going to be broken";
|
||||
}
|
||||
|
||||
QString path = prepareDirectory(jid);
|
||||
if (path.size() > 0) {
|
||||
path = checkFileName(fileName, path);
|
||||
|
@ -319,11 +308,10 @@ void Core::NetworkAccess::onDownloadFinished()
|
|||
err = "Couldn't prepare a directory for file";
|
||||
}
|
||||
|
||||
if (path.size() > 0) {
|
||||
if (path.size() > 0)
|
||||
emit downloadFileComplete(dwn->messages, path);
|
||||
} else {
|
||||
else
|
||||
emit loadFileError(dwn->messages, "Error saving file " + url + "; " + err, false);
|
||||
}
|
||||
}
|
||||
|
||||
dwn->reply->deleteLater();
|
||||
|
@ -332,8 +320,7 @@ void Core::NetworkAccess::onDownloadFinished()
|
|||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::startDownload(const std::list<Shared::MessageInfo>& msgs, const QString& url)
|
||||
{
|
||||
void Core::NetworkAccess::startDownload(const std::list<Shared::MessageInfo>& msgs, const QString& url) {
|
||||
Transfer* dwn = new Transfer({msgs, 0, 0, true, "", url, 0});
|
||||
QNetworkRequest req(url);
|
||||
dwn->reply = manager->get(req);
|
||||
|
@ -349,8 +336,7 @@ void Core::NetworkAccess::startDownload(const std::list<Shared::MessageInfo>& ms
|
|||
emit loadFileProgress(dwn->messages, 0, false);
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::onUploadError(QNetworkReply::NetworkError code)
|
||||
{
|
||||
void Core::NetworkAccess::onUploadError(QNetworkReply::NetworkError code) {
|
||||
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
||||
QString url = rpl->url().toString();
|
||||
std::map<QString, Transfer*>::const_iterator itr = uploads.find(url);
|
||||
|
@ -368,8 +354,7 @@ void Core::NetworkAccess::onUploadError(QNetworkReply::NetworkError code)
|
|||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::onUploadFinished()
|
||||
{
|
||||
void Core::NetworkAccess::onUploadFinished() {
|
||||
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
||||
QString url = rpl->url().toString();
|
||||
std::map<QString, Transfer*>::const_iterator itr = uploads.find(url);
|
||||
|
@ -389,20 +374,16 @@ void Core::NetworkAccess::onUploadFinished()
|
|||
|
||||
// Copy {TEMPDIR}/squawk_img_attach_XXXXXX.png to Download folder
|
||||
bool copyResult = QFile::copy(upl->path, Shared::resolvePath(newPath));
|
||||
|
||||
if (copyResult) {
|
||||
// Change storage
|
||||
upl->path = newPath;
|
||||
} else {
|
||||
if (copyResult)
|
||||
upl->path = newPath; // Change storage
|
||||
else
|
||||
err = "copying to " + newPath + " failed";
|
||||
}
|
||||
} else {
|
||||
err = "Couldn't prepare a directory for file";
|
||||
}
|
||||
|
||||
if (err.size() != 0) {
|
||||
if (err.size() != 0)
|
||||
qDebug() << "failed to copy temporary upload file " << upl->path << " to download folder:" << err;
|
||||
}
|
||||
}
|
||||
|
||||
storage.addFile(upl->messages, upl->url, upl->path);
|
||||
|
@ -417,8 +398,7 @@ void Core::NetworkAccess::onUploadFinished()
|
|||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::onUploadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
||||
{
|
||||
void Core::NetworkAccess::onUploadProgress(qint64 bytesReceived, qint64 bytesTotal) {
|
||||
QNetworkReply* rpl = static_cast<QNetworkReply*>(sender());
|
||||
QString url = rpl->url().toString();
|
||||
std::map<QString, Transfer*>::const_iterator itr = uploads.find(url);
|
||||
|
@ -436,13 +416,12 @@ void Core::NetworkAccess::onUploadProgress(qint64 bytesReceived, qint64 bytesTot
|
|||
}
|
||||
}
|
||||
|
||||
QString Core::NetworkAccess::getFileRemoteUrl(const QString& path)
|
||||
{
|
||||
QString Core::NetworkAccess::getFileRemoteUrl(const QString& path) {
|
||||
QString p = Shared::squawkifyPath(path);
|
||||
|
||||
try {
|
||||
p = storage.getUrl(p);
|
||||
} catch (const Archive::NotFound& err) {
|
||||
} catch (const LMDBAL::NotFound& err) {
|
||||
p = "";
|
||||
} catch (...) {
|
||||
throw;
|
||||
|
@ -451,8 +430,13 @@ QString Core::NetworkAccess::getFileRemoteUrl(const QString& path)
|
|||
return p;
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::uploadFile(const Shared::MessageInfo& info, const QString& path, const QUrl& put, const QUrl& get, const QMap<QString, QString> headers)
|
||||
{
|
||||
void Core::NetworkAccess::uploadFile(
|
||||
const Shared::MessageInfo& info,
|
||||
const QString& path,
|
||||
const QUrl& put,
|
||||
const QUrl& get,
|
||||
const QMap<QString, QString> headers
|
||||
) {
|
||||
QFile* file = new QFile(path);
|
||||
Transfer* upl = new Transfer({{info}, 0, 0, true, path, get.toString(), file});
|
||||
QNetworkRequest req(put);
|
||||
|
@ -479,22 +463,18 @@ void Core::NetworkAccess::uploadFile(const Shared::MessageInfo& info, const QStr
|
|||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::registerFile(const QString& url, const QString& account, const QString& jid, const QString& id)
|
||||
{
|
||||
void Core::NetworkAccess::registerFile(const QString& url, const QString& account, const QString& jid, const QString& id) {
|
||||
storage.addFile(url, account, jid, id);
|
||||
std::map<QString, Transfer*>::iterator itr = downloads.find(url);
|
||||
if (itr != downloads.end()) {
|
||||
if (itr != downloads.end())
|
||||
itr->second->messages.emplace_back(account, jid, id); //TODO notification is going to happen the next tick, is that okay?
|
||||
}
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::registerFile(const QString& url, const QString& path, const QString& account, const QString& jid, const QString& id)
|
||||
{
|
||||
void Core::NetworkAccess::registerFile(const QString& url, const QString& path, const QString& account, const QString& jid, const QString& id) {
|
||||
storage.addFile(url, path, account, jid, id);
|
||||
}
|
||||
|
||||
bool Core::NetworkAccess::checkAndAddToUploading(const QString& acc, const QString& jid, const QString id, const QString path)
|
||||
{
|
||||
bool Core::NetworkAccess::checkAndAddToUploading(const QString& acc, const QString& jid, const QString id, const QString path) {
|
||||
for (const std::pair<const QString, Transfer*>& pair : uploads) {
|
||||
Transfer* info = pair.second;
|
||||
if (pair.second->path == path) {
|
||||
|
@ -516,8 +496,7 @@ bool Core::NetworkAccess::checkAndAddToUploading(const QString& acc, const QStri
|
|||
return false;
|
||||
}
|
||||
|
||||
QString Core::NetworkAccess::prepareDirectory(const QString& jid)
|
||||
{
|
||||
QString Core::NetworkAccess::prepareDirectory(const QString& jid) {
|
||||
QString path = currentPath;
|
||||
QString addition;
|
||||
if (jid.size() > 0) {
|
||||
|
@ -529,25 +508,23 @@ QString Core::NetworkAccess::prepareDirectory(const QString& jid)
|
|||
|
||||
if (!location.exists()) {
|
||||
bool res = location.mkpath(path);
|
||||
if (!res) {
|
||||
if (!res)
|
||||
return "";
|
||||
} else {
|
||||
else
|
||||
return "squawk://" + addition;
|
||||
}
|
||||
}
|
||||
return "squawk://" + addition;
|
||||
}
|
||||
|
||||
QString Core::NetworkAccess::checkFileName(const QString& name, const QString& path)
|
||||
{
|
||||
QString Core::NetworkAccess::checkFileName(const QString& name, const QString& path) {
|
||||
QStringList parts = name.split(".");
|
||||
QString suffix("");
|
||||
QStringList::const_iterator sItr = parts.begin();
|
||||
QString realName = *sItr;
|
||||
++sItr;
|
||||
for (QStringList::const_iterator sEnd = parts.end(); sItr != sEnd; ++sItr) {
|
||||
for (QStringList::const_iterator sEnd = parts.end(); sItr != sEnd; ++sItr)
|
||||
suffix += "." + (*sItr);
|
||||
}
|
||||
|
||||
QString postfix("");
|
||||
QString resolvedPath = Shared::resolvePath(path);
|
||||
QString count("");
|
||||
|
@ -562,18 +539,15 @@ QString Core::NetworkAccess::checkFileName(const QString& name, const QString& p
|
|||
return path + QDir::separator() + realName + count + suffix;
|
||||
}
|
||||
|
||||
QString Core::NetworkAccess::addMessageAndCheckForPath(const QString& url, const QString& account, const QString& jid, const QString& id)
|
||||
{
|
||||
QString Core::NetworkAccess::addMessageAndCheckForPath(const QString& url, const QString& account, const QString& jid, const QString& id) {
|
||||
return storage.addMessageAndCheckForPath(url, account, jid, id);
|
||||
}
|
||||
|
||||
std::list<Shared::MessageInfo> Core::NetworkAccess::reportPathInvalid(const QString& path)
|
||||
{
|
||||
std::list<Shared::MessageInfo> Core::NetworkAccess::reportPathInvalid(const QString& path) {
|
||||
return storage.deletedFile(path);
|
||||
}
|
||||
|
||||
void Core::NetworkAccess::moveFilesDirectory(const QString& newPath)
|
||||
{
|
||||
void Core::NetworkAccess::moveFilesDirectory(const QString& newPath) {
|
||||
QDir dir(currentPath);
|
||||
bool success = true;
|
||||
qDebug() << "moving" << currentPath << "to" << newPath;
|
||||
|
@ -582,8 +556,8 @@ void Core::NetworkAccess::moveFilesDirectory(const QString& newPath)
|
|||
success = dir.rename(fileName, newPath + QDir::separator() + fileName) && success;
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
if (!success)
|
||||
qDebug() << "couldn't move downloads directory, most probably downloads will be broken";
|
||||
}
|
||||
|
||||
currentPath = newPath;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue