2 * DemosceneMusic - UploadTrackPage.java - Copyright © 2012 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.demoscenemusic.page.admin;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.util.List;
28 import net.pterodactylus.demoscenemusic.core.Core;
29 import net.pterodactylus.demoscenemusic.data.Track;
30 import net.pterodactylus.demoscenemusic.data.TrackDerivative;
31 import net.pterodactylus.demoscenemusic.page.ServletRequest;
32 import net.pterodactylus.demoscenemusic.utils.AudioCodecs;
33 import net.pterodactylus.demoscenemusic.utils.AudioCodecs.AudioCodecDescription;
34 import net.pterodactylus.util.database.DatabaseException;
35 import net.pterodactylus.util.io.Closer;
36 import net.pterodactylus.util.io.StreamCopier;
37 import net.pterodactylus.util.template.Template;
38 import net.pterodactylus.util.template.TemplateContext;
39 import net.pterodactylus.util.template.TemplateContextFactory;
40 import net.pterodactylus.util.web.Method;
41 import net.pterodactylus.util.web.RedirectException;
43 import org.apache.commons.fileupload.FileItem;
44 import org.apache.commons.fileupload.FileUploadException;
45 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
46 import org.apache.commons.fileupload.servlet.ServletFileUpload;
49 * This page lets the user upload a {@link TrackDerivative} for a {@link Track}.
51 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53 public class AddTrackDerivativePage extends AdminBasePage {
56 * Creates a new upload-track-derivative page.
60 * @param templateContextFactory
61 * The template context factory
63 * The template to render
65 public AddTrackDerivativePage(Core core, TemplateContextFactory templateContextFactory, Template template) {
66 super(core, templateContextFactory, template, "admin.upload-track");
77 protected void processTemplate(TemplateContext templateContext, ServletRequest request) throws RedirectException {
78 super.processTemplate(templateContext, request);
79 if (request.getMethod() == Method.POST) {
84 File destinationFile = null;
85 String filename = null;
88 @SuppressWarnings("unchecked")
89 List<FileItem> fileItems = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request.getServletRequest());
90 for (FileItem fileItem : fileItems) {
91 if (!fileItem.isFormField()) {
92 filename = fileItem.getName();
93 destinationFile = File.createTempFile("demoscenemusic.", ".data");
94 InputStream fileInputStream = fileItem.getInputStream();
95 OutputStream destinationOutputStream = new FileOutputStream(destinationFile);
97 StreamCopier.copy(fileInputStream, destinationOutputStream);
99 Closer.close(destinationOutputStream);
100 Closer.close(fileInputStream);
103 if (fileItem.getFieldName().equals("track")) {
104 String trackId = fileItem.getString("UTF-8");
105 track = getCore().getDataManager().getTrackById(trackId);
111 templateContext.set("error", "no-track-given");
115 if ((filename == null) || (destinationFile == null)) {
116 /* no file has been uploaded. */
117 templateContext.set("error", "no-file-uploaded");
121 /* okay, we’re here, so everything’s fine, probably. */
122 TrackDerivative derivative = getCore().getDataManager().createTrackDerivative(track);
123 derivative.getProperties().set("file/original-name", filename);
124 if (filename.indexOf('.') > filename.lastIndexOf('/')) {
125 derivative.getProperties().set("file/extension", filename.substring(filename.lastIndexOf('.') + 1));
127 AudioCodecDescription audioCodecDescription = AudioCodecs.detect(filename);
128 if (audioCodecDescription != null) {
129 derivative.getProperties().set("audio/codec", audioCodecDescription.code);
131 /* TODO - identify file type and media information */
132 getCore().getDataManager().saveTrackDerivate(derivative);
134 /* copy the file again. */
135 File finalFile = getCore().getDataDirectory().getFile(derivative.getId());
136 FileOutputStream finalOutputStream = new FileOutputStream(finalFile);
137 FileInputStream tempInputStream = new FileInputStream(destinationFile);
139 StreamCopier.copy(tempInputStream, finalOutputStream);
141 Closer.close(tempInputStream);
142 Closer.close(finalOutputStream);
145 /* back to editing the track! */
146 throw new RedirectException("admin.edit-track?id=" + track.getId());
149 if (destinationFile != null) {
150 destinationFile.delete();
154 } catch (FileUploadException fue1) {
155 throw new RuntimeException("Could not upload file.", fue1);
156 } catch (IOException ioe1) {
157 throw new RuntimeException("Could not upload file.", ioe1);
158 } catch (DatabaseException de1) {
159 throw new RuntimeException("Could not upload file.", de1);