downloaded files now stored with squawk:// prefix, that way I can move downloads folder without messing up the database

This commit is contained in:
Blue 2022-02-19 00:27:09 +03:00
parent 243edff8bd
commit d8b5ccb2da
Signed by: blue
GPG key ID: 9B203B252A63EE38
14 changed files with 75 additions and 44 deletions

View file

@ -305,7 +305,7 @@ void Core::NetworkAccess::onDownloadFinished()
if (path.size() > 0) {
path = checkFileName(fileName, path);
QFile file(path);
QFile file(Shared::resolvePath(path));
if (file.open(QIODevice::WriteOnly)) {
file.write(dwn->reply->readAll());
file.close();
@ -379,23 +379,20 @@ void Core::NetworkAccess::onUploadFinished()
Transfer* upl = itr->second;
if (upl->success) {
qDebug() << "upload success for" << url;
storage.addFile(upl->messages, upl->url, upl->path);
emit uploadFileComplete(upl->messages, upl->url, upl->path);
// Copy file to Download folder if it is a temp file. See Conversation::onImagePasted.
if (upl->path.startsWith(QDir::tempPath() + QStringLiteral("/squawk_img_attach_")) && upl->path.endsWith(".png")) {
if (upl->path.startsWith(QDir::tempPath() + QDir::separator() + QStringLiteral("squawk_img_attach_")) && upl->path.endsWith(".png")) {
QString err = "";
QString downloadDirPath = prepareDirectory(upl->messages.front().jid);
if (downloadDirPath.size() > 0) {
QString newPath = downloadDirPath + "/" + upl->path.mid(QDir::tempPath().length() + 1);
QString newPath = downloadDirPath + QDir::separator() + upl->path.mid(QDir::tempPath().length() + 1);
// Copy {TEMPDIR}/squawk_img_attach_XXXXXX.png to Download folder
bool copyResult = QFile::copy(upl->path, newPath);
bool copyResult = QFile::copy(upl->path, Shared::resolvePath(newPath));
if (copyResult) {
// Change storage
storage.setPath(upl->url, newPath);
upl->path = newPath;
} else {
err = "copying to " + newPath + " failed";
}
@ -407,6 +404,9 @@ void Core::NetworkAccess::onUploadFinished()
qDebug() << "failed to copy temporary upload file " << upl->path << " to download folder:" << err;
}
}
storage.addFile(upl->messages, upl->url, upl->path);
emit uploadFileComplete(upl->messages, upl->url, upl->path);
}
upl->reply->deleteLater();
@ -519,9 +519,12 @@ bool Core::NetworkAccess::checkAndAddToUploading(const QString& acc, const QStri
QString Core::NetworkAccess::prepareDirectory(const QString& jid)
{
QString path = currentPath;
QString addition;
if (jid.size() > 0) {
path += "/" + jid;
addition = jid;
path += QDir::separator() + jid;
}
QDir location(path);
if (!location.exists()) {
@ -529,10 +532,10 @@ QString Core::NetworkAccess::prepareDirectory(const QString& jid)
if (!res) {
return "";
} else {
return path;
return "squawk://" + addition;
}
}
return path;
return "squawk://" + addition;
}
QString Core::NetworkAccess::checkFileName(const QString& name, const QString& path)
@ -546,14 +549,17 @@ QString Core::NetworkAccess::checkFileName(const QString& name, const QString& p
suffix += "." + (*sItr);
}
QString postfix("");
QFileInfo proposedName(path + "/" + realName + suffix);
QString resolvedPath = Shared::resolvePath(path);
QString count("");
QFileInfo proposedName(resolvedPath + QDir::separator() + realName + count + suffix);
int counter = 0;
while (proposedName.exists()) {
QString count = QString("(") + std::to_string(++counter).c_str() + ")";
proposedName = QFileInfo(path + "/" + realName + count + suffix);
count = QString("(") + std::to_string(++counter).c_str() + ")";
proposedName = QFileInfo(resolvedPath + QDir::separator() + realName + count + suffix);
}
return proposedName.absoluteFilePath();
return path + QDir::separator() + realName + count + suffix;
}
QString Core::NetworkAccess::addMessageAndCheckForPath(const QString& url, const QString& account, const QString& jid, const QString& id)
@ -568,5 +574,10 @@ std::list<Shared::MessageInfo> Core::NetworkAccess::reportPathInvalid(const QStr
void Core::NetworkAccess::moveFilesDirectory(const QString& newPath)
{
QDir dir;
if (dir.rename(currentPath, newPath)) {
currentPath = newPath;
} else {
qDebug() << "couldn't move downloads directory, most probably downloads will be broken";
}
}