Added edit profile page.
[photos] / src / main / java / org / wamblee / photos / wicket / UploadPanel.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.photos.wicket;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import java.util.zip.ZipEntry;
24 import java.util.zip.ZipInputStream;
25 import javax.inject.Inject;
26
27 import org.apache.wicket.markup.html.form.Form;
28 import org.apache.wicket.markup.html.form.upload.FileUpload;
29 import org.apache.wicket.markup.html.form.upload.FileUploadField;
30 import org.apache.wicket.markup.html.panel.Panel;
31 import org.apache.wicket.util.lang.Bytes;
32 import org.wamblee.photos.model.Album;
33 import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
34
35 /**
36  * Created with IntelliJ IDEA.
37  * User: erik
38  * Date: 9/23/13
39  * Time: 8:33 PM
40  * To change this template use File | Settings | File Templates.
41  */
42 public class UploadPanel extends Panel {
43
44     private static final Logger LOGGER = Logger.getLogger(UploadPanel.class.getName());
45
46     /**
47      * Extension to use for JPEGs.
48      */
49     private static final String JPG_EXTENSION = ".jpg";
50
51     /**
52      * Extension to use for ZIP files.
53      */
54     private static final String ZIP_EXTENSION = ".zip";
55
56     @Inject
57     @AuthorizedPhotos
58     private transient Album _authorized;
59
60     private String _path;
61
62     /**
63      * Upload field.
64      */
65     private FileUploadField _uploadField;
66
67     public UploadPanel(String aId, String aPath) {
68         super(aId);
69
70         _path = aPath;
71
72         Form form = new Form("uploadForm") {
73             protected void onSubmit() {
74                 final FileUpload upload = _uploadField.getFileUpload();
75
76                 if (upload == null) {
77                     return;
78                 }
79
80                 try {
81                     String filename = upload.getClientFileName();
82                     InputStream is = upload.getInputStream();
83
84                     if (filename.trim().length() == 0) {
85                         return;
86                     }
87                     Album album = (Album) _authorized.getEntry(_path);
88                     if (filename.toLowerCase().endsWith(JPG_EXTENSION)) {
89                         insertPhoto(album, is, filename);
90                     } else if (filename.toLowerCase().endsWith(ZIP_EXTENSION)) {
91
92                         try (ZipInputStream zip = new ZipInputStream(is)) {
93                             insertPhotosFromZipFile(album, zip);
94                         }
95                     } else {
96                         warn("Skipping entry with unknown file type '" + filename + "'");
97                     }
98                 }
99                 catch (Exception e) {
100                     LOGGER.log(Level.WARNING, e.getMessage(), e);
101                     error("ERROR:" + e.getMessage());
102                 }
103             }
104         };
105         add(form);
106         _uploadField = new FileUploadField("file");
107         form.add(_uploadField);
108         form.setMultiPart(true);
109         form.setMaxSize(Bytes.megabytes(500));
110     }
111
112     private void insertPhotosFromZipFile(Album aAlbum, ZipInputStream aZipFile) throws IOException {
113         // zip extension
114         ZipEntry entry;
115         while ((entry = aZipFile.getNextEntry()) != null) {
116             try {
117                 if (!entry.isDirectory() && entry.getName().toLowerCase().endsWith(JPG_EXTENSION)) {
118                     insertPhoto(aAlbum, aZipFile, new File(entry.getName()).getName());
119                 } else {
120                     warn("Skipping entry '" + entry.getName() + "' from zip file.");
121                 }
122             } finally {
123                 aZipFile.closeEntry();
124             }
125         }
126     }
127
128     private void insertPhoto(Album aAlbum, InputStream aPhotoInputStream, String aFilename) throws IOException {
129         String photoName = aFilename.substring(0, aFilename.length() - JPG_EXTENSION.length());
130
131         if (aAlbum.getEntry("/" + photoName) != null) {
132             error("Photo '" + photoName + "' already exists in this album");
133             return;
134         }
135         aAlbum.addImage(photoName, aPhotoInputStream);
136         info("Photo '" + photoName + "' uploaded");
137     }
138 }