mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-30 17:24:08 +00:00
Merge pull request #1199 from xiaoyifang/opt/resource-performance
opt: experimental changes, resource performance, support gdvideo
This commit is contained in:
commit
d1d654226a
|
@ -260,9 +260,10 @@ sptr< Dictionary::DataRequest > ArticleNetworkAccessManager::getResource( QUrl c
|
||||||
|
|
||||||
if ( ( url.scheme() == "bres" || url.scheme() == "gdau" || url.scheme() == "gdvideo" || url.scheme() == "gico" )
|
if ( ( url.scheme() == "bres" || url.scheme() == "gdau" || url.scheme() == "gdvideo" || url.scheme() == "gico" )
|
||||||
&& url.path().size() ) {
|
&& 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();
|
string id = url.host().toStdString();
|
||||||
|
|
||||||
bool search = ( id == "search" );
|
bool search = ( id == "search" );
|
||||||
|
|
|
@ -976,17 +976,17 @@ void MdxDictionary::replaceLinks( QString & id, QString & article )
|
||||||
match = RX::Mdx::srcRe.match( linkTxt );
|
match = RX::Mdx::srcRe.match( linkTxt );
|
||||||
if ( match.hasMatch() ) {
|
if ( match.hasMatch() ) {
|
||||||
QString newText;
|
QString newText;
|
||||||
if ( linkType.at( 1 ) == 'o' ) // "source" tag
|
QString scheme;
|
||||||
{
|
// "source" tag
|
||||||
QString filename = match.captured( 3 );
|
if ( linkType.compare( "source" ) == 0 ) {
|
||||||
QString newName = getCachedFileName( filename );
|
scheme = "gdvideo://";
|
||||||
newName.replace( '\\', '/' );
|
|
||||||
newText = match.captured( 1 ) + match.captured( 2 ) + "file:///" + newName + match.captured( 2 );
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
newText = match.captured( 1 ) + match.captured( 2 ) + "bres://" + id + "/" + match.captured( 3 )
|
scheme = "bres://";
|
||||||
+ match.captured( 2 );
|
|
||||||
}
|
}
|
||||||
|
newText =
|
||||||
|
match.captured( 1 ) + match.captured( 2 ) + scheme + id + "/" + match.captured( 3 ) + match.captured( 2 );
|
||||||
|
|
||||||
newLink = linkTxt.replace( match.capturedStart(), match.capturedLength(), newText );
|
newLink = linkTxt.replace( match.capturedStart(), match.capturedLength(), newText );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -7,26 +7,46 @@ ResourceSchemeHandler::ResourceSchemeHandler( ArticleNetworkAccessManager & arti
|
||||||
}
|
}
|
||||||
void ResourceSchemeHandler::requestStarted( QWebEngineUrlRequestJob * requestJob )
|
void ResourceSchemeHandler::requestStarted( QWebEngineUrlRequestJob * requestJob )
|
||||||
{
|
{
|
||||||
QUrl url = requestJob->requestUrl();
|
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 );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
QNetworkRequest request;
|
|
||||||
request.setUrl( url );
|
void ResourceSchemeHandler::replyJob( sptr< Dictionary::DataRequest > reply,
|
||||||
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
|
QWebEngineUrlRequestJob * requestJob,
|
||||||
QNetworkReply * reply = this->mManager.getArticleReply( request );
|
QString content_type )
|
||||||
connect( reply, &QNetworkReply::finished, requestJob, [ = ]() {
|
{
|
||||||
if ( reply->error() == QNetworkReply::ContentNotFoundError ) {
|
if ( !reply.get() ) {
|
||||||
requestJob->fail( QWebEngineUrlRequestJob::UrlNotFound );
|
requestJob->fail( QWebEngineUrlRequestJob::UrlNotFound );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ( reply->error() != QNetworkReply::NoError ) {
|
const auto & data = reply->getFullData();
|
||||||
qDebug() << "resource handler failed:" << reply->error() << ":" << reply->request().url();
|
if ( data.empty() ) {
|
||||||
requestJob->fail( QWebEngineUrlRequestJob::RequestFailed );
|
requestJob->fail( QWebEngineUrlRequestJob::UrlNotFound );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QMimeType mineType = db.mimeTypeForUrl( url );
|
QByteArray * ba = new QByteArray( data.data(), data.size() );
|
||||||
QString contentType = mineType.name();
|
QBuffer * buffer = new QBuffer( ba );
|
||||||
|
buffer->open( QBuffer::ReadOnly );
|
||||||
|
buffer->seek( 0 );
|
||||||
|
|
||||||
// Reply segment
|
// Reply segment
|
||||||
requestJob->reply( contentType.toLatin1(), reply );
|
requestJob->reply( content_type.toLatin1(), buffer );
|
||||||
|
|
||||||
|
connect( requestJob, &QObject::destroyed, buffer, [ = ]() {
|
||||||
|
buffer->close();
|
||||||
|
ba->clear();
|
||||||
|
delete ba;
|
||||||
|
buffer->deleteLater();
|
||||||
} );
|
} );
|
||||||
connect( requestJob, &QObject::destroyed, reply, &QObject::deleteLater );
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ public:
|
||||||
void requestStarted( QWebEngineUrlRequestJob * requestJob );
|
void requestStarted( QWebEngineUrlRequestJob * requestJob );
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
void replyJob( sptr< Dictionary::DataRequest > reply, QWebEngineUrlRequestJob * requestJob, QString content_type );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ArticleNetworkAccessManager & mManager;
|
ArticleNetworkAccessManager & mManager;
|
||||||
|
|
Loading…
Reference in a new issue