Skip to content

Commit

Permalink
Have uploaded files adapt their last modified to match server to avoi…
Browse files Browse the repository at this point in the history
…d infinite re-upload
  • Loading branch information
nirvn committed Jan 5, 2025
1 parent 659f8ad commit df5030d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 40 deletions.
116 changes: 76 additions & 40 deletions src/core/webdavconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,59 +265,86 @@ void WebdavConnection::processDirParserFinished()
}
else if ( mIsUploadingPath )
{
applyStoredPassword();

QStringList remoteDirs;
for ( const QWebdavItem &item : list )
if ( !mWebdavLastModified.isEmpty() )
{
if ( item.isDir() )
// Adjust modified date to match upload files
for ( const QWebdavItem &item : list )
{
remoteDirs << item.path();
if ( mWebdavLastModified.contains( item.path() ) )
{
QFile file( mProcessLocalPath + item.path().mid( mProcessRemotePath.size() ) );
if ( file.exists() )
{
// The local file should always exist at this stage, just playing safe
file.open( QFile::Append );
file.setFileTime( item.lastModified(), QFileDevice::FileModificationTime );
file.setFileTime( item.lastModified(), QFileDevice::FileAccessTime );
file.close();
}
}
}
else
mWebdavLastModified.clear();

mIsUploadingPath = false;
emit isUploadingPathChanged();
}
else
{
// Filter files to upload
applyStoredPassword();

QStringList remoteDirs;
for ( const QWebdavItem &item : list )
{
QFileInfo fileInfo( mProcessLocalPath + item.path().mid( mProcessRemotePath.size() ) );
if ( fileInfo.exists() )
if ( item.isDir() )
{
auto localFileInfo = std::find_if( mLocalItems.begin(), mLocalItems.end(), [&fileInfo]( const QFileInfo &entry ) {
return entry.absoluteFilePath() == fileInfo.absoluteFilePath();
} );

if ( localFileInfo != mLocalItems.end() )
remoteDirs << item.path();
}
else
{
QFileInfo fileInfo( mProcessLocalPath + item.path().mid( mProcessRemotePath.size() ) );
if ( fileInfo.exists() )
{
if ( localFileInfo->fileTime( QFileDevice::FileModificationTime ) == item.lastModified() )
auto localFileInfo = std::find_if( mLocalItems.begin(), mLocalItems.end(), [&fileInfo]( const QFileInfo &entry ) {
return entry.absoluteFilePath() == fileInfo.absoluteFilePath();
} );

if ( localFileInfo != mLocalItems.end() )
{
mLocalItems.remove( localFileInfo - mLocalItems.begin(), 1 );
if ( localFileInfo->fileTime( QFileDevice::FileModificationTime ) == item.lastModified() )
{
mLocalItems.remove( localFileInfo - mLocalItems.begin(), 1 );
}
}
}
}
}
}

mWebdavMkDirs.clear();
for ( const QFileInfo &fileInfo : mLocalItems )
{
// Insure the path exists remotely
QString remoteDir = mProcessRemotePath + fileInfo.absolutePath().mid( mProcessLocalPath.size() ).replace( QDir::separator(), "/" );
if ( !remoteDirs.contains( remoteDir ) && !mWebdavMkDirs.contains( remoteDir ) )
mWebdavMkDirs.clear();
for ( const QFileInfo &fileInfo : mLocalItems )
{
const QStringList remoteDirParts = remoteDir.mid( mProcessRemotePath.size() ).split( "/", Qt::SkipEmptyParts );
remoteDir = mProcessRemotePath;
for ( const QString &part : remoteDirParts )
// Insure the path exists remotely
QString remoteDir = mProcessRemotePath + fileInfo.absolutePath().mid( mProcessLocalPath.size() ).replace( QDir::separator(), "/" );
if ( !remoteDirs.contains( remoteDir ) && !mWebdavMkDirs.contains( remoteDir ) )
{
remoteDir += part + "/";
if ( !remoteDirs.contains( remoteDir ) && !mWebdavMkDirs.contains( remoteDir ) )
const QStringList remoteDirParts = remoteDir.mid( mProcessRemotePath.size() ).split( "/", Qt::SkipEmptyParts );
remoteDir = mProcessRemotePath;
for ( const QString &part : remoteDirParts )
{
mWebdavMkDirs << remoteDir;
remoteDir += part + "/";
if ( !remoteDirs.contains( remoteDir ) && !mWebdavMkDirs.contains( remoteDir ) )
{
mWebdavMkDirs << remoteDir;
}
}
}

mBytesTotal += fileInfo.size();
}
emit progressChanged();

mBytesTotal += fileInfo.size();
putLocalItems();
}
emit progressChanged();

putLocalItems();
}
}

