tasks.py 30.8 KB
Newer Older
Eliot Berriot's avatar
Eliot Berriot committed
1
import collections
2
import datetime
3
import logging
4
5
import os

6
7
from django.utils import timezone
from django.db import transaction
Eliot Berriot's avatar
Eliot Berriot committed
8
from django.db.models import F, Q
9
10
from django.dispatch import receiver

11
from musicbrainzngs import ResponseError
12
from requests.exceptions import RequestException
13

14
from funkwhale_api import musicbrainz
15
from funkwhale_api.common import channels, preferences
16
from funkwhale_api.common import utils as common_utils
Eliot Berriot's avatar
Eliot Berriot committed
17
from funkwhale_api.federation import routes
18
from funkwhale_api.federation import library as lb
19
from funkwhale_api.federation import utils as federation_utils
20
from funkwhale_api.tags import models as tags_models
21
from funkwhale_api.tags import tasks as tags_tasks
Eliot Berriot's avatar
Eliot Berriot committed
22
from funkwhale_api.taskapp import celery
23

Eliot Berriot's avatar
Eliot Berriot committed
24
from . import licenses
Eliot Berriot's avatar
Eliot Berriot committed
25
from . import models
26
27
from . import metadata
from . import signals
28

29
30
logger = logging.getLogger(__name__)

31

32
def populate_album_cover(album, source=None, replace=False):
Eliot Berriot's avatar
Eliot Berriot committed
33
    if album.attachment_cover and not replace:
34
        return
Eliot Berriot's avatar
Eliot Berriot committed
35
36
37
38
39
    if source and source.startswith("file://"):
        # let's look for a cover in the same directory
        path = os.path.dirname(source.replace("file://", "", 1))
        logger.info("[Album %s] scanning covers from %s", album.pk, path)
        cover = get_cover_from_fs(path)
40
41
42
43
44
45
46
        return common_utils.attach_file(album, "attachment_cover", cover)
    if album.mbid:
        logger.info(
            "[Album %s] Fetching cover from musicbrainz release %s",
            album.pk,
            str(album.mbid),
        )
47
        try:
48
            image_data = musicbrainz.api.images.get_front(str(album.mbid))
Eliot Berriot's avatar
Eliot Berriot committed
49
50
51
52
        except ResponseError as exc:
            logger.warning(
                "[Album %s] cannot fetch cover from musicbrainz: %s", album.pk, str(exc)
            )
53
54
55
56
57
58
59
        else:
            return common_utils.attach_file(
                album,
                "attachment_cover",
                {"content": image_data, "mimetype": "image/jpeg"},
                fetch=True,
            )
Eliot Berriot's avatar
Eliot Berriot committed
60

61

62
63
IMAGE_TYPES = [("jpg", "image/jpeg"), ("jpeg", "image/jpeg"), ("png", "image/png")]
FOLDER_IMAGE_NAMES = ["cover", "folder"]
64
65
66
67


def get_cover_from_fs(dir_path):
    if os.path.exists(dir_path):
68
69
70
71
72
73
74
75
76
        for name in FOLDER_IMAGE_NAMES:
            for e, m in IMAGE_TYPES:
                cover_path = os.path.join(dir_path, "{}.{}".format(name, e))
                if not os.path.exists(cover_path):
                    logger.debug("Cover %s does not exists", cover_path)
                    continue
                with open(cover_path, "rb") as c:
                    logger.info("Found cover at %s", cover_path)
                    return {"mimetype": m, "content": c.read()}
77
78


Eliot Berriot's avatar
Eliot Berriot committed
79
@celery.app.task(name="music.start_library_scan")
80
81
82
83
@celery.require_instance(
    models.LibraryScan.objects.select_related().filter(status="pending"), "library_scan"
)
def start_library_scan(library_scan):
Eliot Berriot's avatar
Eliot Berriot committed
84
85
86
87
88
89
    try:
        data = lb.get_library_data(library_scan.library.fid, actor=library_scan.actor)
    except Exception:
        library_scan.status = "errored"
        library_scan.save(update_fields=["status", "modification_date"])
        raise
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    library_scan.modification_date = timezone.now()
    library_scan.status = "scanning"
    library_scan.total_files = data["totalItems"]
    library_scan.save(update_fields=["status", "modification_date", "total_files"])
    scan_library_page.delay(library_scan_id=library_scan.pk, page_url=data["first"])


