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,
const QMargins &margin, bool isHat)
: currentPixmap(nullptr)
, url(url)
: url(url)
, name(name)
, tooltip(tooltip)
, margin(margin)
, ishat(isHat)
, scale(scale)
, isLoading(false)
{
util::DebugCount::increase("images");
}
@ -65,7 +63,7 @@ void Image::loadImage()
util::NetworkRequest req(this->getUrl());
req.setCaller(this);
req.setUseQuickLoadCache(true);
req.get([lli = this](QByteArray bytes) {
req.get([lli = this](QByteArray bytes) -> bool {
QByteArray copy = QByteArray::fromRawData(bytes.constData(), bytes.length());
QBuffer buffer(&copy);
buffer.open(QIODevice::ReadOnly);
@ -84,6 +82,19 @@ void Image::loadImage()
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) {
if (reader.read(&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) {
lli->animated = true;
@ -116,6 +133,8 @@ void Image::loadImage()
util::postToThread(
[] { singletons::WindowManager::getInstance().layoutVisibleChatWidgets(); });
return true;
});
singletons::EmoteManager::getInstance().getGifUpdateSignal().connect([=]() {

View file

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

View file

@ -135,9 +135,15 @@ public:
// qDebug() << "Loaded cached resource" << this->data.request.url();
onFinished(bytes);
bool success = onFinished(bytes);
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;
}
QByteArray bytes = reply->readAll();
QByteArray readBytes = reply->readAll();
QByteArray bytes;
bytes.setRawData(readBytes.data(), readBytes.size());
data.writeToCache(bytes);
onFinished(bytes);
@ -212,18 +220,26 @@ public:
template <typename FinishedCallback>
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);
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>
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);
onFinished(object);
// XXX: Maybe return onFinished? For now I don't want to force onFinished to have a
// return value
return true;
});
}
};