Export mediafiles to the media service

This commit is contained in:
FinnStutzenstein 2020-02-20 10:18:35 +01:00
parent 758e059f9b
commit 219103129d
4 changed files with 55 additions and 3 deletions

View File

@ -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

View File

@ -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