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

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

First commit for jiwigo-ws-api

File size: 5.8 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.Outil;
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     * @param rafraichir true : refresh the list of images
81     * @return the list of images
82     * @throws IOException
83     */
84    public List<Image> listerParCategory(Integer categoryId, boolean rafraichir) throws IOException {
85        return ImageDao.getInstance(sessionManager).listerParCategory(categoryId, rafraichir);
86    }
87
88    /**
89     * Method called to send an image to the server.
90     * @param filePath
91     * @param idCategory
92     * @param originalWidth width for the original image
93     * @param originalHeight height for the original image
94     * @return
95     * @throws Exception
96     */
97    public boolean creer(String filePath, Integer idCategory, Integer originalWidth, Integer originalHeight,
98            Double chunckSize, Integer privacyLevel) throws Exception {
99        File originalFile = new File(filePath);
100        //get the byte array of the original file, to keep metadata
101        byte[] bytesFichierOriginal = Outil.getBytesFromFile(originalFile);
102
103        //resize the picture (or not)
104        boolean originaleRedimensionnee = ImagesUtil.scale(filePath, "originale.jpg", originalWidth, originalHeight);
105        //create the thumbnail
106        ImagesUtil.scale(filePath, "thumb.jpg", 120, 90);
107        //get the thumbnail
108        File thumbnail = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg");
109        File originale = null;
110        if (originaleRedimensionnee) {
111            originale = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg");
112            //if the original file has been resized, we put the metadata in the resized file
113            //I use here a try catch because if the original file isn't a jpeg
114            //the methode Outil.enrich will fail, but the procedure has to continue
115            try {
116                byte[] fichierEnrichi = Outil.enrich(bytesFichierOriginal, Outil.getBytesFromFile(new File(System
117                        .getProperty("java.io.tmpdir")
118                        + "/originale.jpg")));
119                Outil.byteToFile(System.getProperty("java.io.tmpdir") + "/originale.jpg", fichierEnrichi);
120            } catch (Exception e) {
121            }
122        } else {
123            originale = originalFile;
124
125        }
126        Image image = new Image();
127        image.setName(getImageName(filePath));
128        image.setThumbnail(thumbnail);
129        image.setOriginale(originale);
130        image.setIdCategory(idCategory);
131        image.setPrivacyLevel(String.valueOf(privacyLevel));
132        //now we call the dao to send the image to the webservice
133        return ImageDao.getInstance(sessionManager).creer(image, chunckSize);
134    }
135
136    /**
137     * Add tags to an existing image
138     * @param image the image
139     * @param tagId the ids of the tags
140     * @return true if successful
141     * @throws IOException
142     */
143    public boolean addTags(Image image, String tagId) throws IOException {
144        return ImageDao.getInstance(sessionManager).addTags(image.getIdentifiant(), tagId);
145    }
146
147    /**
148     * Search images from a string
149     * @param queryString the string
150     * @return images matching the string
151     * @throws IOException
152     */
153    public List<Image> search(String queryString) throws IOException {
154        return ImageDao.getInstance(sessionManager).search(queryString);
155    }
156
157    /**
158     * Deletes the file extension
159     * @param path
160     * @return
161     */
162    private String getImageName(String path) {
163        File fichier = new File(path);
164        StringBuffer name = new StringBuffer(fichier.getName());
165        return (name.delete(name.lastIndexOf("."), name.length())).toString();
166
167    }
168
169}
Note: See TracBrowser for help on using the repository browser.