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

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

Changed the licence
to a funniest one.

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