Working upload of photos (individual and zip)
[photos] / src / main / java / org / wamblee / photos / wicket / PhotoPanel.java
1 /*
2  * Copyright 2005-2010 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.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.logging.Logger;
22 import javax.inject.Inject;
23
24 import org.apache.wicket.PageParameters;
25 import org.apache.wicket.markup.html.basic.Label;
26 import org.apache.wicket.markup.html.image.Image;
27 import org.apache.wicket.markup.html.link.Link;
28 import org.apache.wicket.markup.html.panel.Panel;
29 import org.apache.wicket.resource.ByteArrayResource;
30 import org.wamblee.photos.model.Album;
31 import org.wamblee.photos.model.Photo;
32 import org.wamblee.photos.model.PhotoEntry;
33 import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
34
35 /**
36  * Homepage
37  */
38 public class PhotoPanel extends Panel {
39
40     private static final Logger LOGGER = Logger.getLogger(PhotoPanel.class.getName());
41
42     private static final long serialVersionUID = 1L;
43     public static final int MAX_ROWS = 5;
44     public static final int MAX_COLUMNS = 5;
45
46     @Inject
47     @AuthorizedPhotos
48     private transient Album authorized;
49
50     private String path;
51
52     /**
53      * Constructor that is invoked when page is invoked without a session.
54      *
55      * @param parameters Page parameters
56      */
57     public PhotoPanel(String aId, final PageParameters parameters) throws Exception {
58         super(aId);
59
60         path = parameters.getString("path", "/");
61         if (!path.startsWith("/")) {
62             info("Invalid album '" + path + "', showing root album instead");
63             path = "/";
64         }
65         add(new Label("path", path));
66
67         String parentPath = getParentPath();
68
69         Link prevLink = new Link("prevLink") {
70             @Override
71             public void onClick() {
72                 Photo before = getPrevPhoto();
73                 if (before == null) {
74                     return;
75                 }
76                 PageParameters pars = new PageParameters();
77                 pars.put("path", before.getPath());
78                 setResponsePage(HomePage.class, pars);
79             }
80
81             @Override
82             public boolean isEnabled() {
83                 return getPrevPhoto() != null;
84             }
85         };
86
87         add(prevLink);
88
89         Link nextLink = new Link("nextLink") {
90             @Override
91             public void onClick() {
92                 Photo after = getNextPhoto();
93                 if (after == null) {
94                     return;
95                 }
96                 PageParameters pars = new PageParameters();
97                 pars.put("path", after.getPath());
98                 setResponsePage(HomePage.class, pars);
99             }
100
101             @Override
102             public boolean isEnabled() {
103                 return getNextPhoto() != null;
104             }
105         };
106
107         add(nextLink);
108
109         Link parentLink = new Link("parentLink") {
110             {
111                 if ("/".equals(path)) {
112                     setEnabled(false);
113                 }
114             }
115
116             @Override
117             public void onClick() {
118                 PageParameters pars = new PageParameters();
119
120                 pars.put("path", getParentPath());
121                 pars.put("index", 0);
122                 setResponsePage(HomePage.class, pars);
123             }
124         };
125
126         add(parentLink);
127
128         Image image = new Image("photo", new ByteArrayResource("image/jpeg", getData(getPhoto())));
129         add(image);
130     }
131
132     private Photo getPhoto() {
133         PhotoEntry current = authorized.getEntry(path);
134         if (current instanceof Album) {
135             throw new RuntimeException("PhotoPanel can only show a photo: " + current.getClass().getName());
136         }
137         return (Photo) current;
138     }
139
140     private Photo getPrevPhoto() {
141         return getAlbum().findPhotoBefore(getPhoto().getId());
142     }
143
144     private Photo getNextPhoto() {
145         return getAlbum().findPhotoAfter(getPhoto().getId());
146     }
147
148     private Album getAlbum() {
149         return (Album) getAuthorizedPhotos().getEntry(getParentPath());
150     }
151
152     private String getParentPath() {
153         String parentPath = path.substring(0, path.lastIndexOf("/"));
154         if (parentPath.length() == 0) {
155             parentPath = "/";
156         }
157         return parentPath;
158     }
159
160     private byte[] getData(Photo aPhoto) {
161         try (InputStream is = aPhoto.getPhoto()) {
162             return getBytes(is);
163         }
164         catch (IOException e) {
165             // to improve.
166             throw new RuntimeException("Cannot read photo", e);
167         }
168     }
169
170     private byte[] getBytes(InputStream is) throws IOException {
171         ByteArrayOutputStream bos = new ByteArrayOutputStream();
172         byte[] block = new byte[1024];
173         int n = is.read(block);
174         while (n > 0) {
175             bos.write(block, 0, n);
176             n = is.read(block);
177         }
178         return bos.toByteArray();
179     }
180
181     private Album getAuthorizedPhotos() {
182         return authorized;
183     }
184 }