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

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

Complete rewriting of the api
The api now uses interfaces everywhere (for the dao, services and session manager). This allows the developer to use his own implementations if he wants.
Deletion of the singletons. There are now getters and setters. It allows to make dependency injection.
Use of sl4j in the api, instead of using log4j.

File size: 5.1 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.ProxyAuthenticationException;
16import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException;
17import fr.mael.jiwigo.transverse.util.ImagesUtil;
18import fr.mael.jiwigo.transverse.util.Tools;
19
20/*
21 *  jiwigo-ws-api Piwigo webservice access Api
22 *  Copyright (c) 2010-2011 Mael mael@le-guevel.com
23 *                All Rights Reserved
24 *
25 *  This library is free software. It comes without any warranty, to
26 *  the extent permitted by applicable law. You can redistribute it
27 *  and/or modify it under the terms of the Do What The Fuck You Want
28 *  To Public License, Version 2, as published by Sam Hocevar. See
29 *  http://sam.zoy.org/wtfpl/COPYING for more details.
30 */
31/**
32 *
33
34 * @author mael
35 *
36 */
37public class ImageServiceImpl implements ImageService {
38
39    /**
40     * Logger
41     */
42    private final Logger LOG = LoggerFactory.getLogger(ImageServiceImpl.class);
43
44    private ImageDao dao;
45
46    /**
47     * Lists all images for a category
48     * @param categoryId the id of the category
49     * @return the list of images
50     * @throws IOException
51     * @throws ProxyAuthenticationException
52     */
53    public List<Image> listByCategory(Integer categoryId, boolean rafraichir) throws IOException,
54            ProxyAuthenticationException {
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 Exception
71     */
72    public boolean create(String filePath, Integer idCategory, Integer originalWidth, Integer originalHeight,
73            Double chunckSize, Integer privacyLevel) throws IOException, NoSuchAlgorithmException,
74            FileAlreadyExistsException, ProxyAuthenticationException, WrongChunkSizeException {
75        File originalFile = new File(filePath);
76        //get the byte array of the original file, to keep metadata
77        byte[] bytesFichierOriginal = Tools.getBytesFromFile(originalFile);
78
79        //resize the picture (or not)
80        boolean originaleRedimensionnee = ImagesUtil.scale(filePath, "originale.jpg", originalWidth, originalHeight);
81        //create the thumbnail
82        ImagesUtil.scale(filePath, "thumb.jpg", 120, 90);
83        //get the thumbnail
84        File thumbnail = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg");
85        File originale = null;
86        if (originaleRedimensionnee) {
87            originale = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg");
88            //if the original file has been resized, we put the metadata in the resized file
89            //I use here a try catch because if the original file isn't a jpeg
90            //the methode Outil.enrich will fail, but the procedure has to continue
91            try {
92                byte[] fichierEnrichi = Tools.enrich(bytesFichierOriginal, Tools.getBytesFromFile(new File(System
93                        .getProperty("java.io.tmpdir")
94                        + "/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 IOException, ProxyAuthenticationException {
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 IOException, ProxyAuthenticationException {
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 ImageDao getDao() {
148        return dao;
149    }
150
151    public void setDao(ImageDao dao) {
152        this.dao = dao;
153    }
154
155}
Note: See TracBrowser for help on using the repository browser.