Add some error checks to Image::loadImage

Add default value to loadedPixmap
This commit is contained in:
Rasmus Karlsson 2018-04-16 23:48:30 +02:00
parent ea691635a1
commit 5f142e8d52
3 changed files with 45 additions and 10 deletions

View file

@ -21,14 +21,12 @@ namespace messages {
Image::Image(const QString &url, qreal scale, const QString &name, const QString &tooltip, Image::Image(const QString &url, qreal scale, const QString &name, const QString &tooltip,
const QMargins &margin, bool isHat) const QMargins &margin, bool isHat)
: currentPixmap(nullptr) : url(url)
, url(url)
, name(name) , name(name)
, tooltip(tooltip) , tooltip(tooltip)
, margin(margin) , margin(margin)
, ishat(isHat) , ishat(isHat)
, scale(scale) , scale(scale)
, isLoading(false)
{ {
util::DebugCount::increase("images"); util::DebugCount::increase("images");
} }
@ -65,7 +63,7 @@ void Image::loadImage()
util::NetworkRequest req(this->getUrl()); util::NetworkRequest req(this->getUrl());
req.setCaller(this); req.setCaller(this);
req.setUseQuickLoadCache(true); req.setUseQuickLoadCache(true);
req.get([lli = this](QByteArray bytes) { req.get([lli = this](QByteArray bytes) -> bool {
QByteArray copy = QByteArray::fromRawData(bytes.constData(), bytes.length()); QByteArray copy = QByteArray::fromRawData(bytes.constData(), bytes.length());
QBuffer buffer(&copy); QBuffer buffer(&copy);
buffer.open(QIODevice::ReadOnly); buffer.open(QIODevice::ReadOnly);
@ -84,6 +82,19 @@ void Image::loadImage()
util::DebugCount::decrease("loaded images"); util::DebugCount::decrease("loaded images");
} }
if (reader.imageCount() == -1) {
// An error occured in the reader
debug::Log("An error occured reading the image: '{}'", reader.errorString());
debug::Log("Image url: {}", lli->url);
return false;
}
if (reader.imageCount() == 0) {
debug::Log("Error: No images read in the buffer");
// No images read in the buffer. maybe a cache error?
return false;
}
for (int index = 0; index < reader.imageCount(); ++index) { for (int index = 0; index < reader.imageCount(); ++index) {
if (reader.read(&image)) { if (reader.read(&image)) {
auto pixmap = new QPixmap(QPixmap::fromImage(image)); auto pixmap = new QPixmap(QPixmap::fromImage(image));
@ -101,6 +112,12 @@ void Image::loadImage()
} }
} }
if (lli->allFrames.size() != reader.imageCount()) {
// debug::Log("Error: Wrong amount of images read");
// One or more images failed to load from the buffer
// return false;
}
if (lli->allFrames.size() > 1) { if (lli->allFrames.size() > 1) {
lli->animated = true; lli->animated = true;
@ -116,6 +133,8 @@ void Image::loadImage()
util::postToThread( util::postToThread(
[] { singletons::WindowManager::getInstance().layoutVisibleChatWidgets(); }); [] { singletons::WindowManager::getInstance().layoutVisibleChatWidgets(); });
return true;
}); });
singletons::EmoteManager::getInstance().getGifUpdateSignal().connect([=]() { singletons::EmoteManager::getInstance().getGifUpdateSignal().connect([=]() {

View file

@ -40,8 +40,8 @@ private:
int duration; int duration;
}; };
QPixmap *currentPixmap; QPixmap *currentPixmap = nullptr;
QPixmap *loadedPixmap; QPixmap *loadedPixmap = nullptr;
std::vector<FrameData> allFrames; std::vector<FrameData> allFrames;
int currentFrame = 0; int currentFrame = 0;
int currentFrameOffset = 0; int currentFrameOffset = 0;

View file

@ -135,9 +135,15 @@ public:
// qDebug() << "Loaded cached resource" << this->data.request.url(); // qDebug() << "Loaded cached resource" << this->data.request.url();
onFinished(bytes); bool success = onFinished(bytes);
cachedFile.close(); cachedFile.close();
if (!success) {
// The images were not successfully loaded from the file
// XXX: Invalidate the cache file so we don't attempt to load it again next
// time
}
} }
} }
} }
@ -160,7 +166,9 @@ public:
return; return;
} }
QByteArray bytes = reply->readAll(); QByteArray readBytes = reply->readAll();
QByteArray bytes;
bytes.setRawData(readBytes.data(), readBytes.size());
data.writeToCache(bytes); data.writeToCache(bytes);
onFinished(bytes); onFinished(bytes);
@ -212,18 +220,26 @@ public:
template <typename FinishedCallback> template <typename FinishedCallback>
void getJSON(FinishedCallback onFinished) void getJSON(FinishedCallback onFinished)
{ {
this->get([onFinished{std::move(onFinished)}](const QByteArray &bytes) { this->get([onFinished{std::move(onFinished)}](const QByteArray &bytes) -> bool {
auto object = parseJSONFromData(bytes); auto object = parseJSONFromData(bytes);
onFinished(object); onFinished(object);
// XXX: Maybe return onFinished? For now I don't want to force onFinished to have a
// return value
return true;
}); });
} }
template <typename FinishedCallback> template <typename FinishedCallback>
void getJSON2(FinishedCallback onFinished) void getJSON2(FinishedCallback onFinished)
{ {
this->get([onFinished{std::move(onFinished)}](const QByteArray &bytes) { this->get([onFinished{std::move(onFinished)}](const QByteArray &bytes) -> bool {
auto object = parseJSONFromData2(bytes); auto object = parseJSONFromData2(bytes);
onFinished(object); onFinished(object);
// XXX: Maybe return onFinished? For now I don't want to force onFinished to have a
// return value
return true;
}); });
} }
}; };