utils.py 4.06 KB
Newer Older
1
2
import os
import pathlib
Eliot Berriot's avatar
Eliot Berriot committed
3
import mimetypes
4

Eliot Berriot's avatar
Eliot Berriot committed
5
6
import magic
import mutagen
7
import pydub
8

9
10
11
12
13
from django.conf import settings
from django.core.cache import cache
from django.db.models import F

from funkwhale_api.common import throttling
14
15
16
from funkwhale_api.common.search import get_fts_query  # noqa
from funkwhale_api.common.search import get_query  # noqa
from funkwhale_api.common.search import normalize_query  # noqa
17
18
19


def guess_mimetype(f):
20
    b = min(1000000, f.size)
Eliot Berriot's avatar
Eliot Berriot committed
21
    t = magic.from_buffer(f.read(b), mime=True)
Eliot Berriot's avatar
Eliot Berriot committed
22
    if not t.startswith("audio/"):
23
24
25
26
27
28
29
30
31
32
33
34
        t = guess_mimetype_from_name(f.name)

    return t


def guess_mimetype_from_name(name):
    # failure, we try guessing by extension
    mt, _ = mimetypes.guess_type(name)
    if mt:
        t = mt
    else:
        t = EXTENSION_TO_MIMETYPE.get(name.split(".")[-1])
Eliot Berriot's avatar
Eliot Berriot committed
35
    return t
36
37
38


def compute_status(jobs):
Eliot Berriot's avatar
Eliot Berriot committed
39
40
    statuses = jobs.order_by().values_list("status", flat=True).distinct()
    errored = any([status == "errored" for status in statuses])
41
    if errored:
Eliot Berriot's avatar
Eliot Berriot committed
42
43
        return "errored"
    pending = any([status == "pending" for status in statuses])
44
    if pending:
Eliot Berriot's avatar
Eliot Berriot committed
45
46
        return "pending"
    return "finished"
47
48


49
AUDIO_EXTENSIONS_AND_MIMETYPE = [
50
    # keep the most correct mimetype for each extension at the bottom
51
    ("mp3", "audio/mp3"),
52
53
54
    ("mp3", "audio/mpeg3"),
    ("mp3", "audio/x-mp3"),
    ("mp3", "audio/mpeg"),
55
    ("ogg", "video/ogg"),
Eliot Berriot's avatar
Eliot Berriot committed
56
    ("ogg", "audio/ogg"),
57
    ("opus", "audio/opus"),
58
59
    ("aac", "audio/x-m4a"),
    ("m4a", "audio/x-m4a"),
Eliot Berriot's avatar
Eliot Berriot committed
60
    ("flac", "audio/x-flac"),
61
    ("flac", "audio/flac"),
Philipp Wolfer's avatar
Philipp Wolfer committed
62
63
64
65
    ("aif", "audio/aiff"),
    ("aif", "audio/x-aiff"),
    ("aiff", "audio/aiff"),
    ("aiff", "audio/x-aiff"),
66
67
68
69
70
]

EXTENSION_TO_MIMETYPE = {ext: mt for ext, mt in AUDIO_EXTENSIONS_AND_MIMETYPE}
MIMETYPE_TO_EXTENSION = {mt: ext for ext, mt in AUDIO_EXTENSIONS_AND_MIMETYPE}

71
72
73
74
SUPPORTED_EXTENSIONS = list(
    sorted(set([ext for ext, _ in AUDIO_EXTENSIONS_AND_MIMETYPE]))
)

75

76
def get_ext_from_type(mimetype):
77
78
79
80
    return MIMETYPE_TO_EXTENSION.get(mimetype)


def get_type_from_ext(extension):
Eliot Berriot's avatar
Eliot Berriot committed
81
    if extension.startswith("."):
82
83
84
        # we remove leading dot
        extension = extension[1:]
    return EXTENSION_TO_MIMETYPE.get(extension)
85
86
87
88


def get_audio_file_data(f):
    data = mutagen.File(f)
89
90
    if not data:
        return
91
    d = {}
92
    d["bitrate"] = getattr(data.info, "bitrate", 0)
Eliot Berriot's avatar
Eliot Berriot committed
93
    d["length"] = data.info.length
94
95

    return d
96
97
98
99
100
101
102
103
104
105


def get_actor_from_request(request):
    actor = None
    if hasattr(request, "actor"):
        actor = request.actor
    elif request.user.is_authenticated:
        actor = request.user.actor

    return actor
106
107
108
109
110


def transcode_file(input, output, input_format, output_format, **kwargs):
    with input.open("rb"):
        audio = pydub.AudioSegment.from_file(input, format=input_format)
111
112
113
114
    return transcode_audio(audio, output, output_format, **kwargs)


def transcode_audio(audio, output, output_format, **kwargs):
115
116
    with output.open("wb"):
        return audio.export(output, format=output_format, **kwargs)
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138


def increment_downloads_count(upload, user, wsgi_request):
    ident = throttling.get_ident(user=user, request=wsgi_request)
    cache_key = "downloads_count:upload-{}:{}-{}".format(
        upload.pk, ident["type"], ident["id"]
    )

    value = cache.get(cache_key)
    if value:
        # download already tracked
        return

    upload.downloads_count = F("downloads_count") + 1
    upload.track.downloads_count = F("downloads_count") + 1

    upload.save(update_fields=["downloads_count"])
    upload.track.save(update_fields=["downloads_count"])

    duration = max(upload.duration or 0, settings.MIN_DELAY_BETWEEN_DOWNLOADS_COUNT)

    cache.set(cache_key, 1, duration)
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156


def browse_dir(root, path):
    if ".." in path:
        raise ValueError("Relative browsing is not allowed")

    root = pathlib.Path(root)
    real_path = root / path

    dirs = []
    files = []
    for el in sorted(os.listdir(real_path)):
        if os.path.isdir(real_path / el):
            dirs.append({"name": el, "dir": True})
        else:
            files.append({"name": el, "dir": False})

    return dirs + files