Skip to content
Snippets Groups Projects

handle already existing remote files on move

Merged anarcat requested to merge error-handling into main
+ 43
10
@@ -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__":
Loading