Merge pull request #1199 from xiaoyifang/opt/resource-performance

opt: experimental changes, resource performance, support gdvideo
This commit is contained in:
xiaoyifang 2023-10-10 07:38:58 +08:00 committed by GitHub
commit d1d654226a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 32 deletions

View file

@ -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" );

View file

@ -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

View file

@ -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;
QNetworkRequest request; const QMimeType mineType = db.mimeTypeForUrl( url );
request.setUrl( url ); const sptr< Dictionary::DataRequest > reply = this->mManager.getResource( url, content_type );
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache ); content_type = mineType.name();
QNetworkReply * reply = this->mManager.getArticleReply( request ); if ( reply->isFinished() ) {
connect( reply, &QNetworkReply::finished, requestJob, [ = ]() { replyJob( reply, requestJob, content_type );
if ( reply->error() == QNetworkReply::ContentNotFoundError ) { }
requestJob->fail( QWebEngineUrlRequestJob::UrlNotFound ); else
return; connect( reply.get(), &Dictionary::DataRequest::finished, requestJob, [ = ]() {
} replyJob( reply, requestJob, content_type );
if ( reply->error() != QNetworkReply::NoError ) { } );
qDebug() << "resource handler failed:" << reply->error() << ":" << reply->request().url(); }
requestJob->fail( QWebEngineUrlRequestJob::RequestFailed );
return;
} void ResourceSchemeHandler::replyJob( sptr< Dictionary::DataRequest > reply,
QMimeType mineType = db.mimeTypeForUrl( url ); QWebEngineUrlRequestJob * requestJob,
QString contentType = mineType.name(); QString content_type )
// Reply segment {
requestJob->reply( contentType.toLatin1(), reply ); if ( !reply.get() ) {
} ); requestJob->fail( QWebEngineUrlRequestJob::UrlNotFound );
connect( requestJob, &QObject::destroyed, reply, &QObject::deleteLater ); 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();
} );
} }

View file

@ -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;