From 219103129daeb899605572f34ec86741f525abb5 Mon Sep 17 00:00:00 2001 From: FinnStutzenstein Date: Thu, 20 Feb 2020 10:18:35 +0100 Subject: [PATCH] Export mediafiles to the media service --- .travis.yml | 6 +-- openslides/mediafiles/management/__init__.py | 0 .../management/commands/__init__.py | 0 .../management/commands/export_mediafiles.py | 52 +++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 openslides/mediafiles/management/__init__.py create mode 100644 openslides/mediafiles/management/commands/__init__.py create mode 100644 openslides/mediafiles/management/commands/export_mediafiles.py diff --git a/.travis.yml b/.travis.yml index 2432d19ca..5561447b5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -85,7 +85,7 @@ matrix: - "3.6" script: - mypy openslides/ tests/ - - pytest --cov --cov-fail-under=73 + - pytest --cov --cov-fail-under=72 - name: "Server: Tests Python 3.7" language: python @@ -96,7 +96,7 @@ matrix: - isort --check-only --diff --recursive openslides tests - black --check --diff --target-version py36 openslides tests - mypy openslides/ tests/ - - pytest --cov --cov-fail-under=73 + - pytest --cov --cov-fail-under=72 - name: "Server: Tests Python 3.8" language: python @@ -107,7 +107,7 @@ matrix: - isort --check-only --diff --recursive openslides tests - black --check --diff --target-version py36 openslides tests - mypy openslides/ tests/ - - pytest --cov --cov-fail-under=73 + - pytest --cov --cov-fail-under=72 - name: "Client: Linting" language: node_js diff --git a/openslides/mediafiles/management/__init__.py b/openslides/mediafiles/management/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/openslides/mediafiles/management/commands/__init__.py b/openslides/mediafiles/management/commands/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/openslides/mediafiles/management/commands/export_mediafiles.py b/openslides/mediafiles/management/commands/export_mediafiles.py new file mode 100644 index 000000000..bd9629071 --- /dev/null +++ b/openslides/mediafiles/management/commands/export_mediafiles.py @@ -0,0 +1,52 @@ +import mimetypes +from typing import cast + +from django.core.management.base import BaseCommand + +from openslides.mediafiles.models import Mediafile + + +class Command(BaseCommand): + help = "Exports all mediafiles for the external media service." + + def add_arguments(self, parser): + parser.add_argument( + "--path", + default="mediafiles.sql", + help="Path for the mediafile sql file. (Default: mediafiles.sql).", + ) + + def handle(self, *args, **options): + path = cast(str, options.get("path")) + + mediafile_count = 0 + with open(path, "w") as f: + f.write("-- Generated file to import into the media service db\n") + + for mediafile in Mediafile.objects.filter(is_directory=False): + mediafile_count += 1 + id = mediafile.id + mimetype = mimetypes.guess_type(mediafile.mediafile.name)[0] + f.write("\nINSERT INTO mediafile_data (id, mimetype, data) VALUES ") + f.write(f"({id}, '{mimetype}', decode('") + + file_handle = open(mediafile.mediafile.path, "rb") + for chunk in self.read_in_chunks(file_handle): + f.write(chunk.hex()) + f.write("', 'hex'));\n") + + self.stdout.write( + self.style.SUCCESS( + f"All {mediafile_count} mediafiles successfully exported into {path}." + ) + ) + + def read_in_chunks(self, file_handle, chunk_size=1024): + """ + Generator to read a file piece by piece. Default chunk size is 1K + """ + while True: + data = file_handle.read(chunk_size) + if not data: + break + yield data