Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(backend:sync): avoid "parent must exist" error when files are moved before destination folder creation during sync

johaven8c9253551aa1
diff --git a/backend/src/applications/files/services/files-manager.service.ts b/backend/src/applications/files/services/files-manager.service.ts
index 909bae06..19f737b8 100644
--- a/backend/src/applications/files/services/files-manager.service.ts
+++ b/backend/src/applications/files/services/files-manager.service.ts
@@ -270,6 +270,7 @@ export class FilesManager {
dstSpace: SpaceEnv,
isMove: boolean,
overwrite = false,
+ mkdirDstParentPath = false,
dav?: { depth: LOCK_DEPTH; lockTokens: string[] }
): Promise<void> {
// checks
@@ -289,7 +290,16 @@ export class FilesManager {
throw new FileError(HttpStatus.NOT_FOUND, 'Location not found')
}
if (!(await isPathExists(dirName(dstSpace.realPath)))) {
- throw new FileError(HttpStatus.CONFLICT, 'Parent must exists')
+ if (mkdirDstParentPath) {
+ try {
+ await makeDir(dirName(dstSpace.realPath), true)
+ } catch (e) {
+ this.logger.error(`${this.copyMove.name} - Cannot create parent directory for destination ${dstSpace.realPath} : ${e}`)
+ throw new FileError(HttpStatus.INTERNAL_SERVER_ERROR, 'Cannot create parent directory for destination')
+ }
+ } else {
+ throw new FileError(HttpStatus.CONFLICT, 'Parent must exists')
+ }
}
if (srcSpace.realPath === dstSpace.realPath) {
throw new FileError(HttpStatus.FORBIDDEN, 'Cannot copy/move source onto itself')
diff --git a/backend/src/applications/sync/services/sync-manager.service.ts b/backend/src/applications/sync/services/sync-manager.service.ts
index 5e83c726..b99e046c 100644
--- a/backend/src/applications/sync/services/sync-manager.service.ts
+++ b/backend/src/applications/sync/services/sync-manager.service.ts
@@ -115,7 +115,7 @@ export class SyncManager {
async copyMove(req: FastifySpaceRequest, syncCopyMoveDto: SyncCopyMoveDto, isMove: boolean): Promise<void | { ino: number; mtime: number }> {
const dstSpace: SpaceEnv = await this.spacesManager.spaceEnv(req.user, SYNC_PATH_TO_SPACE_SEGMENTS(syncCopyMoveDto.destination))
try {
- await this.filesManager.copyMove(req.user, req.space, dstSpace, isMove, true)
+ await this.filesManager.copyMove(req.user, req.space, dstSpace, isMove, true, true)
if (!isMove) {
if (syncCopyMoveDto.mtime) {
// update mtime
diff --git a/backend/src/applications/webdav/services/webdav-methods.service.ts b/backend/src/applications/webdav/services/webdav-methods.service.ts
index f189b38c..74bfdf13 100644
--- a/backend/src/applications/webdav/services/webdav-methods.service.ts
+++ b/backend/src/applications/webdav/services/webdav-methods.service.ts
@@ -109,35 +109,6 @@ export class WebDAVMethods {
}
}
- private async lockRefresh(req: FastifyDAVRequest, res: FastifyReply, dbFilePath: string) {
- if (req.dav?.ifHeaders?.length !== 1) {
- this.logger.warn('Expected a lock token (only one lock may be refreshed at a time)')
- return res.status(HttpStatus.BAD_REQUEST).send('Expected a lock token (only one lock may be refreshed at a time)')
- }
-
- let token: string
- try {
- token = extractOneToken(req.dav.ifHeaders)
- } catch (e) {
- this.logger.warn(`${this.lockRefresh.name} - unable to extract token : ${JSON.stringify(req.dav.ifHeaders)} (${e})`)
- return res.status(HttpStatus.BAD_REQUEST).send('Unable to extract token')
- }
-
- const lock = await this.filesLockManager.isLockedWithToken(token, dbFilePath)
- if (!lock) {
- this.logger.warn(`Lock token does not exist or not match URL : ${token}`)
- return DAV_ERROR_RES(HttpStatus.PRECONDITION_FAILED, PRECONDITION.LOCK_TOKEN_MISMATCH, res)
- }
- if (lock.owner.id !== req.user.id) {
- this.logger.warn(`Lock token does not match owner : ${lock.owner.login} != ${req.user.login}`)
- return res.status(HttpStatus.FORBIDDEN).send('Lock token does not match owner')
- }
-
- await this.filesLockManager.refreshLockTimeout(lock, req.dav.lock.timeout)
- const lockProp = LOCK_PROP([lock])
- return res.type(XML_CONTENT_TYPE).status(HttpStatus.OK).send(xmlBuild(lockProp))
- }
-
@IfHeaderDecorator()
async unlock(req: FastifyDAVRequest, res: FastifyReply) {
if (!(await isPathExists(req.space.realPath))) {
@@ -374,7 +345,7 @@ export class WebDAVMethods {
return
}
try {
- await this.filesManager.copyMove(req.user, req.space, dstSpace, req.dav.copyMove.isMove, req.dav.copyMove.overwrite, {
+ await this.filesManager.copyMove(req.user, req.space, dstSpace, req.dav.copyMove.isMove, req.dav.copyMove.overwrite, false, {
depth: req.dav.depth,
lockTokens: extractAllTokens(req.dav.ifHeaders)
})
@@ -461,6 +432,35 @@ export class WebDAVMethods {
return atLeastOneOk
}
+ private async lockRefresh(req: FastifyDAVRequest, res: FastifyReply, dbFilePath: string) {
+ if (req.dav?.ifHeaders?.length !== 1) {
+ this.logger.warn('Expected a lock token (only one lock may be refreshed at a time)')
+ return res.status(HttpStatus.BAD_REQUEST).send('Expected a lock token (only one lock may be refreshed at a time)')
+ }
+
+ let token: string
+ try {
+ token = extractOneToken(req.dav.ifHeaders)
+ } catch (e) {
+ this.logger.warn(`${this.lockRefresh.name} - unable to extract token : ${JSON.stringify(req.dav.ifHeaders)} (${e})`)
+ return res.status(HttpStatus.BAD_REQUEST).send('Unable to extract token')
+ }
+
+ const lock = await this.filesLockManager.isLockedWithToken(token, dbFilePath)
+ if (!lock) {
+ this.logger.warn(`Lock token does not exist or not match URL : ${token}`)
+ return DAV_ERROR_RES(HttpStatus.PRECONDITION_FAILED, PRECONDITION.LOCK_TOKEN_MISMATCH, res)
+ }
+ if (lock.owner.id !== req.user.id) {
+ this.logger.warn(`Lock token does not match owner : ${lock.owner.login} != ${req.user.login}`)
+ return res.status(HttpStatus.FORBIDDEN).send('Lock token does not match owner')
+ }
+
+ await this.filesLockManager.refreshLockTimeout(lock, req.dav.lock.timeout)
+ const lockProp = LOCK_PROP([lock])
+ return res.type(XML_CONTENT_TYPE).status(HttpStatus.OK).send(xmlBuild(lockProp))
+ }
+
private handleError(req: FastifyDAVRequest, res: FastifyReply, e: any, toUrl?: string) {
this.logger.error(`Unable to ${req.method} ${req.dav.url}${toUrl ? ` -> ${toUrl}` : ''} : ${e.message}`)
if (e instanceof LockConflict) {