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; |
e5ff42
|
25 |
import java.io.FileWriter; |
U |
26 |
import java.io.IOException; |
|
27 |
import java.security.Principal; |
7342b1
|
28 |
import java.util.ArrayList; |
U |
29 |
import java.util.List; |
e5ff42
|
30 |
import java.util.logging.Level; |
8e51b7
|
31 |
import java.util.logging.Logger; |
c7c502
|
32 |
|
U |
33 |
/** |
|
34 |
* |
|
35 |
* @author ulrich |
|
36 |
*/ |
8931b7
|
37 |
public class FileMgr extends Api { |
8e51b7
|
38 |
private static final Logger logger = Logger.getLogger(FileMgr.class.getName()); |
c7c502
|
39 |
|
7342b1
|
40 |
public static final String PUB_DIR_PATH = "www/"; |
U |
41 |
public static final String HOME_DIR_PATH = "home/"; |
|
42 |
public static final String PUB_DIR_NAME = "Oeffentlich"; |
|
43 |
public static final String HOME_DIR_NAME = "Persoenlicher Ordner"; |
c7c502
|
44 |
|
2121cc
|
45 |
//private FileRef homeFolder; |
U |
46 |
//private FileRef publicFolder; |
|
47 |
//private FileRef namedHomeFolder; |
|
48 |
//private FileRef namedPublicFolder; |
7342b1
|
49 |
|
U |
50 |
|
ea7193
|
51 |
public String hallo() { |
U |
52 |
return "Hallo Welt!"; |
|
53 |
} |
|
54 |
|
7342b1
|
55 |
public List<FileRef> list(String relPath) { |
5dfab6
|
56 |
List<FileRef> files = new ArrayList(); |
ea7193
|
57 |
|
7342b1
|
58 |
if(relPath.length() == 0) { |
5dfab6
|
59 |
/* |
7342b1
|
60 |
publicFolder = new FileRef(getUserPubDir().getAbsolutePath(), true); |
U |
61 |
logger.info(publicFolder.getAbsolutePath()); |
|
62 |
homeFolder = new FileRef(getUserHomeDir().getAbsolutePath(), true); |
|
63 |
logger.info(homeFolder.getAbsolutePath()); |
5dfab6
|
64 |
*/ |
2121cc
|
65 |
FileRef namedPublicFolder = new FileRef(PUB_DIR_NAME, true); |
5dfab6
|
66 |
logger.finer(namedPublicFolder.getAbsolutePath()); |
2121cc
|
67 |
FileRef namedHomeFolder = new FileRef(HOME_DIR_NAME, true); |
5dfab6
|
68 |
logger.finer(namedHomeFolder.getAbsolutePath()); |
2121cc
|
69 |
files = new ArrayList(); |
7342b1
|
70 |
files.add(namedHomeFolder); |
U |
71 |
files.add(namedPublicFolder); |
ea7193
|
72 |
} else { |
2121cc
|
73 |
/* |
U |
74 |
logger.finer(relPath); |
5dfab6
|
75 |
String targetPath = null; |
U |
76 |
if(relPath.startsWith(PUB_DIR_NAME)) { |
|
77 |
targetPath = PUB_DIR_PATH + getUserName() + "/" + relPath.substring(PUB_DIR_NAME.length()); |
|
78 |
} else if(relPath.startsWith(HOME_DIR_NAME)) { |
|
79 |
targetPath = HOME_DIR_PATH + getUserName() + "/" + relPath.substring(HOME_DIR_NAME.length()); |
|
80 |
} else { |
|
81 |
// kann eigentlich nicht sein.. |
|
82 |
} |
2121cc
|
83 |
logger.finer(targetPath); |
5dfab6
|
84 |
File targetDir = new File(getBase().getAbsolutePath(), targetPath); |
2121cc
|
85 |
*/ |
ea7193
|
86 |
|
5dfab6
|
87 |
LocalFileSystem fs = new LocalFileSystem(); |
2121cc
|
88 |
String path = getTargetDir(relPath).getAbsolutePath(); |
U |
89 |
logger.fine(path); |
|
90 |
FileRef[] fileRefs = fs.list(new FileRef(getTargetDir(relPath).getAbsolutePath(), true)); |
5dfab6
|
91 |
for(int i = 0; i < fileRefs.length; i++) { |
U |
92 |
files.add(fileRefs[i]); |
2121cc
|
93 |
logger.finer("added " + fileRefs[i].getAbsolutePath()); |
5dfab6
|
94 |
} |
U |
95 |
//files = Arrays.asList(fileRefs); |
ea7193
|
96 |
} |
7342b1
|
97 |
|
U |
98 |
return files; |
2121cc
|
99 |
} |
U |
100 |
|
|
101 |
private File getTargetDir(String relPath) { |
|
102 |
logger.finer(relPath); |
|
103 |
String targetPath = null; |
|
104 |
if(relPath.startsWith(PUB_DIR_NAME)) { |
|
105 |
targetPath = PUB_DIR_PATH + getUserName() + "/" + relPath.substring(PUB_DIR_NAME.length()); |
|
106 |
} else if(relPath.startsWith(HOME_DIR_NAME)) { |
|
107 |
targetPath = HOME_DIR_PATH + getUserName() + "/" + relPath.substring(HOME_DIR_NAME.length()); |
|
108 |
} else { |
|
109 |
// kann eigentlich nicht sein.. |
|
110 |
} |
|
111 |
logger.finer(targetPath); |
|
112 |
File targetDir = new File(getBase().getAbsolutePath(), targetPath); |
|
113 |
return targetDir; |
7342b1
|
114 |
} |
U |
115 |
|
|
116 |
private FileRef getBase() { |
|
117 |
FileRef base = null; |
|
118 |
Object o = getServletContext().getAttribute(Initialiser.FILE_BASE); |
|
119 |
if(o instanceof FileRef) { |
|
120 |
base = (FileRef) o; |
|
121 |
} |
|
122 |
return base; |
|
123 |
} |
|
124 |
|
2121cc
|
125 |
/* |
7342b1
|
126 |
private File getUserPubDir() { |
U |
127 |
File userDir = null; |
|
128 |
File daten = new File(getBase().getAbsolutePath()); |
|
129 |
Object p = getRequest().getUserPrincipal(); |
|
130 |
if(p instanceof Principal) { |
|
131 |
userDir = new File(daten, PUB_DIR_PATH + ((Principal) p).getName()); |
|
132 |
} |
|
133 |
return userDir; |
|
134 |
} |
2121cc
|
135 |
*/ |
7342b1
|
136 |
|
5dfab6
|
137 |
private String getUserName() { |
U |
138 |
String userName = null; |
|
139 |
Object p = getRequest().getUserPrincipal(); |
|
140 |
if(p instanceof Principal) { |
|
141 |
userName = ((Principal) p).getName(); |
|
142 |
} |
|
143 |
return userName; |
|
144 |
} |
|
145 |
|
2121cc
|
146 |
/* |
7342b1
|
147 |
private File getUserHomeDir() { |
U |
148 |
File userDir = null; |
|
149 |
File daten = new File(getBase().getAbsolutePath()); |
|
150 |
Object p = getRequest().getUserPrincipal(); |
|
151 |
if(p instanceof Principal) { |
|
152 |
userDir = new File(daten, HOME_DIR_PATH + ((Principal) p).getName()); |
|
153 |
} |
|
154 |
return userDir; |
8e51b7
|
155 |
} |
2121cc
|
156 |
*/ |
8e51b7
|
157 |
|
ea7193
|
158 |
public FileRef saveTextFile(String relPath, String fileName, String contents) { |
U |
159 |
FileRef savedFile = null; |
e5ff42
|
160 |
try { |
U |
161 |
FileRef datenRef = getBase(); |
|
162 |
File daten = new File(datenRef.getAbsolutePath()); |
|
163 |
Object p = getRequest().getUserPrincipal(); |
|
164 |
if(p instanceof Principal) { |
2121cc
|
165 |
/* |
e5ff42
|
166 |
File userDir = new File(daten, "www/" + ((Principal) p).getName()); |
U |
167 |
File saveDir = new File(userDir, relPath); |
2121cc
|
168 |
*/ |
U |
169 |
File targetFile = new File(getTargetDir(relPath), fileName); |
7342b1
|
170 |
if(targetFile.exists()) { |
U |
171 |
/* |
|
172 |
muss delete() sein? |
|
173 |
pruefen: ueberschreibt der FileWriter den alteen Inhalt oder |
|
174 |
entsteht eine unerwuenschte Mischung aus altem und neuem |
|
175 |
Inhalt? |
|
176 |
*/ |
|
177 |
targetFile.delete(); |
|
178 |
} else { |
915927
|
179 |
targetFile.getParentFile().mkdirs(); |
e5ff42
|
180 |
} |
7342b1
|
181 |
targetFile.createNewFile(); |
U |
182 |
FileWriter w = new FileWriter(targetFile); |
|
183 |
w.write(contents); |
|
184 |
w.flush(); |
|
185 |
w.close(); |
|
186 |
savedFile = new FileRef( |
|
187 |
targetFile.getAbsolutePath(), |
|
188 |
targetFile.isDirectory(), |
|
189 |
targetFile.isHidden(), |
|
190 |
targetFile.lastModified(), |
|
191 |
targetFile.length()); |
e5ff42
|
192 |
} |
U |
193 |
} catch (IOException ex) { |
|
194 |
logger.log(Level.SEVERE, null, ex); |
|
195 |
} |
ea7193
|
196 |
return savedFile; |
U |
197 |
} |
|
198 |
|
8e51b7
|
199 |
private File getWebappsDir() { |
8931b7
|
200 |
File cfile = new File(this.getClass().getResource( |
U |
201 |
this.getClass().getSimpleName() + ".class").getFile()); |
8e51b7
|
202 |
String path = cfile.getAbsolutePath(); |
8931b7
|
203 |
return new File(path.substring(0, path.indexOf(getRequest().getContextPath()))); |
c7c502
|
204 |
} |
7342b1
|
205 |
|
c7c502
|
206 |
} |