Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(files): Retry deleting a file if currently locked #44901

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions apps/files/src/actions/deleteAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import type { Node, View } from '@nextcloud/files'

import { emit } from '@nextcloud/event-bus'
import { Permission, Node, View, FileAction, FileType } from '@nextcloud/files'
import { showInfo } from '@nextcloud/dialogs'
import { translate as t, translatePlural as n } from '@nextcloud/l10n'
import { Permission, FileAction, FileType } from '@nextcloud/files'
import { showInfo, showWarning } from '@nextcloud/dialogs'
import { translate as t } from '@nextcloud/l10n'
import { isAxiosError } from 'axios'
import axios from '@nextcloud/axios'

import CloseSvg from '@mdi/svg/svg/close.svg?raw'
Expand Down Expand Up @@ -140,7 +143,7 @@ export const action = new FileAction({
.every(permission => (permission & Permission.DELETE) !== 0)
},

async exec(node: Node) {
async exec(node: Node, view: View, dir: string) {
try {
await axios.delete(node.encodedSource)

Expand All @@ -150,6 +153,15 @@ export const action = new FileAction({
emit('files:node:deleted', node)
return true
} catch (error) {
if (isAxiosError(error) && (error.response?.status === 423 || error.status === 423)) {
logger.debug('Delete failed: File is currently locked', { error, source: node.source })
showWarning(t('files', '{file} is currently locked, retry deleting it in 10s.', { file: node.basename }), { timeout: 9000 })
return new Promise((resolve) => {
window.setTimeout(async () => {
resolve(await this.exec(node, view, dir))
}, 10000)
})
}
logger.error('Error while deleting a file', { error, source: node.source, node })
return false
}
Expand Down
79 changes: 79 additions & 0 deletions cypress/e2e/files/files-delete.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @copyright Copyright (c) 2024 Ferdinand Thiessen <opensource@fthiessen.de>
*
* @author Ferdinand Thiessen <opensource@fthiessen.de>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

import type { User } from '@nextcloud/cypress'
import { getRowForFile, triggerActionForFile } from './FilesUtils'

describe('Files: Delete action', { testIsolation: true }, () => {
let user: User

beforeEach(() => cy.createRandomUser().then(($user) => {
user = $user

cy.mkdir(user, '/folder')
cy.uploadContent(user, new Blob([]), 'text/plain', '/file')
cy.login(user)
cy.visit('/apps/files')
}))

it('can delete a file', () => {
getRowForFile('file').should('be.visible')

triggerActionForFile('file', 'delete')

getRowForFile('file').should('not.exist')
cy.contains('.toast-success', '"Delete file" action').should('be.visible')

// reload to check if file is really deleted
cy.reload()
// list loaded
getRowForFile('folder').should('be.visible')
// file is really deleted
getRowForFile('file').should('not.exist')
})

it('retries to delete a locked file', () => {
cy.intercept({
method: 'DELETE',
pathname: /remote\.php\/dav\/files/,
times: 1,
}, { statusCode: 423, body: {} }).as('deleteAndMock')

cy.intercept({
method: 'DELETE',
pathname: /remote\.php\/dav\/files/,
}).as('delete')

getRowForFile('file').should('be.visible')

triggerActionForFile('file', 'delete')

// wait for delete first request
cy.wait('@delete')
// Also wait for the mock
cy.wait('@deleteAndMock')
cy.contains('.toast-warning', 'file is currently locked').should('be.visible')
// now wait for the second real delete request
cy.wait('@delete', { timeout: 15000 })
cy.contains('.toast-success', '"Delete file" action').should('be.visible')
})
})
4 changes: 2 additions & 2 deletions dist/files-init.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/files-init.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@vueuse/components": "^10.9.0",
"@vueuse/core": "^10.9.0",
"@vueuse/integrations": "^10.9.0",
"axios": "^1.6.8",
"backbone": "^1.4.1",
"blueimp-md5": "^2.19.0",
"browserslist-useragent-regexp": "^4.1.1",
Expand Down
Loading