diff --git a/YACReaderLibrary/db_helper.cpp b/YACReaderLibrary/db_helper.cpp index c442af91..44819fb2 100644 --- a/YACReaderLibrary/db_helper.cpp +++ b/YACReaderLibrary/db_helper.cpp @@ -348,7 +348,7 @@ void DBHelper::update(ComicInfo * comicInfo, QSqlDatabase & db) updateComicInfo.bindValue(":comicVineID", comicInfo->comicVineID); - updateComicInfo.exec(); + updateComicInfo.exec(); } void DBHelper::updateRead(ComicInfo * comicInfo, QSqlDatabase & db) @@ -391,6 +391,44 @@ void DBHelper::updateProgress(qulonglong libraryId, const ComicInfo &comicInfo) QSqlDatabase::removeDatabase(libraryPath); } +void DBHelper::updateReadingRemoteProgress(const ComicInfo &comicInfo, QSqlDatabase &db) +{ + QSqlQuery updateComicInfo(db); + updateComicInfo.prepare("UPDATE comic_info SET " + "read = :read, " + "currentPage = :currentPage, " + "hasBeenOpened = :hasBeenOpened" + " WHERE id = :id "); + + updateComicInfo.bindValue(":read", comicInfo.read?1:0); + updateComicInfo.bindValue(":currentPage", comicInfo.currentPage); + updateComicInfo.bindValue(":hasBeenOpened", comicInfo.hasBeenOpened?1:0); + updateComicInfo.bindValue(":id", comicInfo.id); + updateComicInfo.exec(); +} + + +void DBHelper::updateFromRemoteClient(qulonglong libraryId,const ComicInfo & comicInfo) +{ + QString libraryPath = DBHelper::getLibraries().getPath(libraryId); + QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath+"/.yacreaderlibrary"); + + ComicDB comic = DBHelper::loadComic(comicInfo.id,db); + + if(comic.info.hash == comicInfo.hash) + { + if(comic.info.currentPage == comic.info.numPages) + comic.info.read = true; + comic.info.currentPage = comicInfo.currentPage; + comic.info.hasBeenOpened = true; + + DBHelper::updateReadingRemoteProgress(comic.info,db); + } + + db.close(); + QSqlDatabase::removeDatabase(libraryPath); +} + void DBHelper::renameLabel(qulonglong id, const QString &name, QSqlDatabase &db) { QSqlQuery renameLabelQuery(db); diff --git a/YACReaderLibrary/db_helper.h b/YACReaderLibrary/db_helper.h index b104b48f..5eb2eae3 100644 --- a/YACReaderLibrary/db_helper.h +++ b/YACReaderLibrary/db_helper.h @@ -57,6 +57,8 @@ public: static void updateRead(ComicInfo * comicInfo, QSqlDatabase & db); static void update(const Folder & folder, QSqlDatabase & db); static void updateProgress(qulonglong libraryId,const ComicInfo & comicInfo); + static void updateReadingRemoteProgress(const ComicInfo & comicInfo, QSqlDatabase & db); + static void updateFromRemoteClient(qulonglong libraryId,const ComicInfo & comicInfo); static void renameLabel(qulonglong id, const QString & name, QSqlDatabase & db); static void renameList(qulonglong id, const QString & name, QSqlDatabase & db); static void reasignOrderToSublists(QList ids, QSqlDatabase & db); diff --git a/YACReaderLibrary/server/controllers/synccontroller.cpp b/YACReaderLibrary/server/controllers/synccontroller.cpp index 14a2ed2a..7c4c1859 100644 --- a/YACReaderLibrary/server/controllers/synccontroller.cpp +++ b/YACReaderLibrary/server/controllers/synccontroller.cpp @@ -3,6 +3,9 @@ #include "QsLog.h" #include +#include "comic_db.h" +#include "db_helper.h" + SyncController::SyncController() { @@ -10,15 +13,42 @@ SyncController::SyncController() void SyncController::service(HttpRequest &request, HttpResponse &response) { - QString path = QUrl::fromPercentEncoding(request.getPath()).toUtf8(); - QStringList pathElements = path.split('/'); - qulonglong libraryId = pathElements.at(2).toULongLong(); - QString postData = QString::fromUtf8(request.getBody()); QLOG_INFO() << "POST DATA: " << postData; - //TODO Process postData and update the comics + if(postData.length()>0) { + QList data = postData.split("\n"); + + qulonglong libraryId; + qulonglong comicId; + int currentPage; + QString hash; + foreach(QString comicInfo, data) + { + QList comicInfoProgress = comicInfo.split("\t"); + + if(comicInfoProgress.length() == 4) + { + libraryId = comicInfoProgress.at(0).toULongLong(); + comicId = comicInfoProgress.at(1).toULongLong(); + hash = comicInfoProgress.at(2); + currentPage = comicInfoProgress.at(3).toInt(); + + ComicInfo info; + info.currentPage = currentPage; + info.hash = hash; //TODO remove the hash check and add UUIDs for libraries + info.id = comicId; + DBHelper::updateFromRemoteClient(libraryId,info); + } + } + } + else + { + response.setStatus(412,"No comic info received"); + response.writeText("",true); + return; + } response.write("OK",true); } diff --git a/YACReaderLibrary/server/requestmapper.cpp b/YACReaderLibrary/server/requestmapper.cpp index 3da463f4..66b03413 100644 --- a/YACReaderLibrary/server/requestmapper.cpp +++ b/YACReaderLibrary/server/requestmapper.cpp @@ -106,13 +106,14 @@ void RequestMapper::service(HttpRequest& request, HttpResponse& response) { QRegExp comicPage("/library/.+/comic/[0-9]+/page/[0-9]+/?"); //get comic page QRegExp comicPageRemote("/library/.+/comic/[0-9]+/page/[0-9]+/remote?"); //get comic page (remote reading) - QRegExp sync("/library/.+/sync"); + QRegExp sync("/sync"); QRegExp library("/library/([0-9]+)/.+"); //permite verificar que la biblioteca solicitada existe path = QUrl::fromPercentEncoding(path).toUtf8(); - loadSession(request, response); + if(!sync.exactMatch(path)) //no session is needed for syncback info, until security will be added + loadSession(request, response); //primera petición, se ha hecho un post, se sirven las bibliotecas si la seguridad mediante login no está habilitada if(path == "/") //Don't send data to the server using '/' !!!! @@ -123,6 +124,7 @@ void RequestMapper::service(HttpRequest& request, HttpResponse& response) { { if(sync.exactMatch(path)) SyncController().service(request, response); + else { //se comprueba que la sesión sea la correcta con el fin de evitar accesos no autorizados HttpSession session=Static::sessionStore->getSession(request,response,false);