Verified Commit ce6ac97c authored by anarcat's avatar anarcat
Browse files

handle already existing remote files on move

For some reason, we have files in the processing folder that are
already present before moving. Skip those files instead of crashing
with:

webdav3.exceptions.ResponseErrorCode: Request to https://nc.torproject.net/remote.php/dav/files/dangerzone-bot/CVS/dangerzone-processing/2/ failed with code 412 and message: b'<?xml version="1.0" encoding="utf-8"?>\n<d:error xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">\n  <s:exception>Sabre\\DAV\\Exception\\PreconditionFailed</s:exception>\n  <s:message>The destination node already exists, and the overwrite header is set to false</s:message>\n  <s:header>Overwrite</s:header>\n</d:error>\n'
parent 5e61ca19
......@@ -45,7 +45,7 @@ import tempfile
try:
import webdav3.client as wc
from webdav3.exceptions import RemoteResourceNotFound
from webdav3.exceptions import RemoteResourceNotFound, ResponseErrorCode
except ImportError:
print(
"cannot find webdav.client, try `apt install python3-webdavclient` or `pip install webdavclient`",
......@@ -372,7 +372,14 @@ class ProcessingClient(wc.Client):
if not self.dryrun:
self.mkdir(folder + "/dangerzone")
self.mkdir(folder + "/dangerzone/processing")
self.move(remote_path_from=folder + "/" + path, remote_path_to=remote_processing_path)
try:
self.move(remote_path_from=folder + "/" + path, remote_path_to=remote_processing_path)
except ResponseErrorCode as e:
# https://datatracker.ietf.org/doc/html/rfc7232#section-4.2
# actually used in WebDAV to show the file already exists
if e.code == 412:
logging.warning("file already being processed, skipping")
return
with tempfile.TemporaryDirectory() as tmpdir:
# TODO: sanitize path for local use
local_path = tmpdir + "/danger/" + path
......@@ -403,10 +410,23 @@ class ProcessingClient(wc.Client):
)
if not self.dryrun:
self.mkdir(folder + "/dangerzone/rejected")
self.move(
remote_path_from=remote_processing_path,
remote_path_to=remote_rejected_path,
)
try:
self.move(
remote_path_from=remote_processing_path,
remote_path_to=remote_rejected_path,
)
except ResponseErrorCode as e:
# https://datatracker.ietf.org/doc/html/rfc7232#section-4.2
# actually used in WebDAV to show the file already exists
if e.code == 412:
# rejected already exists, fall back
# to delete the processing version
# altogether
#
# XXX: we actually lose data here
# which isn't nice. maybe we should
# find a unique filename instead?
self.clean(remote_processing_path)
return
# 6. on success, upload the sanitized file to a safe/
......@@ -427,10 +447,23 @@ class ProcessingClient(wc.Client):
)
if not self.dryrun:
self.mkdir(folder + "/dangerzone/processed")
self.move(
remote_path_from=remote_processing_path,
remote_path_to=remote_processed_path,
)
try:
self.move(
remote_path_from=remote_processing_path,
remote_path_to=remote_processed_path,
)
except ResponseErrorCode as e:
# https://datatracker.ietf.org/doc/html/rfc7232#section-4.2
# actually used in WebDAV to show the file already exists
if e.code == 412:
# rejected already exists, fall back
# to delete the processing version
# altogether
#
# XXX: we actually lose data here
# which isn't nice. maybe we should
# find a unique filename instead?
self.clean(remote_processing_path)
if __name__ == "__main__":
......
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