utils.py 3.43 KB
Newer Older
Eliot Berriot's avatar
Eliot Berriot committed
1
import mimetypes
2

Eliot Berriot's avatar
Eliot Berriot committed
3
4
import magic
import mutagen
5
import pydub
6

7
8
9
10
11
from django.conf import settings
from django.core.cache import cache
from django.db.models import F

from funkwhale_api.common import throttling
12
13
14
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
15
16
17


def guess_mimetype(f):
18
    b = min(1000000, f.size)
Eliot Berriot's avatar
Eliot Berriot committed
19
    t = magic.from_buffer(f.read(b), mime=True)
Eliot Berriot's avatar
Eliot Berriot committed
20
    if not t.startswith("audio/"):
Eliot Berriot's avatar
Eliot Berriot committed
21
        # failure, we try guessing by extension
22
        mt, _ = mimetypes.guess_type(f.name)
Eliot Berriot's avatar
Eliot Berriot committed
23
24
        if mt:
            t = mt
25
26
        else:
            t = EXTENSION_TO_MIMETYPE.get(f.name.split(".")[-1])
Eliot Berriot's avatar
Eliot Berriot committed
27
    return t
28
29
30


def compute_status(jobs):
Eliot Berriot's avatar
Eliot Berriot committed
31
32
    statuses = jobs.order_by().values_list("status", flat=True).distinct()
    errored = any([status == "errored" for status in statuses])
33
    if errored:
Eliot Berriot's avatar
Eliot Berriot committed
34
35
        return "errored"
    pending = any([status == "pending" for status in statuses])
36
    if pending:
Eliot Berriot's avatar
Eliot Berriot committed
37
38
        return "pending"
    return "finished"
39
40


41
AUDIO_EXTENSIONS_AND_MIMETYPE = [
42
    # keep the most correct mimetype for each extension at the bottom
43
    ("mp3", "audio/mp3"),
44
45
46
    ("mp3", "audio/mpeg3"),
    ("mp3", "audio/x-mp3"),
    ("mp3", "audio/mpeg"),
47
    ("ogg", "video/ogg"),
Eliot Berriot's avatar
Eliot Berriot committed
48
    ("ogg", "audio/ogg"),
49
    ("opus", "audio/opus"),
50
51
    ("aac", "audio/x-m4a"),
    ("m4a", "audio/x-m4a"),
Eliot Berriot's avatar
Eliot Berriot committed
52
    ("flac", "audio/x-flac"),
53
    ("flac", "audio/flac"),
54
55
56
57
58
]

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}

59
60
61
62
SUPPORTED_EXTENSIONS = list(
    sorted(set([ext for ext, _ in AUDIO_EXTENSIONS_AND_MIMETYPE]))
)

63

64
def get_ext_from_type(mimetype):
65
66
67
68
    return MIMETYPE_TO_EXTENSION.get(mimetype)


def get_type_from_ext(extension):
Eliot Berriot's avatar
Eliot Berriot committed
69
    if extension.startswith("."):
70
71
72
        # we remove leading dot
        extension = extension[1:]
    return EXTENSION_TO_MIMETYPE.get(extension)
73
74
75
76


def get_audio_file_data(f):
    data = mutagen.File(f)
77
78
    if not data:
        return
79
    d = {}
80
    d["bitrate"] = getattr(data.info, "bitrate", 0)
Eliot Berriot's avatar
Eliot Berriot committed
81
    d["length"] = data.info.length
82
83

    return d
84
85
86
87
88
89
90
91
92
93


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
94
95
96
97
98


def transcode_file(input, output, input_format, output_format, **kwargs):
    with input.open("rb"):
        audio = pydub.AudioSegment.from_file(input, format=input_format)
99
100
101
102
    return transcode_audio(audio, output, output_format, **kwargs)


def transcode_audio(audio, output, output_format, **kwargs):
103
104
    with output.open("wb"):
        return audio.export(output, format=output_format, **kwargs)
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126


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)