commit | author | age
|
8931b7
|
1 |
/* |
U |
2 |
Dateiverwaltung - File management in your browser |
|
3 |
Copyright (C) 2017 Ulrich Hilger, http://uhilger.de |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
|
c7c502
|
19 |
package de.uhilger.filecms.api; |
U |
20 |
|
7342b1
|
21 |
import de.uhilger.filecms.web.Initialiser; |
c7c502
|
22 |
import de.uhilger.filesystem.FileRef; |
5dfab6
|
23 |
import de.uhilger.filesystem.LocalFileSystem; |
8e51b7
|
24 |
import java.io.File; |
3ad4db
|
25 |
import java.io.FileNotFoundException; |
U |
26 |
import java.io.FileReader; |
e5ff42
|
27 |
import java.io.FileWriter; |
U |
28 |
import java.io.IOException; |
3ad4db
|
29 |
import java.io.StringReader; |
e5ff42
|
30 |
import java.security.Principal; |
7342b1
|
31 |
import java.util.ArrayList; |
U |
32 |
import java.util.List; |
e5ff42
|
33 |
import java.util.logging.Level; |
8e51b7
|
34 |
import java.util.logging.Logger; |
c7c502
|
35 |
|
U |
36 |
/** |
|
37 |
* |
|
38 |
* @author ulrich |
|
39 |
*/ |
8931b7
|
40 |
public class FileMgr extends Api { |
8e51b7
|
41 |
private static final Logger logger = Logger.getLogger(FileMgr.class.getName()); |
c7c502
|
42 |
|
7342b1
|
43 |
public static final String PUB_DIR_PATH = "www/"; |
U |
44 |
public static final String HOME_DIR_PATH = "home/"; |
|
45 |
public static final String PUB_DIR_NAME = "Oeffentlich"; |
|
46 |
public static final String HOME_DIR_NAME = "Persoenlicher Ordner"; |
c7c502
|
47 |
|
ea7193
|
48 |
public String hallo() { |
U |
49 |
return "Hallo Welt!"; |
|
50 |
} |
|
51 |
|
7342b1
|
52 |
public List<FileRef> list(String relPath) { |
5dfab6
|
53 |
List<FileRef> files = new ArrayList(); |
7342b1
|
54 |
if(relPath.length() == 0) { |
2121cc
|
55 |
FileRef namedPublicFolder = new FileRef(PUB_DIR_NAME, true); |
5dfab6
|
56 |
logger.finer(namedPublicFolder.getAbsolutePath()); |
2121cc
|
57 |
FileRef namedHomeFolder = new FileRef(HOME_DIR_NAME, true); |
5dfab6
|
58 |
logger.finer(namedHomeFolder.getAbsolutePath()); |
2121cc
|
59 |
files = new ArrayList(); |
7342b1
|
60 |
files.add(namedHomeFolder); |
U |
61 |
files.add(namedPublicFolder); |
ea7193
|
62 |
} else { |
2121cc
|
63 |
String path = getTargetDir(relPath).getAbsolutePath(); |
U |
64 |
logger.fine(path); |
5efd94
|
65 |
LocalFileSystem fs = new LocalFileSystem(); |
2121cc
|
66 |
FileRef[] fileRefs = fs.list(new FileRef(getTargetDir(relPath).getAbsolutePath(), true)); |
5dfab6
|
67 |
for(int i = 0; i < fileRefs.length; i++) { |
U |
68 |
files.add(fileRefs[i]); |
2121cc
|
69 |
logger.finer("added " + fileRefs[i].getAbsolutePath()); |
5dfab6
|
70 |
} |
c509a0
|
71 |
} |
7342b1
|
72 |
return files; |
2121cc
|
73 |
} |
U |
74 |
|
c509a0
|
75 |
public FileRef newFolder(String relPath, String folderName) { |
U |
76 |
logger.finer(relPath); |
|
77 |
String targetPath = null; |
|
78 |
if(relPath.startsWith(PUB_DIR_NAME)) { |
|
79 |
targetPath = PUB_DIR_PATH + getUserName() + "/" + relPath.substring(PUB_DIR_NAME.length()) + "/" + folderName; |
|
80 |
} else if(relPath.startsWith(HOME_DIR_NAME)) { |
|
81 |
targetPath = HOME_DIR_PATH + getUserName() + "/" + relPath.substring(HOME_DIR_NAME.length()) + "/" + folderName; |
|
82 |
} else { |
|
83 |
// kann eigentlich nicht sein.. |
|
84 |
} |
|
85 |
logger.finer(targetPath); |
|
86 |
File targetDir = new File(getBase().getAbsolutePath(), targetPath); |
|
87 |
targetDir.mkdirs(); |
|
88 |
return new FileRef(targetDir.getAbsolutePath(), true); |
5dfab6
|
89 |
} |
U |
90 |
|
3ad4db
|
91 |
public String getCode(String relPath, String fileName) { |
U |
92 |
String code = null; |
|
93 |
|
|
94 |
Object p = getRequest().getUserPrincipal(); |
|
95 |
if(p instanceof Principal) { |
|
96 |
FileReader reader = null; |
|
97 |
try { |
|
98 |
File targetFile = new File(getTargetDir(relPath), fileName); |
|
99 |
reader = new FileReader(targetFile); |
|
100 |
StringBuffer buf = new StringBuffer(); |
|
101 |
char[] readBuffer = new char[1024]; |
|
102 |
int charsRead = reader.read(readBuffer); |
|
103 |
while(charsRead > -1) { |
|
104 |
buf.append(readBuffer, 0, charsRead); |
|
105 |
charsRead = reader.read(readBuffer); |
|
106 |
} |
|
107 |
code = buf.toString(); |
|
108 |
} catch (FileNotFoundException ex) { |
|
109 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
110 |
} catch (IOException ex) { |
|
111 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
112 |
} finally { |
|
113 |
try { |
|
114 |
reader.close(); |
|
115 |
} catch (IOException ex) { |
|
116 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
117 |
} |
|
118 |
} |
|
119 |
|
|
120 |
} |
|
121 |
|
|
122 |
return code; |
|
123 |
} |
|
124 |
|
ea7193
|
125 |
public FileRef saveTextFile(String relPath, String fileName, String contents) { |
U |
126 |
FileRef savedFile = null; |
e5ff42
|
127 |
try { |
U |
128 |
FileRef datenRef = getBase(); |
3ad4db
|
129 |
//File daten = new File(datenRef.getAbsolutePath()); |
e5ff42
|
130 |
Object p = getRequest().getUserPrincipal(); |
U |
131 |
if(p instanceof Principal) { |
2121cc
|
132 |
File targetFile = new File(getTargetDir(relPath), fileName); |
7342b1
|
133 |
if(targetFile.exists()) { |
U |
134 |
/* |
|
135 |
muss delete() sein? |
|
136 |
pruefen: ueberschreibt der FileWriter den alteen Inhalt oder |
|
137 |
entsteht eine unerwuenschte Mischung aus altem und neuem |
|
138 |
Inhalt? |
|
139 |
*/ |
|
140 |
targetFile.delete(); |
|
141 |
} else { |
915927
|
142 |
targetFile.getParentFile().mkdirs(); |
e5ff42
|
143 |
} |
7342b1
|
144 |
targetFile.createNewFile(); |
U |
145 |
FileWriter w = new FileWriter(targetFile); |
|
146 |
w.write(contents); |
|
147 |
w.flush(); |
|
148 |
w.close(); |
|
149 |
savedFile = new FileRef( |
|
150 |
targetFile.getAbsolutePath(), |
|
151 |
targetFile.isDirectory(), |
|
152 |
targetFile.isHidden(), |
|
153 |
targetFile.lastModified(), |
|
154 |
targetFile.length()); |
e5ff42
|
155 |
} |
U |
156 |
} catch (IOException ex) { |
|
157 |
logger.log(Level.SEVERE, null, ex); |
|
158 |
} |
ea7193
|
159 |
return savedFile; |
U |
160 |
} |
5efd94
|
161 |
|
U |
162 |
/* ---- Hilfsfunktionen ---- */ |
ea7193
|
163 |
|
5efd94
|
164 |
private File getTargetDir(String relPath) { |
U |
165 |
logger.finer(relPath); |
|
166 |
String targetPath = null; |
|
167 |
if(relPath.startsWith(PUB_DIR_NAME)) { |
|
168 |
targetPath = PUB_DIR_PATH + getUserName() + "/" + relPath.substring(PUB_DIR_NAME.length()); |
|
169 |
} else if(relPath.startsWith(HOME_DIR_NAME)) { |
|
170 |
targetPath = HOME_DIR_PATH + getUserName() + "/" + relPath.substring(HOME_DIR_NAME.length()); |
|
171 |
} else { |
|
172 |
// kann eigentlich nicht sein.. |
|
173 |
} |
|
174 |
logger.finer(targetPath); |
|
175 |
File targetDir = new File(getBase().getAbsolutePath(), targetPath); |
|
176 |
return targetDir; |
|
177 |
} |
|
178 |
private FileRef getBase() { |
|
179 |
FileRef base = null; |
|
180 |
Object o = getServletContext().getAttribute(Initialiser.FILE_BASE); |
|
181 |
if(o instanceof FileRef) { |
|
182 |
base = (FileRef) o; |
|
183 |
} |
|
184 |
return base; |
|
185 |
} |
|
186 |
|
|
187 |
private String getUserName() { |
|
188 |
String userName = null; |
|
189 |
Object p = getRequest().getUserPrincipal(); |
|
190 |
if(p instanceof Principal) { |
|
191 |
userName = ((Principal) p).getName(); |
|
192 |
} |
|
193 |
return userName; |
|
194 |
} |
|
195 |
|
|
196 |
/* |
8e51b7
|
197 |
private File getWebappsDir() { |
8931b7
|
198 |
File cfile = new File(this.getClass().getResource( |
U |
199 |
this.getClass().getSimpleName() + ".class").getFile()); |
8e51b7
|
200 |
String path = cfile.getAbsolutePath(); |
8931b7
|
201 |
return new File(path.substring(0, path.indexOf(getRequest().getContextPath()))); |
c7c502
|
202 |
} |
5efd94
|
203 |
*/ |
7342b1
|
204 |
|
c7c502
|
205 |
} |