Skip to content

Commit 8a86e52

Browse files
committed
cleanup clipboard files when no longer wanted
Signed-off-by: Caolán McNamara <caolan.mcnamara@collabora.com> Change-Id: I01164e5139e3c6d67ae5f7a53caf6caee937d94a
1 parent 40b2d76 commit 8a86e52

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

wsd/ClientSession.cpp

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,6 +2097,25 @@ bool ClientSession::handlePresentationInfo(const std::shared_ptr<Message>& paylo
20972097
return forwardToClient(payload);
20982098
}
20992099

2100+
namespace
2101+
{
2102+
class RemoveClipFile
2103+
{
2104+
private:
2105+
std::string _clipFile;
2106+
public:
2107+
RemoveClipFile(const std::string& clipFile)
2108+
: _clipFile(clipFile)
2109+
{
2110+
}
2111+
~RemoveClipFile()
2112+
{
2113+
fprintf(stderr, "remove %s\n", _clipFile.c_str());
2114+
FileUtil::removeFile(_clipFile);
2115+
}
2116+
};
2117+
}
2118+
21002119
bool ClientSession::handleKitToClientMessage(const std::shared_ptr<Message>& payload)
21012120
{
21022121
LOG_TRC("handling kit-to-client [" << payload->abbr() << ']');
@@ -2453,13 +2472,9 @@ bool ClientSession::handleKitToClientMessage(const std::shared_ptr<Message>& pay
24532472

24542473
std::cerr << "FOO len: " << res.size() << "\n";
24552474

2456-
// FileUtil::removeFile(std::string("/tmp/cliptest"));
2457-
// std::ofstream fileStream;
2458-
// fileStream.open("/tmp/cliptest");
2459-
// fileStream.write(res.data(), res.size());
2460-
// fileStream.close();
2475+
// final cleanup via clipFileRemove dtor
2476+
std::shared_ptr<RemoveClipFile> clipFileRemove;
24612477

2462-
// final cleanup ...
24632478
bool removeClipFile = true;
24642479
if (!empty && (!_wopiFileInfo || !_wopiFileInfo->getDisableCopy()))
24652480
{
@@ -2475,6 +2490,9 @@ bool ClientSession::handleKitToClientMessage(const std::shared_ptr<Message>& pay
24752490
else
24762491
fprintf(stderr, "don't save clipboard\n");
24772492

2493+
if (removeClipFile)
2494+
clipFileRemove = std::make_shared<RemoveClipFile>(clipFile);
2495+
24782496
for (const auto& it : _clipSockets)
24792497
{
24802498
auto socket = it.lock();
@@ -2494,25 +2512,18 @@ bool ClientSession::handleKitToClientMessage(const std::shared_ptr<Message>& pay
24942512
headers.emplace_back("Cache-Control", "no-cache");
24952513
headers.emplace_back("Connection", "close");
24962514

2497-
if (removeClipFile)
2498-
{
2499-
fprintf(stderr, "we will want to remove %s\n", clipFile.c_str());
2500-
session->setFinishedHandler([clipFile](const std::shared_ptr<http::ServerSession>&) {
2501-
fprintf(stderr, "FINISHED WITH %s\n", clipFile.c_str());
2502-
FileUtil::removeFile(clipFile);
2503-
});
2504-
}
2515+
// on final session dtor clipFileRemove cleanup removes clipboard file
2516+
session->setFinishedHandler([clipFileRemove](const std::shared_ptr<http::ServerSession>&) {});
25052517

2518+
// Hand over socket to ServerSession which async provides clipboard
2519+
// content backed by clipFile
25062520
session->asyncUpload(clipFile, std::move(headers));
2507-
// session->asyncUpload("/tmp/cliptest", std::move(headers));
25082521
socket->setHandler(std::static_pointer_cast<ProtocolHandlerInterface>(session));
25092522

25102523
LOG_INF("Queued " << (empty?"empty":"clipboard") << " response for send.");
25112524
}
25122525

25132526
fprintf(stderr, "removeClipFile is %d\n", removeClipFile);
2514-
//if (removeClipFile)
2515-
// FileUtil::removeFile(clipFile);
25162527
#endif
25172528
_clipSockets.clear();
25182529
return true;

0 commit comments

Comments
 (0)