--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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);
+ }
+ }
+
+}