From 85557be7a45f85c4f98d92ad4e91e353d604d113 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 29 Jul 2012 01:35:25 +0200 Subject: [PATCH] Add servlet that lets the user download a track. --- .../demoscenemusic/core/DownloadServlet.java | 97 ++++++++++++++++++++++ src/main/webapp/WEB-INF/web.xml.template | 10 +++ 2 files changed, 107 insertions(+) create mode 100644 src/main/java/net/pterodactylus/demoscenemusic/core/DownloadServlet.java diff --git a/src/main/java/net/pterodactylus/demoscenemusic/core/DownloadServlet.java b/src/main/java/net/pterodactylus/demoscenemusic/core/DownloadServlet.java new file mode 100644 index 0000000..15f3db6 --- /dev/null +++ b/src/main/java/net/pterodactylus/demoscenemusic/core/DownloadServlet.java @@ -0,0 +1,97 @@ +/* + * DemosceneMusic - DownloadServlet.java - Copyright © 2012 David Roden + * + * This program is free software: you can redistribute it and/or modify + * 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. + * + * This program is distributed in the hope that it will be useful, + * 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 + * along with this program. If not, see . + */ + +package net.pterodactylus.demoscenemusic.core; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import net.pterodactylus.demoscenemusic.data.Artist; +import net.pterodactylus.demoscenemusic.data.Track; +import net.pterodactylus.demoscenemusic.data.TrackDerivative; +import net.pterodactylus.demoscenemusic.utils.AudioCodecs; +import net.pterodactylus.util.database.DatabaseException; +import net.pterodactylus.util.io.Closer; +import net.pterodactylus.util.io.StreamCopier; + +/** + * {@link HttpServlet} that lets the user download a track. + * + * @author David ‘Bombe’ Roden + */ +public class DownloadServlet extends HttpServlet { + + /** + * {@inheritDoc} + */ + @Override + protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { + Core core = (Core) getServletContext().getAttribute("core"); + try { + String derivativeId = httpServletRequest.getParameter("id"); + TrackDerivative derivative = core.getDataManager().getTrackDerivativeById(derivativeId); + Track track = core.getDataManager().getTrackByDerivativeId(derivativeId); + /* TODO - checking. */ + + /* create name of file. */ + StringBuilder filename = new StringBuilder(); + for (Artist artist : track.getArtists()) { + if (filename.length() > 0) { + filename.append(" & "); + } + filename.append(artist.getName()); + } + filename.append(" - "); + filename.append(track.getName()); + if (track.getRemix() != null) { + filename.append(" ("); + filename.append(track.getRemix()); + filename.append(")"); + } + filename.append('.').append(derivative.getProperties().get("file/extension")); + + /* prepare response. */ + File file = core.getDataDirectory().getFile(derivativeId); + httpServletResponse.addHeader("Content-Disposition", "attachment; filename=" + filename.toString()); + httpServletResponse.setContentType(AudioCodecs.codecDescriptions.get(derivative.getProperties().get("audio/codec")).mimeType); + httpServletResponse.setContentLength((int) file.length()); + httpServletResponse.setStatus(HttpServletResponse.SC_OK); + + /* send file. */ + OutputStream servletOutputStream = httpServletResponse.getOutputStream(); + InputStream fileInputStream = new FileInputStream(file); + try { + StreamCopier.copy(fileInputStream, servletOutputStream); + } finally { + Closer.close(fileInputStream); + Closer.close(servletOutputStream); + } + + } catch (DatabaseException de1) { + throw new RuntimeException("Could not load derivative.", de1); + } + } + +} diff --git a/src/main/webapp/WEB-INF/web.xml.template b/src/main/webapp/WEB-INF/web.xml.template index 573d063..4b5bd70 100644 --- a/src/main/webapp/WEB-INF/web.xml.template +++ b/src/main/webapp/WEB-INF/web.xml.template @@ -93,12 +93,22 @@ 0 + + DownloadServlet + net.pterodactylus.demoscenemusic.core.DownloadServlet + + StaticServlet /static/* + DownloadServlet + /download + + + TemplateServlet /* -- 2.7.4