Search in sources :

Example 36 with MediaFile

use of org.libresonic.player.domain.MediaFile in project libresonic by Libresonic.

the class HLSController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setHeader("Access-Control-Allow-Origin", "*");
    int id = ServletRequestUtils.getIntParameter(request, "id");
    MediaFile mediaFile = mediaFileService.getMediaFile(id);
    Player player = playerService.getPlayer(request, response);
    String username = player.getUsername();
    if (mediaFile == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, "Media file not found: " + id);
        return null;
    }
    if (username != null && !securityService.isFolderAccessAllowed(mediaFile, username)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN, "Access to file " + mediaFile.getId() + " is forbidden for user " + username);
        return null;
    }
    Integer duration = mediaFile.getDurationSeconds();
    if (duration == null || duration == 0) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unknown duration for media file: " + id);
        return null;
    }
    response.setContentType("application/vnd.apple.mpegurl");
    response.setCharacterEncoding(StringUtil.ENCODING_UTF8);
    List<Pair<Integer, Dimension>> bitRates = parseBitRates(request);
    PrintWriter writer = response.getWriter();
    if (bitRates.size() > 1) {
        generateVariantPlaylist(request, id, player, bitRates, writer);
    } else {
        generateNormalPlaylist(request, id, player, bitRates.size() == 1 ? bitRates.get(0) : null, duration, writer);
    }
    return null;
}
Also used : MediaFile(org.libresonic.player.domain.MediaFile) Player(org.libresonic.player.domain.Player) Pair(org.libresonic.player.util.Pair) PrintWriter(java.io.PrintWriter) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 37 with MediaFile

use of org.libresonic.player.domain.MediaFile in project libresonic by Libresonic.

the class TagService method setTags.

/**
     * Updated tags for a given music file.
     *
     * @param id     The ID of the music file.
     * @param track  The track number.
     * @param artist The artist name.
     * @param album  The album name.
     * @param title  The song title.
     * @param year   The release year.
     * @param genre  The musical genre.
     * @return "UPDATED" if the new tags were updated, "SKIPPED" if no update was necessary.
     *         Otherwise the error message is returned.
     */
public String setTags(int id, String track, String artist, String album, String title, String year, String genre) {
    track = StringUtils.trimToNull(track);
    artist = StringUtils.trimToNull(artist);
    album = StringUtils.trimToNull(album);
    title = StringUtils.trimToNull(title);
    year = StringUtils.trimToNull(year);
    genre = StringUtils.trimToNull(genre);
    Integer trackNumber = null;
    if (track != null) {
        try {
            trackNumber = new Integer(track);
        } catch (NumberFormatException x) {
            LOG.warn("Illegal track number: " + track, x);
        }
    }
    Integer yearNumber = null;
    if (year != null) {
        try {
            yearNumber = new Integer(year);
        } catch (NumberFormatException x) {
            LOG.warn("Illegal year: " + year, x);
        }
    }
    try {
        MediaFile file = mediaFileService.getMediaFile(id);
        MetaDataParser parser = metaDataParserFactory.getParser(file.getFile());
        if (!parser.isEditingSupported()) {
            return "Tag editing of " + FilenameUtils.getExtension(file.getPath()) + " files is not supported.";
        }
        if (StringUtils.equals(artist, file.getArtist()) && StringUtils.equals(album, file.getAlbumName()) && StringUtils.equals(title, file.getTitle()) && ObjectUtils.equals(yearNumber, file.getYear()) && StringUtils.equals(genre, file.getGenre()) && ObjectUtils.equals(trackNumber, file.getTrackNumber())) {
            return "SKIPPED";
        }
        MetaData newMetaData = parser.getMetaData(file.getFile());
        // Note: album artist is intentionally set, as it is not user-changeable.
        newMetaData.setArtist(artist);
        newMetaData.setAlbumName(album);
        newMetaData.setTitle(title);
        newMetaData.setYear(yearNumber);
        newMetaData.setGenre(genre);
        newMetaData.setTrackNumber(trackNumber);
        parser.setMetaData(file, newMetaData);
        mediaFileService.refreshMediaFile(file);
        mediaFileService.refreshMediaFile(mediaFileService.getParentOf(file));
        return "UPDATED";
    } catch (Exception x) {
        LOG.warn("Failed to update tags for " + id, x);
        return x.getMessage();
    }
}
Also used : MediaFile(org.libresonic.player.domain.MediaFile) MetaData(org.libresonic.player.service.metadata.MetaData) MetaDataParser(org.libresonic.player.service.metadata.MetaDataParser)

Example 38 with MediaFile

use of org.libresonic.player.domain.MediaFile in project libresonic by Libresonic.

the class MultiService method getSimilarArtists.

private List<SimilarArtist> getSimilarArtists(int mediaFileId, int limit) {
    HttpServletRequest request = WebContextFactory.get().getHttpServletRequest();
    String username = securityService.getCurrentUsername(request);
    List<MusicFolder> musicFolders = settingsService.getMusicFoldersForUser(username);
    MediaFile artist = mediaFileService.getMediaFile(mediaFileId);
    List<MediaFile> similarArtists = lastFmService.getSimilarArtists(artist, limit, false, musicFolders);
    SimilarArtist[] result = new SimilarArtist[similarArtists.size()];
    for (int i = 0; i < result.length; i++) {
        MediaFile similarArtist = similarArtists.get(i);
        result[i] = new SimilarArtist(similarArtist.getId(), similarArtist.getName());
    }
    return Arrays.asList(result);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaFile(org.libresonic.player.domain.MediaFile) MusicFolder(org.libresonic.player.domain.MusicFolder)

Example 39 with MediaFile

use of org.libresonic.player.domain.MediaFile in project libresonic by Libresonic.

the class PlaylistService method toggleStar.

public PlaylistInfo toggleStar(int id, int index) {
    HttpServletRequest request = WebContextFactory.get().getHttpServletRequest();
    String username = securityService.getCurrentUsername(request);
    List<MediaFile> files = playlistService.getFilesInPlaylist(id, true);
    MediaFile file = files.get(index);
    boolean starred = mediaFileDao.getMediaFileStarredDate(file.getId(), username) != null;
    if (starred) {
        mediaFileDao.unstarMediaFile(file.getId(), username);
    } else {
        mediaFileDao.starMediaFile(file.getId(), username);
    }
    return getPlaylist(id);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaFile(org.libresonic.player.domain.MediaFile)

Example 40 with MediaFile

use of org.libresonic.player.domain.MediaFile in project libresonic by Libresonic.

the class PlaylistService method up.

public PlaylistInfo up(int id, int index) {
    List<MediaFile> files = playlistService.getFilesInPlaylist(id, true);
    if (index > 0) {
        MediaFile file = files.remove(index);
        files.add(index - 1, file);
        playlistService.setFilesInPlaylist(id, files);
    }
    return getPlaylist(id);
}
Also used : MediaFile(org.libresonic.player.domain.MediaFile)

Aggregations

MediaFile (org.libresonic.player.domain.MediaFile)43 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 ModelAndView (org.springframework.web.servlet.ModelAndView)8 File (java.io.File)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 MusicFolder (org.libresonic.player.domain.MusicFolder)5 Playlist (org.libresonic.player.domain.Playlist)5 Player (org.libresonic.player.domain.Player)4 FileOutputStream (java.io.FileOutputStream)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 RedirectView (org.springframework.web.servlet.view.RedirectView)3 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 HttpGet (org.apache.http.client.methods.HttpGet)2 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)2 Test (org.junit.Test)2 Share (org.libresonic.player.domain.Share)2 TransferStatus (org.libresonic.player.domain.TransferStatus)2