added a new web controller for providing the content information of a folder

This commit is contained in:
Luis Ángel San Martín
2016-01-24 14:15:18 +01:00
parent 4b3ffdc665
commit 37328edc64
4 changed files with 102 additions and 2 deletions

View File

@ -0,0 +1,70 @@
#include "foldercontentcontroller.h"
#include <QUrl>
#include "db_helper.h"
#include "comic_db.h"
#include "folder.h"
#include "qnaturalsorting.h"
#include <ctime>
using namespace std;
struct LibraryItemSorter
{
bool operator()(const LibraryItem * a,const LibraryItem * b) const
{
return naturalSortLessThanCI(a->name,b->name);
}
};
FolderContentController::FolderContentController() {}
void FolderContentController::service(HttpRequest& request, HttpResponse& response)
{
response.setHeader("Content-Type", "plain/text; charset=utf-8");
QString path = QUrl::fromPercentEncoding(request.getPath()).toUtf8();
QStringList pathElements = path.split('/');
int libraryId = pathElements.at(2).toInt();
qulonglong parentId = pathElements.at(4).toULongLong();
serviceContent(libraryId, parentId, response);
response.writeText("",true);
}
void FolderContentController::serviceContent(const int &library, const qulonglong &folderId, HttpResponse &response)
{
clock_t begin = clock();
QList<LibraryItem *> folderContent = DBHelper::getFolderSubfoldersFromLibrary(library,folderId);
QList<LibraryItem *> folderComics = DBHelper::getFolderComicsFromLibrary(library,folderId);
folderContent.append(folderComics);
qSort(folderContent.begin(),folderContent.end(),LibraryItemSorter());
folderComics.clear();
ComicDB * currentComic;
Folder * currentFolder;
for(QList<LibraryItem *>::const_iterator itr = folderContent.constBegin();itr!=folderContent.constEnd();itr++)
{
if((*itr)->isDir())
{
currentFolder = (Folder *)(*itr);
response.writeText(QString("f:%1:%2:%3:%4\r\n").arg(library).arg(currentFolder->id).arg(currentFolder->name).arg(currentFolder->numChildren));
}
else
{
currentComic = (ComicDB *)(*itr);
response.writeText(QString("c:%1:%2:%3:%4:%5\r\n").arg(library).arg(currentComic->id).arg(currentComic->getFileName()).arg(currentComic->getFileSize()).arg(currentComic->info.hash));
}
}
clock_t end = clock();
double msecs = double(end - begin);
response.writeText(QString("%1ms").arg(msecs));
}

View File

@ -0,0 +1,22 @@
#ifndef FOLDERCONTENTCONTROLLER_H
#define FOLDERCONTENTCONTROLLER_H
#include "httprequest.h"
#include "httpresponse.h"
#include "httprequesthandler.h"
class FolderContentController : public HttpRequestHandler {
Q_OBJECT
Q_DISABLE_COPY(FolderContentController);
public:
/** Constructor */
FolderContentController();
/** Generates the response */
void service(HttpRequest& request, HttpResponse& response);
private:
void serviceContent(const int &library, const qulonglong &folderId, HttpResponse &response);
};
#endif // FOLDERCONTENTCONTROLLER_H

View File

@ -23,6 +23,7 @@
#include "controllers/comicdownloadinfocontroller.h"
#include "controllers/synccontroller.h"
#include "controllers/versioncontroller.h"
#include "controllers/foldercontentcontroller.h"
#include "db_helper.h"
#include "yacreader_libraries.h"
@ -107,6 +108,7 @@ void RequestMapper::service(HttpRequest& request, HttpResponse& response) {
static const QRegExp comicPage("/library/.+/comic/[0-9]+/page/[0-9]+/?"); //get comic page
static const QRegExp comicPageRemote("/library/.+/comic/[0-9]+/page/[0-9]+/remote?"); //get comic page (remote reading)
static const QRegExp serverVersion("/version/?");
static const QRegExp folderContent("/library/.+/folder/[0-9]+/content/?");
static const QRegExp sync("/sync");
@ -169,6 +171,10 @@ void RequestMapper::service(HttpRequest& request, HttpResponse& response) {
{
UpdateComicController().service(request, response);
}
else if(folderContent.exactMatch(path))
{
FolderContentController().service(request, response);
}
}
else
{

View File

@ -17,7 +17,8 @@ HEADERS += \
$$PWD/controllers/comicdownloadinfocontroller.h \
$$PWD/controllers/synccontroller.h \
#v2
$$PWD/controllers/versioncontroller.h
$$PWD/controllers/versioncontroller.h \
$$PWD/controllers/foldercontentcontroller.h
SOURCES += \
$$PWD/static.cpp \
@ -35,7 +36,8 @@ SOURCES += \
$$PWD/controllers/comicdownloadinfocontroller.cpp \
$$PWD/controllers/synccontroller.cpp \
#v2
$$PWD/controllers/versioncontroller.cpp
$$PWD/controllers/versioncontroller.cpp \
$$PWD/controllers/foldercontentcontroller.cpp
include(lib/bfLogging/bfLogging.pri)
include(lib/bfHttpServer/bfHttpServer.pri)