source: extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/impl/ImageServiceImpl.java @ 12416

Last change on this file since 12416 was 12416, checked in by anthony43, 13 years ago
  • upgrading version to 0.4.0 since there are api changes since 0.3.0
  • removing @override annotation on addSimple (not needed)
  • specifying in the pom the jdk version to use (1.5)
File size: 5.6 KB
Line 
1package fr.mael.jiwigo.service.impl;
2
3import java.io.File;
4import java.io.IOException;
5import java.security.NoSuchAlgorithmException;
6import java.util.List;
7
8import org.slf4j.Logger;
9import org.slf4j.LoggerFactory;
10
11import fr.mael.jiwigo.dao.ImageDao;
12import fr.mael.jiwigo.om.Image;
13import fr.mael.jiwigo.service.ImageService;
14import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException;
15import fr.mael.jiwigo.transverse.exception.JiwigoException;
16import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
17import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException;
18import fr.mael.jiwigo.transverse.util.ImagesUtil;
19import fr.mael.jiwigo.transverse.util.Tools;
20
21/*
22 *  jiwigo-ws-api Piwigo webservice access Api
23 *  Copyright (c) 2010-2011 Mael mael@le-guevel.com
24 *                All Rights Reserved
25 *
26 *  This library is free software. It comes without any warranty, to
27 *  the extent permitted by applicable law. You can redistribute it
28 *  and/or modify it under the terms of the Do What The Fuck You Want
29 *  To Public License, Version 2, as published by Sam Hocevar. See
30 *  http://sam.zoy.org/wtfpl/COPYING for more details.
31 */
32/**
33 *
34
35 * @author mael
36 *
37 */
38public class ImageServiceImpl implements ImageService {
39
40    /**
41     * Logger
42     */
43    private final Logger LOG = LoggerFactory.getLogger(ImageServiceImpl.class);
44
45    private ImageDao dao;
46
47    /**
48     * Lists all images for a category
49     * @param categoryId the id of the category
50     * @return the list of images
51     * @throws IOException
52     * @throws ProxyAuthenticationException
53     */
54    public List<Image> listByCategory(Integer categoryId, boolean rafraichir) throws JiwigoException {
55        return dao.listByCategory(categoryId, rafraichir);
56    }
57
58    /**
59     * Method called to send an image to the server.
60     * @param filePath
61     * @param idCategory
62     * @param originalWidth width for the original image
63     * @param originalHeight height for the original image
64     * @return true if the image is created
65     * @throws IOException
66     * @throws WrongChunkSizeException
67     * @throws ProxyAuthenticationException
68     * @throws FileAlreadyExistsException
69     * @throws NoSuchAlgorithmException
70     * @throws JiwigoException
71     * @throws Exception
72     */
73    public boolean create(String filePath, Integer idCategory, Integer originalWidth, Integer originalHeight,
74            Double chunckSize, Integer privacyLevel) throws IOException, NoSuchAlgorithmException,
75            FileAlreadyExistsException, ProxyAuthenticationException, WrongChunkSizeException, JiwigoException {
76        File originalFile = new File(filePath);
77        //get the byte array of the original file, to keep metadata
78        byte[] bytesFichierOriginal = Tools.getBytesFromFile(originalFile);
79
80        //resize the picture (or not)
81        boolean originaleRedimensionnee = ImagesUtil.scale(filePath, "originale.jpg", originalWidth, originalHeight);
82        //create the thumbnail
83        ImagesUtil.scale(filePath, "thumb.jpg", 120, 90);
84        //get the thumbnail
85        File thumbnail = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg");
86        File originale = null;
87        if (originaleRedimensionnee) {
88            originale = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg");
89            //if the original file has been resized, we put the metadata in the resized file
90            //I use here a try catch because if the original file isn't a jpeg
91            //the methode Outil.enrich will fail, but the procedure has to continue
92            try {
93                byte[] fichierEnrichi = Tools.enrich(bytesFichierOriginal,
94                        Tools.getBytesFromFile(new File(System.getProperty("java.io.tmpdir") + "/originale.jpg")));
95                Tools.byteToFile(System.getProperty("java.io.tmpdir") + "/originale.jpg", fichierEnrichi);
96            } catch (Exception e) {
97            }
98        } else {
99            originale = originalFile;
100
101        }
102        Image image = new Image();
103        image.setName(getImageName(filePath));
104        image.setThumbnail(thumbnail);
105        image.setOriginale(originale);
106        image.setIdCategory(idCategory);
107        image.setPrivacyLevel(String.valueOf(privacyLevel));
108        //now we call the dao to send the image to the webservice
109        return dao.create(image, chunckSize * 1000000);
110    }
111
112    /**
113     * Add tags to an existing image
114     * @param image the image
115     * @param tagId the ids of the tags
116     * @return true if successful
117     * @throws IOException
118     * @throws ProxyAuthenticationException
119     */
120    public boolean addTags(Image image, String tagId) throws JiwigoException {
121        return dao.addTags(image.getIdentifier(), tagId);
122    }
123
124    /**
125     * Search images from a string
126     * @param queryString the string
127     * @return images matching the string
128     * @throws IOException
129     * @throws ProxyAuthenticationException
130     */
131    public List<Image> search(String queryString) throws JiwigoException {
132        return dao.search(queryString);
133    }
134
135    /**
136     * Deletes the file extension
137     * @param path
138     * @return
139     */
140    private String getImageName(String path) {
141        File fichier = new File(path);
142        StringBuffer name = new StringBuffer(fichier.getName());
143        return (name.delete(name.lastIndexOf("."), name.length())).toString();
144
145    }
146
147    public void addSimple(File file, Integer category, String title) throws JiwigoException {
148        dao.addSimple(file, category, title, null);
149
150    }
151
152    public boolean delete(Image image) throws JiwigoException {
153        if (image.getIdentifier() == null) {
154            throw new JiwigoException("The identifier of the image cannot be null");
155        } else {
156            return dao.delete(image);
157        }
158
159    }
160
161    public ImageDao getDao() {
162        return dao;
163    }
164
165    public void setDao(ImageDao dao) {
166        this.dao = dao;
167    }
168
169    public void addSimple(File file, Integer category, String title, Integer level) throws JiwigoException {
170        dao.addSimple(file, category, title, level);
171    }
172
173}
Note: See TracBrowser for help on using the repository browser.