- 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/service/ImageService.java
r9893 r9919 1 1 package fr.mael.jiwigo.service; 2 2 3 import java.io.File;4 3 import java.io.IOException; 5 4 import java.security.NoSuchAlgorithmException; 6 5 import java.util.List; 7 6 8 import fr.mael.jiwigo.dao.ImageDao;9 7 import fr.mael.jiwigo.om.Image; 10 8 import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException; 11 9 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 12 10 import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException; 13 import fr.mael.jiwigo.transverse.session.SessionManager;14 import fr.mael.jiwigo.transverse.util.ImagesUtil;15 import fr.mael.jiwigo.transverse.util.Tools;16 11 17 /* 18 * jiwigo-ws-api Piwigo webservice access Api 19 * Copyright (c) 2010-2011 Mael mael@le-guevel.com 20 * All Rights Reserved 21 * 22 * This library is free software. It comes without any warranty, to 23 * the extent permitted by applicable law. You can redistribute it 24 * and/or modify it under the terms of the Do What The Fuck You Want 25 * To Public License, Version 2, as published by Sam Hocevar. See 26 * http://sam.zoy.org/wtfpl/COPYING for more details. 27 */ 28 /** 29 * 30 31 * @author mael 32 * 33 */ 34 public class ImageService { 35 36 /** 37 * Logger 38 */ 39 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 40 .getLog(ImageService.class); 41 42 /** 43 * Singleton 44 */ 45 private static ImageService instance; 46 47 private SessionManager sessionManager; 48 49 /** 50 * @return the singleton 51 */ 52 public static ImageService getInstance(SessionManager sessionManager) { 53 if (instance == null) { 54 instance = new ImageService(sessionManager); 55 } 56 return instance; 57 } 58 59 /** 60 * private constructor to use a singleton 61 */ 62 private ImageService(SessionManager sessionManager) { 63 this.sessionManager = sessionManager; 64 } 12 public interface ImageService { 65 13 66 14 /** … … 72 20 */ 73 21 public List<Image> listByCategory(Integer categoryId, boolean rafraichir) throws IOException, 74 ProxyAuthenticationException { 75 return ImageDao.getInstance(sessionManager).listByCategory(categoryId, rafraichir); 76 } 22 ProxyAuthenticationException; 77 23 78 24 /** … … 92 38 public boolean create(String filePath, Integer idCategory, Integer originalWidth, Integer originalHeight, 93 39 Double chunckSize, Integer privacyLevel) throws IOException, NoSuchAlgorithmException, 94 FileAlreadyExistsException, ProxyAuthenticationException, WrongChunkSizeException { 95 File originalFile = new File(filePath); 96 //get the byte array of the original file, to keep metadata 97 byte[] bytesFichierOriginal = Tools.getBytesFromFile(originalFile); 98 99 //resize the picture (or not) 100 boolean originaleRedimensionnee = ImagesUtil.scale(filePath, "originale.jpg", originalWidth, originalHeight); 101 //create the thumbnail 102 ImagesUtil.scale(filePath, "thumb.jpg", 120, 90); 103 //get the thumbnail 104 File thumbnail = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg"); 105 File originale = null; 106 if (originaleRedimensionnee) { 107 originale = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg"); 108 //if the original file has been resized, we put the metadata in the resized file 109 //I use here a try catch because if the original file isn't a jpeg 110 //the methode Outil.enrich will fail, but the procedure has to continue 111 try { 112 byte[] fichierEnrichi = Tools.enrich(bytesFichierOriginal, Tools.getBytesFromFile(new File(System 113 .getProperty("java.io.tmpdir") 114 + "/originale.jpg"))); 115 Tools.byteToFile(System.getProperty("java.io.tmpdir") + "/originale.jpg", fichierEnrichi); 116 } catch (Exception e) { 117 } 118 } else { 119 originale = originalFile; 120 121 } 122 Image image = new Image(); 123 image.setName(getImageName(filePath)); 124 image.setThumbnail(thumbnail); 125 image.setOriginale(originale); 126 image.setIdCategory(idCategory); 127 image.setPrivacyLevel(String.valueOf(privacyLevel)); 128 //now we call the dao to send the image to the webservice 129 return ImageDao.getInstance(sessionManager).create(image, chunckSize * 1000000); 130 } 40 FileAlreadyExistsException, ProxyAuthenticationException, WrongChunkSizeException; 131 41 132 42 /** … … 138 48 * @throws ProxyAuthenticationException 139 49 */ 140 public boolean addTags(Image image, String tagId) throws IOException, ProxyAuthenticationException { 141 return ImageDao.getInstance(sessionManager).addTags(image.getIdentifier(), tagId); 142 } 50 public boolean addTags(Image image, String tagId) throws IOException, ProxyAuthenticationException; 143 51 144 52 /** … … 149 57 * @throws ProxyAuthenticationException 150 58 */ 151 public List<Image> search(String queryString) throws IOException, ProxyAuthenticationException { 152 return ImageDao.getInstance(sessionManager).search(queryString); 153 } 154 155 /** 156 * Deletes the file extension 157 * @param path 158 * @return 159 */ 160 private String getImageName(String path) { 161 File fichier = new File(path); 162 StringBuffer name = new StringBuffer(fichier.getName()); 163 return (name.delete(name.lastIndexOf("."), name.length())).toString(); 164 165 } 59 public List<Image> search(String queryString) throws IOException, ProxyAuthenticationException; 166 60 167 61 }
Note: See TracChangeset
for help on using the changeset viewer.