From acdfb117a082d22e473e590887aabeadfb29008a Mon Sep 17 00:00:00 2001 From: ArsenArsen Date: Sat, 1 Jul 2017 22:34:11 +0200 Subject: [PATCH] Fix a recording dequeue bug I was going so insane over it I made the system more efficient in the process of figuring out the bug --- recording/recordingcontroller.cpp | 8 +++++--- recording/recordingcontroller.hpp | 4 ++-- recording/recordingformats.cpp | 12 +++--------- recording/recordingformats.hpp | 4 ++-- uploaders/uploadersingleton.cpp | 15 ++++++++++----- uploaders/uploadersingleton.hpp | 2 +- 6 files changed, 23 insertions(+), 22 deletions(-) diff --git a/recording/recordingcontroller.cpp b/recording/recordingcontroller.cpp index cea7dda..19d49b1 100644 --- a/recording/recordingcontroller.cpp +++ b/recording/recordingcontroller.cpp @@ -42,7 +42,7 @@ bool RecordingController::end() { WorkerContext *c = new WorkerContext; c->consumer = [&](QImage) { _QueueContext contx; - contx.arr = _context->finalizer(); + contx.file = _context->finalizer(); contx.format = _context->anotherFormat; contx.postUploadTask = _context->postUploadTask; queue(contx); @@ -94,11 +94,13 @@ void RecordingController::timeout() { if (isRunning()) preview->setTime(QString("%1:%2").arg(QString::number(minute)).arg(QString::number(second)), frame); } else { - timer.stop(); QMutexLocker l(&lock); if (!uploadQueue.isEmpty()) { auto a = uploadQueue.dequeue(); - UploaderSingleton::inst().upload(a.arr, a.format); + if (!a.file.isEmpty()) { + QFile f(a.file); + UploaderSingleton::inst().upload(f, a.format); + } if (a.postUploadTask) a.postUploadTask(); } } diff --git a/recording/recordingcontroller.hpp b/recording/recordingcontroller.hpp index d1dce00..96044d5 100644 --- a/recording/recordingcontroller.hpp +++ b/recording/recordingcontroller.hpp @@ -16,13 +16,13 @@ struct RecordingContext { QImage::Format format; std::function consumer; std::function validator; - std::function finalizer; + std::function finalizer; std::function postUploadTask; QString anotherFormat; }; struct _QueueContext { - QByteArray arr; + QString file; QString format; std::function postUploadTask; }; diff --git a/recording/recordingformats.cpp b/recording/recordingformats.cpp index 20f333d..dfb5d37 100644 --- a/recording/recordingformats.cpp +++ b/recording/recordingformats.cpp @@ -33,15 +33,9 @@ RecordingFormats::RecordingFormats(formats::Recording f) { delete enc; if (interrupt || !frameAdded) { tmpDir.removeRecursively(); - return QByteArray(); + return QString(); } - QFile res(path); - if (!res.open(QFile::ReadOnly)) { - qCritical().noquote() << "Could not open resulting file: " << res.errorString(); - return QByteArray(); - } - QByteArray data = res.readAll(); - return data; + return QFile(path).size() > 0 ? path : QString(); }; validator = [&](QSize s) { if (!enc) { @@ -86,7 +80,7 @@ std::function RecordingFormats::getConsumer() { return consumer; } -std::function RecordingFormats::getFinalizer() { +std::function RecordingFormats::getFinalizer() { return finalizer; } diff --git a/recording/recordingformats.hpp b/recording/recordingformats.hpp index 2c9e50d..ad5825d 100644 --- a/recording/recordingformats.hpp +++ b/recording/recordingformats.hpp @@ -15,7 +15,7 @@ public: RecordingFormats(formats::Recording f); std::function getConsumer(); std::function getValidator(); - std::function getFinalizer(); + std::function getFinalizer(); std::function getPostUploadTask(); QImage::Format getFormat(); QString getAnotherFormat(); @@ -23,7 +23,7 @@ public: private: std::function consumer; std::function validator; - std::function finalizer; + std::function finalizer; std::function postUploadTask; std::vector frames; QImage::Format iFormat; diff --git a/uploaders/uploadersingleton.cpp b/uploaders/uploadersingleton.cpp index 816ed78..e0e7b1f 100644 --- a/uploaders/uploadersingleton.cpp +++ b/uploaders/uploadersingleton.cpp @@ -90,11 +90,16 @@ void UploaderSingleton::upload(QByteArray img, QString format) { uploaders.value(uploader)->doUpload(img, format); } -void UploaderSingleton::upload(QFile img, QString format) { - if (img.open(QIODevice::ReadOnly)) { - uploaders.value(uploader)->doUpload(img.readAll(), format); - img.close(); - } +void UploaderSingleton::upload(QFile &img, QString format) { + if (img.size() <= 0) return; + if (img.rename( + saveDir.absoluteFilePath(formatter::format(settings::settings().value("fileFormat").toString(), format.toLower())))) { + if (img.open(QFile::ReadWrite)) + uploaders.value(uploader)->doUpload(img.readAll(), format); + else + notifications::notify("KShare - Failed to save picture", img.errorString(), QSystemTrayIcon::Warning); + } else + notifications::notify("KShare - Failed to save picture", img.errorString(), QSystemTrayIcon::Warning); } void UploaderSingleton::showSettings() { diff --git a/uploaders/uploadersingleton.hpp b/uploaders/uploadersingleton.hpp index 73a4d76..92862f1 100644 --- a/uploaders/uploadersingleton.hpp +++ b/uploaders/uploadersingleton.hpp @@ -15,7 +15,7 @@ public: void registerUploader(Uploader *uploader); void upload(QPixmap *pixmap); void upload(QByteArray img, QString format); - void upload(QFile img, QString format); + void upload(QFile &img, QString format); void showSettings(); QList uploaderList(); void set(QString uploader);