Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
feat(db): add file hash to file history
Browse files Browse the repository at this point in the history
Not currently used, but there are plans to display if a transfered file
has been modified, which the file hash will be needed for. Adding file
hash at the same time as file history also saves a db schema update.
  • Loading branch information
anthonybilinski authored and sphaerophoria committed Dec 2, 2018
1 parent d9b39b3 commit 8427be6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/core/corefile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ void CoreFile::onFileDataCallback(Tox* tox, uint32_t friendId, uint32_t fileId,
return;
}
file->bytesSent += length;
file->hashGenerator->addData((const char*)data.get(), length);
}

if (!tox_file_send_chunk(tox, friendId, fileId, pos, data.get(), nread, nullptr)) {
Expand Down Expand Up @@ -492,6 +493,7 @@ void CoreFile::onFileRecvChunkCallback(Tox* tox, uint32_t friendId, uint32_t fil
else
file->file->write((char*)data, length);
file->bytesSent += length;
file->hashGenerator->addData((const char*)data, length);

if (file->fileKind != TOX_FILE_KIND_AVATAR)
emit static_cast<Core*>(core)->fileTransferInfo(*file);
Expand Down
2 changes: 2 additions & 0 deletions src/core/toxfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <QString>
#include <memory>
#include <QCryptographicHash>

class QFile;
class QTimer;
Expand Down Expand Up @@ -51,6 +52,7 @@ struct ToxFile
FileDirection direction;
QByteArray avatarData;
QByteArray resumeFileId;
std::shared_ptr<QCryptographicHash> hashGenerator = std::make_shared<QCryptographicHash>(QCryptographicHash::Sha256);
};

#endif // CORESTRUCTS_H
20 changes: 11 additions & 9 deletions src/persistence/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ History::History(std::shared_ptr<RawDatabase> db)
"file_restart_id BLOB NOT NULL,"
"file_name BLOB NOT NULL, "
"file_path BLOB NOT NULL,"
"file_hash BLOB NOT NULL,"
"file_size INTEGER NOT NULL,"
"direction INTEGER NOT NULL,"
"file_state INTEGER NOT NULL);"
Expand Down Expand Up @@ -279,13 +280,13 @@ void History::onFileInsertionReady(FileDbInsertionData data)
auto fileId = data.fileId;
queries +=
RawDatabase::Query(QStringLiteral("INSERT INTO file_transfers (chat_id, file_restart_id, "
"file_path, file_name, file_size, direction, file_state) "
"VALUES (%1, ?, ?, ?, %2, %3, %4);")
"file_path, file_name, file_hash, file_size, direction, file_state) "
"VALUES (%1, ?, ?, ?, ?, %2, %3, %4);")
.arg(peerId)
.arg(data.size)
.arg(static_cast<int>(data.direction))
.arg(ToxFile::CANCELED),
{data.fileId.toUtf8(), data.filePath.toUtf8(), data.fileName},
{data.fileId.toUtf8(), data.filePath.toUtf8(), data.fileName, QByteArray()},
[weakThis, fileId](int64_t id) {
auto pThis = weakThis.lock();
if (pThis) {
Expand All @@ -306,24 +307,24 @@ void History::onFileInserted(int64_t dbId, QString fileId)
{
auto& fileInfo = fileInfos[fileId];
if (fileInfo.finished) {
db->execLater(generateFileFinished(dbId, fileInfo.success, fileInfo.filePath));
db->execLater(generateFileFinished(dbId, fileInfo.success, fileInfo.filePath, fileInfo.fileHash));
fileInfos.remove(fileId);
} else {
fileInfo.finished = false;
fileInfo.fileId = dbId;
}
}

RawDatabase::Query History::generateFileFinished(int64_t id, bool success, const QString& filePath)
RawDatabase::Query History::generateFileFinished(int64_t id, bool success, const QString& filePath, const QByteArray& fileHash)
{
auto file_state = success ? ToxFile::FINISHED : ToxFile::CANCELED;
if (filePath.length()) {
return RawDatabase::Query(QStringLiteral("UPDATE file_transfers "
"SET file_state = %1, file_path = ? "
"SET file_state = %1, file_path = ?, file_hash = ?"
"WHERE id = %2")
.arg(file_state)
.arg(id),
{filePath.toUtf8()});
{filePath.toUtf8(), fileHash});
} else {
return RawDatabase::Query(QStringLiteral("UPDATE file_transfers "
"SET finished = %1 "
Expand Down Expand Up @@ -405,15 +406,16 @@ void History::addNewMessage(const QString& friendPk, const QString& message, con
insertIdCallback));
}

void History::setFileFinished(const QString& fileId, bool success, const QString& filePath)
void History::setFileFinished(const QString& fileId, bool success, const QString& filePath, const QByteArray& fileHash)
{
auto& fileInfo = fileInfos[fileId];
if (fileInfo.fileId == -1) {
fileInfo.finished = true;
fileInfo.success = success;
fileInfo.filePath = filePath;
fileInfo.fileHash = fileHash;
} else {
db->execLater(generateFileFinished(fileInfo.fileId, success, filePath));
db->execLater(generateFileFinished(fileInfo.fileId, success, filePath, fileHash));
}

fileInfos.remove(fileId);
Expand Down
5 changes: 3 additions & 2 deletions src/persistence/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class History : public QObject, public std::enable_shared_from_this<History>
const QByteArray& fileName, const QString& filePath, int64_t size,
const QString& sender, const QDateTime& time, QString const& dispName);

void setFileFinished(const QString& fileId, bool success, const QString& filePath);
void setFileFinished(const QString& fileId, bool success, const QString& filePath, const QByteArray& fileHash);

QList<HistMessage> getChatHistoryFromDate(const QString& friendPk, const QDateTime& from,
const QDateTime& to);
Expand Down Expand Up @@ -198,7 +198,7 @@ private slots:
const QDateTime& to, int numMessages);

static RawDatabase::Query generateFileFinished(int64_t fileId, bool success,
const QString& filePath);
const QString& filePath, const QByteArray& fileHash);
void dbSchemaUpgrade();

std::shared_ptr<RawDatabase> db;
Expand All @@ -210,6 +210,7 @@ private slots:
bool finished = false;
bool success = false;
QString filePath;
QByteArray fileHash;
int64_t fileId = -1;
};

Expand Down
6 changes: 3 additions & 3 deletions src/widget/form/chatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,19 +329,19 @@ void ChatForm::startFileSend(ToxFile file)

void ChatForm::onFileTransferFinished(ToxFile file)
{
history->setFileFinished(file.resumeFileId, true, file.filePath);
history->setFileFinished(file.resumeFileId, true, file.filePath, file.hashGenerator->result());
}

void ChatForm::onFileTransferBrokenUnbroken(ToxFile file, bool broken)
{
if (broken) {
history->setFileFinished(file.resumeFileId, false, file.filePath);
history->setFileFinished(file.resumeFileId, false, file.filePath, file.hashGenerator->result());
}
}

void ChatForm::onFileTransferCancelled(ToxFile file)
{
history->setFileFinished(file.resumeFileId, false, file.filePath);
history->setFileFinished(file.resumeFileId, false, file.filePath, file.hashGenerator->result());
}

void ChatForm::onFileRecvRequest(ToxFile file)
Expand Down

0 comments on commit 8427be6

Please sign in to comment.