Commit b5231d63 authored by kezzle's avatar kezzle
Browse files

Use os.path.join instead of string concatenation

parent 660ee858
......@@ -38,6 +38,7 @@ import argparse
from getpass import getpass
import logging
import os
from os.path import join
import re
import shutil
import subprocess
......@@ -136,7 +137,7 @@ class Sanitizer:
for path in dirs:
self.sanitize_dir(path)
root = root.rstrip("/")
safe_dir = self.safe_dir + "/" + os.path.basename(root)
safe_dir = join(self.safe_dir, os.path.basename(root))
logging.info(
"processing %d files in dir %s to safe_dir: %s",
len(files),
......@@ -152,7 +153,10 @@ class Sanitizer:
os.makedirs(safe_dir, exist_ok=True)
logging.info("sanitizing file %s into %s", path, safe_dir)
container_id, output = self.runner.run(
docker_args=["--volume", os.path.abspath(path) + ":/tmp/input_file"],
docker_args=[
"--volume",
join(os.path.abspath(path) + ":", "tmp", "input_file"),
],
args=["document-to-pixels-unpriv"],
)
......@@ -326,7 +330,7 @@ def main():
logging.info("authenticated with webdav %s", args.location)
with tempfile.TemporaryDirectory() as tmpdir:
safe_dir = tmpdir + "/safe/"
safe_dir = join(tmpdir, "safe")
client.sanitizer = Sanitizer(
safe_dir, args.image, args.verbose or args.debug, dryrun=args.dryrun
)
......@@ -361,7 +365,7 @@ class ProcessingClient(wc.Client):
# 2. when a file is found, move it to a dangerzone/processing folder as
# an ad-hoc locking mechanis
try:
listing = self.list(folder + "/" + path)
listing = self.list(join(folder, path))
except RemoteResourceNotFound as e:
# XXX: Nextcloud has this weird thing where it shows the
# actual folder we're listing as the first element, but
......@@ -374,13 +378,13 @@ class ProcessingClient(wc.Client):
logging.info("sanitizing %s %s", folder, path)
# non-empty folder or regular file
if len(listing) > 1 or not path.endswith("/"):
remote_processing_path = folder + "/dangerzone/processing/" + path
remote_processing_path = join(folder, "dangerzone", "processing", path)
logging.info("moving %s to %s before processing", path, remote_processing_path)
if not self.dryrun:
self.mkdir(folder + "/dangerzone")
self.mkdir(folder + "/dangerzone/processing")
self.mkdir(join(folder, "dangerzone"))
self.mkdir(join(folder, "dangerzone", "processing"))
try:
self.move(remote_path_from=folder + "/" + path, remote_path_to=remote_processing_path)
self.move(remote_path_from=join(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
......@@ -389,11 +393,11 @@ class ProcessingClient(wc.Client):
return
with tempfile.TemporaryDirectory() as tmpdir:
# TODO: sanitize path for local use
local_path = tmpdir + "/danger/" + path
local_path = join(tmpdir, "danger", path)
# 3. download the file locally
logging.info("downloading %s to %s", remote_processing_path, local_path)
if not self.dryrun:
os.mkdir(tmpdir + "/danger/")
os.mkdir(join(tmpdir, "danger"))
self.download_sync(
remote_path=remote_processing_path, local_path=local_path
)
......@@ -408,7 +412,7 @@ class ProcessingClient(wc.Client):
# TODO: error handling:
# 5. on failure, delete the failed file locally, and
# move it to a dangerzone/rejected folder remotely
remote_rejected_path = folder + "/dangerzone/rejected/" + path
remote_rejected_path = join(folder, "dangerzone", "rejected", path)
logging.warning(
"cannot process %s (%s), moving to %s",
path,
......@@ -416,7 +420,7 @@ class ProcessingClient(wc.Client):
remote_rejected_path,
)
if not self.dryrun:
self.mkdir(folder + "/dangerzone/rejected")
self.mkdir(join(folder, "dangerzone", "rejected"))
try:
self.move(
remote_path_from=remote_processing_path,
......@@ -438,8 +442,8 @@ class ProcessingClient(wc.Client):
# 6. on success, upload the sanitized file to a safe/
# folder, move the original to dangerzone/processed
remote_safe_path = folder + "/safe/" + path
local_safe_path = self.sanitizer.safe_dir + "/" + path
remote_safe_path = join(folder, "safe", path)
local_safe_path = join(self.sanitizer.safe_dir, "/", path)
logging.info(
"uploading %s to %s", local_safe_path, remote_safe_path
)
......@@ -449,12 +453,13 @@ class ProcessingClient(wc.Client):
remote_path=remote_safe_path,
local_path=local_safe_path,
)
remote_processed_path = folder + "/dangerzone/processed/" + path
remote_processed_path = join(folder, "dangerzone", "processed", path)
logging.info(
"renaming %s to %s", remote_processing_path, remote_processed_path,
)
if not self.dryrun:
self.mkdir(folder + "/dangerzone/processed")
# TODO: turn "dangerzone/processed" into a constant
self.mkdir(join(folder, "dangerzone", "processed"))
try:
self.move(
remote_path_from=remote_processing_path,
......
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