You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
4.3 KiB
136 lines
4.3 KiB
8 years ago
|
/*
|
||
8 years ago
|
This file is part of Libresonic.
|
||
8 years ago
|
|
||
8 years ago
|
Libresonic is free software: you can redistribute it and/or modify
|
||
8 years ago
|
it under the terms of the GNU General Public License as published by
|
||
|
the Free Software Foundation, either version 3 of the License, or
|
||
|
(at your option) any later version.
|
||
|
|
||
8 years ago
|
Libresonic is distributed in the hope that it will be useful,
|
||
8 years ago
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
GNU General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU General Public License
|
||
8 years ago
|
along with Libresonic. If not, see <http://www.gnu.org/licenses/>.
|
||
8 years ago
|
|
||
8 years ago
|
Copyright 2016 (C) Libresonic Authors
|
||
|
Based upon Subsonic, Copyright 2009 (C) Sindre Mehus
|
||
8 years ago
|
*/
|
||
8 years ago
|
package org.libresonic.player.controller;
|
||
|
|
||
|
import org.libresonic.player.domain.MediaFile;
|
||
|
import org.libresonic.player.domain.Playlist;
|
||
|
import org.libresonic.player.service.PlaylistService;
|
||
|
import org.libresonic.player.service.SecurityService;
|
||
|
import org.libresonic.player.service.SettingsService;
|
||
|
import org.libresonic.player.util.StringUtil;
|
||
8 years ago
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
import org.springframework.stereotype.Controller;
|
||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||
8 years ago
|
import org.springframework.web.servlet.ModelAndView;
|
||
|
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
8 years ago
|
|
||
8 years ago
|
import java.text.DateFormat;
|
||
|
import java.text.SimpleDateFormat;
|
||
8 years ago
|
import java.util.*;
|
||
8 years ago
|
|
||
|
/**
|
||
|
* Controller for the page used to generate the Podcast XML file.
|
||
|
*
|
||
|
* @author Sindre Mehus
|
||
|
*/
|
||
8 years ago
|
@Controller
|
||
|
@RequestMapping("/podcast")
|
||
|
public class PodcastController {
|
||
8 years ago
|
|
||
|
private static final DateFormat RSS_DATE_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
|
||
8 years ago
|
@Autowired
|
||
8 years ago
|
private PlaylistService playlistService;
|
||
8 years ago
|
@Autowired
|
||
8 years ago
|
private SettingsService settingsService;
|
||
8 years ago
|
@Autowired
|
||
8 years ago
|
private SecurityService securityService;
|
||
|
|
||
8 years ago
|
@RequestMapping(method = RequestMethod.GET)
|
||
8 years ago
|
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||
|
|
||
|
String url = request.getRequestURL().toString();
|
||
|
String username = securityService.getCurrentUsername(request);
|
||
|
List<Playlist> playlists = playlistService.getReadablePlaylistsForUser(username);
|
||
8 years ago
|
List<Podcast> podcasts = new ArrayList<>();
|
||
8 years ago
|
|
||
|
for (Playlist playlist : playlists) {
|
||
|
|
||
|
List<MediaFile> songs = playlistService.getFilesInPlaylist(playlist.getId());
|
||
|
if (songs.isEmpty()) {
|
||
|
continue;
|
||
|
}
|
||
|
long length = 0L;
|
||
|
for (MediaFile song : songs) {
|
||
|
length += song.getFileSize();
|
||
|
}
|
||
|
String publishDate = RSS_DATE_FORMAT.format(playlist.getCreated());
|
||
|
|
||
|
// Resolve content type.
|
||
|
String suffix = songs.get(0).getFormat();
|
||
|
String type = StringUtil.getMimeType(suffix);
|
||
|
|
||
7 years ago
|
String enclosureUrl = url + "/stream?playlist=" + playlist.getId();
|
||
8 years ago
|
|
||
|
podcasts.add(new Podcast(playlist.getName(), publishDate, enclosureUrl, length, type));
|
||
|
}
|
||
|
|
||
8 years ago
|
Map<String, Object> map = new HashMap<>();
|
||
8 years ago
|
|
||
|
map.put("url", url);
|
||
|
map.put("podcasts", podcasts);
|
||
|
|
||
8 years ago
|
return new ModelAndView("podcast","model",map);
|
||
8 years ago
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Contains information about a single Podcast.
|
||
|
*/
|
||
|
public static class Podcast {
|
||
|
private String name;
|
||
|
private String publishDate;
|
||
|
private String enclosureUrl;
|
||
|
private long length;
|
||
|
private String type;
|
||
|
|
||
|
public Podcast(String name, String publishDate, String enclosureUrl, long length, String type) {
|
||
|
this.name = name;
|
||
|
this.publishDate = publishDate;
|
||
|
this.enclosureUrl = enclosureUrl;
|
||
|
this.length = length;
|
||
|
this.type = type;
|
||
|
}
|
||
|
|
||
|
public String getName() {
|
||
|
return name;
|
||
|
}
|
||
|
|
||
|
public String getPublishDate() {
|
||
|
return publishDate;
|
||
|
}
|
||
|
|
||
|
public String getEnclosureUrl() {
|
||
|
return enclosureUrl;
|
||
|
}
|
||
|
|
||
|
public long getLength() {
|
||
|
return length;
|
||
|
}
|
||
|
|
||
|
public String getType() {
|
||
|
return type;
|
||
|
}
|
||
|
}
|
||
|
}
|