fix: mdx file not found exception

This commit is contained in:
Xiao YiFang 2023-03-25 20:35:50 +08:00 committed by xiaoyifang
parent 91d539b105
commit 6abdaab5d3
2 changed files with 33 additions and 62 deletions

View file

@ -114,7 +114,6 @@ void Class::open( char const * filename, char const * mode )
f.setFileName( FsEncoding::decode( filename ) ); f.setFileName( FsEncoding::decode( filename ) );
//maybe directory, the xapian use directory to store the index.
if ( !f.open( openMode ) ) if ( !f.open( openMode ) )
throw exCantOpen( std::string( filename ) + ": " + f.errorString().toUtf8().data() ); throw exCantOpen( std::string( filename ) + ": " + f.errorString().toUtf8().data() );
} }

94
mdx.cc
View file

@ -311,6 +311,7 @@ private:
friend class MdxHeadwordsRequest; friend class MdxHeadwordsRequest;
friend class MdxArticleRequest; friend class MdxArticleRequest;
friend class MddResourceRequest; friend class MddResourceRequest;
void loadResourceFile( const wstring & resourceName, vector< char > & data );
}; };
MdxDictionary::MdxDictionary( string const & id, string const & indexFile, MdxDictionary::MdxDictionary( string const & id, string const & indexFile,
@ -738,7 +739,7 @@ void MddResourceRequest::run()
} }
// In order to prevent recursive internal redirection... // In order to prevent recursive internal redirection...
set< QByteArray > resourceIncluded; set< wstring > resourceIncluded;
for ( ;; ) for ( ;; )
{ {
@ -748,43 +749,14 @@ void MddResourceRequest::run()
finish(); finish();
return; return;
} }
string u8ResourceName = Utf8::encode( resourceName ); string u8ResourceName = Utf8::encode( resourceName );
QCryptographicHash hash( QCryptographicHash::Md5 ); if( !resourceIncluded.insert( resourceName ).second )
hash.addData( u8ResourceName.data(), u8ResourceName.size() );
if ( !resourceIncluded.insert( hash.result() ).second )
continue; continue;
// Convert to the Windows separator
std::replace( resourceName.begin(), resourceName.end(), '/', '\\' );
if(resourceName[0]=='.'){
resourceName.erase(0,1);
}
if ( resourceName[ 0 ] != '\\' )
{
resourceName.insert( 0, 1, '\\' );
}
Mutex::Lock _( dataMutex ); Mutex::Lock _( dataMutex );
data.clear(); data.clear();
try dict.loadResourceFile( resourceName, data );
{
// local file takes precedence
string fn = FsEncoding::dirname( dict.getDictionaryFilenames()[ 0 ] ) +
FsEncoding::separator() + u8ResourceName;
File::loadFromFile( fn, data );
}
catch ( File::exCantOpen & )
{
for ( vector< sptr< IndexedMdd > >::const_iterator i = dict.mddResources.begin();
i != dict.mddResources.end(); ++i )
{
sptr< IndexedMdd > mddResource = *i;
if ( mddResource->loadFile( resourceName, data ) )
break;
}
}
// Check if this file has a redirection // Check if this file has a redirection
// Always encoded in UTF16-LE // Always encoded in UTF16-LE
@ -1195,40 +1167,14 @@ QString MdxDictionary::getCachedFileName( QString filename )
vector< char > data; vector< char > data;
// In order to prevent recursive internal redirection... // In order to prevent recursive internal redirection...
set< QByteArray > resourceIncluded; set< wstring > resourceIncluded;
for ( ;; ) for ( ;; )
{ {
string u8ResourceName = Utf8::encode( resourceName ); if( !resourceIncluded.insert( resourceName ).second )
QCryptographicHash hash( QCryptographicHash::Md5 );
hash.addData( u8ResourceName.data(), u8ResourceName.size() );
if ( !resourceIncluded.insert( hash.result() ).second )
continue; continue;
// Convert to the Windows separator loadResourceFile( resourceName, data );
std::replace( resourceName.begin(), resourceName.end(), '/', '\\' );
if ( resourceName[ 0 ] != '\\' )
{
resourceName.insert( 0, 1, '\\' );
}
try
{
// local file takes precedence
string fn = FsEncoding::dirname( getDictionaryFilenames()[ 0 ] ) +
FsEncoding::separator() + u8ResourceName;
File::loadFromFile( fn, data );
}
catch ( File::exCantOpen & )
{
for ( vector< sptr< IndexedMdd > >::const_iterator i = mddResources.begin();
i != mddResources.end(); ++i )
{
sptr< IndexedMdd > mddResource = *i;
if ( mddResource->loadFile( resourceName, data ) )
break;
}
}
// Check if this file has a redirection // Check if this file has a redirection
// Always encoded in UTF16-LE // Always encoded in UTF16-LE
@ -1276,6 +1222,32 @@ QString MdxDictionary::getCachedFileName( QString filename )
return fullName; return fullName;
} }
void MdxDictionary::loadResourceFile( const wstring & resourceName, vector< char > & data )
{
wstring newResourceName = resourceName;
string u8ResourceName = Utf8::encode( resourceName );
// Convert to the Windows separator
std::replace( newResourceName.begin(), newResourceName.end(), '/', '\\' );
if( newResourceName[ 0 ] == '.' ) {
newResourceName.erase( 0, 1 );
}
if( newResourceName[ 0 ] != '\\' ) {
newResourceName.insert( 0, 1, '\\' );
}
// local file takes precedence
string fn = FsEncoding::dirname( getDictionaryFilenames()[ 0 ] ) + FsEncoding::separator() + u8ResourceName;
if( File::exists( fn ) ) {
File::loadFromFile( fn, data );
return;
}
for( auto mddResource : mddResources ) {
if( mddResource->loadFile( newResourceName, data ) )
break;
}
}
void MdxDictionary::removeDirectory( QString const & directory ) void MdxDictionary::removeDirectory( QString const & directory )
{ {
QDir dir( directory ); QDir dir( directory );