Search in sources :

Example 41 with MediaFile

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

the class PlaylistService method getPlaylist.

public PlaylistInfo getPlaylist(int id) {
    HttpServletRequest request = WebContextFactory.get().getHttpServletRequest();
    Playlist playlist = playlistService.getPlaylist(id);
    List<MediaFile> files = playlistService.getFilesInPlaylist(id, true);
    String username = securityService.getCurrentUsername(request);
    mediaFileService.populateStarredDate(files, username);
    populateAccess(files, username);
    return new PlaylistInfo(playlist, createEntries(files));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaFile(org.libresonic.player.domain.MediaFile) Playlist(org.libresonic.player.domain.Playlist)

Example 42 with MediaFile

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

the class CoverArtService method saveCoverArt.

private void saveCoverArt(String path, String url) throws Exception {
    InputStream input = null;
    OutputStream output = null;
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(// 20 seconds
        20 * 1000).setSocketTimeout(// 20 seconds
        20 * 1000).build();
        HttpGet method = new HttpGet(url);
        method.setConfig(requestConfig);
        try (CloseableHttpResponse response = client.execute(method)) {
            input = response.getEntity().getContent();
            // Attempt to resolve proper suffix.
            String suffix = "jpg";
            if (url.toLowerCase().endsWith(".gif")) {
                suffix = "gif";
            } else if (url.toLowerCase().endsWith(".png")) {
                suffix = "png";
            }
            // Check permissions.
            File newCoverFile = new File(path, "cover." + suffix);
            if (!securityService.isWriteAllowed(newCoverFile)) {
                throw new Exception("Permission denied: " + StringUtil.toHtml(newCoverFile.getPath()));
            }
            // If file exists, create a backup.
            backup(newCoverFile, new File(path, "cover." + suffix + ".backup"));
            // Write file.
            output = new FileOutputStream(newCoverFile);
            IOUtils.copy(input, output);
            MediaFile dir = mediaFileService.getMediaFile(path);
            // Refresh database.
            mediaFileService.refreshMediaFile(dir);
            dir = mediaFileService.getMediaFile(dir.getId());
            // Rename existing cover files if new cover file is not the preferred.
            try {
                while (true) {
                    File coverFile = mediaFileService.getCoverArt(dir);
                    if (coverFile != null && !isMediaFile(coverFile) && !newCoverFile.equals(coverFile)) {
                        if (!coverFile.renameTo(new File(coverFile.getCanonicalPath() + ".old"))) {
                            LOG.warn("Unable to rename old image file " + coverFile);
                            break;
                        }
                        LOG.info("Renamed old image file " + coverFile);
                        // Must refresh again.
                        mediaFileService.refreshMediaFile(dir);
                        dir = mediaFileService.getMediaFile(dir.getId());
                    } else {
                        break;
                    }
                }
            } catch (Exception x) {
                LOG.warn("Failed to rename existing cover file.", x);
            }
        }
    } finally {
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(output);
    }
}
Also used : MediaFile(org.libresonic.player.domain.MediaFile) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) HttpGet(org.apache.http.client.methods.HttpGet) FileOutputStream(java.io.FileOutputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) MediaFile(org.libresonic.player.domain.MediaFile) File(java.io.File)

Example 43 with MediaFile

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

the class CoverArtService method setCoverArtImage.

/**
     * Downloads and saves the cover art at the given URL.
     *
     * @param albumId ID of the album in question.
     * @param url  The image URL.
     * @return The error string if something goes wrong, <code>null</code> otherwise.
     */
public String setCoverArtImage(int albumId, String url) {
    try {
        MediaFile mediaFile = mediaFileService.getMediaFile(albumId);
        saveCoverArt(mediaFile.getPath(), url);
        return null;
    } catch (Exception x) {
        LOG.warn("Failed to save cover art for album " + albumId, x);
        return x.toString();
    }
}
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