diff --git a/parsing.py b/parsing.py index 353426e..a411b37 100644 --- a/parsing.py +++ b/parsing.py @@ -169,7 +169,7 @@ class MediaFileParser(GenericFileParser): "audio/mid", "audio/mpeg", "audio/mp4", "audio/x-aiff", "audio/ogg", "audio/vorbis" "audio/x-realaudio", "audio/x-wav", "audio/flac", "audio/x-monkeys-audio", "audio/wav", "audio/wave", - "audio/x-wav", "audio/x-ms-wma" + "audio/x-wav", "audio/x-ms-wma", "audio/x-flac", ] def parse(self, full_path: str): diff --git a/run.py b/run.py index 0502779..34a735a 100644 --- a/run.py +++ b/run.py @@ -13,6 +13,7 @@ from crawler import TaskManager from search import Search from storage import Directory, Option, Task, User from storage import LocalStorage, DuplicateDirectoryException, DuplicateUserException +from thumbnail import ThumbnailGenerator app = Flask(__name__) app.secret_key = "A very secret key" @@ -174,7 +175,6 @@ def document(doc_id): @app.route("/dl/") def serve_file(doc_id): - doc = search.get_doc(doc_id)["_source"] directory = storage.dirs()[doc["directory"]] @@ -205,16 +205,24 @@ def thumb(doc_id): doc = search.get_doc(doc_id) if doc is not None: - - tn_path = os.path.join("static/thumbnails/", str(doc["_source"]["directory"]), doc_id) - print(tn_path) + dest_path = "static/thumbnails/" + str(doc["_source"]["directory"]) + os.makedirs(dest_path, exist_ok=True) + tn_path = os.path.join(dest_path, doc_id) if os.path.isfile(tn_path): return send_file(tn_path) - default_thumbnail = BytesIO() - Image.new("RGB", (255, 128), (0, 0, 0)).save(default_thumbnail, "JPEG") - default_thumbnail.seek(0) - return send_file(default_thumbnail, "image/jpeg") + directory = storage.dirs()[doc["_source"]["directory"]] + extension = "" if doc["_source"]["extension"] is None or doc["_source"]["extension"] == "" else \ + "." + doc["_source"]["extension"] + full_path = os.path.join(directory.path, + doc["_source"]["path"], + doc["_source"]["name"] + extension) + tn_generator = ThumbnailGenerator(int(directory.get_option("ThumbnailSize")), + int(directory.get_option("ThumbnailQuality")), + directory.get_option("ThumbnailColor")) + tn_generator.generate(full_path, tn_path, doc["_source"]["mime"]) + if os.path.isfile(tn_path): + return send_file(tn_path) default_thumbnail = BytesIO() Image.new("RGB", (255, 128), (0, 0, 0)).save(default_thumbnail, "JPEG") diff --git a/thumbnail.py b/thumbnail.py index 6182faa..e9e4a6d 100644 --- a/thumbnail.py +++ b/thumbnail.py @@ -24,9 +24,9 @@ class ThumbnailGenerator: if mime is None: return + tmpfile = dest_path + "_tmp" if mime == "image/svg+xml" and config.cairosvg: - tmpfile = dest_path + "_tmp" try: p = Process(target=cairosvg.svg2png, kwargs={"url": path, "write_to": tmpfile}) p.start() @@ -54,15 +54,15 @@ class ThumbnailGenerator: try: (ffmpeg. input(path) - .output("tmp", vframes=1, f="image2", loglevel="error") + .output(tmpfile, vframes=1, f="image2", loglevel="error") .run() ) - self.generate_image("tmp", dest_path) + self.generate_image(tmpfile, dest_path) except Exception: print("Couldn't make thumbnail for " + path) - if os.path.exists("tmp"): - os.remove("tmp") + if os.path.exists(tmpfile): + os.remove(tmpfile) def worker(self, in_q: Queue, counter: Value, dest_path, directory): @@ -114,25 +114,28 @@ class ThumbnailGenerator: def generate_image(self, path, dest_path): - with open(path, "rb") as image_file: - with Image.open(image_file) as image: + try: + with open(path, "rb") as image_file: + with Image.open(image_file) as image: - # https://stackoverflow.com/questions/43978819 - if image.mode == "I;16": - image.mode = "I" - image.point(lambda i: i * (1. / 256)).convert('L') + # https://stackoverflow.com/questions/43978819 + if image.mode == "I;16": + image.mode = "I" + image.point(lambda i: i * (1. / 256)).convert('L') - image.thumbnail(self.size, Image.BICUBIC) - canvas = Image.new("RGB", image.size, self.color) + image.thumbnail(self.size, Image.BICUBIC) + canvas = Image.new("RGB", image.size, self.color) - if image.mode in ('RGBA', 'LA') or (image.mode == 'P' and 'transparency' in image.info): + if image.mode in ('RGBA', 'LA') or (image.mode == 'P' and 'transparency' in image.info): - try: - canvas.paste(image, mask=image.split()[-1]) - except ValueError: + try: + canvas.paste(image, mask=image.split()[-1]) + except ValueError: + canvas.paste(image) + else: canvas.paste(image) - else: - canvas.paste(image) - canvas.save(dest_path, "JPEG", quality=self.quality, optimize=True) - canvas.close() + canvas.save(dest_path, "JPEG", quality=self.quality, optimize=True) + canvas.close() + except Exception as e: + print(e)