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