Merge pull request #5230 from FinnStutzenstein/exportMediafiles
Export mediafiles to the media service
This commit is contained in:
commit
3ad8944b9c
@ -85,7 +85,7 @@ matrix:
|
|||||||
- "3.6"
|
- "3.6"
|
||||||
script:
|
script:
|
||||||
- mypy openslides/ tests/
|
- mypy openslides/ tests/
|
||||||
- pytest --cov --cov-fail-under=73
|
- pytest --cov --cov-fail-under=72
|
||||||
|
|
||||||
- name: "Server: Tests Python 3.7"
|
- name: "Server: Tests Python 3.7"
|
||||||
language: python
|
language: python
|
||||||
@ -96,7 +96,7 @@ matrix:
|
|||||||
- isort --check-only --diff --recursive openslides tests
|
- isort --check-only --diff --recursive openslides tests
|
||||||
- black --check --diff --target-version py36 openslides tests
|
- black --check --diff --target-version py36 openslides tests
|
||||||
- mypy openslides/ tests/
|
- mypy openslides/ tests/
|
||||||
- pytest --cov --cov-fail-under=73
|
- pytest --cov --cov-fail-under=72
|
||||||
|
|
||||||
- name: "Server: Tests Python 3.8"
|
- name: "Server: Tests Python 3.8"
|
||||||
language: python
|
language: python
|
||||||
@ -107,7 +107,7 @@ matrix:
|
|||||||
- isort --check-only --diff --recursive openslides tests
|
- isort --check-only --diff --recursive openslides tests
|
||||||
- black --check --diff --target-version py36 openslides tests
|
- black --check --diff --target-version py36 openslides tests
|
||||||
- mypy openslides/ tests/
|
- mypy openslides/ tests/
|
||||||
- pytest --cov --cov-fail-under=73
|
- pytest --cov --cov-fail-under=72
|
||||||
|
|
||||||
- name: "Client: Linting"
|
- name: "Client: Linting"
|
||||||
language: node_js
|
language: node_js
|
||||||
|
0
openslides/mediafiles/management/__init__.py
Normal file
0
openslides/mediafiles/management/__init__.py
Normal 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
|
Loading…
Reference in New Issue
Block a user