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.Main; |
---|
8 | import fr.mael.jiwigo.dao.ImageDao; |
---|
9 | import fr.mael.jiwigo.om.Image; |
---|
10 | import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum; |
---|
11 | import fr.mael.jiwigo.transverse.util.ImagesUtil; |
---|
12 | import fr.mael.jiwigo.transverse.util.MetadataExtractor; |
---|
13 | import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement; |
---|
14 | |
---|
15 | /** |
---|
16 | * |
---|
17 | Copyright (c) 2010, Mael |
---|
18 | All rights reserved. |
---|
19 | |
---|
20 | Redistribution and use in source and binary forms, with or without |
---|
21 | modification, are permitted provided that the following conditions are met: |
---|
22 | * Redistributions of source code must retain the above copyright |
---|
23 | notice, this list of conditions and the following disclaimer. |
---|
24 | * Redistributions in binary form must reproduce the above copyright |
---|
25 | notice, this list of conditions and the following disclaimer in the |
---|
26 | documentation and/or other materials provided with the distribution. |
---|
27 | * Neither the name of jiwigo nor the |
---|
28 | names of its contributors may be used to endorse or promote products |
---|
29 | derived from this software without specific prior written permission. |
---|
30 | |
---|
31 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
32 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
33 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
34 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
35 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
36 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
37 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
38 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
39 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
40 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
41 | |
---|
42 | * @author mael |
---|
43 | * |
---|
44 | */ |
---|
45 | public class ImageService { |
---|
46 | /** |
---|
47 | * Logger |
---|
48 | */ |
---|
49 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
50 | .getLog(ImageService.class); |
---|
51 | |
---|
52 | private static ImageService instance; |
---|
53 | |
---|
54 | public static ImageService getInstance() { |
---|
55 | if (instance == null) { |
---|
56 | instance = new ImageService(); |
---|
57 | } |
---|
58 | return instance; |
---|
59 | } |
---|
60 | |
---|
61 | private ImageService() { |
---|
62 | |
---|
63 | } |
---|
64 | |
---|
65 | public List<Image> listerParCategory(Integer categoryId, boolean rafraichir) throws IOException { |
---|
66 | return ImageDao.getInstance().listerParCategory(categoryId, rafraichir); |
---|
67 | } |
---|
68 | |
---|
69 | public boolean creer(String filePath, Integer idCategory) throws Exception { |
---|
70 | int widthOriginale = Integer |
---|
71 | .valueOf(PreferencesManagement.getValue(PreferencesEnum.WIDTH_ORIGINALE.getLabel())); |
---|
72 | int heightOriginale = Integer.valueOf(PreferencesManagement |
---|
73 | .getValue(PreferencesEnum.HEIGHT_ORIGINAL.getLabel())); |
---|
74 | boolean originaleRedimensionnee = ImagesUtil.scale(filePath, "originale.jpg", widthOriginale, heightOriginale); |
---|
75 | ImagesUtil.scale(filePath, "thumb.jpg", 120, 90); |
---|
76 | File thumbnail = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg"); |
---|
77 | File originale = null; |
---|
78 | if (originaleRedimensionnee) { |
---|
79 | originale = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg"); |
---|
80 | } else { |
---|
81 | originale = new File(filePath); |
---|
82 | } |
---|
83 | MetadataExtractor medataExtractor = new MetadataExtractor(originale.getAbsolutePath()); |
---|
84 | String auteur = medataExtractor.getAuteur(); |
---|
85 | if (auteur == null) { |
---|
86 | auteur = Main.sessionManager.getLogin(); |
---|
87 | } |
---|
88 | Image image = new Image(); |
---|
89 | image.setName(getImageName(filePath)); |
---|
90 | image.setThumbnail(thumbnail); |
---|
91 | image.setOriginale(originale); |
---|
92 | image.setAuteur(auteur); |
---|
93 | image.setIdCategory(idCategory); |
---|
94 | return ImageDao.getInstance().creer(image); |
---|
95 | } |
---|
96 | |
---|
97 | private String getImageName(String path) { |
---|
98 | File fichier = new File(path); |
---|
99 | StringBuffer name = new StringBuffer(fichier.getName()); |
---|
100 | return (name.delete(name.lastIndexOf("."), name.length())).toString(); |
---|
101 | |
---|
102 | } |
---|
103 | |
---|
104 | } |
---|