mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-23 20:14:05 +00:00
opt: use a different way to handle the resources
This commit is contained in:
parent
3f1b43aa3a
commit
ffc4ccbb03
|
@ -260,9 +260,10 @@ sptr< Dictionary::DataRequest > ArticleNetworkAccessManager::getResource( QUrl c
|
|||
|
||||
if ( ( url.scheme() == "bres" || url.scheme() == "gdau" || url.scheme() == "gdvideo" || url.scheme() == "gico" )
|
||||
&& url.path().size() ) {
|
||||
//GD_DPRINTF( "Get %s\n", req.url().host().toLocal8Bit().data() );
|
||||
//GD_DPRINTF( "Get %s\n", req.url().path().toLocal8Bit().data() );
|
||||
|
||||
|
||||
QMimeType mineType = db.mimeTypeForUrl( url );
|
||||
contentType = mineType.name();
|
||||
string id = url.host().toStdString();
|
||||
|
||||
bool search = ( id == "search" );
|
||||
|
|
|
@ -7,26 +7,46 @@ ResourceSchemeHandler::ResourceSchemeHandler( ArticleNetworkAccessManager & arti
|
|||
}
|
||||
void ResourceSchemeHandler::requestStarted( QWebEngineUrlRequestJob * requestJob )
|
||||
{
|
||||
QUrl url = requestJob->requestUrl();
|
||||
|
||||
QNetworkRequest request;
|
||||
request.setUrl( url );
|
||||
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
|
||||
QNetworkReply * reply = this->mManager.getArticleReply( request );
|
||||
connect( reply, &QNetworkReply::finished, requestJob, [ = ]() {
|
||||
if ( reply->error() == QNetworkReply::ContentNotFoundError ) {
|
||||
requestJob->fail( QWebEngineUrlRequestJob::UrlNotFound );
|
||||
return;
|
||||
}
|
||||
if ( reply->error() != QNetworkReply::NoError ) {
|
||||
qDebug() << "resource handler failed:" << reply->error() << ":" << reply->request().url();
|
||||
requestJob->fail( QWebEngineUrlRequestJob::RequestFailed );
|
||||
return;
|
||||
}
|
||||
QMimeType mineType = db.mimeTypeForUrl( url );
|
||||
QString contentType = mineType.name();
|
||||
// Reply segment
|
||||
requestJob->reply( contentType.toLatin1(), reply );
|
||||
} );
|
||||
connect( requestJob, &QObject::destroyed, reply, &QObject::deleteLater );
|
||||
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();
|
||||
if ( reply->isFinished() ) {
|
||||
replyJob( reply, requestJob, content_type );
|
||||
}
|
||||
else
|
||||
connect( reply.get(), &Dictionary::DataRequest::finished, requestJob, [ = ]() {
|
||||
replyJob( reply, requestJob, content_type );
|
||||
} );
|
||||
}
|
||||
|
||||
|
||||
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 );
|
||||
|
||||
connect( requestJob, &QObject::destroyed, buffer, [ = ]() {
|
||||
buffer->close();
|
||||
ba->clear();
|
||||
delete ba;
|
||||
buffer->deleteLater();
|
||||
} );
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ public:
|
|||
void requestStarted( QWebEngineUrlRequestJob * requestJob );
|
||||
|
||||
protected:
|
||||
void replyJob( sptr< Dictionary::DataRequest > reply, QWebEngineUrlRequestJob * requestJob, QString content_type );
|
||||
|
||||
private:
|
||||
ArticleNetworkAccessManager & mManager;
|
||||
|
|
Loading…
Reference in a new issue