Mercurial > minori
comparison src/core/anime_db.cc @ 378:5912dafc6e28
anime: add poster cache
:)
| author | Paper <paper@tflc.us> |
|---|---|
| date | Wed, 05 Nov 2025 12:50:35 -0500 |
| parents | 47c9f8502269 |
| children | 5eaafed6c10b |
comparison
equal
deleted
inserted
replaced
| 377:1b0b8e746d83 | 378:5912dafc6e28 |
|---|---|
| 2 #include "core/anime.h" | 2 #include "core/anime.h" |
| 3 #include "core/filesystem.h" | 3 #include "core/filesystem.h" |
| 4 #include "core/json.h" | 4 #include "core/json.h" |
| 5 #include "core/session.h" | 5 #include "core/session.h" |
| 6 #include "core/strings.h" | 6 #include "core/strings.h" |
| 7 #include "core/http.h" | |
| 7 | 8 |
| 8 #include "gui/translate/anilist.h" | 9 #include "gui/translate/anilist.h" |
| 9 #include "gui/translate/anime.h" | 10 #include "gui/translate/anime.h" |
| 10 | 11 |
| 11 #include <QDate> | 12 #include <QDate> |
| 377 } | 378 } |
| 378 | 379 |
| 379 return res; | 380 return res; |
| 380 } | 381 } |
| 381 | 382 |
| 383 QImage Database::GetAnimePoster(int id) | |
| 384 { | |
| 385 /* Creates an anime poster and then saves it to disk */ | |
| 386 QImage res; | |
| 387 QString posterfile; | |
| 388 | |
| 389 std::filesystem::path posterpath = Filesystem::GetAnimePostersPath(); | |
| 390 | |
| 391 posterpath = posterpath / ((Strings::ToUtf8String(id) + ".png")); | |
| 392 | |
| 393 Filesystem::CreateDirectories(posterpath); | |
| 394 | |
| 395 /* Load from disk if available */ | |
| 396 posterfile = Strings::ToQString(posterpath.string()); | |
| 397 | |
| 398 if (res.load(posterfile)) | |
| 399 return res; | |
| 400 | |
| 401 /* TODO check if the anime even exists.. */ | |
| 402 | |
| 403 Anime &anime = db.items[id]; | |
| 404 | |
| 405 QByteArray arr = HTTP::Request(anime.GetPosterUrl()); | |
| 406 | |
| 407 /* If it's not valid, return dummy QImage */ | |
| 408 if (arr.isNull() || !res.loadFromData(arr)) | |
| 409 return QImage(); | |
| 410 | |
| 411 qDebug() << posterfile; | |
| 412 | |
| 413 /* Save the result to disk | |
| 414 * TODO might be more efficient to save with original format, | |
| 415 * and save filename as well */ | |
| 416 if (!res.save(posterfile, "png")) | |
| 417 qDebug() << "oops\n"; | |
| 418 | |
| 419 return res; | |
| 420 } | |
| 421 | |
| 382 Database db; | 422 Database db; |
| 383 | 423 |
| 384 } // namespace Anime | 424 } // namespace Anime |
