Search in sources :

Example 6 with Playlist

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);
    }
}
Also used : Playlist(org.libresonic.player.domain.Playlist)

Example 7 with Playlist

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;
}
Also used : Playlist(org.libresonic.player.domain.Playlist) JDOMException(org.jdom.JDOMException)

Example 8 with 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();
}
Also used : Playlist(org.libresonic.player.domain.Playlist)

Example 9 with Playlist

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);
}
Also used : Player(org.libresonic.player.domain.Player) Playlist(org.libresonic.player.domain.Playlist) User(org.libresonic.player.domain.User) HashMap(java.util.HashMap) UserSettings(org.libresonic.player.domain.UserSettings) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectView(org.springframework.web.servlet.view.RedirectView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with Playlist

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";
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) Playlist(org.libresonic.player.domain.Playlist) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) HashMap(java.util.HashMap) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Playlist (org.libresonic.player.domain.Playlist)25 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 MediaFile (org.libresonic.player.domain.MediaFile)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 DateFormat (java.text.DateFormat)3 HashMap (java.util.HashMap)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 User (org.libresonic.player.domain.User)3 JDOMException (org.jdom.JDOMException)2 Player (org.libresonic.player.domain.Player)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)1 FileItem (org.apache.commons.fileupload.FileItem)1 FileItemFactory (org.apache.commons.fileupload.FileItemFactory)1 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 MusicFolder (org.libresonic.player.domain.MusicFolder)1 UserSettings (org.libresonic.player.domain.UserSettings)1 RedirectView (org.springframework.web.servlet.view.RedirectView)1