[Feature] Youtube importing and refreshing implementation (ytdlp) (#1)
Reviewed-on: #1
This commit is contained in:
@ -1,6 +1,5 @@
|
||||
package com.bivashy.backend.composer.controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
@ -14,40 +13,51 @@ import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.bivashy.backend.composer.auth.CustomUserDetails;
|
||||
import com.bivashy.backend.composer.dto.importing.TrackProgressDTO;
|
||||
import com.bivashy.backend.composer.dto.track.AddLocalTrackRequest;
|
||||
import com.bivashy.backend.composer.dto.track.PlaylistTrackResponse;
|
||||
import com.bivashy.backend.composer.dto.track.TrackBulkReorderRequest;
|
||||
import com.bivashy.backend.composer.dto.track.TrackResponse;
|
||||
import com.bivashy.backend.composer.model.SourceTypes;
|
||||
import com.bivashy.backend.composer.dto.track.YoutubeTrackRequest;
|
||||
import com.bivashy.backend.composer.dto.track.service.AddLocalTrackParamsBuilder;
|
||||
import com.bivashy.backend.composer.exception.ImportTrackException;
|
||||
import com.bivashy.backend.composer.service.TrackService;
|
||||
import com.bivashy.backend.composer.service.importing.RedisProgressService;
|
||||
import com.bivashy.backend.composer.util.SimpleBlob.MultipartBlob;
|
||||
|
||||
@RestController
|
||||
public class TrackController {
|
||||
private final TrackService trackService;
|
||||
private final RedisProgressService redisProgressService;
|
||||
|
||||
public TrackController(TrackService trackService, RedisProgressService redisProgressService) {
|
||||
public TrackController(TrackService trackService) {
|
||||
this.trackService = trackService;
|
||||
this.redisProgressService = redisProgressService;
|
||||
}
|
||||
|
||||
@PostMapping(path = "/playlist/{playlistId}/track/local", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
public ResponseEntity<TrackResponse> addLocalTrack(
|
||||
@AuthenticationPrincipal CustomUserDetails user,
|
||||
@PathVariable Long playlistId,
|
||||
@ModelAttribute AddLocalTrackRequest request) throws IOException {
|
||||
TrackResponse response = trackService.addLocalTrack(user, playlistId, request);
|
||||
redisProgressService.saveProgress(new TrackProgressDTO(playlistId,
|
||||
response.trackId(),
|
||||
response.title(),
|
||||
response.fileFormat(),
|
||||
SourceTypes.FILE,
|
||||
100,
|
||||
null,
|
||||
System.currentTimeMillis(),
|
||||
user.getId()));
|
||||
@PathVariable long playlistId,
|
||||
@ModelAttribute AddLocalTrackRequest request) throws ImportTrackException {
|
||||
var params = AddLocalTrackParamsBuilder.builder()
|
||||
.blob(new MultipartBlob(request.source()))
|
||||
.build();
|
||||
TrackResponse response = trackService.addLocalTrack(user, playlistId, params);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
@PostMapping(path = "/playlist/{playlistId}/track/youtube/refresh/{sourceId}")
|
||||
public ResponseEntity<List<TrackResponse>> addYoutubeTrack(
|
||||
@AuthenticationPrincipal CustomUserDetails user,
|
||||
@PathVariable long playlistId,
|
||||
@PathVariable long sourceId) throws ImportTrackException {
|
||||
List<TrackResponse> response = trackService.refreshYoutubePlaylist(user, playlistId, sourceId);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
@PostMapping(path = "/playlist/{playlistId}/track/youtube")
|
||||
public ResponseEntity<List<TrackResponse>> addYoutubeTrack(
|
||||
@AuthenticationPrincipal CustomUserDetails user,
|
||||
@PathVariable long playlistId,
|
||||
@RequestBody YoutubeTrackRequest request) throws ImportTrackException {
|
||||
List<TrackResponse> response = trackService.addYoutubeTrack(user, playlistId, request);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
|
||||
@ -1,31 +1,34 @@
|
||||
package com.bivashy.backend.composer.controller.importing;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.ServerSentEvent;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import com.bivashy.backend.composer.auth.CustomUserDetails;
|
||||
import com.bivashy.backend.composer.dto.importing.ImportTrackKey;
|
||||
import com.bivashy.backend.composer.dto.importing.TrackProgressDTO;
|
||||
import com.bivashy.backend.composer.service.importing.RedisMessageSubscriber;
|
||||
import com.bivashy.backend.composer.service.importing.RedisProgressService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.bivashy.backend.composer.auth.CustomUserDetails;
|
||||
import com.bivashy.backend.composer.dto.importing.BaseTrackProgress;
|
||||
import com.bivashy.backend.composer.dto.importing.ImportTrackKey;
|
||||
import com.bivashy.backend.composer.service.importing.RedisMessageSubscriber;
|
||||
import com.bivashy.backend.composer.service.importing.RedisProgressService;
|
||||
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
|
||||
@RestController
|
||||
public class ProgressSSEController {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ProgressSSEController.class);
|
||||
|
||||
private final RedisProgressService redisProgressService;
|
||||
private final RedisMessageSubscriber redisSubscriber;
|
||||
private final Map<String, Sinks.Many<String>> sinks = new ConcurrentHashMap<>();
|
||||
private final Map<String, Sinks.Many<BaseTrackProgress>> sinks = new ConcurrentHashMap<>();
|
||||
|
||||
public ProgressSSEController(RedisProgressService redisProgressService,
|
||||
RedisMessageSubscriber redisSubscriber) {
|
||||
@ -34,7 +37,7 @@ public class ProgressSSEController {
|
||||
}
|
||||
|
||||
@GetMapping(value = "/importing/stream/{playlistId}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public Flux<ServerSentEvent<String>> streamProgress(
|
||||
public Flux<BaseTrackProgress> streamProgress(
|
||||
@PathVariable long playlistId,
|
||||
@AuthenticationPrincipal CustomUserDetails user,
|
||||
HttpServletResponse response) {
|
||||
@ -46,8 +49,8 @@ public class ProgressSSEController {
|
||||
|
||||
String connectionKey = ImportTrackKey.subscriptionKey(playlistId, userId);
|
||||
|
||||
Sinks.Many<String> sink = sinks.computeIfAbsent(connectionKey, k -> {
|
||||
Sinks.Many<String> newSink = Sinks.many().replay().latest();
|
||||
Sinks.Many<BaseTrackProgress> sink = sinks.computeIfAbsent(connectionKey, k -> {
|
||||
Sinks.Many<BaseTrackProgress> newSink = Sinks.many().replay().latest();
|
||||
|
||||
redisSubscriber.subscribeToPlaylist(playlistId, userId, message -> {
|
||||
newSink.tryEmitNext(message);
|
||||
@ -59,18 +62,14 @@ public class ProgressSSEController {
|
||||
redisProgressService.addActiveConnection(playlistId, userId);
|
||||
|
||||
return sink.asFlux()
|
||||
.map(data -> ServerSentEvent.<String>builder()
|
||||
.data(data)
|
||||
.event("progress-update")
|
||||
.build())
|
||||
.doFirst(() -> {
|
||||
try {
|
||||
List<TrackProgressDTO> existingProgresses = redisProgressService.getPlaylistProgress(playlistId,
|
||||
List<BaseTrackProgress> existingProgresses = redisProgressService.getPlaylistProgress(
|
||||
playlistId,
|
||||
userId);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
for (TrackProgressDTO progress : existingProgresses) {
|
||||
sink.tryEmitNext(mapper.writeValueAsString(progress));
|
||||
for (BaseTrackProgress progress : existingProgresses) {
|
||||
sink.tryEmitNext(progress);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@ -89,7 +88,7 @@ public class ProgressSSEController {
|
||||
}
|
||||
|
||||
private void cleanupConnection(Long playlistId, long userId,
|
||||
Sinks.Many<String> sink, String connectionKey) {
|
||||
Sinks.Many<BaseTrackProgress> sink, String connectionKey) {
|
||||
try {
|
||||
redisProgressService.removeActiveConnection(playlistId, userId);
|
||||
redisSubscriber.unsubscribeFromPlaylist(playlistId, userId);
|
||||
|
||||
@ -0,0 +1,58 @@
|
||||
package com.bivashy.backend.composer.dto.importing;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
|
||||
@JsonSubTypes({
|
||||
@JsonSubTypes.Type(value = PlaylistProgress.class, name = "PLAYLIST"),
|
||||
@JsonSubTypes.Type(value = SingleTrackProgress.class, name = "TRACK"),
|
||||
})
|
||||
public abstract class BaseTrackProgress {
|
||||
protected UUID id;
|
||||
protected long playlistId;
|
||||
protected long trackSourceId;
|
||||
protected long userId;
|
||||
|
||||
protected long timestamp;
|
||||
private ProgressEntryType type;
|
||||
|
||||
public BaseTrackProgress(long playlistId, long trackSourceId, long userId) {
|
||||
this.id = UUID.randomUUID();
|
||||
this.playlistId = playlistId;
|
||||
this.trackSourceId = trackSourceId;
|
||||
this.userId = userId;
|
||||
this.timestamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public ProgressEntryType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public long getPlaylistId() {
|
||||
return playlistId;
|
||||
}
|
||||
|
||||
public long getTrackSourceId() {
|
||||
return trackSourceId;
|
||||
}
|
||||
|
||||
protected void setType(ProgressEntryType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
}
|
||||
@ -5,8 +5,8 @@ public class ImportTrackKey {
|
||||
return String.format("progress:%d:%d", userId, playlistId);
|
||||
}
|
||||
|
||||
public static String trackKey(long playlistId, long trackId, long userId) {
|
||||
return String.format("track:%d:%d:%d", userId, playlistId, trackId);
|
||||
public static String trackKey(long playlistId, long trackSourceId, long userId) {
|
||||
return String.format("track:%d:%d:%d", userId, playlistId, trackSourceId);
|
||||
}
|
||||
|
||||
public static String redisChannelKey(long playlistId, long userId) {
|
||||
|
||||
@ -0,0 +1,48 @@
|
||||
package com.bivashy.backend.composer.dto.importing;
|
||||
|
||||
public class PlaylistProgress extends BaseTrackProgress {
|
||||
private String ytdlnStdout;
|
||||
private int overallProgress;
|
||||
private int trackCount;
|
||||
private ProgressStatus status;
|
||||
|
||||
PlaylistProgress() {
|
||||
super(0, 0, 0);
|
||||
}
|
||||
|
||||
public PlaylistProgress(long playlistId, long trackSourceId, long userId, int trackCount) {
|
||||
super(playlistId, trackSourceId, userId);
|
||||
this.setType(ProgressEntryType.PLAYLIST);
|
||||
this.status = ProgressStatus.LOADING;
|
||||
this.trackCount = trackCount;
|
||||
}
|
||||
|
||||
public String getYtdlnStdout() {
|
||||
return ytdlnStdout;
|
||||
}
|
||||
|
||||
public void setYtdlnStdout(String ytdlnStdout) {
|
||||
this.ytdlnStdout = ytdlnStdout;
|
||||
}
|
||||
|
||||
public int getOverallProgress() {
|
||||
return overallProgress;
|
||||
}
|
||||
|
||||
public void setOverallProgress(int overallProgress) {
|
||||
this.overallProgress = overallProgress;
|
||||
}
|
||||
|
||||
public ProgressStatus getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(ProgressStatus status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public int getTrackCount() {
|
||||
return trackCount;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
package com.bivashy.backend.composer.dto.importing;
|
||||
|
||||
public enum ProgressEntryType {
|
||||
PLAYLIST,
|
||||
TRACK,
|
||||
EXTERNAL_TRACK
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
package com.bivashy.backend.composer.dto.importing;
|
||||
|
||||
public enum ProgressStatus {
|
||||
LOADING, FINISHED
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
package com.bivashy.backend.composer.dto.importing;
|
||||
|
||||
public class SingleTrackProgress extends BaseTrackProgress {
|
||||
private String title;
|
||||
private String format;
|
||||
|
||||
SingleTrackProgress() {
|
||||
super(0, 0, 0);
|
||||
}
|
||||
|
||||
public SingleTrackProgress(long playlistId, long trackSourceId, long userId, String title, String format) {
|
||||
super(playlistId, trackSourceId, userId);
|
||||
this.setType(ProgressEntryType.TRACK);
|
||||
this.title = title;
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
}
|
||||
@ -1,116 +0,0 @@
|
||||
package com.bivashy.backend.composer.dto.importing;
|
||||
|
||||
public class TrackProgressDTO {
|
||||
private long playlistId;
|
||||
private long trackId;
|
||||
private String trackTitle;
|
||||
private String format;
|
||||
private String sourceType;
|
||||
private int progress;
|
||||
private String metadata;
|
||||
private Long timestamp;
|
||||
private long userId;
|
||||
|
||||
public TrackProgressDTO() {
|
||||
}
|
||||
|
||||
public TrackProgressDTO(long playlistId, long trackId, long userId) {
|
||||
this.playlistId = playlistId;
|
||||
this.trackId = trackId;
|
||||
this.userId = userId;
|
||||
this.timestamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public TrackProgressDTO(long playlistId,
|
||||
long trackId,
|
||||
String trackTitle,
|
||||
String format,
|
||||
String sourceType,
|
||||
int progress,
|
||||
String metadata,
|
||||
Long timestamp,
|
||||
long userId) {
|
||||
this.playlistId = playlistId;
|
||||
this.trackId = trackId;
|
||||
this.trackTitle = trackTitle;
|
||||
this.format = format;
|
||||
this.sourceType = sourceType;
|
||||
this.progress = progress;
|
||||
this.metadata = metadata;
|
||||
this.timestamp = timestamp;
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public long getPlaylistId() {
|
||||
return playlistId;
|
||||
}
|
||||
|
||||
public void setPlaylistId(long playlistId) {
|
||||
this.playlistId = playlistId;
|
||||
}
|
||||
|
||||
public long getTrackId() {
|
||||
return trackId;
|
||||
}
|
||||
|
||||
public void setTrackId(long trackId) {
|
||||
this.trackId = trackId;
|
||||
}
|
||||
|
||||
public String getTrackTitle() {
|
||||
return trackTitle;
|
||||
}
|
||||
|
||||
public void setTrackTitle(String trackTitle) {
|
||||
this.trackTitle = trackTitle;
|
||||
}
|
||||
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
public void setFormat(String format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public String getSourceType() {
|
||||
return sourceType;
|
||||
}
|
||||
|
||||
public void setSourceType(String sourceType) {
|
||||
this.sourceType = sourceType;
|
||||
}
|
||||
|
||||
public int getProgress() {
|
||||
return progress;
|
||||
}
|
||||
|
||||
public void setProgress(int progress) {
|
||||
this.progress = progress;
|
||||
}
|
||||
|
||||
public String getMetadata() {
|
||||
return metadata;
|
||||
}
|
||||
|
||||
public void setMetadata(String metadata) {
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
public Long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public void setTimestamp(Long timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
public long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
package com.bivashy.backend.composer.dto.track;
|
||||
|
||||
public record YoutubeTrackRequest(String youtubeUrl) {
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package com.bivashy.backend.composer.dto.track.service;
|
||||
|
||||
import com.bivashy.backend.composer.util.SimpleBlob;
|
||||
|
||||
import io.soabase.recordbuilder.core.RecordBuilder;
|
||||
|
||||
@RecordBuilder
|
||||
public record AddLocalTrackParams(SimpleBlob blob, String ytdlpMetadata, boolean includeProgressHistory) {
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package com.bivashy.backend.composer.exception;
|
||||
|
||||
public class ImportTrackException extends Exception {
|
||||
public ImportTrackException(String message, Exception cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public ImportTrackException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@ -28,7 +28,7 @@ public class Playlist {
|
||||
@JoinColumn(name = "owner_id", nullable = false)
|
||||
private User owner;
|
||||
|
||||
@Column(unique = true, nullable = false, length = 500)
|
||||
@Column(unique = true, nullable = false)
|
||||
private String title;
|
||||
|
||||
@Column(name = "created_at", nullable = false, updatable = false)
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
package com.bivashy.backend.composer.model;
|
||||
|
||||
public enum SourceMetadataType {
|
||||
YOUTUBE
|
||||
}
|
||||
@ -1,47 +1,5 @@
|
||||
package com.bivashy.backend.composer.model;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "source_type")
|
||||
public class SourceType {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false, length = 500)
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "sourceType", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
private Set<TrackSource> trackSources = new HashSet<>();
|
||||
|
||||
SourceType() {
|
||||
}
|
||||
|
||||
public SourceType(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Set<TrackSource> getTrackSources() {
|
||||
return trackSources;
|
||||
}
|
||||
|
||||
public enum SourceType {
|
||||
VIDEO, PLAYLIST, PLAYLIST_ITEM, FILE, URL
|
||||
}
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
package com.bivashy.backend.composer.model;
|
||||
|
||||
public class SourceTypes {
|
||||
public static final String AUDIO = "VIDEO";
|
||||
public static final String PLAYLIST = "PLAYLIST";
|
||||
public static final String FILE = "FILE";
|
||||
public static final String URL = "URL";
|
||||
}
|
||||
@ -4,15 +4,16 @@ import java.time.LocalDateTime;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.annotations.JdbcType;
|
||||
import org.hibernate.dialect.PostgreSQLEnumJdbcType;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.Enumerated;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@ -23,11 +24,12 @@ public class TrackSource {
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "source_url", nullable = false, length = 500)
|
||||
@Column(name = "source_url", nullable = false)
|
||||
private String sourceUrl;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "source_type_id", nullable = false)
|
||||
@Enumerated
|
||||
@Column(name = "source_type", nullable = false)
|
||||
@JdbcType(PostgreSQLEnumJdbcType.class)
|
||||
private SourceType sourceType;
|
||||
|
||||
@Column(name = "last_fetched_at", nullable = false)
|
||||
|
||||
@ -0,0 +1,47 @@
|
||||
package com.bivashy.backend.composer.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "track_source_metadata")
|
||||
public class TrackSourceMetadata {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "source_id", nullable = false, unique = true)
|
||||
private TrackSource source;
|
||||
|
||||
@Column(name = "url", nullable = false)
|
||||
private String url;
|
||||
|
||||
TrackSourceMetadata() {
|
||||
}
|
||||
|
||||
public TrackSourceMetadata(TrackSource source, String url) {
|
||||
this.source = source;
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public TrackSource getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
}
|
||||
@ -20,7 +20,7 @@ public class User {
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false, length = 500)
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
@Column(name = "created_at", nullable = false, updatable = false)
|
||||
|
||||
@ -1,11 +0,0 @@
|
||||
package com.bivashy.backend.composer.repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.bivashy.backend.composer.model.SourceType;
|
||||
|
||||
public interface SourceTypeRepository extends JpaRepository<SourceType, Long> {
|
||||
Optional<SourceType> findByName(String name);
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package com.bivashy.backend.composer.repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.bivashy.backend.composer.model.TrackSourceMetadata;
|
||||
|
||||
@Repository
|
||||
public interface TrackSourceMetadataRepository extends JpaRepository<TrackSourceMetadata, Long> {
|
||||
@Query("SELECT tsm FROM TrackSourceMetadata tsm " +
|
||||
"JOIN FETCH tsm.source " +
|
||||
"WHERE tsm.source.id = :sourceId")
|
||||
Optional<TrackSourceMetadata> findBySourceIdWithSource(@Param("sourceId") Long sourceId);
|
||||
}
|
||||
@ -7,6 +7,8 @@ import java.util.Map;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
public interface AudioBlobStorageService {
|
||||
String storeFolder();
|
||||
|
||||
String store(InputStream inputStream);
|
||||
|
||||
String store(byte[] data);
|
||||
@ -15,6 +17,8 @@ public interface AudioBlobStorageService {
|
||||
|
||||
String store(byte[] data, Map<String, String> metadata);
|
||||
|
||||
String store(String key, byte[] data, Map<String, String> metadata);
|
||||
|
||||
byte[] readRaw(String path) throws IOException;
|
||||
|
||||
Blob read(String path);
|
||||
|
||||
@ -19,6 +19,7 @@ import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
|
||||
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
|
||||
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
||||
|
||||
@Service
|
||||
@ -36,6 +37,18 @@ public class AudioS3StorageService implements AudioBlobStorageService {
|
||||
this.tika = tika;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String storeFolder() {
|
||||
String objectKey = newObjectName();
|
||||
if (!objectKey.endsWith("/"))
|
||||
objectKey = objectKey + "/";
|
||||
s3Client.putObject(PutObjectRequest.builder()
|
||||
.bucket(bucket)
|
||||
.key(objectKey)
|
||||
.build(), RequestBody.empty());
|
||||
return objectKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String store(InputStream inputStream) {
|
||||
return store(inputStream, Map.of());
|
||||
@ -61,7 +74,34 @@ public class AudioS3StorageService implements AudioBlobStorageService {
|
||||
.contentType(contentType)
|
||||
.metadata(metadata)
|
||||
.build(), RequestBody.fromBytes(data));
|
||||
return String.join("/", bucket, objectKey);
|
||||
return objectKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String store(String key, byte[] data, Map<String, String> metadata) {
|
||||
try {
|
||||
ResponseInputStream<GetObjectResponse> response = s3Client.getObject(GetObjectRequest.builder()
|
||||
.bucket(bucket)
|
||||
.key(key)
|
||||
.build());
|
||||
|
||||
s3Client.putObject(PutObjectRequest.builder()
|
||||
.bucket(bucket)
|
||||
.key(key)
|
||||
.contentType(response.response().contentType())
|
||||
.metadata(metadata)
|
||||
.build(), RequestBody.fromBytes(data));
|
||||
|
||||
return key;
|
||||
} catch (NoSuchKeyException e) {
|
||||
System.out.println("no existing found");
|
||||
s3Client.putObject(PutObjectRequest.builder()
|
||||
.bucket(bucket)
|
||||
.key(key)
|
||||
.metadata(metadata)
|
||||
.build(), RequestBody.fromBytes(data));
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -71,9 +111,6 @@ public class AudioS3StorageService implements AudioBlobStorageService {
|
||||
|
||||
@Override
|
||||
public Blob read(String path) {
|
||||
if (path.startsWith(bucket + "/")) {
|
||||
path = path.substring(bucket.length());
|
||||
}
|
||||
ResponseInputStream<GetObjectResponse> response = s3Client.getObject(GetObjectRequest.builder()
|
||||
.bucket(bucket)
|
||||
.key(path)
|
||||
|
||||
@ -1,56 +1,91 @@
|
||||
package com.bivashy.backend.composer.service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import com.bivashy.backend.composer.auth.CustomUserDetails;
|
||||
import com.bivashy.backend.composer.dto.track.AddLocalTrackRequest;
|
||||
import com.bivashy.backend.composer.dto.importing.SingleTrackProgress;
|
||||
import com.bivashy.backend.composer.dto.track.PlaylistTrackResponse;
|
||||
import com.bivashy.backend.composer.dto.track.TrackBulkReorderRequest;
|
||||
import com.bivashy.backend.composer.dto.track.TrackResponse;
|
||||
import com.bivashy.backend.composer.model.SourceTypes;
|
||||
import com.bivashy.backend.composer.dto.track.YoutubeTrackRequest;
|
||||
import com.bivashy.backend.composer.dto.track.service.AddLocalTrackParams;
|
||||
import com.bivashy.backend.composer.exception.ImportTrackException;
|
||||
import com.bivashy.backend.composer.model.SourceType;
|
||||
import com.bivashy.backend.composer.model.Track;
|
||||
import com.bivashy.backend.composer.model.TrackMetadata;
|
||||
import com.bivashy.backend.composer.model.TrackSource;
|
||||
import com.bivashy.backend.composer.repository.TrackRepository;
|
||||
import com.bivashy.backend.composer.service.MetadataParseService.Metadata;
|
||||
import com.bivashy.backend.composer.service.importing.RedisProgressService;
|
||||
import com.jfposton.ytdlp.YtDlp;
|
||||
import com.jfposton.ytdlp.YtDlpException;
|
||||
import com.jfposton.ytdlp.mapper.VideoInfo;
|
||||
|
||||
@Service
|
||||
public class TrackService {
|
||||
private static final Logger logger = LoggerFactory.getLogger(TrackService.class);
|
||||
public static final String DOWNLOADED_METADATA_FILE = "downloaded";
|
||||
|
||||
private final TrackRepository trackRepository;
|
||||
private final TrackSourceService trackSourceService;
|
||||
private final TrackMetadataService trackMetadataService;
|
||||
private final TrackPlaylistService trackPlaylistService;
|
||||
private final MetadataParseService metadataParseService;
|
||||
private final RedisProgressService redisProgressService;
|
||||
private final YoutubeTrackService youtubeTrackService;
|
||||
|
||||
public TrackService(TrackRepository trackRepository,
|
||||
TrackSourceService trackSourceService,
|
||||
TrackMetadataService trackMetadataService,
|
||||
TrackPlaylistService trackPlaylistService,
|
||||
MetadataParseService metadataParseService) {
|
||||
public TrackService(TrackRepository trackRepository, TrackSourceService trackSourceService,
|
||||
TrackMetadataService trackMetadataService, TrackPlaylistService trackPlaylistService,
|
||||
MetadataParseService metadataParseService, RedisProgressService redisProgressService,
|
||||
YoutubeTrackService youtubeTrackService) {
|
||||
this.trackRepository = trackRepository;
|
||||
this.trackSourceService = trackSourceService;
|
||||
this.trackMetadataService = trackMetadataService;
|
||||
this.trackPlaylistService = trackPlaylistService;
|
||||
this.metadataParseService = metadataParseService;
|
||||
this.redisProgressService = redisProgressService;
|
||||
this.youtubeTrackService = youtubeTrackService;
|
||||
}
|
||||
|
||||
public TrackResponse addLocalTrack(CustomUserDetails user, Long playlistId, AddLocalTrackRequest request)
|
||||
throws IOException {
|
||||
Optional<Metadata> metadata = metadataParseService.extractMetadata(request.source().getInputStream());
|
||||
public TrackResponse addLocalTrack(CustomUserDetails user,
|
||||
long playlistId,
|
||||
AddLocalTrackParams params)
|
||||
throws ImportTrackException {
|
||||
var request = params.blob();
|
||||
Optional<Metadata> metadata = Optional.empty();
|
||||
try (var inputStream = request.inputStream()) {
|
||||
metadata = metadataParseService.extractMetadata(inputStream);
|
||||
} catch (IOException e) {
|
||||
throw new ImportTrackException("cannot extract metadata from " + request.fileName());
|
||||
}
|
||||
String ffprobeJson = metadata.map(Metadata::rawJson).orElse("{}");
|
||||
|
||||
TrackSource trackSource = trackSourceService.createTrackSource(
|
||||
request.source().getBytes(), ffprobeJson, SourceTypes.FILE);
|
||||
TrackSource trackSource;
|
||||
try {
|
||||
trackSource = trackSourceService.createLocalTrackSource(
|
||||
request.body(), ffprobeJson, params.ytdlpMetadata(), SourceType.FILE);
|
||||
} catch (IOException e) {
|
||||
throw new ImportTrackException("cannot read blob body", e);
|
||||
}
|
||||
|
||||
Track track = trackRepository.save(new Track(trackSource));
|
||||
|
||||
String fileName = fileNameWithoutExtension(request.source().getOriginalFilename());
|
||||
String fileName = fileNameWithoutExtension(request.fileName());
|
||||
String title = metadata.map(Metadata::title).orElse(fileName);
|
||||
String artist = metadata.map(Metadata::artist).orElse(null);
|
||||
int durationSeconds = metadata.map(Metadata::durationSeconds).map(Float::intValue).orElse(0);
|
||||
@ -66,6 +101,13 @@ public class TrackService {
|
||||
if (metadata.isPresent()) {
|
||||
fileFormat = metadata.map(m -> m.formatName()).get();
|
||||
}
|
||||
|
||||
if (params.includeProgressHistory()) {
|
||||
redisProgressService
|
||||
.saveProgress(
|
||||
new SingleTrackProgress(playlistId, trackSource.getId(), user.getId(), title, fileFormat));
|
||||
}
|
||||
|
||||
return new TrackResponse(
|
||||
track.getId(),
|
||||
title,
|
||||
@ -76,6 +118,56 @@ public class TrackService {
|
||||
fileName);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<TrackResponse> refreshYoutubePlaylist(CustomUserDetails user, long playlistId, long sourceId)
|
||||
throws ImportTrackException {
|
||||
return youtubeTrackService.refreshYoutubePlaylist(user, playlistId, sourceId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<TrackResponse> addYoutubeTrack(CustomUserDetails user, long playlistId,
|
||||
YoutubeTrackRequest request) throws ImportTrackException {
|
||||
List<VideoInfo> videoInfos = Collections.emptyList();
|
||||
try {
|
||||
videoInfos = YtDlp.getVideoInfo(request.youtubeUrl());
|
||||
} catch (YtDlpException e) {
|
||||
throw new ImportTrackException("cannot `yt-dlp --dump-json` from " + request.youtubeUrl(), e);
|
||||
}
|
||||
|
||||
logger.info("videoinfos count {}", videoInfos.size());
|
||||
|
||||
if (videoInfos.size() == 0) {
|
||||
throw new ImportTrackException("cannot find videoInfos");
|
||||
}
|
||||
|
||||
if (videoInfos.size() == 1) {
|
||||
try {
|
||||
VideoInfo videoInfo = videoInfos.get(0);
|
||||
Path temporaryFolder = Files.createTempDirectory("yt-dlp-tmp");
|
||||
|
||||
var params = youtubeTrackService.downloadYoutubeTrack(temporaryFolder, videoInfo,
|
||||
request.youtubeUrl());
|
||||
TrackResponse result = addLocalTrack(user, playlistId, params);
|
||||
|
||||
try (Stream<Path> pathStream = Files.walk(temporaryFolder)) {
|
||||
pathStream.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
}
|
||||
return List.of(result);
|
||||
} catch (IOException e) {
|
||||
throw new ImportTrackException("i/o during single youtube video downloading", e);
|
||||
} catch (YtDlpException e) {
|
||||
throw new ImportTrackException("cannot download youtube video " + request.youtubeUrl(), e);
|
||||
}
|
||||
}
|
||||
|
||||
TrackSource trackSource = trackSourceService.createYoutubeTrackSource(SourceType.PLAYLIST,
|
||||
request.youtubeUrl());
|
||||
return youtubeTrackService.refreshYoutubePlaylist(user.getId(), playlistId, trackSource, videoInfos,
|
||||
request.youtubeUrl());
|
||||
}
|
||||
|
||||
public List<PlaylistTrackResponse> getPlaylistTracks(CustomUserDetails user, Long playlistId) {
|
||||
return trackPlaylistService.getPlaylistTracks(playlistId).stream()
|
||||
.map(pt -> {
|
||||
|
||||
@ -1,33 +1,68 @@
|
||||
package com.bivashy.backend.composer.service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.bivashy.backend.composer.model.SourceType;
|
||||
import com.bivashy.backend.composer.model.TrackSource;
|
||||
import com.bivashy.backend.composer.repository.SourceTypeRepository;
|
||||
import com.bivashy.backend.composer.model.TrackSourceMetadata;
|
||||
import com.bivashy.backend.composer.repository.TrackSourceMetadataRepository;
|
||||
import com.bivashy.backend.composer.repository.TrackSourceRepository;
|
||||
|
||||
@Service
|
||||
public class TrackSourceService {
|
||||
public static final String FFPROBE_METADATA_KEY = "ffprobe";
|
||||
public static final String YTDLP_METADATA_KEY = "ytdlp";
|
||||
|
||||
private final TrackSourceRepository trackSourceRepository;
|
||||
private final SourceTypeRepository sourceTypeRepository;
|
||||
private final TrackSourceMetadataRepository trackSourceMetadataRepository;
|
||||
private final AudioBlobStorageService s3Service;
|
||||
|
||||
public TrackSourceService(TrackSourceRepository trackSourceRepository,
|
||||
SourceTypeRepository sourceTypeRepository,
|
||||
AudioBlobStorageService s3Service) {
|
||||
TrackSourceMetadataRepository trackSourceMetadataRepository, AudioBlobStorageService s3Service) {
|
||||
this.trackSourceRepository = trackSourceRepository;
|
||||
this.sourceTypeRepository = sourceTypeRepository;
|
||||
this.trackSourceMetadataRepository = trackSourceMetadataRepository;
|
||||
this.s3Service = s3Service;
|
||||
}
|
||||
|
||||
public TrackSource createTrackSource(byte[] audioBytes, String ffprobeJson, String sourceType) {
|
||||
String audioPath = s3Service.store(audioBytes, Map.of("ffprobe", ffprobeJson));
|
||||
SourceType type = sourceTypeRepository.findByName(sourceType)
|
||||
.orElseThrow(() -> new IllegalStateException("Source type not found: " + sourceType));
|
||||
return trackSourceRepository.save(new TrackSource(audioPath, type, LocalDateTime.now()));
|
||||
public TrackSource createLocalTrackSource(byte[] audioBytes,
|
||||
String ffprobeJson,
|
||||
String ytdlpMetadata,
|
||||
SourceType sourceType) {
|
||||
Map<String, String> metadata = new HashMap<>(Map.of(YTDLP_METADATA_KEY, ffprobeJson));
|
||||
if (ytdlpMetadata != null) {
|
||||
// TODO: Add tag or smth?
|
||||
}
|
||||
String audioPath = s3Service.store(audioBytes, metadata);
|
||||
|
||||
return trackSourceRepository.save(new TrackSource(audioPath, sourceType, LocalDateTime.now()));
|
||||
}
|
||||
|
||||
public TrackSource createTrackSourceWithKey(String key, byte[] audioBytes, String ffprobeJson,
|
||||
String ytdlpMetadata, SourceType sourceType) {
|
||||
Map<String, String> metadata = new HashMap<>(Map.of(YTDLP_METADATA_KEY, ffprobeJson));
|
||||
if (ytdlpMetadata != null) {
|
||||
// TODO: Add tag or smth?
|
||||
}
|
||||
String audioPath = s3Service.store(key, audioBytes, metadata);
|
||||
|
||||
return trackSourceRepository.save(new TrackSource(audioPath, sourceType, LocalDateTime.now()));
|
||||
}
|
||||
|
||||
public TrackSource createYoutubeTrackSource(SourceType sourceType, String youtubeUrl) {
|
||||
String folderPath = s3Service.storeFolder();
|
||||
TrackSource trackSource = trackSourceRepository
|
||||
.save(new TrackSource(folderPath, sourceType, LocalDateTime.now()));
|
||||
trackSourceMetadataRepository.save(new TrackSourceMetadata(trackSource, youtubeUrl));
|
||||
return trackSource;
|
||||
}
|
||||
|
||||
public Optional<TrackSourceMetadata> findWithMetadata(long sourceId) {
|
||||
return trackSourceMetadataRepository.findBySourceIdWithSource(sourceId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,244 @@
|
||||
package com.bivashy.backend.composer.service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.bivashy.backend.composer.auth.CustomUserDetails;
|
||||
import com.bivashy.backend.composer.dto.importing.PlaylistProgress;
|
||||
import com.bivashy.backend.composer.dto.importing.ProgressStatus;
|
||||
import com.bivashy.backend.composer.dto.track.TrackResponse;
|
||||
import com.bivashy.backend.composer.dto.track.service.AddLocalTrackParams;
|
||||
import com.bivashy.backend.composer.dto.track.service.AddLocalTrackParamsBuilder;
|
||||
import com.bivashy.backend.composer.exception.ImportTrackException;
|
||||
import com.bivashy.backend.composer.model.SourceType;
|
||||
import com.bivashy.backend.composer.model.Track;
|
||||
import com.bivashy.backend.composer.model.TrackSource;
|
||||
import com.bivashy.backend.composer.model.TrackSourceMetadata;
|
||||
import com.bivashy.backend.composer.repository.TrackRepository;
|
||||
import com.bivashy.backend.composer.service.MetadataParseService.Metadata;
|
||||
import com.bivashy.backend.composer.service.importing.RedisProgressService;
|
||||
import com.bivashy.backend.composer.util.SimpleBlob.PathBlob;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.jfposton.ytdlp.YtDlp;
|
||||
import com.jfposton.ytdlp.YtDlpException;
|
||||
import com.jfposton.ytdlp.YtDlpRequest;
|
||||
import com.jfposton.ytdlp.mapper.VideoInfo;
|
||||
|
||||
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
|
||||
|
||||
@Service
|
||||
public class YoutubeTrackService {
|
||||
private static final Logger logger = LoggerFactory.getLogger(YoutubeTrackService.class);
|
||||
public static final String DOWNLOADED_METADATA_FILE = "downloaded";
|
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
private final AudioS3StorageService s3StorageService;
|
||||
private final MetadataParseService metadataParseService;
|
||||
private final TrackRepository trackRepository;
|
||||
private final TrackMetadataService trackMetadataService;
|
||||
private final TrackPlaylistService trackPlaylistService;
|
||||
private final TrackSourceService trackSourceService;
|
||||
private final RedisProgressService redisProgressService;
|
||||
|
||||
public YoutubeTrackService(AudioS3StorageService s3StorageService, MetadataParseService metadataParseService,
|
||||
TrackRepository trackRepository, TrackMetadataService trackMetadataService,
|
||||
TrackPlaylistService trackPlaylistService, TrackSourceService trackSourceService,
|
||||
RedisProgressService redisProgressService) {
|
||||
this.s3StorageService = s3StorageService;
|
||||
this.metadataParseService = metadataParseService;
|
||||
this.trackRepository = trackRepository;
|
||||
this.trackMetadataService = trackMetadataService;
|
||||
this.trackPlaylistService = trackPlaylistService;
|
||||
this.trackSourceService = trackSourceService;
|
||||
this.redisProgressService = redisProgressService;
|
||||
}
|
||||
|
||||
public AddLocalTrackParams downloadYoutubeTrack(Path temporaryFolder, VideoInfo videoInfo, String youtubeUrl)
|
||||
throws IOException, YtDlpException, ImportTrackException {
|
||||
var ytDlpRequest = new YtDlpRequest(youtubeUrl, temporaryFolder.toAbsolutePath().toString());
|
||||
ytDlpRequest.setOption("output", "%(id)s");
|
||||
var response = YtDlp.execute(ytDlpRequest);
|
||||
// TODO: write to RedisProgressService
|
||||
|
||||
TrackResponse result = null;
|
||||
try (Stream<Path> pathStream = Files.walk(temporaryFolder)) {
|
||||
List<Path> downloadedFiles = Files.walk(temporaryFolder).toList();
|
||||
|
||||
if (downloadedFiles.isEmpty())
|
||||
throw new ImportTrackException("yt-dlp didn't downloaded anything for " + youtubeUrl);
|
||||
|
||||
for (Path downloadedFile : downloadedFiles) {
|
||||
var params = AddLocalTrackParamsBuilder.builder()
|
||||
.blob(new PathBlob(downloadedFile))
|
||||
.ytdlpMetadata(OBJECT_MAPPER.writeValueAsString(videoInfo))
|
||||
.includeProgressHistory(false)
|
||||
.build();
|
||||
|
||||
return params;
|
||||
}
|
||||
}
|
||||
throw new ImportTrackException("cannot download any youtube track");
|
||||
}
|
||||
|
||||
public List<TrackResponse> refreshYoutubePlaylist(CustomUserDetails user, long playlistId, long sourceId)
|
||||
throws ImportTrackException {
|
||||
Optional<TrackSourceMetadata> trackSourceMetadataOpt = trackSourceService.findWithMetadata(sourceId);
|
||||
if (trackSourceMetadataOpt.isEmpty())
|
||||
throw new ImportTrackException("cannot find track source with metadata with id " + sourceId);
|
||||
TrackSourceMetadata trackSourceMetadata = trackSourceMetadataOpt.get();
|
||||
String youtubeUrl = trackSourceMetadata.getUrl();
|
||||
|
||||
List<VideoInfo> videoInfos = Collections.emptyList();
|
||||
try {
|
||||
videoInfos = YtDlp.getVideoInfo(youtubeUrl);
|
||||
} catch (YtDlpException e) {
|
||||
throw new ImportTrackException("cannot `yt-dlp --dump-json` from " + youtubeUrl, e);
|
||||
}
|
||||
return refreshYoutubePlaylist(user.getId(), playlistId, trackSourceMetadata.getSource(), videoInfos,
|
||||
youtubeUrl);
|
||||
}
|
||||
|
||||
public List<TrackResponse> refreshYoutubePlaylist(long userId, long playlistId, TrackSource trackSource,
|
||||
List<VideoInfo> videoInfos,
|
||||
String youtubeUrl) throws ImportTrackException {
|
||||
List<TrackResponse> result = new ArrayList<>();
|
||||
logger.info(trackSource.getSourceUrl());
|
||||
try {
|
||||
Path temporaryFolder = Files.createTempDirectory("yt-dlp-tmp");
|
||||
logger.info("temporaryFolder created {}", temporaryFolder.toString());
|
||||
|
||||
String downloadedMetadataKey = trackSource.getSourceUrl() + DOWNLOADED_METADATA_FILE;
|
||||
try {
|
||||
var rawBody = s3StorageService
|
||||
.readRaw(downloadedMetadataKey);
|
||||
Files.write(temporaryFolder.resolve(DOWNLOADED_METADATA_FILE), rawBody);
|
||||
} catch (NoSuchKeyException e) {
|
||||
logger.warn(".downloaded metadata file was not found, ignoring");
|
||||
}
|
||||
|
||||
var ytDlpRequest = new YtDlpRequest(youtubeUrl, temporaryFolder.toAbsolutePath().toString());
|
||||
ytDlpRequest.setOption("output", "%(id)s");
|
||||
ytDlpRequest.setOption("download-archive", DOWNLOADED_METADATA_FILE);
|
||||
ytDlpRequest.setOption("extract-audio");
|
||||
ytDlpRequest.setOption("audio-quality", 0);
|
||||
ytDlpRequest.setOption("audio-format", "best");
|
||||
ytDlpRequest.setOption("no-overwrites");
|
||||
|
||||
PlaylistProgress playlistProgress = new PlaylistProgress(playlistId, trackSource.getId(), userId,
|
||||
videoInfos.size());
|
||||
redisProgressService.saveProgress(playlistProgress);
|
||||
|
||||
var response = YtDlp.execute(ytDlpRequest, (downloadProgress, ignored) -> {
|
||||
redisProgressService.<PlaylistProgress>updateTrackProgressField(playlistId, trackSource.getId(), userId,
|
||||
progress -> {
|
||||
progress.setOverallProgress((int) downloadProgress);
|
||||
});
|
||||
|
||||
}, stdoutLine -> {
|
||||
redisProgressService.<PlaylistProgress>updateTrackProgressField(playlistId, trackSource.getId(), userId,
|
||||
progress -> {
|
||||
progress.setYtdlnStdout(String.join("\n", progress.getYtdlnStdout(), stdoutLine));
|
||||
});
|
||||
}, null);
|
||||
redisProgressService.<PlaylistProgress>updateTrackProgressField(playlistId, trackSource.getId(), userId,
|
||||
progress -> {
|
||||
progress.setOverallProgress(100);
|
||||
progress.setStatus(ProgressStatus.FINISHED);
|
||||
});
|
||||
logger.info("yt dlp response {}", response);
|
||||
|
||||
try (Stream<Path> pathStream = Files.walk(temporaryFolder)) {
|
||||
List<Path> downloadedFiles = Files.walk(temporaryFolder).toList();
|
||||
logger.info("downloaded file count {}", downloadedFiles.size());
|
||||
|
||||
for (Path path : downloadedFiles) {
|
||||
if (Files.isDirectory(path))
|
||||
continue;
|
||||
boolean isMetadataFile = path.getFileName().toString().equals(DOWNLOADED_METADATA_FILE);
|
||||
var body = Files.readAllBytes(path);
|
||||
|
||||
if (isMetadataFile) {
|
||||
s3StorageService.store(downloadedMetadataKey, body, Map.of());
|
||||
continue;
|
||||
}
|
||||
String fileName = fileNameWithoutExtension(path.getFileName().toString());
|
||||
VideoInfo videoInfo = videoInfos.stream()
|
||||
.filter(v -> v.getId().equals(fileName))
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
|
||||
String audioKey = trackSource.getSourceUrl() + UUID.randomUUID().toString();
|
||||
|
||||
logger.info("downloaded file {} and info {}, key {}", fileName, videoInfo.getTitle(), audioKey);
|
||||
|
||||
Optional<Metadata> metadata = Optional.empty();
|
||||
|
||||
try (var inputStream = Files.newInputStream(path)) {
|
||||
metadata = metadataParseService.extractMetadata(inputStream);
|
||||
} catch (IOException e) {
|
||||
throw new ImportTrackException("cannot extract metadata from " + path.toString());
|
||||
}
|
||||
String ffprobeJson = metadata.map(Metadata::rawJson).orElse("{}");
|
||||
|
||||
TrackSource playlistEntrySource;
|
||||
try {
|
||||
playlistEntrySource = trackSourceService.createTrackSourceWithKey(audioKey, body, ffprobeJson,
|
||||
OBJECT_MAPPER.writeValueAsString(videoInfo), SourceType.PLAYLIST_ITEM);
|
||||
} catch (IOException e) {
|
||||
throw new ImportTrackException("cannot read blob body", e);
|
||||
}
|
||||
|
||||
Track track = trackRepository.save(new Track(playlistEntrySource));
|
||||
|
||||
String title = videoInfo.getTitle();
|
||||
String artist = metadata.map(Metadata::artist).orElse(null);
|
||||
int durationSeconds = metadata.map(Metadata::durationSeconds).map(Float::intValue).orElse(0);
|
||||
// TODO: thumbnail
|
||||
// TODO: Recognize music if the duration is less than five minutes
|
||||
// (configurable), and if not, it is a playlist and should be marked as is
|
||||
trackMetadataService.createTrackMetadata(
|
||||
track, title, fileName, audioKey, artist, null, durationSeconds);
|
||||
|
||||
trackPlaylistService.insertTrackAtEnd(playlistId, track.getId());
|
||||
|
||||
String fileFormat = "unknown";
|
||||
if (metadata.isPresent()) {
|
||||
fileFormat = metadata.map(m -> m.formatName()).get();
|
||||
}
|
||||
|
||||
var trackResponse = new TrackResponse(
|
||||
track.getId(),
|
||||
title,
|
||||
artist,
|
||||
audioKey,
|
||||
fileFormat,
|
||||
durationSeconds,
|
||||
fileName);
|
||||
result.add(trackResponse);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
} catch (IOException e) {
|
||||
throw new ImportTrackException("i/o during playlist youtube video downloading", e);
|
||||
} catch (YtDlpException e) {
|
||||
throw new ImportTrackException("cannot download youtube video " + youtubeUrl, e);
|
||||
}
|
||||
}
|
||||
|
||||
private String fileNameWithoutExtension(String fileName) {
|
||||
return fileName.replaceFirst("[.][^.]+$", "");
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,28 +1,35 @@
|
||||
package com.bivashy.backend.composer.service.importing;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.redis.connection.Message;
|
||||
import org.springframework.data.redis.connection.MessageListener;
|
||||
import org.springframework.data.redis.listener.ChannelTopic;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.bivashy.backend.composer.dto.importing.BaseTrackProgress;
|
||||
import com.bivashy.backend.composer.dto.importing.ImportTrackKey;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Consumer;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@Component
|
||||
public class RedisMessageSubscriber {
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
private static final Logger logger = LoggerFactory.getLogger(Logger.class);
|
||||
|
||||
private final RedisMessageListenerContainer container;
|
||||
private final Map<String, Consumer<String>> subscriptions = new ConcurrentHashMap<>();
|
||||
private final Map<String, Consumer<BaseTrackProgress>> subscriptions = new ConcurrentHashMap<>();
|
||||
|
||||
public RedisMessageSubscriber(RedisMessageListenerContainer container) {
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
public void subscribeToPlaylist(long playlistId, long userId, Consumer<String> messageHandler) {
|
||||
public void subscribeToPlaylist(long playlistId, long userId, Consumer<BaseTrackProgress> messageHandler) {
|
||||
String channel = ImportTrackKey.redisChannelKey(playlistId, userId);
|
||||
String subscriptionKey = ImportTrackKey.subscriptionKey(playlistId, userId);
|
||||
|
||||
@ -32,7 +39,13 @@ public class RedisMessageSubscriber {
|
||||
public void onMessage(Message message, byte[] pattern) {
|
||||
String receivedMessage = new String(message.getBody());
|
||||
if (subscriptions.containsKey(subscriptionKey)) {
|
||||
messageHandler.accept(receivedMessage);
|
||||
try {
|
||||
BaseTrackProgress progress = OBJECT_MAPPER.readValue(receivedMessage,
|
||||
BaseTrackProgress.class);
|
||||
messageHandler.accept(progress);
|
||||
} catch (JsonProcessingException e) {
|
||||
logger.error("cannot deserialize message into BaseTrackProgress.class", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, new ChannelTopic(channel));
|
||||
|
||||
@ -1,14 +1,22 @@
|
||||
package com.bivashy.backend.composer.service.importing;
|
||||
|
||||
import com.bivashy.backend.composer.dto.importing.ImportTrackKey;
|
||||
import com.bivashy.backend.composer.dto.importing.TrackProgressDTO;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import com.bivashy.backend.composer.dto.importing.BaseTrackProgress;
|
||||
import com.bivashy.backend.composer.dto.importing.ImportTrackKey;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@Service
|
||||
public class RedisProgressService {
|
||||
@ -22,16 +30,16 @@ public class RedisProgressService {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public void saveProgress(TrackProgressDTO progress) {
|
||||
public void saveProgress(BaseTrackProgress progress) {
|
||||
try {
|
||||
String key = ImportTrackKey.progressKey(progress.getPlaylistId(), progress.getUserId());
|
||||
String trackKey = ImportTrackKey.trackKey(
|
||||
progress.getPlaylistId(),
|
||||
progress.getTrackId(),
|
||||
progress.getTrackSourceId(),
|
||||
progress.getUserId());
|
||||
|
||||
String progressJson = objectMapper.writeValueAsString(progress);
|
||||
redisTemplate.opsForHash().put(key, Long.toString(progress.getTrackId()), progressJson);
|
||||
redisTemplate.opsForHash().put(key, Long.toString(progress.getTrackSourceId()), progressJson);
|
||||
|
||||
redisTemplate.opsForValue().set(trackKey, progressJson);
|
||||
|
||||
@ -44,16 +52,48 @@ public class RedisProgressService {
|
||||
}
|
||||
}
|
||||
|
||||
public List<TrackProgressDTO> getPlaylistProgress(long playlistId, long userId) {
|
||||
public <T extends BaseTrackProgress> void updateTrackProgressField(long playlistId, long trackSourceId, long userId,
|
||||
Consumer<T> updater) {
|
||||
try {
|
||||
String trackKey = ImportTrackKey.trackKey(playlistId, trackSourceId, userId);
|
||||
String hashKey = ImportTrackKey.progressKey(playlistId, userId);
|
||||
|
||||
String existingJson = redisTemplate.opsForValue().get(trackKey);
|
||||
if (existingJson == null) {
|
||||
throw new RuntimeException("Track progress not found");
|
||||
}
|
||||
|
||||
JavaType progressType = objectMapper.getTypeFactory()
|
||||
.constructType(BaseTrackProgress.class);
|
||||
|
||||
T progress = objectMapper.readValue(existingJson, progressType);
|
||||
|
||||
updater.accept(progress);
|
||||
|
||||
String updatedJson = objectMapper.writeValueAsString(progress);
|
||||
redisTemplate.opsForHash().put(hashKey, Long.toString(trackSourceId), updatedJson);
|
||||
redisTemplate.opsForValue().set(trackKey, updatedJson);
|
||||
|
||||
redisTemplate.expire(hashKey, 24, TimeUnit.HOURS);
|
||||
redisTemplate.expire(trackKey, 24, TimeUnit.HOURS);
|
||||
|
||||
publishProgressUpdate(progress);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to update track progress", e);
|
||||
}
|
||||
}
|
||||
|
||||
public List<BaseTrackProgress> getPlaylistProgress(long playlistId, long userId) {
|
||||
try {
|
||||
String key = ImportTrackKey.progressKey(playlistId, userId);
|
||||
Map<Object, Object> progressMap = redisTemplate.opsForHash().entries(key);
|
||||
|
||||
List<TrackProgressDTO> progressList = new ArrayList<>();
|
||||
List<BaseTrackProgress> progressList = new ArrayList<>();
|
||||
for (Object value : progressMap.values()) {
|
||||
TrackProgressDTO progress = objectMapper.readValue(
|
||||
BaseTrackProgress progress = objectMapper.readValue(
|
||||
(String) value,
|
||||
TrackProgressDTO.class);
|
||||
BaseTrackProgress.class);
|
||||
progressList.add(progress);
|
||||
}
|
||||
|
||||
@ -65,13 +105,13 @@ public class RedisProgressService {
|
||||
}
|
||||
}
|
||||
|
||||
public TrackProgressDTO getTrackProgress(long playlistId, long trackId, long userId) {
|
||||
public BaseTrackProgress getTrackProgress(long playlistId, long trackSourceId, long userId) {
|
||||
try {
|
||||
String key = ImportTrackKey.trackKey(playlistId, trackId, userId);
|
||||
String key = ImportTrackKey.trackKey(playlistId, trackSourceId, userId);
|
||||
String progressJson = redisTemplate.opsForValue().get(key);
|
||||
|
||||
if (progressJson != null) {
|
||||
return objectMapper.readValue(progressJson, TrackProgressDTO.class);
|
||||
return objectMapper.readValue(progressJson, BaseTrackProgress.class);
|
||||
}
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
@ -79,7 +119,7 @@ public class RedisProgressService {
|
||||
}
|
||||
}
|
||||
|
||||
private void publishProgressUpdate(TrackProgressDTO progress) {
|
||||
private void publishProgressUpdate(BaseTrackProgress progress) {
|
||||
try {
|
||||
String channel = ImportTrackKey.redisChannelKey(progress.getPlaylistId(), progress.getUserId());
|
||||
String message = objectMapper.writeValueAsString(progress);
|
||||
|
||||
@ -0,0 +1,64 @@
|
||||
package com.bivashy.backend.composer.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
public interface SimpleBlob {
|
||||
InputStream inputStream() throws IOException;
|
||||
|
||||
byte[] body() throws IOException;
|
||||
|
||||
String fileName();
|
||||
|
||||
public static class MultipartBlob implements SimpleBlob {
|
||||
private final MultipartFile multipartFile;
|
||||
|
||||
public MultipartBlob(MultipartFile multipartFile) {
|
||||
this.multipartFile = multipartFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream inputStream() throws IOException {
|
||||
return multipartFile.getInputStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] body() throws IOException {
|
||||
return multipartFile.getBytes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String fileName() {
|
||||
return multipartFile.getOriginalFilename();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class PathBlob implements SimpleBlob {
|
||||
private final Path path;
|
||||
|
||||
public PathBlob(Path path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream inputStream() throws IOException {
|
||||
return Files.newInputStream(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] body() throws IOException {
|
||||
return Files.readAllBytes(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String fileName() {
|
||||
return path.getFileName().toString();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,80 +1,69 @@
|
||||
CREATE TABLE IF NOT EXISTS "users" (
|
||||
"id" bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
"name" varchar(500) NOT NULL,
|
||||
"created_at" timestamp NOT NULL DEFAULT NOW(),
|
||||
"updated_at" timestamp NOT NULL DEFAULT NOW()
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "source_type" (
|
||||
"id" bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
"name" varchar(500) NOT NULL
|
||||
CREATE TYPE source_type_enum AS ENUM (
|
||||
'VIDEO', 'PLAYLIST', 'PLAYLIST_ITEM', 'FILE', 'URL'
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "track_source" (
|
||||
"id" bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
"source_url" varchar(500) NOT NULL,
|
||||
"source_type_id" bigint NOT NULL,
|
||||
"last_fetched_at" timestamp NOT NULL DEFAULT NOW(),
|
||||
"created_at" timestamp NOT NULL DEFAULT NOW(),
|
||||
"updated_at" timestamp NOT NULL DEFAULT NOW(),
|
||||
CONSTRAINT "fk_track_source_source_type_id"
|
||||
FOREIGN KEY ("source_type_id") REFERENCES "source_type" ("id")
|
||||
CREATE TABLE IF NOT EXISTS track_source (
|
||||
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
source_url TEXT NOT NULL,
|
||||
source_type SOURCE_TYPE_ENUM NOT NULL,
|
||||
last_fetched_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "track" (
|
||||
"id" bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
"source_id" bigint NOT NULL,
|
||||
CONSTRAINT "fk_track_source_id"
|
||||
FOREIGN KEY ("source_id") REFERENCES "track_source" ("id")
|
||||
CREATE TABLE IF NOT EXISTS track_source_metadata (
|
||||
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
source_id BIGINT NOT NULL UNIQUE REFERENCES track_source (
|
||||
id
|
||||
) ON DELETE CASCADE,
|
||||
url TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "track_metadata" (
|
||||
"id" bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
"track_id" bigint NOT NULL,
|
||||
"title" varchar(500) NOT NULL,
|
||||
"file_name" varchar(500) NOT NULL,
|
||||
"audio_path" varchar(500) NOT NULL,
|
||||
"artist" varchar(500),
|
||||
"thumbnail_path" varchar(500),
|
||||
"duration_seconds" integer,
|
||||
CONSTRAINT "fk_track_metadata_track_id"
|
||||
FOREIGN KEY ("track_id") REFERENCES "track" ("id")
|
||||
CREATE TABLE IF NOT EXISTS track (
|
||||
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
source_id BIGINT NOT NULL REFERENCES track_source (id) ON DELETE RESTRICT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "playlist" (
|
||||
"id" bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
"owner_id" bigint NOT NULL,
|
||||
"title" varchar(500) NOT NULL,
|
||||
"created_at" timestamp NOT NULL DEFAULT NOW(),
|
||||
"updated_at" timestamp NOT NULL DEFAULT NOW(),
|
||||
CONSTRAINT "fk_playlist_owner_id"
|
||||
FOREIGN KEY ("owner_id") REFERENCES "users" ("id"),
|
||||
CONSTRAINT "uq_playlist_owner_title"
|
||||
UNIQUE ("owner_id", "title")
|
||||
CREATE TABLE IF NOT EXISTS track_metadata (
|
||||
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
track_id BIGINT NOT NULL REFERENCES track (id) ON DELETE CASCADE,
|
||||
title TEXT NOT NULL,
|
||||
file_name TEXT NOT NULL,
|
||||
audio_path TEXT NOT NULL,
|
||||
artist TEXT,
|
||||
thumbnail_path TEXT,
|
||||
duration_seconds INTEGER
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "playlist_track" (
|
||||
"playlist_id" bigint NOT NULL,
|
||||
"track_id" bigint NOT NULL,
|
||||
"order_index" numeric NOT NULL,
|
||||
CONSTRAINT "pk_playlist_track_new" PRIMARY KEY ("playlist_id", "track_id"),
|
||||
CONSTRAINT "fk_playlist_track_playlist_id_new"
|
||||
FOREIGN KEY ("playlist_id") REFERENCES "playlist" ("id"),
|
||||
CONSTRAINT "fk_playlist_track_track_id_new"
|
||||
FOREIGN KEY ("track_id") REFERENCES "track" ("id")
|
||||
CREATE TABLE IF NOT EXISTS playlist (
|
||||
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
owner_id BIGINT NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
title TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
UNIQUE (owner_id, title)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "track_version" (
|
||||
"id" bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
"track_id" bigint NOT NULL,
|
||||
"metadata_id" bigint NOT NULL,
|
||||
"source_id" bigint NOT NULL,
|
||||
"created_at" timestamp NOT NULL DEFAULT NOW(),
|
||||
CONSTRAINT "fk_track_version_track_id"
|
||||
FOREIGN KEY ("track_id") REFERENCES "track" ("id"),
|
||||
CONSTRAINT "fk_track_version_metadata_id"
|
||||
FOREIGN KEY ("metadata_id") REFERENCES "track_metadata" ("id"),
|
||||
CONSTRAINT "fk_track_version_source_id"
|
||||
FOREIGN KEY ("source_id") REFERENCES "track_source" ("id")
|
||||
CREATE TABLE IF NOT EXISTS playlist_track (
|
||||
playlist_id BIGINT NOT NULL REFERENCES playlist (id) ON DELETE CASCADE,
|
||||
track_id BIGINT NOT NULL REFERENCES track (id) ON DELETE CASCADE,
|
||||
order_index DECIMAL NOT NULL,
|
||||
PRIMARY KEY (playlist_id, track_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS track_version (
|
||||
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
track_id BIGINT NOT NULL REFERENCES track (id) ON DELETE CASCADE,
|
||||
metadata_id BIGINT NOT NULL REFERENCES track_metadata (
|
||||
id
|
||||
) ON DELETE CASCADE,
|
||||
source_id BIGINT NOT NULL REFERENCES track_source (id) ON DELETE RESTRICT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
@ -1,9 +0,0 @@
|
||||
INSERT INTO "source_type" ("id", "name")
|
||||
OVERRIDING SYSTEM VALUE
|
||||
VALUES
|
||||
(1, 'VIDEO'),
|
||||
(2, 'PLAYLIST'),
|
||||
(3, 'FILE'),
|
||||
(4, 'URL')
|
||||
ON CONFLICT ("id") DO NOTHING;
|
||||
|
||||
Reference in New Issue
Block a user