use of org.libresonic.player.domain.Playlist in project libresonic by Libresonic.
the class PlaylistService method importPlaylistIfUpdated.
private void importPlaylistIfUpdated(File file, List<Playlist> allPlaylists) throws Exception {
String format = FilenameUtils.getExtension(file.getPath());
if (getPlaylistFormat(format) == null) {
return;
}
String fileName = file.getName();
Playlist existingPlaylist = null;
for (Playlist playlist : allPlaylists) {
if (fileName.equals(playlist.getImportedFrom())) {
existingPlaylist = playlist;
if (file.lastModified() <= playlist.getChanged().getTime()) {
// Already imported and not changed since.
return;
}
}
}
InputStream in = new FileInputStream(file);
try {
importPlaylist(User.USERNAME_ADMIN, FilenameUtils.getBaseName(fileName), fileName, format, in, existingPlaylist);
LOG.info("Auto-imported playlist " + file);
} finally {
IOUtils.closeQuietly(in);
}
}
use of org.libresonic.player.domain.Playlist in project libresonic by Libresonic.
the class PlaylistService method importPlaylist.
public Playlist importPlaylist(String username, String playlistName, String fileName, String format, InputStream inputStream, Playlist existingPlaylist) throws Exception {
PlaylistFormat playlistFormat = getPlaylistFormat(format);
if (playlistFormat == null) {
throw new Exception("Unsupported playlist format: " + format);
}
Pair<List<MediaFile>, List<String>> result = parseFiles(IOUtils.toByteArray(inputStream), playlistFormat);
if (result.getFirst().isEmpty() && !result.getSecond().isEmpty()) {
throw new Exception("No songs in the playlist were found.");
}
for (String error : result.getSecond()) {
LOG.warn("File in playlist '" + fileName + "' not found: " + error);
}
Date now = new Date();
Playlist playlist;
if (existingPlaylist == null) {
playlist = new Playlist();
playlist.setUsername(username);
playlist.setCreated(now);
playlist.setChanged(now);
playlist.setShared(true);
playlist.setName(playlistName);
playlist.setComment("Auto-imported from " + fileName);
playlist.setImportedFrom(fileName);
createPlaylist(playlist);
} else {
playlist = existingPlaylist;
}
setFilesInPlaylist(playlist.getId(), result.getFirst());
return playlist;
}
use of org.libresonic.player.domain.Playlist in project libresonic by Libresonic.
the class SonosService method addToContainer.
@Override
public AddToContainerResult addToContainer(String id, String parentId, int index, String updateId) {
if (parentId.startsWith(ID_PLAYLIST_PREFIX)) {
int playlistId = Integer.parseInt(parentId.replace(ID_PLAYLIST_PREFIX, ""));
Playlist playlist = playlistService.getPlaylist(playlistId);
if (playlist != null && playlist.getUsername().equals(getUsername())) {
addItemToPlaylist(playlistId, id, index);
}
}
return new AddToContainerResult();
}
use of org.libresonic.player.domain.Playlist in project libresonic by Libresonic.
the class PlaylistController method handleRequestInternal.
@RequestMapping(method = RequestMethod.GET)
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, Object> map = new HashMap<>();
int id = ServletRequestUtils.getRequiredIntParameter(request, "id");
User user = securityService.getCurrentUser(request);
String username = user.getUsername();
UserSettings userSettings = settingsService.getUserSettings(username);
Player player = playerService.getPlayer(request, response);
Playlist playlist = playlistService.getPlaylist(id);
if (playlist == null) {
return new ModelAndView(new RedirectView("notFound"));
}
map.put("playlist", playlist);
map.put("user", user);
map.put("player", player);
map.put("editAllowed", username.equals(playlist.getUsername()) || securityService.isAdmin(username));
map.put("partyMode", userSettings.isPartyModeEnabled());
return new ModelAndView("playlist", "model", map);
}
use of org.libresonic.player.domain.Playlist in project libresonic by Libresonic.
the class ImportPlaylistController method handlePost.
@RequestMapping(method = RequestMethod.POST)
protected String handlePost(RedirectAttributes redirectAttributes, HttpServletRequest request) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
try {
if (ServletFileUpload.isMultipartContent(request)) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<?> items = upload.parseRequest(request);
for (Object o : items) {
FileItem item = (FileItem) o;
if ("file".equals(item.getFieldName()) && !StringUtils.isBlank(item.getName())) {
if (item.getSize() > MAX_PLAYLIST_SIZE_MB * 1024L * 1024L) {
throw new Exception("The playlist file is too large. Max file size is " + MAX_PLAYLIST_SIZE_MB + " MB.");
}
String playlistName = FilenameUtils.getBaseName(item.getName());
String fileName = FilenameUtils.getName(item.getName());
String format = StringUtils.lowerCase(FilenameUtils.getExtension(item.getName()));
String username = securityService.getCurrentUsername(request);
Playlist playlist = playlistService.importPlaylist(username, playlistName, fileName, format, item.getInputStream(), null);
map.put("playlist", playlist);
}
}
}
} catch (Exception e) {
map.put("error", e.getMessage());
}
redirectAttributes.addFlashAttribute("model", map);
return "redirect:importPlaylist";
}
Aggregations