source: extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/ImageService.java @ 9394

Last change on this file since 9394 was 9394, checked in by mlg, 13 years ago

Major bug fix :
The ImageService allowed the chunk size to be 0 which led to fill the temporary folder.
Now an exception is thrown if the value is 0

File size: 5.7 KB
Line 
1package fr.mael.jiwigo.service;
2
3import java.io.File;
4import java.io.IOException;
5import java.util.List;
6
7import fr.mael.jiwigo.dao.ImageDao;
8import fr.mael.jiwigo.om.Image;
9import fr.mael.jiwigo.transverse.session.SessionManager;
10import fr.mael.jiwigo.transverse.util.ImagesUtil;
11import fr.mael.jiwigo.transverse.util.Tools;
12
13/*
14Copyright (c) 2010, Mael
15All rights reserved.
16
17Redistribution and use in source and binary forms, with or without
18modification, are permitted provided that the following conditions are met:
19 * Redistributions of source code must retain the above copyright
20   notice, this list of conditions and the following disclaimer.
21 * Redistributions in binary form must reproduce the above copyright
22   notice, this list of conditions and the following disclaimer in the
23   documentation and/or other materials provided with the distribution.
24 * Neither the name of jiwigo nor the
25   names of its contributors may be used to endorse or promote products
26   derived from this software without specific prior written permission.
27
28THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
29ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
32DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38*/
39/**
40 *
41
42 * @author mael
43 *
44 */
45public class ImageService {
46
47    /**
48     * Logger
49     */
50    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
51            .getLog(ImageService.class);
52
53    /**
54     * Singleton
55     */
56    private static ImageService instance;
57
58    private SessionManager sessionManager;
59
60    /**
61     * @return the singleton
62     */
63    public static ImageService getInstance(SessionManager sessionManager) {
64        if (instance == null) {
65            instance = new ImageService(sessionManager);
66        }
67        return instance;
68    }
69
70    /**
71     * private constructor to use a singleton
72     */
73    private ImageService(SessionManager sessionManager) {
74        this.sessionManager = sessionManager;
75    }
76
77    /**
78     * Lists all images for a category
79     * @param categoryId the id of the category
80     * @return the list of images
81     * @throws IOException
82     */
83    public List<Image> listByCategory(Integer categoryId, boolean rafraichir) throws IOException {
84        return ImageDao.getInstance(sessionManager).listByCategory(categoryId, rafraichir);
85    }
86
87    /**
88     * Method called to send an image to the server.
89     * @param filePath
90     * @param idCategory
91     * @param originalWidth width for the original image
92     * @param originalHeight height for the original image
93     * @return true if the image is created
94     * @throws Exception
95     */
96    public boolean create(String filePath, Integer idCategory, Integer originalWidth, Integer originalHeight,
97            Double chunckSize, Integer privacyLevel) throws Exception {
98        File originalFile = new File(filePath);
99        //get the byte array of the original file, to keep metadata
100        byte[] bytesFichierOriginal = Tools.getBytesFromFile(originalFile);
101
102        //resize the picture (or not)
103        boolean originaleRedimensionnee = ImagesUtil.scale(filePath, "originale.jpg", originalWidth, originalHeight);
104        //create the thumbnail
105        ImagesUtil.scale(filePath, "thumb.jpg", 120, 90);
106        //get the thumbnail
107        File thumbnail = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg");
108        File originale = null;
109        if (originaleRedimensionnee) {
110            originale = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg");
111            //if the original file has been resized, we put the metadata in the resized file
112            //I use here a try catch because if the original file isn't a jpeg
113            //the methode Outil.enrich will fail, but the procedure has to continue
114            try {
115                byte[] fichierEnrichi = Tools.enrich(bytesFichierOriginal, Tools.getBytesFromFile(new File(System
116                        .getProperty("java.io.tmpdir")
117                        + "/originale.jpg")));
118                Tools.byteToFile(System.getProperty("java.io.tmpdir") + "/originale.jpg", fichierEnrichi);
119            } catch (Exception e) {
120            }
121        } else {
122            originale = originalFile;
123
124        }
125        Image image = new Image();
126        image.setName(getImageName(filePath));
127        image.setThumbnail(thumbnail);
128        image.setOriginale(originale);
129        image.setIdCategory(idCategory);
130        image.setPrivacyLevel(String.valueOf(privacyLevel));
131        //now we call the dao to send the image to the webservice
132        return ImageDao.getInstance(sessionManager).create(image, chunckSize * 1000000);
133    }
134
135    /**
136     * Add tags to an existing image
137     * @param image the image
138     * @param tagId the ids of the tags
139     * @return true if successful
140     * @throws IOException
141     */
142    public boolean addTags(Image image, String tagId) throws IOException {
143        return ImageDao.getInstance(sessionManager).addTags(image.getIdentifier(), tagId);
144    }
145
146    /**
147     * Search images from a string
148     * @param queryString the string
149     * @return images matching the string
150     * @throws IOException
151     */
152    public List<Image> search(String queryString) throws IOException {
153        return ImageDao.getInstance(sessionManager).search(queryString);
154    }
155
156    /**
157     * Deletes the file extension
158     * @param path
159     * @return
160     */
161    private String getImageName(String path) {
162        File fichier = new File(path);
163        StringBuffer name = new StringBuffer(fichier.getName());
164        return (name.delete(name.lastIndexOf("."), name.length())).toString();
165
166    }
167
168}
Note: See TracBrowser for help on using the repository browser.