KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > attachment > storage > FileAttachmentStorage


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.snip.attachment.storage;
27
28 import org.snipsnap.snip.attachment.Attachment;
29 import org.snipsnap.config.Configuration;
30 import org.snipsnap.app.Application;
31
32 import java.io.*;
33
34 /**
35  * AttachmentStorage which stores attachment in the file system
36  *
37  * @author Stephan J. Schmidt
38  * @version $Id: FileAttachmentStorage.java 1693 2004-06-27 10:59:44Z leo $
39  */

40
41 public class FileAttachmentStorage implements AttachmentStorage {
42   private File getFile(Attachment attachment) {
43     Configuration config = Application.get().getConfiguration();
44     File filePath = config.getFilePath();
45
46     return new File(filePath, attachment.getLocation());
47   }
48
49   public boolean exists(Attachment attachment) {
50     return getFile(attachment).exists();
51   }
52
53   public OutputStream getOutputStream(Attachment attachment) throws IOException {
54     File file = getFile(attachment);
55
56     // check and create the directory, where to store the snip attachments
57
if (!file.getParentFile().isDirectory()) {
58       file.getParentFile().mkdirs();
59     }
60
61     return new FileOutputStream(file);
62   }
63
64   public InputStream getInputStream(Attachment attachment) throws IOException {
65     return new FileInputStream(getFile(attachment));
66   }
67
68   public void delete(Attachment attachment) {
69     getFile(attachment).delete();
70   }
71
72   public boolean verify(Attachment attachment) throws IOException {
73     if(exists(attachment)) {
74       boolean modified = false;
75       File file = getFile(attachment);
76       if(file.length() != attachment.getSize()) {
77         attachment.setSize(file.length());
78         modified = true;
79       }
80       return !modified;
81     }
82
83     // throw file not found exception if it does not exist
84
throw new FileNotFoundException(getFile(attachment).getCanonicalPath());
85   }
86 }
87
Popular Tags