@celery.app.task(
    name="music.scan_library_page",
    retry_backoff=60,
    max_retries=5,
    autoretry_for=[RequestException],
)
@celery.require_instance(
    models.LibraryScan.objects.select_related().filter(status="scanning"),
    "library_scan",
)
def scan_library_page(library_scan, page_url):
    data = lb.get_library_page(library_scan.library, page_url, library_scan.actor)
Eliot Berriot's avatar
Eliot Berriot committed
109
    uploads = []
110
111

    for item_serializer in data["items"]:
Eliot Berriot's avatar
Eliot Berriot committed
112
113
        upload = item_serializer.save(library=library_scan.library)
        uploads.append(upload)
114

Eliot Berriot's avatar
Eliot Berriot committed
115
    library_scan.processed_files = F("processed_files") + len(uploads)
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    library_scan.modification_date = timezone.now()
    update_fields = ["modification_date", "processed_files"]

    next_page = data.get("next")
    fetch_next = next_page and next_page != page_url

    if not fetch_next:
        update_fields.append("status")
        library_scan.status = "finished"
    library_scan.save(update_fields=update_fields)

    if fetch_next:
        scan_library_page.delay(library_scan_id=library_scan.pk, page_url=next_page)


Eliot Berriot's avatar
Eliot Berriot committed
131
def getter(data, *keys, default=None):
132
    if not data:
Eliot Berriot's avatar
Eliot Berriot committed
133
        return default
134
135
    v = data
    for k in keys:
136
137
138
        try:
            v = v[k]
        except KeyError:
Eliot Berriot's avatar
Eliot Berriot committed
139
            return default
140
141
142
143

    return v


Eliot Berriot's avatar
Eliot Berriot committed
144
class UploadImportError(ValueError):
145
146
147
148
149
    def __init__(self, code):
        self.code = code
        super().__init__(code)


150
def fail_import(upload, error_code, detail=None, **fields):
Eliot Berriot's avatar
Eliot Berriot committed
151
152
    old_status = upload.import_status
    upload.import_status = "errored"
153
154
    upload.import_details = {"error_code": error_code, "detail": detail}
    upload.import_details.update(fields)
Eliot Berriot's avatar
Eliot Berriot committed
155
156
    upload.import_date = timezone.now()
    upload.save(update_fields=["import_details", "import_status", "import_date"])
Eliot Berriot's avatar
Eliot Berriot committed
157
158
159

    broadcast = getter(
        upload.import_metadata, "funkwhale", "config", "broadcast", default=True
160
    )
Eliot Berriot's avatar
Eliot Berriot committed
161
162
163
164
165
166
167
    if broadcast:
        signals.upload_import_status_updated.send(
            old_status=old_status,
            new_status=upload.import_status,
            upload=upload,
            sender=None,
        )
168
169


170
@celery.app.task(name="music.process_upload")
171
@celery.require_instance(
Eliot Berriot's avatar
Eliot Berriot committed
172
    models.Upload.objects.filter(import_status="pending").select_related(
173
        "library__actor__user", "library__channel__artist",
174
    ),
Eliot Berriot's avatar
Eliot Berriot committed
175
    "upload",
176
)
177
def process_upload(upload, update_denormalization=True):
178
179
180
181
    """
    Main handler to process uploads submitted by user and create the corresponding
    metadata (tracks/artists/albums) in our DB.
    """
182
183
    from . import serializers

184
185
186
187
188
189
    channel = upload.library.get_channel()
    # When upload is linked to a channel instead of a library
    # we willingly ignore the metadata embedded in the file itself
    # and rely on user metadata only
    use_file_metadata = channel is None

Eliot Berriot's avatar
Eliot Berriot committed
190
    import_metadata = upload.import_metadata or {}
191
192
    internal_config = {"funkwhale": import_metadata.get("funkwhale", {})}
    forced_values_serializer = serializers.ImportMetadataSerializer(
193
194
        data=import_metadata,
        context={"actor": upload.library.actor, "channel": channel},
195
196
197
198
199
    )
    if forced_values_serializer.is_valid():
        forced_values = forced_values_serializer.validated_data
    else:
        forced_values = {}
200
201
202
203
204
205
        if not use_file_metadata:
            detail = forced_values_serializer.errors
            metadata_dump = import_metadata
            return fail_import(
                upload, "invalid_metadata", detail=detail, file_metadata=metadata_dump
            )
206

207
    if channel:
208
209
        # ensure the upload is associated with the channel artist
        forced_values["artist"] = upload.library.channel.artist
210

Eliot Berriot's avatar
Eliot Berriot committed
211
    old_status = upload.import_status
