diff 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
line wrap: on
line diff
--- a/src/core/anime_db.cc	Fri Jul 25 12:40:25 2025 -0400
+++ b/src/core/anime_db.cc	Wed Nov 05 12:50:35 2025 -0500
@@ -4,6 +4,7 @@
 #include "core/json.h"
 #include "core/session.h"
 #include "core/strings.h"
+#include "core/http.h"
 
 #include "gui/translate/anilist.h"
 #include "gui/translate/anime.h"
@@ -379,6 +380,45 @@
 	return res;
 }
 
+QImage Database::GetAnimePoster(int id)
+{
+	/* Creates an anime poster and then saves it to disk */
+	QImage res;
+	QString posterfile;
+
+	std::filesystem::path posterpath = Filesystem::GetAnimePostersPath();
+
+	posterpath = posterpath / ((Strings::ToUtf8String(id) + ".png"));
+
+	Filesystem::CreateDirectories(posterpath);
+
+	/* Load from disk if available */
+	posterfile = Strings::ToQString(posterpath.string());
+
+	if (res.load(posterfile))
+		return res;
+
+	/* TODO check if the anime even exists.. */
+
+	Anime &anime = db.items[id];
+
+	QByteArray arr = HTTP::Request(anime.GetPosterUrl());
+
+	/* If it's not valid, return dummy QImage */
+	if (arr.isNull() || !res.loadFromData(arr))
+		return QImage();
+
+	qDebug() << posterfile;
+
+	/* Save the result to disk
+	 * TODO might be more efficient to save with original format,
+	 * and save filename as well */
+	if (!res.save(posterfile, "png"))
+		qDebug() << "oops\n";
+
+	return res;
+}
+
 Database db;
 
 } // namespace Anime