@@ -62,6 +62,11 @@ int FileManager::perform()
6262 resultErrorType = this ->storeFile (task.getExecutor (),*task.getObject (),result);
6363 writeIndex = true ;
6464 }
65+ else if (task.getAction () == FileManagerTask::Action::ReplaceFile)
66+ {
67+ resultErrorType = this ->replaceFile (task.getExecutor (),*task.getObject (),result);
68+ writeIndex = true ;
69+ }
6570 else if (task.getAction () == FileManagerTask::Action::UpdateFile)
6671 {
6772 resultErrorType = this ->updateFile (task.getExecutor (),*task.getObject (),result);
@@ -188,6 +193,11 @@ QUuid FileManager::requestStoreFile(const RUserInfo &executor, FileObject *objec
188193 return this ->enqueueTask (FileManagerTask (executor,FileManagerTask::StoreFile,object));
189194}
190195
196+ QUuid FileManager::requestReplaceFile (const RUserInfo &executor, FileObject *object)
197+ {
198+ return this ->enqueueTask (FileManagerTask (executor,FileManagerTask::ReplaceFile,object));
199+ }
200+
191201QUuid FileManager::requestUpdateFile (const RUserInfo &executor, FileObject *object)
192202{
193203 return this ->enqueueTask (FileManagerTask (executor,FileManagerTask::UpdateFile,object));
@@ -434,6 +444,57 @@ RError::Type FileManager::storeFile(const RUserInfo &executor, const FileObject
434444 R_LOG_TRACE_RETURN (RError::None);
435445}
436446
447+ RError::Type FileManager::replaceFile (const RUserInfo &executor, const FileObject &object, QByteArray &output)
448+ {
449+ R_LOG_TRACE_IN;
450+ RLogger::debug (" [%s] replaceFile: executor=\" %s\" , storePath=\" %s\" .\n " ,
451+ this ->settings .getName ().toUtf8 ().constData (),
452+ executor.getName ().toUtf8 ().constData (),
453+ this ->storePath .toUtf8 ().constData ());
454+
455+ // List all files owned by executor.
456+ QList<RFileInfo> files = this ->fileIndex .listUserObjects (
457+ [=](const RFileInfo &fileInfo)
458+ {
459+
460+ return (fileInfo.getPath () == object.getInfo ().getPath () && // File paths must be equal
461+ executor.isUser (fileInfo.getAccessRights ().getOwner ().getUser ()) && // File must be owned by the executor
462+ UserManager::authorizeUserAccess (executor,fileInfo.getAccessRights (),RAccessMode::Write)); // File must be writable
463+ }
464+ );
465+
466+ QJsonObject jsonOutput;
467+ QJsonArray jsonRemoveFileArray;
468+
469+ // Store new file object.
470+ QByteArray uploadFileOutput;
471+ RError::Type errorType = this ->storeFile (executor,object,uploadFileOutput);
472+ if (errorType == RError::None)
473+ {
474+ if (!files.isEmpty ())
475+ {
476+ // Remove all replaced files.
477+ for (const RFileInfo &fileInfo : std::as_const (files))
478+ {
479+ QByteArray removeFileOutput;
480+ errorType = this ->removeFile (executor,fileInfo.getId (),removeFileOutput);
481+ jsonRemoveFileArray.append (QJsonDocument::fromJson (removeFileOutput).object ());
482+ if (errorType != RError::None)
483+ {
484+ break ;
485+ }
486+ }
487+ }
488+ }
489+
490+ jsonOutput[" upload" ] = QJsonDocument::fromJson (uploadFileOutput).object ();
491+ jsonOutput[" remove" ] = jsonRemoveFileArray;
492+
493+ output = QJsonDocument (jsonOutput).toJson ();
494+
495+ R_LOG_TRACE_RETURN (errorType);
496+ }
497+
437498RError::Type FileManager::updateFile (const RUserInfo &executor, const FileObject &object, QByteArray &output)
438499{
439500 R_LOG_TRACE_IN;
@@ -757,7 +818,7 @@ RError::Type FileManager::removeFile(const RUserInfo &executor, const QUuid &id,
757818 R_LOG_TRACE_RETURN (RError::InvalidInput);
758819 }
759820
760- RFileInfo fileInfo = this ->fileIndex .unregisterObject (id);
821+ RFileInfo fileInfo ( this ->fileIndex .getObjectInfo (id) );
761822
762823 if (!UserManager::authorizeUserAccess (executor,fileInfo.getAccessRights (),RAccessMode::Write))
763824 {
@@ -767,6 +828,7 @@ RError::Type FileManager::removeFile(const RUserInfo &executor, const QUuid &id,
767828 output.constData ());
768829 R_LOG_TRACE_RETURN (RError::Unauthorized);
769830 }
831+ fileInfo = this ->fileIndex .unregisterObject (id);
770832
771833 QDir storeDir (this ->storePath );
772834 if (!storeDir.remove (fileInfo.getId ().toString (QUuid::WithoutBraces)))
0 commit comments