From 0333c9f050596cfbc94a9ce2a506d3b85284d9b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Tue, 25 May 2021 21:45:07 +0200 Subject: [PATCH] Add a new server controller for opening comics from reading lists --- .../v2/comiccontrollerinreadinglist_v2.cpp | 101 ++++++++++++++++++ .../v2/comiccontrollerinreadinglist_v2.h | 18 ++++ YACReaderLibrary/server/server.pri | 6 +- 3 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 YACReaderLibrary/server/controllers/v2/comiccontrollerinreadinglist_v2.cpp create mode 100644 YACReaderLibrary/server/controllers/v2/comiccontrollerinreadinglist_v2.h diff --git a/YACReaderLibrary/server/controllers/v2/comiccontrollerinreadinglist_v2.cpp b/YACReaderLibrary/server/controllers/v2/comiccontrollerinreadinglist_v2.cpp new file mode 100644 index 00000000..bbadf8e0 --- /dev/null +++ b/YACReaderLibrary/server/controllers/v2/comiccontrollerinreadinglist_v2.cpp @@ -0,0 +1,101 @@ +#include "comiccontrollerinreadinglist_v2.h" + +#include "db_helper.h" +#include "yacreader_libraries.h" +#include "yacreader_http_session.h" + +#include "template.h" +#include "../static.h" + +#include "comic_db.h" +#include "comic.h" + +#include "qnaturalsorting.h" + +#include "QsLog.h" + +#include + +using stefanfrings::HttpRequest; +using stefanfrings::HttpResponse; + +ComicControllerInReadingListV2::ComicControllerInReadingListV2() { } + +void ComicControllerInReadingListV2::service(HttpRequest &request, HttpResponse &response) +{ + + QByteArray token = request.getHeader("x-request-id"); + YACReaderHttpSession *ySession = Static::yacreaderSessionStore->getYACReaderSessionHttpSession(token); + + if (ySession == nullptr) { + response.setStatus(404, "not found"); + response.write("404 not found", true); + return; + } + + QString path = QUrl::fromPercentEncoding(request.getPath()).toUtf8(); + QStringList pathElements = path.split('/'); + qulonglong libraryId = pathElements.at(3).toLongLong(); + QString libraryName = DBHelper::getLibraryName(libraryId); + qulonglong readingListId = pathElements.at(5).toULongLong(); + qulonglong comicId = pathElements.at(7).toULongLong(); + + YACReaderLibraries libraries = DBHelper::getLibraries(); + + ComicDB comic = DBHelper::getComicInfo(libraryId, comicId); + + Comic *comicFile = FactoryComic::newComic(libraries.getPath(libraryId) + comic.path); + + if (comicFile != nullptr) { + QThread *thread = nullptr; + + thread = new QThread(); + + comicFile->moveToThread(thread); + + connect(comicFile, SIGNAL(errorOpening()), thread, SLOT(quit())); + connect(comicFile, SIGNAL(errorOpening(QString)), thread, SLOT(quit())); + connect(comicFile, SIGNAL(imagesLoaded()), thread, SLOT(quit())); + connect(thread, SIGNAL(started()), comicFile, SLOT(process())); + connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); + + comicFile->load(libraries.getPath(libraryId) + comic.path); + + if (thread != nullptr) + thread->start(); + + QLOG_TRACE() << "remote comic requested"; + ySession->setCurrentRemoteComic(comic.id, comicFile); + + response.setHeader("Content-Type", "text/plain; charset=utf-8"); + //TODO this field is not used by the client! + response.write(QString("library:%1\r\n").arg(libraryName).toUtf8()); + response.write(QString("libraryId:%1\r\n").arg(libraryId).toUtf8()); + + QList siblings = DBHelper::getReadingListFullContent(libraryId, readingListId); + + bool found = false; + int i; + for (i = 0; i < siblings.length(); i++) { + if (siblings.at(i).id == comic.id) { + found = true; + break; + } + } + if (found) { + if (i > 0) + response.write(QString("previousComic:%1\r\n").arg(siblings.at(i - 1).id).toUtf8()); + if (i < siblings.length() - 1) + response.write(QString("nextComic:%1\r\n").arg(siblings.at(i + 1).id).toUtf8()); + } else { + //ERROR + } + + response.write(comic.toTXT().toUtf8(), true); + } else { + //delete comicFile; + response.setStatus(404, "not found"); + response.write("404 not found", true); + } + //response.write(t.toLatin1(),true); +} diff --git a/YACReaderLibrary/server/controllers/v2/comiccontrollerinreadinglist_v2.h b/YACReaderLibrary/server/controllers/v2/comiccontrollerinreadinglist_v2.h new file mode 100644 index 00000000..424fcbf7 --- /dev/null +++ b/YACReaderLibrary/server/controllers/v2/comiccontrollerinreadinglist_v2.h @@ -0,0 +1,18 @@ +#ifndef COMICCONTROLLERINREADINGLISTV2_H +#define COMICCONTROLLERINREADINGLISTV2_H + +#include "httprequest.h" +#include "httpresponse.h" +#include "httprequesthandler.h" + +class ComicControllerInReadingListV2 : public stefanfrings::HttpRequestHandler +{ + Q_OBJECT + Q_DISABLE_COPY(ComicControllerInReadingListV2) +public: + ComicControllerInReadingListV2(); + + void service(stefanfrings::HttpRequest &request, stefanfrings::HttpResponse &response) override; +}; + +#endif // COMICCONTROLLERINREADINGLISTV2_H diff --git a/YACReaderLibrary/server/server.pri b/YACReaderLibrary/server/server.pri index e0f715b5..9c17a2b3 100644 --- a/YACReaderLibrary/server/server.pri +++ b/YACReaderLibrary/server/server.pri @@ -46,7 +46,8 @@ HEADERS += \ $$PWD/controllers/v2/readinglistcontentcontroller_v2.h \ $$PWD/controllers/v2/comicfullinfocontroller_v2.h \ $$PWD/controllers/v2/readinglistinfocontroller_v2.h \ - $$PWD/controllers/v2/taginfocontroller_v2.h + $$PWD/controllers/v2/taginfocontroller_v2.h \ + $$PWD/controllers/v2/comiccontrollerinreadinglist_v2.h SOURCES += \ @@ -87,7 +88,8 @@ SOURCES += \ $$PWD/controllers/v2/readinglistcontentcontroller_v2.cpp \ $$PWD/controllers/v2/comicfullinfocontroller_v2.cpp \ $$PWD/controllers/v2/readinglistinfocontroller_v2.cpp \ - $$PWD/controllers/v2/taginfocontroller_v2.cpp + $$PWD/controllers/v2/taginfocontroller_v2.cpp \ + $$PWD/controllers/v2/comiccontrollerinreadinglist_v2.cpp include(../../third_party/QtWebApp/httpserver/httpserver.pri) include(../../third_party/QtWebApp/templateengine/templateengine.pri)