source: extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/impl/ImageServiceImpl.java @ 10505

Last change on this file since 10505 was 10505, checked in by anthony43, 13 years ago

huge refactoring to introduce JiwigoException : a generic exception encapsulating low level exceptions (IOException, ProxyAuthentication, etc...)
The aim is to have, on the consumer side, just one exception to catch them all.
this refactoring may need a code review to check the changes.

File size: 5.3 KB
Line 
1package fr.mael.jiwigo.service.impl;
2
3import java.io.File;
4import java.io.IOException;
5import java.security.NoSuchAlgorithmException;
6import java.util.List;
7
8import org.slf4j.Logger;
9import org.slf4j.LoggerFactory;
10
11import fr.mael.jiwigo.dao.ImageDao;
12import fr.mael.jiwigo.om.Image;
13import fr.mael.jiwigo.service.ImageService;
14import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException;
15import fr.mael.jiwigo.transverse.exception.JiwigoException;
16import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
17import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException;
18import fr.mael.jiwigo.transverse.util.ImagesUtil;
19import fr.mael.jiwigo.transverse.util.Tools;
20
21/*
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 */
32/**
33 *
34
35 * @author mael
36 *
37 */
38public class ImageServiceImpl implements ImageService {
39
40    /**
41     * Logger
42     */
43    private final Logger LOG = LoggerFactory.getLogger(ImageServiceImpl.class);
44
45    private ImageDao dao;
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
52     * @throws ProxyAuthenticationException
53     */
54    public List<Image> listByCategory(Integer categoryId, boolean rafraichir) throws JiwigoException {
55        return dao.listByCategory(categoryId, rafraichir);
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
64     * @return true if the image is created
65     * @throws IOException
66     * @throws WrongChunkSizeException
67     * @throws ProxyAuthenticationException
68     * @throws FileAlreadyExistsException
69     * @throws NoSuchAlgorithmException
70     * @throws JiwigoException
71     * @throws Exception
72     */
73    public boolean create(String filePath, Integer idCategory, Integer originalWidth, Integer originalHeight,
74            Double chunckSize, Integer privacyLevel) throws IOException, NoSuchAlgorithmException,
75            FileAlreadyExistsException, ProxyAuthenticationException, WrongChunkSizeException,
76            JiwigoException {
77        File originalFile = new File(filePath);
78        //get the byte array of the original file, to keep metadata
79        byte[] bytesFichierOriginal = Tools.getBytesFromFile(originalFile);
80
81        //resize the picture (or not)
82        boolean originaleRedimensionnee = ImagesUtil.scale(filePath, "originale.jpg", originalWidth, originalHeight);
83        //create the thumbnail
84        ImagesUtil.scale(filePath, "thumb.jpg", 120, 90);
85        //get the thumbnail
86        File thumbnail = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg");
87        File originale = null;
88        if (originaleRedimensionnee) {
89            originale = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg");
90            //if the original file has been resized, we put the metadata in the resized file
91            //I use here a try catch because if the original file isn't a jpeg
92            //the methode Outil.enrich will fail, but the procedure has to continue
93            try {
94                byte[] fichierEnrichi = Tools.enrich(bytesFichierOriginal,
95                        Tools.getBytesFromFile(new File(System.getProperty("java.io.tmpdir") + "/originale.jpg")));
96                Tools.byteToFile(System.getProperty("java.io.tmpdir") + "/originale.jpg", fichierEnrichi);
97            } catch (Exception e) {
98            }
99        } else {
100            originale = originalFile;
101
102        }
103        Image image = new Image();
104        image.setName(getImageName(filePath));
105        image.setThumbnail(thumbnail);
106        image.setOriginale(originale);
107        image.setIdCategory(idCategory);
108        image.setPrivacyLevel(String.valueOf(privacyLevel));
109        //now we call the dao to send the image to the webservice
110        return dao.create(image, chunckSize * 1000000);
111    }
112
113    /**
114     * Add tags to an existing image
115     * @param image the image
116     * @param tagId the ids of the tags
117     * @return true if successful
118     * @throws IOException
119     * @throws ProxyAuthenticationException
120     */
121    public boolean addTags(Image image, String tagId) throws JiwigoException {
122        return dao.addTags(image.getIdentifier(), tagId);
123    }
124
125    /**
126     * Search images from a string
127     * @param queryString the string
128     * @return images matching the string
129     * @throws IOException
130     * @throws ProxyAuthenticationException
131     */
132    public List<Image> search(String queryString) throws JiwigoException {
133        return dao.search(queryString);
134    }
135
136    /**
137     * Deletes the file extension
138     * @param path
139     * @return
140     */
141    private String getImageName(String path) {
142        File fichier = new File(path);
143        StringBuffer name = new StringBuffer(fichier.getName());
144        return (name.delete(name.lastIndexOf("."), name.length())).toString();
145
146    }
147
148    public void addSimple(File file, Integer category, String title) throws JiwigoException {
149        dao.addSimple(file, category, title);
150
151    }
152
153    public ImageDao getDao() {
154        return dao;
155    }
156
157    public void setDao(ImageDao dao) {
158        this.dao = dao;
159    }
160
161}
Note: See TracBrowser for help on using the repository browser.