Add page that lets the user add a derivative.
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / page / admin / AddTrackDerivativePage.java
1 /*
2  * DemosceneMusic - UploadTrackPage.java - Copyright © 2012 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.demoscenemusic.page.admin;
19
20 import java.io.File;
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;
27
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.util.database.DatabaseException;
33 import net.pterodactylus.util.io.Closer;
34 import net.pterodactylus.util.io.StreamCopier;
35 import net.pterodactylus.util.template.Template;
36 import net.pterodactylus.util.template.TemplateContext;
37 import net.pterodactylus.util.template.TemplateContextFactory;
38 import net.pterodactylus.util.web.Method;
39 import net.pterodactylus.util.web.RedirectException;
40
41 import org.apache.commons.fileupload.FileItem;
42 import org.apache.commons.fileupload.FileUploadException;
43 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
44 import org.apache.commons.fileupload.servlet.ServletFileUpload;
45
46 /**
47  * This page lets the user upload a {@link TrackDerivative} for a {@link Track}.
48  *
49  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50  */
51 public class AddTrackDerivativePage extends AdminBasePage {
52
53         /**
54          * Creates a new upload-track-derivative page.
55          *
56          * @param core
57          *            The core
58          * @param templateContextFactory
59          *            The template context factory
60          * @param template
61          *            The template to render
62          */
63         public AddTrackDerivativePage(Core core, TemplateContextFactory templateContextFactory, Template template) {
64                 super(core, templateContextFactory, template, "admin.upload-track");
65         }
66
67         //
68         // BASEPAGE METHODS
69         //
70
71         /**
72          * {@inheritDoc}
73          */
74         @Override
75         protected void processTemplate(TemplateContext templateContext, ServletRequest request) throws RedirectException {
76                 super.processTemplate(templateContext, request);
77                 if (request.getMethod() == Method.POST) {
78
79                         try {
80
81                                 Track track = null;
82                                 File destinationFile = null;
83                                 String filename = null;
84
85                                 try {
86                                         @SuppressWarnings("unchecked")
87                                         List<FileItem> fileItems = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request.getServletRequest());
88                                         for (FileItem fileItem : fileItems) {
89                                                 if (!fileItem.isFormField()) {
90                                                         filename = fileItem.getName();
91                                                         destinationFile = File.createTempFile("demoscenemusic.", ".data");
92                                                         InputStream fileInputStream = fileItem.getInputStream();
93                                                         OutputStream destinationOutputStream = new FileOutputStream(destinationFile);
94                                                         try {
95                                                                 StreamCopier.copy(fileInputStream, destinationOutputStream);
96                                                         } finally {
97                                                                 Closer.close(destinationOutputStream);
98                                                                 Closer.close(fileInputStream);
99                                                         }
100                                                 } else {
101                                                         if (fileItem.getFieldName().equals("track")) {
102                                                                 String trackId = fileItem.getString("UTF-8");
103                                                                 track = getCore().getDataManager().getTrackById(trackId);
104                                                         }
105                                                 }
106                                         }
107
108                                         if (track == null) {
109                                                 templateContext.set("error", "no-track-given");
110                                                 return;
111                                         }
112
113                                         if ((filename == null) || (destinationFile == null)) {
114                                                 /* no file has been uploaded. */
115                                                 templateContext.set("error", "no-file-uploaded");
116                                                 return;
117                                         }
118
119                                         /* okay, we’re here, so everything’s fine, probably. */
120                                         TrackDerivative derivative = getCore().getDataManager().createTrackDerivative(track);
121                                         derivative.getProperties().set("file.original-name", filename);
122                                         getCore().getDataManager().saveTrackDerivate(derivative);
123
124                                         /* copy the file again. */
125                                         File finalFile = getCore().getDataDirectory().getFile(derivative.getId());
126                                         FileOutputStream finalOutputStream = new FileOutputStream(finalFile);
127                                         FileInputStream tempInputStream = new FileInputStream(destinationFile);
128                                         try {
129                                                 StreamCopier.copy(tempInputStream, finalOutputStream);
130                                         } finally {
131                                                 Closer.close(tempInputStream);
132                                                 Closer.close(finalOutputStream);
133                                         }
134
135                                         /* back to editing the track! */
136                                         throw new RedirectException("admin.edit-track?id=" + track.getId());
137
138                                 } finally {
139                                         if (destinationFile != null) {
140                                                 destinationFile.delete();
141                                         }
142                                 }
143
144                         } catch (FileUploadException fue1) {
145                                 throw new RuntimeException("Could not upload file.", fue1);
146                         } catch (IOException ioe1) {
147                                 throw new RuntimeException("Could not upload file.", ioe1);
148                         } catch (DatabaseException de1) {
149                                 throw new RuntimeException("Could not upload file.", de1);
150                         }
151                 }
152         }
153
154 }