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

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

Adds a better Exception management
new Exception : FileAlreadyExistsException.

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