Commit 4de277b7 authored by armin's avatar armin
Browse files

fix fileupload on android

parent 4a59d1e9
...@@ -25,7 +25,11 @@ ...@@ -25,7 +25,11 @@
FileUploader::FileUploader( QObject *parent, RocketChatServerData *pServer, const QUrl &pUri, const QString &pChannelId ): QObject( parent ), FileUploader::FileUploader( QObject *parent, RocketChatServerData *pServer, const QUrl &pUri, const QString &pChannelId ): QObject( parent ),
mUri( pUri ), mChannelId( pChannelId ), mServer( pServer ) mUri( pUri ), mChannelId( pChannelId ), mServer( pServer )
{ {
#ifdef Q_OS_ANDROID
auto file = QSharedPointer<QFile>::create( pUri.path() );
#else
auto file = QSharedPointer<QFile>::create( pUri.toLocalFile() ); auto file = QSharedPointer<QFile>::create( pUri.toLocalFile() );
#endif
if ( file->isOpen() ) { if ( file->isOpen() ) {
file->close(); file->close();
...@@ -195,12 +199,14 @@ void FileUploader::nextRequest() ...@@ -195,12 +199,14 @@ void FileUploader::nextRequest()
auto request = mPartialRequests.first(); auto request = mPartialRequests.first();
double diff = request->getProgress() - progress; double diff = request->getProgress() - progress;
uint now = QDateTime::currentDateTime().toTime_t(); uint now = QDateTime::currentDateTime().toTime_t();
if( diff >= 0.05 && now - lastUpdate){
if ( diff >= 0.05 && now - lastUpdate ) {
progress = request->getProgress(); progress = request->getProgress();
qDebug() << "progress is "<< progress; qDebug() << "progress is " << progress;
emit progressChanged( progress ); emit progressChanged( progress );
lastUpdate = now; lastUpdate = now;
} }
mServer->sendApiRequest( request, true ); mServer->sendApiRequest( request, true );
} }
} }
......
...@@ -70,74 +70,74 @@ FileService::FileService( RocketChatServerData *server ) ...@@ -70,74 +70,74 @@ FileService::FileService( RocketChatServerData *server )
*/ */
bool FileService::getFileRessource( const QSharedPointer< FileRequest > &pRequest ) bool FileService::getFileRessource( const QSharedPointer< FileRequest > &pRequest )
{ {
if(mServer){ if ( mServer ) {
auto repo = mServer->getFiles(); auto repo = mServer->getFiles();
if ( !pRequest.isNull() && pRequest->url.size() > 0 ) { if ( !pRequest.isNull() && pRequest->url.size() > 0 ) {
if ( mCurrentDownloads.contains( pRequest->url ) ) { if ( mCurrentDownloads.contains( pRequest->url ) ) {
return true; return true;
} }
auto tempFile = getFileFromCache( pRequest ); auto tempFile = getFileFromCache( pRequest );
if ( !tempFile.isNull() ) { if ( !tempFile.isNull() ) {
if ( !pRequest->showInline ) { if ( !pRequest->showInline ) {
auto location = determineLocation( pRequest->url, pRequest->type, false ); auto location = determineLocation( pRequest->url, pRequest->type, false );
QFile targetFile( location ); QFile targetFile( location );
if ( targetFile.exists() ) { if ( targetFile.exists() ) {
auto parts = location.split( "." ); auto parts = location.split( "." );
location.clear(); location.clear();
for ( auto i = 0 ; i < ( parts.length() - 1 ); i++ ) { for ( auto i = 0 ; i < ( parts.length() - 1 ); i++ ) {
location += parts[i]; location += parts[i];
if ( i < ( parts.length() - 1 ) ) { if ( i < ( parts.length() - 1 ) ) {
location += QStringLiteral( "." ); location += QStringLiteral( "." );
}
} }
}
location += QStringLiteral( "_" ) + QString::number( QDateTime::currentSecsSinceEpoch() ); location += QStringLiteral( "_" ) + QString::number( QDateTime::currentSecsSinceEpoch() );
location += QStringLiteral( "." ) + parts[parts.size() - 1]; location += QStringLiteral( "." ) + parts[parts.size() - 1];
QFile oldFile( tempFile->getFilePath() ); QFile oldFile( tempFile->getFilePath() );
if ( oldFile.copy( location ) ) {
tempFile = QSharedPointer<TempFile>::create( location, pRequest->url );
} else {
qWarning() << oldFile.errorString();
}
} else {
QFile oldFile( tempFile->getFilePath() );
QString path = location;
QStringList pathParts = location.split( "/" );
pathParts.removeLast();
QString dirStr = pathParts.join( "/" );
QDir dir;
if ( dir.mkpath( dirStr ) ) {
if ( oldFile.copy( location ) ) { if ( oldFile.copy( location ) ) {
tempFile = QSharedPointer<TempFile>::create( location, pRequest->url ); tempFile = QSharedPointer<TempFile>::create( location, pRequest->url );
} else { } else {
qWarning() << oldFile.errorString(); qWarning() << oldFile.errorString();
} }
} else { } else {
qWarning() << "could not create: " << dirStr; QFile oldFile( tempFile->getFilePath() );
QString path = location;
QStringList pathParts = location.split( "/" );
pathParts.removeLast();
QString dirStr = pathParts.join( "/" );
QDir dir;
if ( dir.mkpath( dirStr ) ) {
if ( oldFile.copy( location ) ) {
tempFile = QSharedPointer<TempFile>::create( location, pRequest->url );
} else {
qWarning() << oldFile.errorString();
}
} else {
qWarning() << "could not create: " << dirStr;
}
} }
} }
}
pRequest->then( tempFile, pRequest->showInline );
} else {
getFileFromServer( pRequest );
}
pRequest->then( tempFile, pRequest->showInline );
} else {
getFileFromServer( pRequest );
} }
}
} }
return false; return false;
...@@ -145,7 +145,7 @@ bool FileService::getFileRessource( const QSharedPointer< FileRequest > &pReques ...@@ -145,7 +145,7 @@ bool FileService::getFileRessource( const QSharedPointer< FileRequest > &pReques
void FileService::getFilesFromDb() void FileService::getFilesFromDb()
{ {
if(mServer){ if ( mServer ) {
auto files = mStorage->getFiles(); auto files = mStorage->getFiles();
auto repo = mServer->getFiles(); auto repo = mServer->getFiles();
...@@ -159,7 +159,7 @@ void FileService::getFilesFromDb() ...@@ -159,7 +159,7 @@ void FileService::getFilesFromDb()
QSharedPointer<TempFile> FileService::getFileFromCache( const QSharedPointer< FileRequest > &pRequest ) QSharedPointer<TempFile> FileService::getFileFromCache( const QSharedPointer< FileRequest > &pRequest )
{ {
if ( !pRequest.isNull()&&mServer ) { if ( !pRequest.isNull() && mServer ) {
auto repo = mServer->getFiles(); auto repo = mServer->getFiles();
if ( repo == nullptr ) { if ( repo == nullptr ) {
...@@ -170,10 +170,6 @@ QSharedPointer<TempFile> FileService::getFileFromCache( const QSharedPointer< Fi ...@@ -170,10 +170,6 @@ QSharedPointer<TempFile> FileService::getFileFromCache( const QSharedPointer< Fi
QString fullUrl = baseUrl + pRequest->url ; QString fullUrl = baseUrl + pRequest->url ;
qDebug() << "file from cache requested" << fullUrl; qDebug() << "file from cache requested" << fullUrl;
if(fullUrl.contains("ras")){
qDebug()<<"ras found";
}
if ( repo->contains( fullUrl ) ) { if ( repo->contains( fullUrl ) ) {
auto storageEntry = repo->get( fullUrl ); auto storageEntry = repo->get( fullUrl );
...@@ -184,15 +180,16 @@ QSharedPointer<TempFile> FileService::getFileFromCache( const QSharedPointer< Fi ...@@ -184,15 +180,16 @@ QSharedPointer<TempFile> FileService::getFileFromCache( const QSharedPointer< Fi
} }
if (storageEntry->getFileChecked() ) { if ( storageEntry->getFileChecked() ) {
return storageEntry; return storageEntry;
}else{ } else {
QFile cacheHit( storageEntry->getFilePath() ); QFile cacheHit( storageEntry->getFilePath() );
if(cacheHit.exists()){
storageEntry->setFileChecked(true); if ( cacheHit.exists() ) {
return storageEntry; storageEntry->setFileChecked( true );
} return storageEntry;
} }
}
mStorage->removeFileCacheEntry( pRequest->url, storageEntry->getFilePath() ); mStorage->removeFileCacheEntry( pRequest->url, storageEntry->getFilePath() );
repo->remove( fullUrl ); repo->remove( fullUrl );
...@@ -209,7 +206,7 @@ QSharedPointer<TempFile> FileService::getFileFromCache( const QSharedPointer< Fi ...@@ -209,7 +206,7 @@ QSharedPointer<TempFile> FileService::getFileFromCache( const QSharedPointer< Fi
void FileService::getFileFromServer( const QSharedPointer< FileRequest > &pRequest ) void FileService::getFileFromServer( const QSharedPointer< FileRequest > &pRequest )
{ {
if ( Q_LIKELY( !pRequest.isNull()&&mServer ) ) { if ( Q_LIKELY( !pRequest.isNull() && mServer ) ) {
auto baseUrl = mServer->getRestApi()->getBaseUrl(); auto baseUrl = mServer->getRestApi()->getBaseUrl();
auto fullUrl = baseUrl + pRequest->url ; auto fullUrl = baseUrl + pRequest->url ;
...@@ -356,7 +353,7 @@ QString FileService::determineLocation( const QUrl &pUrl, const QString &pType, ...@@ -356,7 +353,7 @@ QString FileService::determineLocation( const QUrl &pUrl, const QString &pType,
filePath = mMusicPath + urlPath; filePath = mMusicPath + urlPath;
} else if ( pType == "document" ) { } else if ( pType == "document" ) {
filePath = mDocumentPath + urlPath; filePath = mDocumentPath + urlPath;
}else{ } else {
filePath = mTempPath + urlPath; filePath = mTempPath + urlPath;
} }
} else { } else {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment