From c793e56769b7f11843bd8bdcac4000c929c5fd69 Mon Sep 17 00:00:00 2001 From: "Phyks (Lucas Verney)" Date: Wed, 12 Oct 2016 17:03:49 -0400 Subject: [PATCH] Add a `/clean` route Add a route to clean temporary downloaded files (actually deletes all temporary folders created by cozyweboob). --- README.md | 5 +++++ server.py | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/README.md b/README.md index abc24a2..9bda4c0 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,11 @@ It exposes a couple of routes: * the `/retrieve` route, which supports `POST` method and a single `path` `POST` parameter which is the path to the previously downloaded file to retrieve. + Note that this route will not delete the temporary file whose content has + been retrieved, and you should delete it manually. + +* the `/clean` route, which will delete all temporary downloaded files. This + route will return a JSON list of deleted folders. **IMPORTANT:** Note this small webserver is **not** production ready and only here as a proof of concept and to be used in a controlled development diff --git a/server.py b/server.py index 889168d..b3fd757 100755 --- a/server.py +++ b/server.py @@ -4,6 +4,7 @@ HTTP server wrapper around weboob """ import logging import os +import shutil import tempfile from bottle import post, request, route, run, static_file @@ -40,6 +41,29 @@ def retrieve_view(): download=True) +@route("/clean") +def clean_view(): + """ + Delete all the temporary downloaded files. These are the + "cozyweboob-*-tmp" folders in your system tmp dir. + """ + sys_tmp_dir = tempfile.gettempdir() + tmp_dirs = [ + x + for x in os.listdir(sys_tmp_dir) + if os.path.isdir(os.path.join(sys_tmp_dir, x)) + ] + removed_dirs = [] + for tmp_dir in tmp_dirs: + if tmp_dir.startswith("cozyweboob-") and tmp_dir.endswith("-tmp"): + tmp_dir = os.path.join(sys_tmp_dir, tmp_dir) + removed_dirs.append(tmp_dir) + shutil.rmtree(tmp_dir) + return { + "removed_dirs": removed_dirs + } + + @route("/list") def list_view(): """