KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > editor > UploadManagerImpl


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.components.editor;
25
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.HashMap JavaDoc;
29
30 import org.riotfamily.common.image.ImageCropper;
31 import org.riotfamily.common.util.PasswordGenerator;
32 import org.riotfamily.common.web.file.FileStore;
33
34 /**
35  * @author Felix Gnass [fgnass at neteye dot de]
36  * @since 6.5
37  */

38 public class UploadManagerImpl implements UploadManager {
39
40     private PasswordGenerator tokenGenerator =
41             new PasswordGenerator(16, true, true, true);
42     
43     private HashMap JavaDoc filePaths = new HashMap JavaDoc();
44
45     private FileStore fileStore;
46     
47     private ImageCropper imageCropper;
48     
49     public UploadManagerImpl(FileStore fileStore, ImageCropper imageCropper) {
50         this.fileStore = fileStore;
51         this.imageCropper = imageCropper;
52     }
53
54     public String JavaDoc generateToken() {
55         String JavaDoc token = tokenGenerator.generate();
56         filePaths.put(token, null);
57         return token;
58     }
59     
60     public String JavaDoc getFilePath(String JavaDoc token) {
61         return (String JavaDoc) filePaths.get(token);
62     }
63     
64     boolean isValidToken(String JavaDoc token) {
65         return filePaths.containsKey(token);
66     }
67     
68     public void invalidateToken(String JavaDoc token) {
69         filePaths.remove(token);
70     }
71     
72     void storeFile(String JavaDoc uploadId, File JavaDoc file, String JavaDoc originalFileName)
73             throws IOException JavaDoc {
74         
75         String JavaDoc path = fileStore.store(file, originalFileName);
76         filePaths.put(uploadId, path);
77     }
78     
79     public String JavaDoc cropImage(String JavaDoc path, int width, int height, int x, int y,
80             int scaledWidth) throws IOException JavaDoc {
81         
82         File JavaDoc src = fileStore.retrieve(path);
83         File JavaDoc dest = File.createTempFile("crop", ".img");
84         imageCropper.cropImage(src, dest, width, height, x, y, scaledWidth);
85         fileStore.delete(path);
86         return fileStore.store(dest, src.getName());
87     }
88 }
89
Popular Tags