-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There is no metadata provided at the moment, since I haven't found where it's stored (or if it even is). Right now, it is assumed that all the files in the archive are images and the order in the archive is the order in which they are to be presented. Refs #78.
- Loading branch information
Showing
10 changed files
with
291 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* Copyright 2015 Robert Schroll | ||
* | ||
* This file is part of Beru and is distributed under the terms of | ||
* the GPL. See the file COPYING for full details. | ||
*/ | ||
|
||
#include "cbzreader.h" | ||
#include <QJsonObject> | ||
#include <QJsonArray> | ||
#include <QJsonDocument> | ||
#include <QtGui/QImage> | ||
#include <QBuffer> | ||
#include <QDir> | ||
#include <QCryptographicHash> | ||
#include "quazip/quazip.h" | ||
#include "quazip/quazipfile.h" | ||
#include "../qhttpserver/qhttpresponse.h" | ||
|
||
QString guessMimeType(const QString &filename); | ||
|
||
CBZReader::CBZReader(QObject *parent) : | ||
QObject(parent) | ||
{ | ||
this->zip = NULL; | ||
} | ||
|
||
bool CBZReader::load(const QString &filename) | ||
{ | ||
if (this->zip != NULL) { | ||
delete this->zip; | ||
this->zip = NULL; | ||
} | ||
this->_hash = ""; | ||
this->spine.clear(); | ||
|
||
this->zip = new QuaZip(filename); | ||
if (!this->zip->open(QuaZip::mdUnzip)) { | ||
delete this->zip; | ||
this->zip = NULL; | ||
return false; | ||
} | ||
if (!this->parse()) { | ||
delete this->zip; | ||
this->zip = NULL; | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
bool CBZReader::parse() { | ||
QList<QuaZipFileInfo> fileList = this->zip->getFileInfoList(); | ||
foreach (const QuaZipFileInfo info, fileList) { | ||
if (info.uncompressedSize > 0) | ||
this->spine.append(info.name); | ||
} | ||
return true; | ||
} | ||
|
||
QString CBZReader::hash() { | ||
if (this->_hash != "") | ||
return this->_hash; | ||
|
||
if (!this->zip || !this->zip->isOpen()) | ||
return this->_hash; | ||
|
||
QByteArray CRCarray; | ||
QDataStream CRCstream(&CRCarray, QIODevice::WriteOnly); | ||
QList<QuaZipFileInfo> fileList = this->zip->getFileInfoList(); | ||
foreach (const QuaZipFileInfo info, fileList) { | ||
CRCstream << info.crc; | ||
} | ||
this->_hash = QCryptographicHash::hash(CRCarray, QCryptographicHash::Md5).toHex(); | ||
return this->_hash; | ||
} | ||
|
||
QString CBZReader::title() { | ||
return ""; | ||
} | ||
|
||
void CBZReader::serveComponent(const QString &filename, QHttpResponse *response) | ||
{ | ||
if (!this->zip || !this->zip->isOpen()) { | ||
response->writeHead(500); | ||
response->end("Epub file not open for reading"); | ||
return; | ||
} | ||
|
||
this->zip->setCurrentFile(filename); | ||
QuaZipFile zfile(this->zip); | ||
if (!zfile.open(QIODevice::ReadOnly)) { | ||
response->writeHead(404); | ||
response->end("Could not find \"" + filename + "\" in epub file"); | ||
return; | ||
} | ||
|
||
response->setHeader("Content-Type", guessMimeType(filename)); | ||
response->writeHead(200); | ||
// Important -- use write instead of end, so binary data doesn't get messed up! | ||
response->write(zfile.readAll()); | ||
response->end(); | ||
zfile.close(); | ||
} | ||
|
||
QVariantList CBZReader::getContents() | ||
{ | ||
QVariantList res; | ||
for (int i=0; i<this->spine.length(); i++) { | ||
QVariantMap entry; | ||
entry["title"] = "%PAGE% " + QString::number(i + 1); | ||
entry["src"] = this->spine[i]; | ||
res.append(entry); | ||
} | ||
emit contentsReady(res); | ||
return res; | ||
} | ||
|
||
void CBZReader::serveBookData(QHttpResponse *response) | ||
{ | ||
if (!this->zip || !this->zip->isOpen()) { | ||
response->writeHead(500); | ||
response->end("Epub file not open for reading"); | ||
return; | ||
} | ||
|
||
response->setHeader("Content-Type", guessMimeType("js")); | ||
response->writeHead(200); | ||
QJsonDocument spine(QJsonArray::fromStringList(this->spine)); | ||
QJsonDocument contents(QJsonArray::fromVariantList(this->getContents())); | ||
QString res = "var bookData = {" \ | ||
"getComponents: function () { return %1; }, " \ | ||
"getContents: function () { return %2; }, " \ | ||
"getComponent: function (component) { return " \ | ||
"\"<img style='display: block; margin: auto; max-height: 100% !important' src='\"" \ | ||
"+ component.replace(/\"/g, \""\").replace(/'/g, \"'\") + \"' />\"; }, " \ | ||
"getMetaData: function (key) { return ''; } }"; | ||
response->write(res.arg(QString(spine.toJson()), QString(contents.toJson()))); | ||
response->end(); | ||
} | ||
|
||
QVariantMap CBZReader::getCoverInfo(int thumbsize, int fullsize) | ||
{ | ||
QVariantMap res; | ||
if (!this->zip || !this->zip->isOpen()) | ||
return res; | ||
|
||
res["title"] = "ZZZnone"; | ||
res["author"] = ""; | ||
res["authorsort"] = "zzznone"; | ||
res["cover"] = "ZZZnone"; | ||
|
||
this->zip->setCurrentFile(this->spine[0]); | ||
QuaZipFile zfile(this->zip); | ||
if (!zfile.open(QIODevice::ReadOnly)) | ||
return res; | ||
|
||
QImage coverimg; | ||
if (!coverimg.loadFromData(zfile.readAll())) { | ||
zfile.close(); | ||
return res; | ||
} | ||
zfile.close(); | ||
QByteArray byteArray; | ||
QBuffer buffer(&byteArray); | ||
coverimg.scaledToWidth(thumbsize, Qt::SmoothTransformation).save(&buffer, "PNG"); | ||
res["cover"] = "data:image/png;base64," + QString(byteArray.toBase64()); | ||
QByteArray byteArrayf; | ||
QBuffer bufferf(&byteArrayf); | ||
coverimg.scaledToWidth(fullsize, Qt::SmoothTransformation).save(&bufferf, "PNG"); | ||
res["fullcover"] = "data:image/png;base64," + QString(byteArrayf.toBase64()); | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* Copyright 2015 Robert Schroll | ||
* | ||
* This file is part of Beru and is distributed under the terms of | ||
* the GPL. See the file COPYING for full details. | ||
*/ | ||
|
||
#ifndef CBZREADER_H | ||
#define CBZREADER_H | ||
|
||
#include <QObject> | ||
#include <QDomDocument> | ||
#include <QVariant> | ||
#include "quazip/quazip.h" | ||
#include "../qhttpserver/qhttpresponse.h" | ||
|
||
class CBZReader : public QObject | ||
{ | ||
Q_OBJECT | ||
Q_PROPERTY(QString hash READ hash) | ||
Q_PROPERTY(QString title READ title) | ||
public: | ||
explicit CBZReader(QObject *parent = 0); | ||
QString hash(); | ||
QString title(); | ||
Q_INVOKABLE bool load(const QString &filename); | ||
Q_INVOKABLE void serveBookData(QHttpResponse *response); | ||
Q_INVOKABLE void serveComponent(const QString &filename, QHttpResponse *response); | ||
Q_INVOKABLE QVariantMap getCoverInfo(int thumbsize, int fullsize); | ||
|
||
signals: | ||
void contentsReady(QVariantList contents); | ||
|
||
private: | ||
bool parse(); | ||
QVariantList getContents(); | ||
|
||
QuaZip* zip; | ||
QString _hash; | ||
QStringList spine; | ||
|
||
}; | ||
|
||
#endif // CBZREADER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* Copyright 2015 Robert Schroll | ||
* | ||
* This file is part of Beru and is distributed under the terms of | ||
* the GPL. See the file COPYING for full details. | ||
*/ | ||
|
||
import QtQuick 2.0 | ||
import Epub 1.0 | ||
|
||
|
||
Item { | ||
id: reader | ||
|
||
signal contentsReady(var contents) | ||
|
||
property var currentReader | ||
|
||
EpubReader { | ||
id: epub | ||
onContentsReady: reader.contentsReady(contents) | ||
} | ||
|
||
CBZReader { | ||
id: cbz | ||
onContentsReady: reader.contentsReady(contents) | ||
} | ||
|
||
function isCBZ(filename) { | ||
return (filename.slice(-4) == ".cbz") | ||
} | ||
|
||
function load(filename) { | ||
currentReader = isCBZ(filename) ? cbz : epub | ||
return currentReader.load(filename) | ||
} | ||
|
||
function hash() { | ||
return currentReader.hash | ||
} | ||
|
||
function title() { | ||
return currentReader.title | ||
} | ||
|
||
function serveBookData(response) { | ||
currentReader.serveBookData(response) | ||
} | ||
|
||
function serveComponent(filename, response) { | ||
currentReader.serveComponent(filename, response) | ||
} | ||
|
||
function getCoverInfo(thumbsize, fullsize) { | ||
return currentReader.getCoverInfo(thumbsize, fullsize) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.