[9919] | 1 | package fr.mael.jiwigo.service.impl; |
---|
[9387] | 2 | |
---|
| 3 | import java.io.File; |
---|
| 4 | import java.io.IOException; |
---|
[9893] | 5 | import java.security.NoSuchAlgorithmException; |
---|
[9387] | 6 | import java.util.List; |
---|
| 7 | |
---|
[9919] | 8 | import org.slf4j.Logger; |
---|
| 9 | import org.slf4j.LoggerFactory; |
---|
| 10 | |
---|
[9387] | 11 | import fr.mael.jiwigo.dao.ImageDao; |
---|
| 12 | import fr.mael.jiwigo.om.Image; |
---|
[9919] | 13 | import fr.mael.jiwigo.service.ImageService; |
---|
[9893] | 14 | import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException; |
---|
[10505] | 15 | import fr.mael.jiwigo.transverse.exception.JiwigoException; |
---|
[9430] | 16 | import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; |
---|
[9893] | 17 | import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException; |
---|
[9387] | 18 | import fr.mael.jiwigo.transverse.util.ImagesUtil; |
---|
[9392] | 19 | import fr.mael.jiwigo.transverse.util.Tools; |
---|
[9387] | 20 | |
---|
| 21 | /* |
---|
[9879] | 22 | * jiwigo-ws-api Piwigo webservice access Api |
---|
| 23 | * Copyright (c) 2010-2011 Mael mael@le-guevel.com |
---|
| 24 | * All Rights Reserved |
---|
| 25 | * |
---|
| 26 | * This library is free software. It comes without any warranty, to |
---|
| 27 | * the extent permitted by applicable law. You can redistribute it |
---|
| 28 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
| 29 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
| 30 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
| 31 | */ |
---|
[9387] | 32 | /** |
---|
| 33 | * |
---|
| 34 | |
---|
| 35 | * @author mael |
---|
| 36 | * |
---|
| 37 | */ |
---|
[9919] | 38 | public class ImageServiceImpl implements ImageService { |
---|
[9387] | 39 | |
---|
| 40 | /** |
---|
| 41 | * Logger |
---|
| 42 | */ |
---|
[9919] | 43 | private final Logger LOG = LoggerFactory.getLogger(ImageServiceImpl.class); |
---|
[9387] | 44 | |
---|
[9919] | 45 | private ImageDao dao; |
---|
[9387] | 46 | |
---|
| 47 | /** |
---|
| 48 | * Lists all images for a category |
---|
| 49 | * @param categoryId the id of the category |
---|
| 50 | * @return the list of images |
---|
| 51 | * @throws IOException |
---|
[9430] | 52 | * @throws ProxyAuthenticationException |
---|
[9387] | 53 | */ |
---|
[10505] | 54 | public List<Image> listByCategory(Integer categoryId, boolean rafraichir) throws JiwigoException { |
---|
[9919] | 55 | return dao.listByCategory(categoryId, rafraichir); |
---|
[9387] | 56 | } |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * Method called to send an image to the server. |
---|
| 60 | * @param filePath |
---|
| 61 | * @param idCategory |
---|
| 62 | * @param originalWidth width for the original image |
---|
| 63 | * @param originalHeight height for the original image |
---|
[9392] | 64 | * @return true if the image is created |
---|
[9893] | 65 | * @throws IOException |
---|
| 66 | * @throws WrongChunkSizeException |
---|
| 67 | * @throws ProxyAuthenticationException |
---|
| 68 | * @throws FileAlreadyExistsException |
---|
| 69 | * @throws NoSuchAlgorithmException |
---|
[10505] | 70 | * @throws JiwigoException |
---|
[9387] | 71 | * @throws Exception |
---|
| 72 | */ |
---|
[9392] | 73 | public boolean create(String filePath, Integer idCategory, Integer originalWidth, Integer originalHeight, |
---|
[9893] | 74 | Double chunckSize, Integer privacyLevel) throws IOException, NoSuchAlgorithmException, |
---|
[10716] | 75 | FileAlreadyExistsException, ProxyAuthenticationException, WrongChunkSizeException, JiwigoException { |
---|
[9387] | 76 | File originalFile = new File(filePath); |
---|
| 77 | //get the byte array of the original file, to keep metadata |
---|
[9392] | 78 | byte[] bytesFichierOriginal = Tools.getBytesFromFile(originalFile); |
---|
[9387] | 79 | |
---|
| 80 | //resize the picture (or not) |
---|
| 81 | boolean originaleRedimensionnee = ImagesUtil.scale(filePath, "originale.jpg", originalWidth, originalHeight); |
---|
| 82 | //create the thumbnail |
---|
| 83 | ImagesUtil.scale(filePath, "thumb.jpg", 120, 90); |
---|
| 84 | //get the thumbnail |
---|
| 85 | File thumbnail = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg"); |
---|
| 86 | File originale = null; |
---|
| 87 | if (originaleRedimensionnee) { |
---|
| 88 | originale = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg"); |
---|
| 89 | //if the original file has been resized, we put the metadata in the resized file |
---|
| 90 | //I use here a try catch because if the original file isn't a jpeg |
---|
| 91 | //the methode Outil.enrich will fail, but the procedure has to continue |
---|
| 92 | try { |
---|
[10494] | 93 | byte[] fichierEnrichi = Tools.enrich(bytesFichierOriginal, |
---|
| 94 | Tools.getBytesFromFile(new File(System.getProperty("java.io.tmpdir") + "/originale.jpg"))); |
---|
[9392] | 95 | Tools.byteToFile(System.getProperty("java.io.tmpdir") + "/originale.jpg", fichierEnrichi); |
---|
[9387] | 96 | } catch (Exception e) { |
---|
| 97 | } |
---|
| 98 | } else { |
---|
| 99 | originale = originalFile; |
---|
| 100 | |
---|
| 101 | } |
---|
| 102 | Image image = new Image(); |
---|
| 103 | image.setName(getImageName(filePath)); |
---|
| 104 | image.setThumbnail(thumbnail); |
---|
| 105 | image.setOriginale(originale); |
---|
| 106 | image.setIdCategory(idCategory); |
---|
| 107 | image.setPrivacyLevel(String.valueOf(privacyLevel)); |
---|
| 108 | //now we call the dao to send the image to the webservice |
---|
[9919] | 109 | return dao.create(image, chunckSize * 1000000); |
---|
[9387] | 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * Add tags to an existing image |
---|
| 114 | * @param image the image |
---|
| 115 | * @param tagId the ids of the tags |
---|
| 116 | * @return true if successful |
---|
| 117 | * @throws IOException |
---|
[9430] | 118 | * @throws ProxyAuthenticationException |
---|
[9387] | 119 | */ |
---|
[10505] | 120 | public boolean addTags(Image image, String tagId) throws JiwigoException { |
---|
[9919] | 121 | return dao.addTags(image.getIdentifier(), tagId); |
---|
[9387] | 122 | } |
---|
| 123 | |
---|
| 124 | /** |
---|
| 125 | * Search images from a string |
---|
| 126 | * @param queryString the string |
---|
| 127 | * @return images matching the string |
---|
| 128 | * @throws IOException |
---|
[9430] | 129 | * @throws ProxyAuthenticationException |
---|
[9387] | 130 | */ |
---|
[10505] | 131 | public List<Image> search(String queryString) throws JiwigoException { |
---|
[9919] | 132 | return dao.search(queryString); |
---|
[9387] | 133 | } |
---|
| 134 | |
---|
| 135 | /** |
---|
| 136 | * Deletes the file extension |
---|
| 137 | * @param path |
---|
| 138 | * @return |
---|
| 139 | */ |
---|
| 140 | private String getImageName(String path) { |
---|
| 141 | File fichier = new File(path); |
---|
| 142 | StringBuffer name = new StringBuffer(fichier.getName()); |
---|
| 143 | return (name.delete(name.lastIndexOf("."), name.length())).toString(); |
---|
| 144 | |
---|
| 145 | } |
---|
| 146 | |
---|
[10505] | 147 | public void addSimple(File file, Integer category, String title) throws JiwigoException { |
---|
[10749] | 148 | dao.addSimple(file, category, title, null); |
---|
[10494] | 149 | |
---|
| 150 | } |
---|
| 151 | |
---|
[10716] | 152 | public boolean delete(Image image) throws JiwigoException { |
---|
| 153 | if (image.getIdentifier() == null) { |
---|
| 154 | throw new JiwigoException("The identifier of the image cannot be null"); |
---|
| 155 | } else { |
---|
| 156 | return dao.delete(image); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | } |
---|
| 160 | |
---|
[9919] | 161 | public ImageDao getDao() { |
---|
| 162 | return dao; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | public void setDao(ImageDao dao) { |
---|
| 166 | this.dao = dao; |
---|
| 167 | } |
---|
| 168 | |
---|
[10749] | 169 | public void addSimple(File file, Integer category, String title, Integer level) throws JiwigoException { |
---|
| 170 | dao.addSimple(file, category, title, level); |
---|
| 171 | } |
---|
| 172 | |
---|
[9387] | 173 | } |
---|