- Timestamp:
- Mar 29, 2011, 8:21:25 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/ImageDao.java
r9902 r9919 1 1 package fr.mael.jiwigo.dao; 2 2 3 import java.io.File;4 3 import java.io.IOException; 5 4 import java.security.NoSuchAlgorithmException; 6 import java.util.ArrayList;7 import java.util.HashMap;8 5 import java.util.List; 9 6 10 import org.w3c.dom.Document;11 import org.w3c.dom.Element;12 import org.w3c.dom.Node;13 import org.w3c.dom.NodeList;14 15 import sun.misc.BASE64Encoder;16 7 import fr.mael.jiwigo.om.Image; 17 import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;18 8 import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException; 19 9 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 20 10 import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException; 21 import fr.mael.jiwigo.transverse.session.SessionManager;22 import fr.mael.jiwigo.transverse.util.Tools;23 11 24 /* 25 * jiwigo-ws-api Piwigo webservice access Api 26 * Copyright (c) 2010-2011 Mael mael@le-guevel.com 27 * All Rights Reserved 28 * 29 * This library is free software. It comes without any warranty, to 30 * the extent permitted by applicable law. You can redistribute it 31 * and/or modify it under the terms of the Do What The Fuck You Want 32 * To Public License, Version 2, as published by Sam Hocevar. See 33 * http://sam.zoy.org/wtfpl/COPYING for more details. 34 */ 35 /** 36 37 * Dao of the images 38 * @author mael 39 * 40 */ 41 public class ImageDao { 42 43 /** 44 * Logger 45 */ 46 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 47 .getLog(ImageDao.class); 48 49 /** 50 * Singleton 51 */ 52 private static ImageDao instance; 53 54 /** 55 * cache to avoid downloading image for each access 56 */ 57 private HashMap<Integer, List<Image>> cache; 58 59 /** 60 * 61 */ 62 private Integer firstCatInCache; 63 64 private SessionManager sessionManager; 65 66 private ArrayList<File> filesToSend; 67 68 /** 69 * Private singleton, to use a singleton 70 */ 71 private ImageDao(SessionManager sessionManager) { 72 cache = new HashMap<Integer, List<Image>>(); 73 this.sessionManager = sessionManager; 74 } 75 76 /** 77 * @return le singleton 78 */ 79 public static ImageDao getInstance(SessionManager sessionManager) { 80 if (instance == null) { 81 instance = new ImageDao(sessionManager); 82 } 83 return instance; 84 } 85 12 public interface ImageDao { 86 13 /** 87 14 * Lists all images … … 90 17 * @throws ProxyAuthenticationException 91 18 */ 92 public List<Image> list(boolean refresh) throws IOException, ProxyAuthenticationException { 93 return listByCategory(null, refresh); 94 } 19 public List<Image> list(boolean refresh) throws IOException, ProxyAuthenticationException; 95 20 96 21 /** … … 102 27 */ 103 28 public List<Image> listByCategory(Integer categoryId, boolean refresh) throws IOException, 104 ProxyAuthenticationException { 105 if (refresh || cache.get(categoryId) == null) { 106 Document doc = null; 107 if (categoryId != null) { 108 doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_IMAGES.getLabel(), "cat_id", String 109 .valueOf(categoryId)); 110 } else { 111 doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_IMAGES.getLabel()); 112 } 113 Element element = (Element) doc.getDocumentElement().getElementsByTagName("images").item(0); 114 List<Image> images = getImagesFromElement(element); 115 cache.remove(categoryId); 116 cache.put(categoryId, images); 117 if (firstCatInCache == null) { 118 firstCatInCache = categoryId; 119 } 120 return images; 121 } else { 122 return cache.get(categoryId); 123 } 124 } 29 ProxyAuthenticationException; 125 30 126 31 /** … … 143 48 //TODO ne pas continuer si une des reponses precedentes est negative 144 49 public boolean create(Image image, Double chunkSize) throws FileAlreadyExistsException, IOException, 145 ProxyAuthenticationException, NoSuchAlgorithmException, WrongChunkSizeException { 146 //thumbnail converted to base64 147 BASE64Encoder base64 = new BASE64Encoder(); 148 149 String thumbnailBase64 = base64.encode(Tools.getBytesFromFile(image.getThumbnail())); 150 //sends the thumbnail and gets the result 151 Document reponseThumb = (sessionManager.executeReturnDocument("pwg.images.addChunk", "data", thumbnailBase64, 152 "type", "thumb", "position", "1", "original_sum", Tools.getMD5Checksum(image.getOriginale() 153 .getAbsolutePath()))); 154 155 //begin feature:0001827 156 int chunk = chunkSize.intValue(); 157 if (chunk == 0) { 158 throw new WrongChunkSizeException("Error : the chunk size cannot be 0"); 159 } 160 filesToSend = Tools.splitFile(image.getOriginale(), chunk); 161 boolean echec = false; 162 for (int i = 0; i < filesToSend.size(); i++) { 163 File fichierAEnvoyer = filesToSend.get(i); 164 String originaleBase64 = base64.encode(Tools.getBytesFromFile(fichierAEnvoyer)); 165 Document reponseOriginale = (sessionManager.executeReturnDocument("pwg.images.addChunk", "data", 166 originaleBase64, "type", "file", "position", String.valueOf(i), "original_sum", Tools 167 .getMD5Checksum(image.getOriginale().getAbsolutePath()))); 168 if (!Tools.checkOk(reponseOriginale)) { 169 echec = true; 170 break; 171 } 172 } 173 //end 174 175 //add the image in the database and get the result of the webservice 176 Document reponseAjout = (sessionManager.executeReturnDocument("pwg.images.add", "file_sum", Tools 177 .getMD5Checksum(image.getOriginale().getAbsolutePath()), "thumbnail_sum", Tools.getMD5Checksum(image 178 .getThumbnail().getCanonicalPath()), "position", "1", "original_sum", Tools.getMD5Checksum(image 179 .getOriginale().getAbsolutePath()), "categories", String.valueOf(image.getIdCategory()), "name", image 180 .getName(), "author", sessionManager.getLogin(), "level", image.getPrivacyLevel())); 181 LOG.debug("Response add : " + Tools.documentToString(reponseAjout)); 182 // System.out.println(Main.sessionManager.executerReturnString("pwg.images.add", "file_sum", Outil 183 // .getMD5Checksum(image.getOriginale().getAbsolutePath()), "thumbnail_sum", Outil.getMD5Checksum(image 184 // .getThumbnail().getCanonicalPath()), "position", "1", "original_sum", Outil.getMD5Checksum(image 185 // .getOriginale().getAbsolutePath()), "categories", String.valueOf(image.getIdCategory()), "name", image 186 // .getName(), "author", Main.sessionManager.getLogin())); 187 // Document reponsePrivacy = null; 188 // if (Outil.checkOk(reponseAjout)) { 189 // reponsePrivacy = Main.sessionManager.executerReturnDocument(MethodsEnum.SET_PRIVACY_LEVEL.getLabel()); 190 // } 191 boolean reussite = true; 192 if (!Tools.checkOk(reponseThumb) || echec || !Tools.checkOk(reponseAjout)) { 193 reussite = false; 194 } 195 deleteTempFiles(); 196 return reussite; 197 198 } 50 ProxyAuthenticationException, NoSuchAlgorithmException, WrongChunkSizeException; 199 51 200 52 /** … … 205 57 * @throws ProxyAuthenticationException 206 58 */ 207 public boolean addTags(Integer imageId, String tagId) throws IOException, ProxyAuthenticationException { 208 Document doc = sessionManager.executeReturnDocument(MethodsEnum.SET_INFO.getLabel(), "image_id", String 209 .valueOf(imageId), "tag_ids", tagId); 210 try { 211 return Tools.checkOk(doc); 212 } catch (FileAlreadyExistsException e) { 213 LOG.error(Tools.getStackTrace(e)); 214 return false; 215 } 216 217 } 218 219 /** 220 * parse an element to find images 221 * @param element the element to parse 222 * @return the list of images 223 */ 224 private List<Image> getImagesFromElement(Element element) { 225 // List<Element> listElement = (List<Element>) element.getChildren("image"); 226 NodeList listImages = element.getElementsByTagName("image"); 227 ArrayList<Image> images = new ArrayList<Image>(); 228 for (int i = 0; i < listImages.getLength(); i++) { 229 Node nodeImage = listImages.item(i); 230 if (nodeImage.getNodeType() == Node.ELEMENT_NODE) { 231 Element im = (Element) nodeImage; 232 Image myImage = new Image(); 233 myImage.setThumbnailUrl(im.getAttribute("tn_url")); 234 myImage.setUrl(im.getAttribute("element_url")); 235 myImage.setWidth(Integer.valueOf(im.getAttribute("width"))); 236 myImage.setHeight(Integer.valueOf(im.getAttribute("height"))); 237 myImage.setFile(im.getAttribute("file")); 238 myImage.setSeen(Integer.valueOf(im.getAttribute("hit"))); 239 myImage.setIdentifier(Integer.valueOf(im.getAttribute("id"))); 240 myImage.setName(Tools.getStringValueDom(im, "name")); 241 Element elementCategories = (Element) im.getElementsByTagName("categories").item(0); 242 Element elementCategory = (Element) elementCategories.getElementsByTagName("category").item(0); 243 myImage.setIdCategory(Integer.valueOf(elementCategory.getAttribute("id"))); 244 if (myImage.getName() == null) { 245 myImage.setName(myImage.getFile()); 246 } 247 images.add(myImage); 248 } 249 } 250 return images; 251 } 59 public boolean addTags(Integer imageId, String tagId) throws IOException, ProxyAuthenticationException; 252 60 253 61 /** … … 258 66 * @throws ProxyAuthenticationException 259 67 */ 260 public List<Image> search(String searchString) throws IOException, ProxyAuthenticationException { 261 Document doc = sessionManager.executeReturnDocument(MethodsEnum.SEARCH.getLabel(), "query", searchString); 262 LOG.debug(doc); 263 Element element = (Element) doc.getDocumentElement().getElementsByTagName("images").item(0); 264 return getImagesFromElement(element); 265 266 } 267 268 private void deleteTempFiles() { 269 File file = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg"); 270 file.delete(); 271 file = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg"); 272 file.delete(); 273 for (File tempCut : filesToSend) { 274 tempCut.delete(); 275 } 276 277 } 68 public List<Image> search(String searchString) throws IOException, ProxyAuthenticationException; 278 69 279 70 }
Note: See TracChangeset
for help on using the changeset viewer.