Expand Down Expand Up @@ -362,8 +389,6 @@ void WebdavConnection::getWebdavItems()
file.setFileTime( itemLastModified, QFileDevice::FileModificationTime );
file.setFileTime( itemLastModified, QFileDevice::FileAccessTime );
file.close();

QFileInfo fi( file );
}
else
{
Expand Down Expand Up @@ -446,6 +471,8 @@ void WebdavConnection::putLocalItems()
mLastError = tr( "Failed to upload file %1 due to network error (%2)" ).arg( remoteItemPath ).arg( reply->error() );
}

mWebdavLastModified << remoteItemPath;

mLocalItems.removeFirst();
putLocalItems();
reply->deleteLater();
Expand All @@ -455,8 +482,15 @@ void WebdavConnection::putLocalItems()
{
if ( mIsUploadingPath )
{
mIsUploadingPath = false;
emit isUploadingPathChanged();
if ( !mWebdavLastModified.isEmpty() )
{
mWebdavDirParser.listDirectory( &mWebdavConnection, mProcessRemotePath, true );
}
else
{
mIsUploadingPath = false;
emit isUploadingPathChanged();
}
}
}
}
Expand Down Expand Up @@ -495,7 +529,7 @@ void WebdavConnection::downloadPath( const QString &localPath )
QStringList remoteChildrenPath;
while ( !webdavConfigurationExists )
{
remoteChildrenPath << dir.dirName();
remoteChildrenPath.prepend( dir.dirName() );
if ( !dir.cdUp() )
break;

Expand Down Expand Up @@ -544,7 +578,7 @@ void WebdavConnection::uploadPath( const QString &localPath )
QStringList remoteChildrenPath;
while ( !webdavConfigurationExists )
{
remoteChildrenPath << dir.dirName();
remoteChildrenPath.prepend( dir.dirName() );
if ( !dir.cdUp() )
break;

Expand Down Expand Up @@ -572,6 +606,8 @@ void WebdavConnection::uploadPath( const QString &localPath )
}
mProcessLocalPath = QDir::cleanPath( localPath ) + QDir::separator();

mWebdavLastModified.clear();

mLocalItems.clear();
QDirIterator it( mProcessLocalPath, QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories );
while ( it.hasNext() )
Expand All @@ -598,7 +634,7 @@ void WebdavConnection::uploadPath( const QString &localPath )

double WebdavConnection::progress() const
{
if ( ( mIsImportingPath || mIsDownloadingPath ) && mBytesTotal > 0 )
if ( ( mIsImportingPath || mIsDownloadingPath || mIsUploadingPath ) && mBytesTotal > 0 )
{
return static_cast<double>( mBytesProcessed + mCurrentBytesProcessed ) / mBytesTotal;
}
Expand Down
1 change: 1 addition & 0 deletions src/core/webdavconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class WebdavConnection : public QObject
QList<QWebdavItem> mWebdavItems;
QList<QString> mWebdavMkDirs;
QList<QFileInfo> mLocalItems;
QList<QString> mWebdavLastModified;

QString mProcessRemotePath;
QString mProcessLocalPath;
Expand Down
1 change: 1 addition & 0 deletions vcpkg/ports/qtwebdav/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ vcpkg_from_github(
)

list(APPEND QTWEBDAV_OPTIONS -DBUILD_WITH_QT6=True)
list(APPEND QTWEBDAV_OPTIONS -DWITH_DEBUG=True)
if(VCPKG_CROSSCOMPILING)
list(APPEND QTWEBDAV_OPTIONS -DQT_HOST_PATH=${CURRENT_HOST_INSTALLED_DIR})
list(APPEND QTWEBDAV_OPTIONS -DQT_HOST_PATH_CMAKE_DIR:PATH=${CURRENT_HOST_INSTALLED_DIR}/share)
Expand Down

0 comments on commit df5030d

Please sign in to comment.