diff --git a/src/main/java/me/kavin/piped/server/ServerLauncher.java b/src/main/java/me/kavin/piped/server/ServerLauncher.java index 07a9b9b..10a8d3d 100644 --- a/src/main/java/me/kavin/piped/server/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/server/ServerLauncher.java @@ -1,6 +1,7 @@ package me.kavin.piped.server; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; import com.rometools.rome.feed.synd.SyndFeed; import com.rometools.rome.io.SyndFeedInput; import io.activej.config.Config; @@ -31,6 +32,8 @@ import org.xml.sax.InputSource; import java.io.ByteArrayInputStream; import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Objects; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; @@ -377,8 +380,20 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { try { var json = Constants.mapper.readTree(request.loadBody().getResult().asArray()); var playlistId = json.get("playlistId").textValue(); - var videoId = json.get("videoId").textValue(); - return getJsonResponse(AuthPlaylistHandlers.addToPlaylistResponse(request.getHeader(AUTHORIZATION), playlistId, videoId), "private"); + var videoIds = new ArrayList(); + // backwards compatibility + var videoIdField = json.get("videoId"); + if (videoIdField != null) { + videoIds.add(videoIdField.textValue()); + } + var videoIdsField = json.get("videoIds"); + if (videoIdsField != null) { + for (JsonNode node : videoIdsField) { + videoIds.add(node.asText()); + } + } + + return getJsonResponse(AuthPlaylistHandlers.addToPlaylistResponse(request.getHeader(AUTHORIZATION), playlistId, videoIds), "private"); } catch (Exception e) { return getErrorResponse(e, request.getPath()); } diff --git a/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java b/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java index a23ba54..f04b695 100644 --- a/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java +++ b/src/main/java/me/kavin/piped/server/handlers/auth/AuthPlaylistHandlers.java @@ -198,9 +198,9 @@ public class AuthPlaylistHandlers { return mapper.writeValueAsBytes(new AcceptedResponse()); } - public static byte[] addToPlaylistResponse(String session, String playlistId, String videoId) throws IOException, ExtractionException { + public static byte[] addToPlaylistResponse(String session, String playlistId, ArrayList videoIds) throws IOException, ExtractionException { - if (StringUtils.isBlank(session) || StringUtils.isBlank(playlistId) || StringUtils.isBlank(videoId)) + if (StringUtils.isBlank(session) || StringUtils.isBlank(playlistId)) ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session, playlistId and videoId are required parameters")); var user = DatabaseHelper.getUserFromSession(session); @@ -223,33 +223,33 @@ public class AuthPlaylistHandlers { return mapper.writeValueAsBytes(mapper.createObjectNode() .put("error", "You are not the owner this playlist")); - var video = DatabaseHelper.getPlaylistVideoFromId(s, videoId); + var tr = s.beginTransaction(); + for (String videoId : videoIds) { + if (StringUtils.isEmpty(videoId)) continue; - if (video == null) { - StreamInfo info = StreamInfo.getInfo("https://www.youtube.com/watch?v=" + videoId); + var video = DatabaseHelper.getPlaylistVideoFromId(s, videoId); - String channelId = StringUtils.substringAfter(info.getUploaderUrl(), "/channel/"); + if (video == null) { + StreamInfo info = StreamInfo.getInfo("https://www.youtube.com/watch?v=" + videoId); - var channel = DatabaseHelper.getChannelFromId(s, channelId); + String channelId = StringUtils.substringAfter(info.getUploaderUrl(), "/channel/"); - if (channel == null) { - channel = DatabaseHelper.saveChannel(channelId); + var channel = DatabaseHelper.getChannelFromId(s, channelId); + + if (channel == null) { + channel = DatabaseHelper.saveChannel(channelId); + } + + video = new PlaylistVideo(videoId, info.getName(), info.getThumbnailUrl(), info.getDuration(), channel); + + s.persist(video); } - video = new PlaylistVideo(videoId, info.getName(), info.getThumbnailUrl(), info.getDuration(), channel); - - var tr = s.beginTransaction(); - s.persist(video); - tr.commit(); + if (playlist.getVideos().isEmpty()) playlist.setThumbnail(video.getThumbnail()); + playlist.getVideos().add(video); } - if (playlist.getVideos().isEmpty()) - playlist.setThumbnail(video.getThumbnail()); - - playlist.getVideos().add(video); - - var tr = s.beginTransaction(); s.merge(playlist); tr.commit();