2 * Copyright 2005-2011 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.photos.wicket;
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;
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;
36 * Created with IntelliJ IDEA.
40 * To change this template use File | Settings | File Templates.
42 public class UploadPanel extends Panel {
44 private static final Logger LOGGER = Logger.getLogger(UploadPanel.class.getName());
47 * Extension to use for JPEGs.
49 private static final String JPG_EXTENSION = ".jpg";
52 * Extension to use for ZIP files.
54 private static final String ZIP_EXTENSION = ".zip";
58 private transient Album _authorized;
65 private FileUploadField _uploadField;
67 public UploadPanel(String aId, String aPath) {
72 Form form = new Form("uploadForm") {
73 protected void onSubmit() {
74 final FileUpload upload = _uploadField.getFileUpload();
81 String filename = upload.getClientFileName();
82 InputStream is = upload.getInputStream();
84 if (filename.trim().length() == 0) {
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)) {
92 try (ZipInputStream zip = new ZipInputStream(is)) {
93 insertPhotosFromZipFile(album, zip);
96 warn("Skipping entry with unknown file type '" + filename + "'");
100 LOGGER.log(Level.WARNING, e.getMessage(), e);
101 error("ERROR:" + e.getMessage());
106 _uploadField = new FileUploadField("file");
107 form.add(_uploadField);
108 form.setMultiPart(true);
109 form.setMaxSize(Bytes.megabytes(500));
112 private void insertPhotosFromZipFile(Album aAlbum, ZipInputStream aZipFile) throws IOException {
115 while ((entry = aZipFile.getNextEntry()) != null) {
117 if (!entry.isDirectory() && entry.getName().toLowerCase().endsWith(JPG_EXTENSION)) {
118 insertPhoto(aAlbum, aZipFile, new File(entry.getName()).getName());
120 warn("Skipping entry '" + entry.getName() + "' from zip file.");
123 aZipFile.closeEntry();
128 private void insertPhoto(Album aAlbum, InputStream aPhotoInputStream, String aFilename) throws IOException {
129 String photoName = aFilename.substring(0, aFilename.length() - JPG_EXTENSION.length());
131 if (aAlbum.getEntry("/" + photoName) != null) {
132 error("Photo '" + photoName + "' already exists in this album");
135 aAlbum.addImage(photoName, aPhotoInputStream);
136 info("Photo '" + photoName + "' uploaded");