1 package org.paneris.messageboard.model;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.sql.ResultSet;
6
7 import org.paneris.jal.model.DDField;
8 import org.paneris.jal.model.DDRecord;
9
10
11
12
13
14 public class Attachment extends DDRecord {
15
16 private static final long serialVersionUID = 1L;
17 Integer board = new Integer(0);
18 byte[] data = null;
19
20
21
22
23
24 public Attachment(String db, ResultSet rs) throws Exception {
25 super(db,"attachments",rs);
26 }
27
28
29
30
31
32
33 public Attachment(String db) throws Exception {
34 super(db,"attachments");
35 }
36
37
38
39
40
41
42
43
44 public Attachment(String db, Integer record) throws Exception {
45 super(db,"attachments",record);
46 }
47
48
49
50
51 public void setData(byte[] data_) {
52 data = data_;
53 }
54
55
56
57
58 public void setBoard(Integer b) {
59 board = b;
60 }
61
62
63
64
65 public String getSize() {
66 Integer size = new Integer(getFieldValue("size"));
67 int s = size.intValue();
68 if (s<1024) {
69 return new String(s+" Bytes");
70 }
71 if (s<1048576) {
72 return new String((s/1024)+" K");
73 } else {
74 return new String((s/1048576)+" MB");
75 }
76 }
77
78
79 public String getAttachmentPath() throws Exception {
80 DDRecord boardRec = new DDRecord(database,"messageboards",board);
81 return boardRec.getFieldValue("attachmentpath").trim();
82 }
83
84
85
86
87
88
89 public synchronized void write() throws Exception {
90 if (data != null) {
91 String path = getAttachmentPath();
92 String extension = null;
93 File outputDir = new File(path);
94 boolean missing = !outputDir.exists();
95 if (missing) {
96 throw new RuntimeException(
97 "Attachment directory (" +path+") does not exist.");
98 }
99
100 String filename = getFieldValue("filename").trim();
101
102 if (filename.length() == 0) {
103 filename = "att";
104 DDField ct = (DDField) get("type");
105 DDRecord ctype = ct.getLookup();
106 DDField ext = (DDField) ctype.get("extension");
107 if (ext != null && ext.getValue() != null)
108 filename += ext.getValue().toString().trim();
109 }
110
111 filename = filename.replace(' ','_');
112 filename = path + "/" + filename;
113 int filenumber = 0;
114 String testFilename = new String(filename);
115 File outputFile = new File(testFilename);
116 boolean found = outputFile.exists();
117 if (found) {
118 if (testFilename.lastIndexOf(".") != -1) {
119 extension = new String(filename.substring(filename.lastIndexOf(".")+1,
120 filename.length()));
121 filename = filename.substring(0,filename.lastIndexOf("."));
122 }
123 }
124 while (found) {
125 filenumber++;
126 testFilename = new String(filename);
127 String filenumberString = "" + filenumber;
128 for (int i=0; i < (8 - filenumberString.length()); i++) {
129 testFilename += "0";
130 }
131 testFilename += filenumber;
132 if (extension != null) {
133 testFilename += "." + extension;
134 }
135 outputFile = new File(testFilename);
136 found = outputFile.exists();
137 }
138 filename = testFilename.trim();
139 setFieldValue("filename",filename.substring(path.length() + 1));
140 FileOutputStream os = new FileOutputStream(outputFile);
141 os.write(data);
142 os.close();
143 ((DDField)get("size")).setValue(new Integer(data.length));
144 }
145 super.write();
146 }
147
148 }
149