1 | package fr.mael.jiwigo.service; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.IOException; |
---|
5 | import java.util.List; |
---|
6 | |
---|
7 | import fr.mael.jiwigo.dao.ImageDao; |
---|
8 | import fr.mael.jiwigo.om.Image; |
---|
9 | import fr.mael.jiwigo.transverse.session.SessionManager; |
---|
10 | import fr.mael.jiwigo.transverse.util.ImagesUtil; |
---|
11 | import fr.mael.jiwigo.transverse.util.Outil; |
---|
12 | |
---|
13 | /* |
---|
14 | Copyright (c) 2010, Mael |
---|
15 | All rights reserved. |
---|
16 | |
---|
17 | Redistribution and use in source and binary forms, with or without |
---|
18 | modification, 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 | |
---|
28 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
29 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
30 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
31 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
32 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
33 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
34 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
35 | ON 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 |
---|
37 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
38 | */ |
---|
39 | /** |
---|
40 | * |
---|
41 | |
---|
42 | * @author mael |
---|
43 | * |
---|
44 | */ |
---|
45 | public 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 | } |
---|