212
    additional_data = {"upload_source": upload.source}
213

214
215
216
217
    if use_file_metadata:
        audio_file = upload.get_audio_file()

        m = metadata.Metadata(audio_file)
218
        try:
219
220
221
222
223
224
225
226
227
228
229
230
231
232
            serializer = metadata.TrackMetadataSerializer(data=m)
            serializer.is_valid()
        except Exception:
            fail_import(upload, "unknown_error")
            raise
        if not serializer.is_valid():
            detail = serializer.errors
            try:
                metadata_dump = m.all()
            except Exception as e:
                logger.warn("Cannot dump metadata for file %s: %s", audio_file, str(e))
            return fail_import(
                upload, "invalid_metadata", detail=detail, file_metadata=metadata_dump
            )
233

234
235
236
237
238
239
240
        final_metadata = collections.ChainMap(
            additional_data, serializer.validated_data, internal_config
        )
    else:
        final_metadata = collections.ChainMap(
            additional_data, forced_values, internal_config,
        )
241
    try:
Eliot Berriot's avatar
Eliot Berriot committed
242
        track = get_track_from_import_metadata(
243
            final_metadata, attributed_to=upload.library.actor, **forced_values
Eliot Berriot's avatar
Eliot Berriot committed
244
        )
Eliot Berriot's avatar
Eliot Berriot committed
245
246
    except UploadImportError as e:
        return fail_import(upload, e.code)
247
    except Exception:
Eliot Berriot's avatar
Eliot Berriot committed
248
249
        fail_import(upload, "unknown_error")
        raise
Eliot Berriot's avatar
Eliot Berriot committed
250

251
252
    # under some situations, we want to skip the import (
    # for instance if the user already owns the files)
Eliot Berriot's avatar
Eliot Berriot committed
253
254
    owned_duplicates = get_owned_duplicates(upload, track)
    upload.track = track
255
256

    if owned_duplicates:
Eliot Berriot's avatar
Eliot Berriot committed
257
258
        upload.import_status = "skipped"
        upload.import_details = {
259
260
261
            "code": "already_imported_in_owned_libraries",
            "duplicates": list(owned_duplicates),
        }
Eliot Berriot's avatar
Eliot Berriot committed
262
263
        upload.import_date = timezone.now()
        upload.save(
264
265
            update_fields=["import_details", "import_status", "import_date", "track"]
        )
Eliot Berriot's avatar
Eliot Berriot committed
266
        signals.upload_import_status_updated.send(
267
            old_status=old_status,
Eliot Berriot's avatar
Eliot Berriot committed
268
269
            new_status=upload.import_status,
            upload=upload,
270
271
272
273
274
            sender=None,
        )
        return

    # all is good, let's finalize the import
Eliot Berriot's avatar
Eliot Berriot committed
275
    audio_data = upload.get_audio_data()
276
    if audio_data:
Eliot Berriot's avatar
Eliot Berriot committed
277
278
279
280
281
282
        upload.duration = audio_data["duration"]
        upload.size = audio_data["size"]
        upload.bitrate = audio_data["bitrate"]
    upload.import_status = "finished"
    upload.import_date = timezone.now()
    upload.save(
283
284
285
286
287
288
289
290
291
        update_fields=[
            "track",
            "import_status",
            "import_date",
            "size",
            "duration",
            "bitrate",
        ]
    )
292
293
    if channel:
        common_utils.update_modification_date(channel.artist)
294

295
296
297
298
299
300
301
    if update_denormalization:
        models.TrackActor.create_entries(
            library=upload.library,
            upload_and_track_ids=[(upload.pk, upload.track_id)],
            delete_existing=False,
        )

302
    # update album cover, if needed
303
    if track.album and not track.album.attachment_cover:
304
305
        populate_album_cover(
            track.album, source=final_metadata.get("upload_source"),
306
307
        )

Eliot Berriot's avatar
Eliot Berriot committed
308
    broadcast = getter(
309
        internal_config, "funkwhale", "config", "broadcast", default=True
310
    )
Eliot Berriot's avatar
Eliot Berriot committed
311
312
313
314
315
316
317
318
    if broadcast:
        signals.upload_import_status_updated.send(
            old_status=old_status,
            new_status=upload.import_status,
            upload=upload,
            sender=None,
        )
    dispatch_outbox = getter(
319
        internal_config, "funkwhale", "config", "dispatch_outbox", default=True
Eliot Berriot's avatar
Eliot Berriot committed
320
    )
Eliot Berriot's avatar
Eliot Berriot committed
321
322
323
324
    if dispatch_outbox:
        routes.outbox.dispatch(
            {"type": "Create", "object": {"type": "Audio"}}, context={"upload": upload}
        )
325
326


327
328
329
def get_cover(obj, field):
    cover = obj.get(field)
    if cover:
330
331
332
333
334
335
        try:
            url = cover["url"]
        except KeyError:
            url = cover["href"]

        return {"mimetype": cover["mediaType"], "url": url}
336
337


Eliot Berriot's avatar
Eliot Berriot committed
338
def federation_audio_track_to_metadata(payload, references):
Eliot Berriot's avatar
Eliot Berriot committed
339
340
341
342
343
344
    """
    Given a valid payload as returned by federation.serializers.TrackSerializer.validated_data,
    returns a correct metadata payload for use with get_track_from_import_metadata.
    """
    new_data = {
        "title": payload["name"],
345
        "position": payload.get("position") or 1,
346
        "disc_number": payload.get("disc"),
Eliot Berriot's avatar
Eliot Berriot committed
347
348
        "license": payload.get("license"),
        "copyright": payload.get("copyright"),
349
        "description": payload.get("description"),
Eliot Berriot's avatar
Eliot Berriot committed
350
        "attributed_to": references.get(payload.get("attributedTo")),
351
352
        "mbid": str(payload.get("musicbrainzId"))
        if payload.get("musicbrainzId")
Eliot Berriot's avatar
Eliot Berriot committed
353
        else None,
354
        "cover_data": get_cover(payload, "image"),
355
356
357
358
        "album": {
            "title": payload["album"]["name"],
            "fdate": payload["album"]["published"],
            "fid": payload["album"]["id"],
359
            "description": payload["album"].get("description"),
Eliot Berriot's avatar
Eliot Berriot committed
360
            "attributed_to": references.get(payload["album"].get("attributedTo")),
361
362
363
            "mbid": str(payload["album"]["musicbrainzId"])
            if payload["album"].get("musicbrainzId")
            else None,
364
            "cover_data": get_cover(payload["album"], "cover"),
365
            "release_date": payload["album"].get("released"),
366
            "tags": [t["name"] for t in payload["album"].get("tags", []) or []],
367
368
369
370
371
            "artists": [
                {
                    "fid": a["id"],
                    "name": a["name"],
                    "fdate": a["published"],
372
                    "cover_data": get_cover(a, "image"),
373
                    "description": a.get("description"),
Eliot Berriot's avatar
Eliot Berriot committed
374
                    "attributed_to": references.get(a.get("attributedTo")),
375
                    "mbid": str(a["musicbrainzId"]) if a.get("musicbrainzId") else None,
376
                    "tags": [t["name"] for t in a.get("tags", []) or []],
377
378
379
380
381
382
383
384
385
                }
                for a in payload["album"]["artists"]
            ],
        },
        "artists": [
            {
                "fid": a["id"],
                "name": a["name"],
                "fdate": a["published"],
386
                "description": a.get("description"),
Eliot Berriot's avatar
Eliot Berriot committed
387
                "attributed_to": references.get(a.get("attributedTo")),
388
                "mbid": str(a["musicbrainzId"]) if a.get("musicbrainzId") else None,
389
                "tags": [t["name"] for t in a.get("tags", []) or []],
390
                "cover_data": get_cover(a, "image"),
391
392
393
            }
            for a in payload["artists"]
        ],
Eliot Berriot's avatar
Eliot Berriot committed
394
395
396
        # federation
        "fid": payload["id"],
        "fdate": payload["published"],
397
        "tags": [t["name"] for t in payload.get("tags", []) or []],
Eliot Berriot's avatar
Eliot Berriot committed
398
399
    }
    return new_data
400
401


Eliot Berriot's avatar
Eliot Berriot committed
402
def get_owned_duplicates(upload, track):
403
404
405
    """
    Ensure we skip duplicate tracks to avoid wasting user/instance storage
    """
Eliot Berriot's avatar
Eliot Berriot committed
406
    owned_libraries = upload.library.actor.libraries.all()
407
    return (
Eliot Berriot's avatar
Eliot Berriot committed
408
        models.Upload.objects.filter(
409
410
            track__isnull=False, library__in=owned_libraries, track=track
        )
Eliot Berriot's avatar
Eliot Berriot committed
411
        .exclude(pk=upload.pk)
412
413
414
415
        .values_list("uuid", flat=True)
    )


Eliot Berriot's avatar
Eliot Berriot committed
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
def get_best_candidate_or_create(model, query, defaults, sort_fields):
    """
    Like queryset.get_or_create() but does not crash if multiple objects
    are returned on the get() call
    """
    candidates = model.objects.filter(query)
    if candidates:

        return sort_candidates(candidates, sort_fields)[0], False

    return model.objects.create(**defaults), True


def sort_candidates(candidates, important_fields):
    """
    Given a list of objects and a list of fields,
    will return a sorted list of those objects by score.

    Score is higher for objects that have a non-empty attribute
    that is also present in important fields::

        artist1 = Artist(mbid=None, fid=None)
        artist2 = Artist(mbid="something", fid=None)

        # artist2 has a mbid, so is sorted first
        assert sort_candidates([artist1, artist2], ['mbid'])[0] == artist2

    Only supports string fields.
    """

    # map each fields to its score, giving a higher score to first fields
    fields_scores = {f: i + 1 for i, f in enumerate(sorted(important_fields))}
    candidates_with_scores = []
    for candidate in candidates:
        current_score = 0
        for field, score in fields_scores.items():
            v = getattr(candidate, field, "")
            if v:
                current_score += score

        candidates_with_scores.append((candidate, current_score))

    return [c for c, s in reversed(sorted(candidates_with_scores, key=lambda v: v[1]))]


461
@transaction.atomic
462
463
464
465
def get_track_from_import_metadata(
    data, update_cover=False, attributed_to=None, **forced_values
):
    track = _get_track(data, attributed_to=attributed_to, **forced_values)
Eliot Berriot's avatar
Eliot Berriot committed
466
    if update_cover and track and not track.album.attachment_cover:
467
        populate_album_cover(track.album, source=data.get("upload_source"))
468
469
470
    return track


471
472
473
474
475
476
def truncate(v, length):
    if v is None:
        return v
    return v[:length]


477
def _get_track(data, attributed_to=None, **forced_values):
Eliot Berriot's avatar
Eliot Berriot committed
478
479
480
481
482
483
484
485
486
487
488
489
490
    track_uuid = getter(data, "funkwhale", "track", "uuid")

    if track_uuid:
        # easy case, we have a reference to a uuid of a track that
        # already exists in our database
        try:
            track = models.Track.objects.get(uuid=track_uuid)
        except models.Track.DoesNotExist:
            raise UploadImportError(code="track_uuid_not_found")

        return track

    from_activity_id = data.get("from_activity_id", None)
491
492
493
    track_mbid = (
        forced_values["mbid"] if "mbid" in forced_values else data.get("mbid", None)
    )
494
495
496
497
498
    try:
        album_mbid = getter(data, "album", "mbid")
    except TypeError:
        # album is forced
        album_mbid = None
Eliot Berriot's avatar
Eliot Berriot committed
499
500
501
    track_fid = getter(data, "fid")

    query = None
502
503

    if album_mbid and track_mbid:
Eliot Berriot's avatar
Eliot Berriot committed
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
        query = Q(mbid=track_mbid, album__mbid=album_mbid)

    if track_fid:
        query = query | Q(fid=track_fid) if query else Q(fid=track_fid)

    if query:
        # second easy case: we have a (track_mbid, album_mbid) pair or
        # a federation uuid we can check on
        try:
            return sort_candidates(models.Track.objects.filter(query), ["mbid", "fid"])[
                0
            ]
        except IndexError:
            pass

    # get / create artist and album artist
520
521
    if "artist" in forced_values:
        artist = forced_values["artist"]
Eliot Berriot's avatar
Eliot Berriot committed
522
    else:
523
524
525
526
527
528
529
530
531
532
533
534
        artists = getter(data, "artists", default=[])
        artist_data = artists[0]
        artist_mbid = artist_data.get("mbid", None)
        artist_fid = artist_data.get("fid", None)
        artist_name = truncate(artist_data["name"], models.MAX_LENGTHS["ARTIST_NAME"])

        if artist_mbid:
            query = Q(mbid=artist_mbid)
        else:
            query = Q(name__iexact=artist_name)
        if artist_fid:
            query |= Q(fid=artist_fid)
Eliot Berriot's avatar
Eliot Berriot committed
535
        defaults = {
536
537
538
            "name": artist_name,
            "mbid": artist_mbid,
            "fid": artist_fid,
Eliot Berriot's avatar
Eliot Berriot committed
539
            "from_activity_id": from_activity_id,
540
            "attributed_to": artist_data.get("attributed_to", attributed_to),
Eliot Berriot's avatar
Eliot Berriot committed
541
        }
542
543
        if artist_data.get("fdate"):
            defaults["creation_date"] = artist_data.get("fdate")
Eliot Berriot's avatar
Eliot Berriot committed
544

545
        artist, created = get_best_candidate_or_create(
Eliot Berriot's avatar
Eliot Berriot committed
546
            models.Artist, query, defaults=defaults, sort_fields=["mbid", "fid"]
547
548
        )
        if created:
549
            tags_models.add_tags(artist, *artist_data.get("tags", []))
550
551
552
            common_utils.attach_content(
                artist, "description", artist_data.get("description")
            )
553
554
555
            common_utils.attach_file(
                artist, "attachment_cover", artist_data.get("cover_data")
            )
556

557
558
    if "album" in forced_values:
        album = forced_values["album"]
559
    else:
560
561
        if "artist" in forced_values:
            album_artist = forced_values["artist"]
562
        else:
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
            album_artists = getter(data, "album", "artists", default=artists) or artists
            album_artist_data = album_artists[0]
            album_artist_name = truncate(
                album_artist_data.get("name"), models.MAX_LENGTHS["ARTIST_NAME"]
            )
            if album_artist_name == artist_name:
                album_artist = artist
            else:
                query = Q(name__iexact=album_artist_name)
                album_artist_mbid = album_artist_data.get("mbid", None)
                album_artist_fid = album_artist_data.get("fid", None)
                if album_artist_mbid:
                    query |= Q(mbid=album_artist_mbid)
                if album_artist_fid:
                    query |= Q(fid=album_artist_fid)
                defaults = {
                    "name": album_artist_name,
                    "mbid": album_artist_mbid,
                    "fid": album_artist_fid,
                    "from_activity_id": from_activity_id,
                    "attributed_to": album_artist_data.get(
                        "attributed_to", attributed_to
                    ),
                }
                if album_artist_data.get("fdate"):
                    defaults["creation_date"] = album_artist_data.get("fdate")

                album_artist, created = get_best_candidate_or_create(
                    models.Artist, query, defaults=defaults, sort_fields=["mbid", "fid"]
                )
                if created:
                    tags_models.add_tags(
                        album_artist, *album_artist_data.get("tags", [])
                    )
                    common_utils.attach_content(
                        album_artist,
                        "description",
                        album_artist_data.get("description"),
                    )
                    common_utils.attach_file(
                        album_artist,
                        "attachment_cover",
                        album_artist_data.get("cover_data"),
                    )

        # get / create album
        if "album" in data:
            album_data = data["album"]
            album_title = truncate(
                album_data["title"], models.MAX_LENGTHS["ALBUM_TITLE"]
            )
            album_fid = album_data.get("fid", None)

            if album_mbid:
                query = Q(mbid=album_mbid)
            else:
                query = Q(title__iexact=album_title, artist=album_artist)

            if album_fid:
                query |= Q(fid=album_fid)
623
            defaults = {
624
625
626
627
628
                "title": album_title,
                "artist": album_artist,
                "mbid": album_mbid,
                "release_date": album_data.get("release_date"),
                "fid": album_fid,
629
                "from_activity_id": from_activity_id,
630
                "attributed_to": album_data.get("attributed_to", attributed_to),
631
            }
632
633
            if album_data.get("fdate"):
                defaults["creation_date"] = album_data.get("fdate")
634

635
636
            album, created = get_best_candidate_or_create(
                models.Album, query, defaults=defaults, sort_fields=["mbid", "fid"]
637
638
            )
            if created:
639
                tags_models.add_tags(album, *album_data.get("tags", []))
640
                common_utils.attach_content(
641
                    album, "description", album_data.get("description")
642
                )
643
                common_utils.attach_file(
644
                    album, "attachment_cover", album_data.get("cover_data")
645
                )
646
        else:
647
            album = None
Eliot Berriot's avatar
Eliot Berriot committed
648
    # get / create track
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
    track_title = (
        forced_values["title"]
        if "title" in forced_values
        else truncate(data["title"], models.MAX_LENGTHS["TRACK_TITLE"])
    )
    position = (
        forced_values["position"]
        if "position" in forced_values
        else data.get("position", 1)
    )
    disc_number = (
        forced_values["disc_number"]
        if "disc_number" in forced_values
        else data.get("disc_number")
    )
    license = (
        forced_values["license"]
        if "license" in forced_values
        else licenses.match(data.get("license"), data.get("copyright"))
    )
    copyright = (
        forced_values["copyright"]
        if "copyright" in forced_values
        else truncate(data.get("copyright"), models.MAX_LENGTHS["COPYRIGHT"])
    )
674
675
676
677
678
679
680
681
    description = (
        {"text": forced_values["description"], "content_type": "text/markdown"}
        if "description" in forced_values
        else data.get("description")
    )
    cover_data = (
        forced_values["cover"] if "cover" in forced_values else data.get("cover_data")
    )
682

683
684
685
686
687
688
689
    query = Q(
        title__iexact=track_title,
        artist=artist,
        album=album,
        position=position,
        disc_number=disc_number,
    )
Eliot Berriot's avatar
Eliot Berriot committed
690
    if track_mbid:
691
692
693
694
        if album_mbid:
            query |= Q(mbid=track_mbid, album__mbid=album_mbid)
        else:
            query |= Q(mbid=track_mbid)
Eliot Berriot's avatar
Eliot Berriot committed
695
696
    if track_fid:
        query |= Q(fid=track_fid)
697

Eliot Berriot's avatar
Eliot Berriot committed
698
699
700
701
702
    defaults = {
        "title": track_title,
        "album": album,
        "mbid": track_mbid,
        "artist": artist,
703
        "position": position,
704
        "disc_number": disc_number,
Eliot Berriot's avatar
Eliot Berriot committed
705
706
        "fid": track_fid,
        "from_activity_id": from_activity_id,
Eliot Berriot's avatar
Eliot Berriot committed
707
        "attributed_to": data.get("attributed_to", attributed_to),
708
709
        "license": license,
        "copyright": copyright,
Eliot Berriot's avatar
Eliot Berriot committed
710
711
712
713
    }
    if data.get("fdate"):
        defaults["creation_date"] = data.get("fdate")

714
    track, created = get_best_candidate_or_create(
Eliot Berriot's avatar
Eliot Berriot committed
715
        models.Track, query, defaults=defaults, sort_fields=["mbid", "fid"]
716
    )
Eliot Berriot's avatar
Eliot Berriot committed
717

718
    if created:
719
720
721
722
        tags = (
            forced_values["tags"] if "tags" in forced_values else data.get("tags", [])
        )
        tags_models.add_tags(track, *tags)
723
724
        common_utils.attach_content(track, "description", description)
        common_utils.attach_file(track, "attachment_cover", cover_data)
725

726
727
728
    return track


Eliot Berriot's avatar
Eliot Berriot committed
729
730
731
@receiver(signals.upload_import_status_updated)
def broadcast_import_status_update_to_owner(old_status, new_status, upload, **kwargs):
    user = upload.library.actor.get_user()
732
733
    if not user:
        return
Eliot Berriot's avatar
Eliot Berriot committed
734

Eliot Berriot's avatar
Eliot Berriot committed
735
736
    from . import serializers

737
738
739
740
741
742
743
744
    group = "user.{}.imports".format(user.pk)
    channels.group_send(
        group,
        {
            "type": "event.send",
            "text": "",
            "data": {
                "type": "import.status_updated",
Eliot Berriot's avatar
Eliot Berriot committed
745
                "upload": serializers.UploadForOwnerSerializer(upload).data,
746
747
748
749
750
                "old_status": old_status,
                "new_status": new_status,
            },
        },
    )
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766


@celery.app.task(name="music.clean_transcoding_cache")
def clean_transcoding_cache():
    delay = preferences.get("music__transcoding_cache_duration")
    if delay < 1:
        return  # cache clearing disabled
    limit = timezone.now() - datetime.timedelta(minutes=delay)
    candidates = (
        models.UploadVersion.objects.filter(
            (Q(accessed_date__lt=limit) | Q(accessed_date=None))
        )
        .only("audio_file", "id")
        .order_by("id")
    )
    return candidates.delete()
767
768


769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
@celery.app.task(name="music.albums_set_tags_from_tracks")
@transaction.atomic
def albums_set_tags_from_tracks(ids=None, dry_run=False):
    qs = models.Album.objects.filter(tagged_items__isnull=True).order_by("id")
    qs = federation_utils.local_qs(qs)
    qs = qs.values_list("id", flat=True)
    if ids is not None:
        qs = qs.filter(pk__in=ids)
    data = tags_tasks.get_tags_from_foreign_key(
        ids=qs, foreign_key_model=models.Track, foreign_key_attr="album",
    )
    logger.info("Found automatic tags for %s albums…", len(data))
    if dry_run:
        logger.info("Running in dry-run mode, not commiting")
        return

    tags_tasks.add_tags_batch(
        data, model=models.Album,
    )
    return data


