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.enumeration.PreferencesEnum; |
---|
10 | import fr.mael.jiwigo.transverse.util.ImagesUtil; |
---|
11 | import fr.mael.jiwigo.transverse.util.Outil; |
---|
12 | import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement; |
---|
13 | |
---|
14 | /** |
---|
15 | * |
---|
16 | Copyright (c) 2010, Mael |
---|
17 | All rights reserved. |
---|
18 | |
---|
19 | Redistribution and use in source and binary forms, with or without |
---|
20 | modification, are permitted provided that the following conditions are met: |
---|
21 | * Redistributions of source code must retain the above copyright |
---|
22 | notice, this list of conditions and the following disclaimer. |
---|
23 | * Redistributions in binary form must reproduce the above copyright |
---|
24 | notice, this list of conditions and the following disclaimer in the |
---|
25 | documentation and/or other materials provided with the distribution. |
---|
26 | * Neither the name of jiwigo nor the |
---|
27 | names of its contributors may be used to endorse or promote products |
---|
28 | derived from this software without specific prior written permission. |
---|
29 | |
---|
30 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
31 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
32 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
33 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
34 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
35 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
36 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
37 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
38 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
39 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
40 | |
---|
41 | * @author mael |
---|
42 | * |
---|
43 | */ |
---|
44 | public class ImageService { |
---|
45 | /** |
---|
46 | * Logger |
---|
47 | */ |
---|
48 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
49 | .getLog(ImageService.class); |
---|
50 | |
---|
51 | private static ImageService instance; |
---|
52 | |
---|
53 | public static ImageService getInstance() { |
---|
54 | if (instance == null) { |
---|
55 | instance = new ImageService(); |
---|
56 | } |
---|
57 | return instance; |
---|
58 | } |
---|
59 | |
---|
60 | private ImageService() { |
---|
61 | |
---|
62 | } |
---|
63 | |
---|
64 | public List<Image> listerParCategory(Integer categoryId, boolean rafraichir) throws IOException { |
---|
65 | return ImageDao.getInstance().listerParCategory(categoryId, rafraichir); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Method called to send an image to the server. |
---|
70 | |
---|
71 | * @param filePath |
---|
72 | * @param idCategory |
---|
73 | * @return |
---|
74 | * @throws Exception |
---|
75 | */ |
---|
76 | public boolean creer(String filePath, Integer idCategory) throws Exception { |
---|
77 | //get the byte array of the original file, to keep metadata |
---|
78 | byte[] bytesFichierOriginal = Outil.getBytesFromFile(new File(filePath)); |
---|
79 | |
---|
80 | //size taken from the user's preferences |
---|
81 | int widthOriginale = Integer |
---|
82 | .valueOf(PreferencesManagement.getValue(PreferencesEnum.WIDTH_ORIGINALE.getLabel())); |
---|
83 | int heightOriginale = Integer.valueOf(PreferencesManagement |
---|
84 | .getValue(PreferencesEnum.HEIGHT_ORIGINAL.getLabel())); |
---|
85 | //resize the picture (or not) |
---|
86 | boolean originaleRedimensionnee = ImagesUtil.scale(filePath, "originale.jpg", widthOriginale, heightOriginale); |
---|
87 | //create the thumbnail |
---|
88 | ImagesUtil.scale(filePath, "thumb.jpg", 120, 90); |
---|
89 | //get the thumbnail |
---|
90 | File thumbnail = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg"); |
---|
91 | File originale = null; |
---|
92 | if (originaleRedimensionnee) { |
---|
93 | originale = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg"); |
---|
94 | //if the original file has been resized, we put the metadata in the resized file |
---|
95 | //I use here a try catch because if the original file isn't a jpeg |
---|
96 | //the methode Outil.enrich will fail, but the procedure has to continue |
---|
97 | try { |
---|
98 | byte[] fichierEnrichi = Outil.enrich(bytesFichierOriginal, Outil.getBytesFromFile(new File(System |
---|
99 | .getProperty("java.io.tmpdir") |
---|
100 | + "/originale.jpg"))); |
---|
101 | Outil.byteToFile(System.getProperty("java.io.tmpdir") + "/originale.jpg", fichierEnrichi); |
---|
102 | } catch (Exception e) { |
---|
103 | } |
---|
104 | } else { |
---|
105 | originale = new File(filePath); |
---|
106 | |
---|
107 | } |
---|
108 | Image image = new Image(); |
---|
109 | image.setName(getImageName(filePath)); |
---|
110 | image.setThumbnail(thumbnail); |
---|
111 | image.setOriginale(originale); |
---|
112 | image.setIdCategory(idCategory); |
---|
113 | //now we call the dao to send the image to the webservice |
---|
114 | return ImageDao.getInstance().creer(image); |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * Deletes the file extension |
---|
119 | * @param path |
---|
120 | * @return |
---|
121 | */ |
---|
122 | private String getImageName(String path) { |
---|
123 | File fichier = new File(path); |
---|
124 | StringBuffer name = new StringBuffer(fichier.getName()); |
---|
125 | return (name.delete(name.lastIndexOf("."), name.length())).toString(); |
---|
126 | |
---|
127 | } |
---|
128 | |
---|
129 | } |
---|