different backends can be selected

This commit is contained in:
boyska 2024-10-22 13:11:47 +02:00
parent f5f487c084
commit 3cb9439adf
2 changed files with 39 additions and 4 deletions

View file

@ -36,8 +36,14 @@ class RcloneBackend:
"""
self.remote_name = remote_name
def actual_path(self, destination_path: str):
return destination_path
def exists(self, destination_path: bytes):
destination = b"%s:%s" % (self.remote_name.encode("utf8"), self.actual_path(destination_path))
destination = b"%s:%s" % (
self.remote_name.encode("utf8"),
self.actual_path(destination_path),
)
cmd = ["rclone", "--quiet", "lsjson", destination]
try:
output = subprocess.check_output(cmd)
@ -58,6 +64,12 @@ class RcloneBackend:
subprocess.run(cmd, check=True)
class ArchiveBackend(RcloneBackend):
# XXX: there should be a way to "reserve" a bucket
# with that, we could now the URL before copying
pass
class ArkiwiBackend(RcloneBackend):
def __init__(self, remote_name: str, prefix: str):
super().__init__(remote_name)
@ -77,6 +89,9 @@ class ArkiwiBackend(RcloneBackend):
)
return f"https://www.arkiwi.org/path64/{path}/redirect"
def exists(self, destination_path: bytes):
return False
def copy(self, filename: Path, destination_path: bytes) -> str:
"""
returns the URL
@ -95,6 +110,13 @@ class ArkiwiBackend(RcloneBackend):
return url
BACKENDS = {
"arkiwi.org": ArkiwiBackend,
"archive.org": ArchiveBackend,
"default": ArkiwiBackend,
}
class Carichello:
def __init__(self):
self.parser = self.get_parser()
@ -127,12 +149,24 @@ class Carichello:
self.args = self.parser.parse_args()
with self.args.config.open() as buf:
self.config = json.load(buf)
self.config.setdefault("type", "default")
BackendCls = BACKENDS[self.config["type"]]
now = datetime.datetime.now()
dest_directory = f"/{now.year}/{now.month}"
dest_file = f"{dest_directory}/{self.args.file.name}".encode("utf8")
backend = ArkiwiBackend(self.config["remote"], self.config["prefix"])
url = backend.path_to_url(dest_file)
LOG.info("file %s would be uploaded to %s", str(self.args.file), url)
if (
BackendCls == ArkiwiBackend
): # XXX: reimplementa in modo che questo if non sia necessario
backend = BackendCls(self.config["remote"], self.config["prefix"])
else:
backend = BackendCls(self.config["remote"])
if hasattr(backend, "path_to_url"):
url = backend.path_to_url(dest_file)
LOG.info("file %s would be uploaded to %s", str(self.args.file), url)
else:
url = None
LOG.info("file %s would be uploaded", str(self.args.file))
zenity = Popen(
[
"zenity",

View file

@ -1,4 +1,5 @@
{
"type": "arkiwi",
"remote": "arkiwiOndaRossa",
"prefix": "Radio_Onda_Rossa"
}