1 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 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 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 new FileNotFoundException(getFile(attachment).getCanonicalPath()); 85 } 86 } 87 | Popular Tags |