Working upload of photos (individual and zip)
[photos] / src / main / java / org / wamblee / photos / wicket / UploadPanel.java
diff --git a/src/main/java/org/wamblee/photos/wicket/UploadPanel.java b/src/main/java/org/wamblee/photos/wicket/UploadPanel.java
new file mode 100644 (file)
index 0000000..8a0e8d4
--- /dev/null
@@ -0,0 +1,138 @@
+/*
+ * Copyright 2005-2011 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.photos.wicket;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+import javax.inject.Inject;
+
+import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.upload.FileUpload;
+import org.apache.wicket.markup.html.form.upload.FileUploadField;
+import org.apache.wicket.markup.html.panel.Panel;
+import org.apache.wicket.util.lang.Bytes;
+import org.wamblee.photos.model.Album;
+import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: erik
+ * Date: 9/23/13
+ * Time: 8:33 PM
+ * To change this template use File | Settings | File Templates.
+ */
+public class UploadPanel extends Panel {
+
+    private static final Logger LOGGER = Logger.getLogger(UploadPanel.class.getName());
+
+    /**
+     * Extension to use for JPEGs.
+     */
+    private static final String JPG_EXTENSION = ".jpg";
+
+    /**
+     * Extension to use for ZIP files.
+     */
+    private static final String ZIP_EXTENSION = ".zip";
+
+    @Inject
+    @AuthorizedPhotos
+    private transient Album _authorized;
+
+    private String _path;
+
+    /**
+     * Upload field.
+     */
+    private FileUploadField _uploadField;
+
+    public UploadPanel(String aId, String aPath) {
+        super(aId);
+
+        _path = aPath;
+
+        Form form = new Form("uploadForm") {
+            protected void onSubmit() {
+                final FileUpload upload = _uploadField.getFileUpload();
+
+                if (upload == null) {
+                    return;
+                }
+
+                try {
+                    String filename = upload.getClientFileName();
+                    InputStream is = upload.getInputStream();
+
+                    if (filename.trim().length() == 0) {
+                        return;
+                    }
+                    Album album = (Album) _authorized.getEntry(_path);
+                    if (filename.toLowerCase().endsWith(JPG_EXTENSION)) {
+                        insertPhoto(album, is, filename);
+                    } else if (filename.toLowerCase().endsWith(ZIP_EXTENSION)) {
+
+                        try (ZipInputStream zip = new ZipInputStream(is)) {
+                            insertPhotosFromZipFile(album, zip);
+                        }
+                    } else {
+                        warn("Skipping entry with unknown file type '" + filename + "'");
+                    }
+                }
+                catch (Exception e) {
+                    LOGGER.log(Level.WARNING, e.getMessage(), e);
+                    error("ERROR:" + e.getMessage());
+                }
+            }
+        };
+        add(form);
+        _uploadField = new FileUploadField("file");
+        form.add(_uploadField);
+        form.setMultiPart(true);
+        form.setMaxSize(Bytes.megabytes(500));
+    }
+
+    private void insertPhotosFromZipFile(Album aAlbum, ZipInputStream aZipFile) throws IOException {
+        // zip extension
+        ZipEntry entry;
+        while ((entry = aZipFile.getNextEntry()) != null) {
+            try {
+                if (!entry.isDirectory() && entry.getName().toLowerCase().endsWith(JPG_EXTENSION)) {
+                    insertPhoto(aAlbum, aZipFile, new File(entry.getName()).getName());
+                } else {
+                    warn("Skipping entry '" + entry.getName() + "' from zip file.");
+                }
+            } finally {
+                aZipFile.closeEntry();
+            }
+        }
+    }
+
+    private void insertPhoto(Album aAlbum, InputStream aPhotoInputStream, String aFilename) throws IOException {
+        String photoName = aFilename.substring(0, aFilename.length() - JPG_EXTENSION.length());
+
+        if (aAlbum.getEntry("/" + photoName) != null) {
+            error("Photo '" + photoName + "' already exists in this album");
+            return;
+        }
+        aAlbum.addImage(photoName, aPhotoInputStream);
+        info("Photo '" + photoName + "' uploaded");
+    }
+}