Skip to content
Snippets Groups Projects
Commit f9a34f99 authored by Sébastien Blin's avatar Sébastien Blin
Browse files

data_transfer: channel can live longer than IncomingFile

Change-Id: Ifbc66d0197949f82826bd743b3a015ae5b540e6f
parent 062111c7
No related branches found
No related tags found
No related merge requests found
...@@ -832,32 +832,37 @@ IncomingFile::cancel() ...@@ -832,32 +832,37 @@ IncomingFile::cancel()
void void
IncomingFile::process() IncomingFile::process()
{ {
channel_->setOnRecv([this](const uint8_t* buf, size_t len) { channel_->setOnRecv([w=weak()](const uint8_t* buf, size_t len) {
if (stream_.is_open()) if (auto shared = w.lock()) {
stream_ << std::string_view((const char*) buf, len); if (shared->stream_.is_open())
info_.bytesProgress = stream_.tellp(); shared->stream_ << std::string_view((const char*) buf, len);
shared->info_.bytesProgress = shared->stream_.tellp();
}
return len; return len;
}); });
channel_->onShutdown([this] { channel_->onShutdown([w=weak()] {
auto correct = sha3Sum_.empty(); auto shared = w.lock();
if (!shared)
return;
auto correct = shared->sha3Sum_.empty();
if (!correct) { if (!correct) {
if (stream_ && stream_.is_open()) if (shared->stream_ && shared->stream_.is_open())
stream_.close(); shared->stream_.close();
// Verify shaSum // Verify shaSum
auto sha3Sum = fileutils::sha3File(info_.path); auto sha3Sum = fileutils::sha3File(shared->info_.path);
if (sha3Sum_ == sha3Sum) { if (shared->sha3Sum_ == sha3Sum) {
JAMI_INFO() << "New file received: " << info_.path; JAMI_INFO() << "New file received: " << shared->info_.path;
correct = true; correct = true;
} else { } else {
JAMI_WARN() << "Remove file, invalid sha3sum detected for " << info_.path; JAMI_WARN() << "Remove file, invalid sha3sum detected for " << shared->info_.path;
fileutils::remove(info_.path, true); fileutils::remove(shared->info_.path, true);
} }
} }
if (isUserCancelled_) if (shared->isUserCancelled_)
return; return;
auto code = correct ? DRing::DataTransferEventCode::finished auto code = correct ? DRing::DataTransferEventCode::finished
: DRing::DataTransferEventCode::closed_by_host; : DRing::DataTransferEventCode::closed_by_host;
emit(code); shared->emit(code);
}); });
} }
......
...@@ -79,7 +79,7 @@ protected: ...@@ -79,7 +79,7 @@ protected:
std::function<void(uint32_t)> finishedCb_ {}; std::function<void(uint32_t)> finishedCb_ {};
}; };
class IncomingFile : public FileInfo class IncomingFile : public FileInfo, public std::enable_shared_from_this<IncomingFile>
{ {
public: public:
IncomingFile(const std::shared_ptr<ChannelSocket>& channel, IncomingFile(const std::shared_ptr<ChannelSocket>& channel,
...@@ -92,6 +92,10 @@ public: ...@@ -92,6 +92,10 @@ public:
void cancel() override; void cancel() override;
private: private:
std::weak_ptr<IncomingFile> weak()
{
return std::static_pointer_cast<IncomingFile>(shared_from_this());
}
std::ofstream stream_; std::ofstream stream_;
std::string sha3Sum_ {}; std::string sha3Sum_ {};
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment