Tracks
- Track.IsPublished (migration marks existing tracks as published). New
tracks are created unpublished.
- PUT /tracks/{id}/published and PUT /tracks/published change the flag;
publishing requires completed audio processing (the batch endpoint
reports skipped tracks).
- An unpublished track is only visible to its owner. A WherePublished /
VisibleTo filter is applied to search, genres, listening history, last
listened, likes, mixes, album and playlist contents and counts, recently
listened albums, public user tracks, track details and stream tickets.
- isPublished is part of the track responses, GET /tracks/users/{userId}
accepts a published filter and status-counts reports published and
unpublished totals.
- CreateTrackRequest.pictureIntentId is optional: without it the track
uses the album cover or the preset picture and no picture processing
runs. CreateTrackResourcesEvent carries nullable picture fields, so
queues must be drained before deploying.
- POST /tracks/upload-urls accepts an audios list and one optional picture
shared by all of them, and returns an intent per file. The upload intent
limits are raised so a batch fits.
Albums
- AlbumType (Album, EP, Single) on create, update and responses.
- CreateAlbumRequest.trackIds creates the album and links the tracks in a
single save.
121 lines
4.9 KiB
C#
121 lines
4.9 KiB
C#
using ErrorOr;
|
|
using Mediator;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
using Musify.Application.Configuration;
|
|
using Musify.Application.Contracts;
|
|
using Musify.Application.Events;
|
|
using Musify.Application.Services;
|
|
using Musify.Application.Shared;
|
|
using Musify.Application.Tracks.Responses;
|
|
using Musify.Domain.Entities;
|
|
using Musify.Domain.ValueObjects;
|
|
|
|
namespace Musify.Application.Tracks
|
|
{
|
|
public record CreateTrackCommand(
|
|
long UserId,
|
|
string Title,
|
|
Guid? PictureIntentId,
|
|
Guid AudioIntentId,
|
|
IReadOnlyCollection<Genre> Tags,
|
|
bool IsExplicit = false)
|
|
: ICommand<ErrorOr<TrackApplicationResponse>>;
|
|
|
|
public class CreateTrackCommandHandler(
|
|
IDatabase database,
|
|
IEventBus eventBus,
|
|
UploadIntentValidator uploadIntentValidator,
|
|
ILogger<CreateTrackCommandHandler> logger,
|
|
TrackConfiguration trackConfiguration)
|
|
: ICommandHandler<CreateTrackCommand, ErrorOr<TrackApplicationResponse>>
|
|
{
|
|
public async ValueTask<ErrorOr<TrackApplicationResponse>> Handle(CreateTrackCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var user = await database.Users
|
|
.SingleOrDefaultAsync(u => u.Id == request.UserId, cancellationToken);
|
|
if (user == null)
|
|
{
|
|
logger.LogWarning("User {UserId} not found", request.UserId);
|
|
return Error.NotFound(description: $"User {request.UserId} not found");
|
|
}
|
|
|
|
var distinctTags = request.Tags.Distinct().ToList();
|
|
|
|
if (request.PictureIntentId == request.AudioIntentId)
|
|
return Error.Validation(description: "Picture and audio must use different upload intents.");
|
|
|
|
UploadIntent? pictureIntent = null;
|
|
if (request.PictureIntentId is { } pictureIntentId)
|
|
{
|
|
var pictureValidation = await uploadIntentValidator.ValidateAndLoadAsync(
|
|
pictureIntentId, request.UserId, UploadIntentPurpose.TrackPicture, cancellationToken);
|
|
if (pictureValidation.IsError)
|
|
return pictureValidation.Errors;
|
|
|
|
pictureIntent = pictureValidation.Value;
|
|
}
|
|
|
|
var audioValidation = await uploadIntentValidator.ValidateAndLoadAsync(
|
|
request.AudioIntentId, request.UserId, UploadIntentPurpose.TrackAudio, cancellationToken);
|
|
if (audioValidation.IsError)
|
|
return audioValidation.Errors;
|
|
|
|
var audioIntent = audioValidation.Value;
|
|
|
|
var finalPictureKey = pictureIntent == null
|
|
? null
|
|
: trackConfiguration.Routes.BuildOriginalPicturePath(request.UserId, pictureIntent.ObjectName);
|
|
var finalAudioKey = trackConfiguration.Routes.BuildOriginalAudioPath(request.UserId, audioIntent.ObjectName);
|
|
var audioProcessedFolderKey = trackConfiguration.Routes.BuildProcessedAudioPath(Guid.NewGuid().ToString());
|
|
|
|
var trackEntity = new Track
|
|
{
|
|
Title = request.Title.Trim(),
|
|
NormalizedTitle = TextNormalizer.Normalize(request.Title),
|
|
OwnerUserId = user.Id,
|
|
Owner = user,
|
|
IsExplicit = request.IsExplicit,
|
|
Pictures = new TrackPictures
|
|
{
|
|
OriginalName = pictureIntent?.ObjectName,
|
|
SmallName = trackConfiguration.Routes.PresetSmallPicture,
|
|
MediumName = trackConfiguration.Routes.PresetMediumPicture,
|
|
LargeName = trackConfiguration.Routes.PresetLargePicture,
|
|
ProcessingStatus = pictureIntent == null ? ProcessingStatus.Completed : ProcessingStatus.Pending
|
|
},
|
|
Audio = new TrackAudio
|
|
{
|
|
OriginalName = audioIntent.ObjectName,
|
|
TranscodeStatus = ProcessingStatus.Pending
|
|
}
|
|
};
|
|
|
|
trackEntity.Tags = distinctTags
|
|
.Select(tag => new TrackTag { TrackId = trackEntity.Id, Tag = tag })
|
|
.ToList();
|
|
|
|
await database.Tracks.AddAsync(trackEntity, cancellationToken);
|
|
|
|
var sizes = trackConfiguration.PicturesSizes.ToImageSizes(trackConfiguration.Routes);
|
|
await eventBus.PublishAsync(
|
|
new CreateTrackResourcesEvent(
|
|
trackEntity.Id,
|
|
pictureIntent?.Id,
|
|
audioIntent.Id,
|
|
audioIntent.Bucket,
|
|
pictureIntent?.Key,
|
|
finalPictureKey,
|
|
audioIntent.Key,
|
|
finalAudioKey,
|
|
audioProcessedFolderKey,
|
|
sizes),
|
|
cancellationToken);
|
|
|
|
await database.SaveChangesAsync(cancellationToken);
|
|
|
|
return TrackApplicationResponse.FromEntity(trackEntity, listensCount: 0);
|
|
}
|
|
}
|
|
}
|