@celery.app.task(name="music.artists_set_tags_from_tracks")
@transaction.atomic
def artists_set_tags_from_tracks(ids=None, dry_run=False):
    qs = models.Artist.objects.filter(tagged_items__isnull=True).order_by("id")
    qs = federation_utils.local_qs(qs)
    qs = qs.values_list("id", flat=True)
    if ids is not None:
        qs = qs.filter(pk__in=ids)
    data = tags_tasks.get_tags_from_foreign_key(
        ids=qs, foreign_key_model=models.Track, foreign_key_attr="artist",
    )
    logger.info("Found automatic tags for %s artists…", len(data))
    if dry_run:
        logger.info("Running in dry-run mode, not commiting")
        return

    tags_tasks.add_tags_batch(
        data, model=models.Artist,
    )
    return data


813
814
815
816
817
818
819
def get_prunable_tracks(
    exclude_favorites=True, exclude_playlists=True, exclude_listenings=True
):
    """
    Returns a list of tracks with no associated uploads,
    excluding the one that were listened/favorited/included in playlists.
    """
820
821
822
823
824
    purgeable_tracks_with_upload = (
        models.Upload.objects.exclude(track=None)
        .filter(import_status="skipped")
        .values("track")
    )
825
    queryset = models.Track.objects.all()
826
827
828
    queryset = queryset.filter(
        Q(uploads__isnull=True) | Q(pk__in=purgeable_tracks_with_upload)
    )
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
    if exclude_favorites:
        queryset = queryset.filter(track_favorites__isnull=True)
    if exclude_playlists:
        queryset = queryset.filter(playlist_tracks__isnull=True)
    if exclude_listenings:
        queryset = queryset.filter(listenings__isnull=True)

    return queryset


def get_prunable_albums():
    return models.Album.objects.filter(tracks__isnull=True)


def get_prunable_artists():
    return models.Artist.objects.filter(tracks__isnull=True, albums__isnull=True)
Eliot Berriot's avatar
Eliot Berriot committed
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859


def update_library_entity(obj, data):
    """
    Given an obj and some updated fields, will persist the changes on the obj
    and also check if the entity need to be aliased with existing objs (i.e
    if a mbid was added on the obj, and match another entity with the same mbid)
    """
    for key, value in data.items():
        setattr(obj, key, value)

    # Todo: handle integrity error on unique fields (such as MBID)
    obj.save(update_fields=list(data.keys()))

    return obj
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927


UPDATE_CONFIG = {
    "track": {
        "position": {},
        "title": {},
        "mbid": {},
        "disc_number": {},
        "copyright": {},
        "license": {
            "getter": lambda data, field: licenses.match(
                data.get("license"), data.get("copyright")
            )
        },
    },
    "album": {"title": {}, "mbid": {}, "release_date": {}},
    "artist": {"name": {}, "mbid": {}},
    "album_artist": {"name": {}, "mbid": {}},
}


@transaction.atomic
def update_track_metadata(audio_metadata, track):
    # XXX: implement this to support updating metadata when an imported files
    # is updated by an outside tool (e.g beets).
    serializer = metadata.TrackMetadataSerializer(data=audio_metadata)
    serializer.is_valid(raise_exception=True)
    new_data = serializer.validated_data

    to_update = [
        ("track", track, lambda data: data),
        ("album", track.album, lambda data: data["album"]),
        ("artist", track.artist, lambda data: data["artists"][0]),
        (
            "album_artist",
            track.album.artist if track.album else None,
            lambda data: data["album"]["artists"][0],
        ),
    ]
    for id, obj, data_getter in to_update:
        if not obj:
            continue
        obj_updated_fields = []
        try:
            obj_data = data_getter(new_data)
        except IndexError:
            continue
        for field, config in UPDATE_CONFIG[id].items():
            getter = config.get(
                "getter", lambda data, field: data[config.get("field", field)]
            )
            try:
                new_value = getter(obj_data, field)
            except KeyError:
                continue
            old_value = getattr(obj, field)
            if new_value == old_value:
                continue
            obj_updated_fields.append(field)
            setattr(obj, field, new_value)

        if obj_updated_fields:
            obj.save(update_fields=obj_updated_fields)

    if track.album and "album" in new_data and new_data["album"].get("cover_data"):
        common_utils.attach_file(
            track.album, "attachment_cover", new_data["album"].get("cover_data")
        )