From 89374975e400fb63e2c0a8e68986c98ed76bd711 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 28 Jul 2012 12:16:38 +0200 Subject: [PATCH] Add page that lets the user add a derivative. --- .../page/admin/AddTrackDerivativePage.java | 154 +++++++++++++++++++++ src/main/resources/templates/admin.add-derivative | 12 ++ src/main/webapp/WEB-INF/web.xml.template | 4 + 3 files changed, 170 insertions(+) create mode 100644 src/main/java/net/pterodactylus/demoscenemusic/page/admin/AddTrackDerivativePage.java create mode 100644 src/main/resources/templates/admin.add-derivative diff --git a/src/main/java/net/pterodactylus/demoscenemusic/page/admin/AddTrackDerivativePage.java b/src/main/java/net/pterodactylus/demoscenemusic/page/admin/AddTrackDerivativePage.java new file mode 100644 index 0000000..579940c --- /dev/null +++ b/src/main/java/net/pterodactylus/demoscenemusic/page/admin/AddTrackDerivativePage.java @@ -0,0 +1,154 @@ +/* + * DemosceneMusic - UploadTrackPage.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.page.admin; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.List; + +import net.pterodactylus.demoscenemusic.core.Core; +import net.pterodactylus.demoscenemusic.data.Track; +import net.pterodactylus.demoscenemusic.data.TrackDerivative; +import net.pterodactylus.demoscenemusic.page.ServletRequest; +import net.pterodactylus.util.database.DatabaseException; +import net.pterodactylus.util.io.Closer; +import net.pterodactylus.util.io.StreamCopier; +import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateContext; +import net.pterodactylus.util.template.TemplateContextFactory; +import net.pterodactylus.util.web.Method; +import net.pterodactylus.util.web.RedirectException; + +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileUploadException; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; +import org.apache.commons.fileupload.servlet.ServletFileUpload; + +/** + * This page lets the user upload a {@link TrackDerivative} for a {@link Track}. + * + * @author David ‘Bombe’ Roden + */ +public class AddTrackDerivativePage extends AdminBasePage { + + /** + * Creates a new upload-track-derivative page. + * + * @param core + * The core + * @param templateContextFactory + * The template context factory + * @param template + * The template to render + */ + public AddTrackDerivativePage(Core core, TemplateContextFactory templateContextFactory, Template template) { + super(core, templateContextFactory, template, "admin.upload-track"); + } + + // + // BASEPAGE METHODS + // + + /** + * {@inheritDoc} + */ + @Override + protected void processTemplate(TemplateContext templateContext, ServletRequest request) throws RedirectException { + super.processTemplate(templateContext, request); + if (request.getMethod() == Method.POST) { + + try { + + Track track = null; + File destinationFile = null; + String filename = null; + + try { + @SuppressWarnings("unchecked") + List fileItems = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request.getServletRequest()); + for (FileItem fileItem : fileItems) { + if (!fileItem.isFormField()) { + filename = fileItem.getName(); + destinationFile = File.createTempFile("demoscenemusic.", ".data"); + InputStream fileInputStream = fileItem.getInputStream(); + OutputStream destinationOutputStream = new FileOutputStream(destinationFile); + try { + StreamCopier.copy(fileInputStream, destinationOutputStream); + } finally { + Closer.close(destinationOutputStream); + Closer.close(fileInputStream); + } + } else { + if (fileItem.getFieldName().equals("track")) { + String trackId = fileItem.getString("UTF-8"); + track = getCore().getDataManager().getTrackById(trackId); + } + } + } + + if (track == null) { + templateContext.set("error", "no-track-given"); + return; + } + + if ((filename == null) || (destinationFile == null)) { + /* no file has been uploaded. */ + templateContext.set("error", "no-file-uploaded"); + return; + } + + /* okay, we’re here, so everything’s fine, probably. */ + TrackDerivative derivative = getCore().getDataManager().createTrackDerivative(track); + derivative.getProperties().set("file.original-name", filename); + getCore().getDataManager().saveTrackDerivate(derivative); + + /* copy the file again. */ + File finalFile = getCore().getDataDirectory().getFile(derivative.getId()); + FileOutputStream finalOutputStream = new FileOutputStream(finalFile); + FileInputStream tempInputStream = new FileInputStream(destinationFile); + try { + StreamCopier.copy(tempInputStream, finalOutputStream); + } finally { + Closer.close(tempInputStream); + Closer.close(finalOutputStream); + } + + /* back to editing the track! */ + throw new RedirectException("admin.edit-track?id=" + track.getId()); + + } finally { + if (destinationFile != null) { + destinationFile.delete(); + } + } + + } catch (FileUploadException fue1) { + throw new RuntimeException("Could not upload file.", fue1); + } catch (IOException ioe1) { + throw new RuntimeException("Could not upload file.", ioe1); + } catch (DatabaseException de1) { + throw new RuntimeException("Could not upload file.", de1); + } + } + } + +} diff --git a/src/main/resources/templates/admin.add-derivative b/src/main/resources/templates/admin.add-derivative new file mode 100644 index 0000000..91751b1 --- /dev/null +++ b/src/main/resources/templates/admin.add-derivative @@ -0,0 +1,12 @@ +<%include include/header title=="Add Track Derivative"> + +

Add a New Track Derivative

+ +<%if error|matches value=="no-track-given"> +

You did not specify a track.

+<%/if> +<%if error|matches value=="no-file-uploaded"> +

You did not select a file.

+<%/if> + +<%include include/footer> \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml.template b/src/main/webapp/WEB-INF/web.xml.template index 48dda54..9c8b1c0 100644 --- a/src/main/webapp/WEB-INF/web.xml.template +++ b/src/main/webapp/WEB-INF/web.xml.template @@ -71,6 +71,10 @@ net.pterodactylus.demoscenemusic.page.admin.AddTrackPage + admin.add-derivative + net.pterodactylus.demoscenemusic.page.admin.AddTrackDerivativePage + + admin.artists net.pterodactylus.demoscenemusic.page.admin.ArtistsAdminPage -- 2.7.4