2023-04-17 20:12:27 +00:00
|
|
|
#include "resourceschemehandler.hh"
|
2021-08-28 02:35:23 +00:00
|
|
|
|
2022-12-22 01:08:41 +00:00
|
|
|
ResourceSchemeHandler::ResourceSchemeHandler( ArticleNetworkAccessManager & articleNetMgr, QObject * parent ):
|
|
|
|
QWebEngineUrlSchemeHandler( parent ),
|
|
|
|
mManager( articleNetMgr )
|
|
|
|
{
|
2021-08-28 02:35:23 +00:00
|
|
|
}
|
2022-01-09 01:52:40 +00:00
|
|
|
void ResourceSchemeHandler::requestStarted( QWebEngineUrlRequestJob * requestJob )
|
2021-08-28 02:35:23 +00:00
|
|
|
{
|
2023-10-06 01:13:55 +00:00
|
|
|
const QUrl url = requestJob->requestUrl();
|
|
|
|
QString content_type;
|
|
|
|
const QMimeType mineType = db.mimeTypeForUrl( url );
|
|
|
|
const sptr< Dictionary::DataRequest > reply = this->mManager.getResource( url, content_type );
|
|
|
|
content_type = mineType.name();
|
2024-03-19 07:18:21 +00:00
|
|
|
|
|
|
|
if ( reply == nullptr ) {
|
|
|
|
qDebug() << "Resource failed to load: " << url.toString();
|
|
|
|
requestJob->fail( QWebEngineUrlRequestJob::RequestFailed );
|
|
|
|
}
|
|
|
|
else if ( reply->isFinished() ) {
|
2023-10-06 01:13:55 +00:00
|
|
|
replyJob( reply, requestJob, content_type );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
connect( reply.get(), &Dictionary::DataRequest::finished, requestJob, [ = ]() {
|
|
|
|
replyJob( reply, requestJob, content_type );
|
|
|
|
} );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2023-10-06 01:13:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ResourceSchemeHandler::replyJob( sptr< Dictionary::DataRequest > reply,
|
|
|
|
QWebEngineUrlRequestJob * requestJob,
|
|
|
|
QString content_type )
|
|
|
|
{
|
|
|
|
if ( !reply.get() ) {
|
|
|
|
requestJob->fail( QWebEngineUrlRequestJob::UrlNotFound );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto & data = reply->getFullData();
|
|
|
|
if ( data.empty() ) {
|
|
|
|
requestJob->fail( QWebEngineUrlRequestJob::UrlNotFound );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QByteArray * ba = new QByteArray( data.data(), data.size() );
|
|
|
|
QBuffer * buffer = new QBuffer( ba );
|
|
|
|
buffer->open( QBuffer::ReadOnly );
|
|
|
|
buffer->seek( 0 );
|
|
|
|
|
|
|
|
// Reply segment
|
|
|
|
requestJob->reply( content_type.toLatin1(), buffer );
|
2021-08-28 02:35:23 +00:00
|
|
|
|
2023-10-06 01:13:55 +00:00
|
|
|
connect( requestJob, &QObject::destroyed, buffer, [ = ]() {
|
|
|
|
buffer->close();
|
|
|
|
ba->clear();
|
|
|
|
delete ba;
|
|
|
|
buffer->deleteLater();
|
2022-01-08 04:22:41 +00:00
|
|
|
} );
|
2021-08-28 02:35:23 +00:00
|
|
|
}
|