commit | author | age
|
8931b7
|
1 |
/* |
U |
2 |
|
|
3 |
Dateiverwaltung - File management in your browser |
|
4 |
Copyright (C) 2017 Ulrich Hilger, http://uhilger.de |
|
5 |
|
|
6 |
This program is free software: you can redistribute it and/or modify |
|
7 |
it under the terms of the GNU Affero General Public License as |
|
8 |
published by the Free Software Foundation, either version 3 of the |
|
9 |
License, or (at your option) any later version. |
|
10 |
|
|
11 |
This program is distributed in the hope that it will be useful, |
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
GNU Affero General Public License for more details. |
|
15 |
|
|
16 |
You should have received a copy of the GNU Affero General Public License |
|
17 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
18 |
|
|
19 |
*/ |
|
20 |
|
c7c502
|
21 |
package de.uhilger.filecms.api; |
U |
22 |
|
|
23 |
import de.uhilger.filesystem.FileRef; |
8e51b7
|
24 |
import java.io.File; |
e5ff42
|
25 |
import java.io.FileWriter; |
U |
26 |
import java.io.IOException; |
|
27 |
import java.security.Principal; |
|
28 |
import java.util.logging.Level; |
8e51b7
|
29 |
import java.util.logging.Logger; |
c7c502
|
30 |
|
U |
31 |
/** |
|
32 |
* |
|
33 |
* @author ulrich |
|
34 |
*/ |
8931b7
|
35 |
public class FileMgr extends Api { |
8e51b7
|
36 |
private static final Logger logger = Logger.getLogger(FileMgr.class.getName()); |
c7c502
|
37 |
|
U |
38 |
public static final String FILE_BASE = "fileBase"; |
|
39 |
|
ea7193
|
40 |
public String hallo() { |
U |
41 |
return "Hallo Welt!"; |
|
42 |
} |
|
43 |
|
|
44 |
// http://localhost:8097/file-cms/rpc/de.uhilger.filecms.api.FileMgr/getBase/JSONNICE |
c7c502
|
45 |
public FileRef getBase() { |
U |
46 |
FileRef ref = null; |
8e51b7
|
47 |
//String fileBase = getServletContext().getInitParameter(FILE_BASE); |
U |
48 |
// -Dfilecms.base=/pfad/zu/daten |
ea7193
|
49 |
//String fileBase = System.getProperty("filecms.base"); |
U |
50 |
//File file = new File(fileBase); |
|
51 |
//logger.info(file.getAbsolutePath()); |
|
52 |
//logger.info(getWebappsDir().getAbsolutePath()); |
|
53 |
|
|
54 |
/* |
|
55 |
file = new File("."); liefert |
|
56 |
/home/ulrich/dev/lib/java/tomcat/tomcat2-8.5.9/bin/. |
|
57 |
|
|
58 |
..auf der WebBox aber |
|
59 |
/home/ulrich/srv/wbx_probe/. |
|
60 |
..weil das Startskript dort liegt |
|
61 |
|
|
62 |
der Tomcat der WebBox ist unter |
|
63 |
sys/jrs/tomcat/bin |
|
64 |
|
|
65 |
also z.B. |
|
66 |
/home/ulrich/srv/wbx_probe/sys/jrs/tomcat/bin |
|
67 |
|
|
68 |
das Datenverzeichnis ist z.B. auf |
|
69 |
/home/ulrich/srv/wbx_probe/daten |
|
70 |
|
|
71 |
dann ist das Datenverzeichnis unter |
|
72 |
../../../daten |
|
73 |
|
|
74 |
Der Ausdruck file = new File("."); liefert stets den |
|
75 |
Ort von dem aus der Java-Prozess gestartet wurde. |
|
76 |
|
|
77 |
Die unten folgende Bestimmung des Datenverzeichnisses |
|
78 |
ist beschraenkt auf das Datenverzeichnis der WebBox, |
|
79 |
entweder relativ zum Startskript der WebBox oder |
|
80 |
dem Startskript von Tomcat, wie es aus Netbeans heraus |
|
81 |
waehrend der Entwicklung genutzt wird. |
|
82 |
|
|
83 |
Besser ware vielleicht eine Bestimmung ueber einen |
|
84 |
Systemparameter -Dfilecms.base=... wie weiter oben |
|
85 |
auskommentiert. Damit liesse sich das file-cms auch |
|
86 |
ohne WebBox einsetzen. Allerdings muss dann das |
|
87 |
Datenverzeichnis im Start-Skript gebildet werden, |
|
88 |
also ausserhalb von Java, wenn es dynamisch aus |
|
89 |
einem Pfad relativ zum Start-Skript erzeugt werden |
|
90 |
soll. |
|
91 |
*/ |
|
92 |
|
|
93 |
File file = new File("."); |
|
94 |
String path = file.getAbsolutePath(); |
|
95 |
path = path.substring(0, path.length() - 1); |
|
96 |
file = new File(path); |
|
97 |
if(path.endsWith("bin")) { |
|
98 |
file = file.getParentFile().getParentFile().getParentFile(); |
|
99 |
} else { |
|
100 |
|
|
101 |
} |
|
102 |
file = new File(file, "daten/"); |
8e51b7
|
103 |
ref = new FileRef(file.getAbsolutePath(), file.isDirectory()); |
c7c502
|
104 |
return ref; |
8e51b7
|
105 |
} |
U |
106 |
|
ea7193
|
107 |
public FileRef saveTextFile(String relPath, String fileName, String contents) { |
U |
108 |
FileRef savedFile = null; |
e5ff42
|
109 |
try { |
U |
110 |
FileRef datenRef = getBase(); |
|
111 |
File daten = new File(datenRef.getAbsolutePath()); |
|
112 |
Object p = getRequest().getUserPrincipal(); |
|
113 |
if(p instanceof Principal) { |
|
114 |
File userDir = new File(daten, "www/" + ((Principal) p).getName()); |
|
115 |
File saveDir = new File(userDir, relPath); |
|
116 |
File targetFile = new File(saveDir, fileName); |
|
117 |
if(!targetFile.exists()) { |
915927
|
118 |
targetFile.getParentFile().mkdirs(); |
e5ff42
|
119 |
targetFile.createNewFile(); |
U |
120 |
FileWriter w = new FileWriter(targetFile); |
|
121 |
w.write(contents); |
|
122 |
w.flush(); |
|
123 |
w.close(); |
8931b7
|
124 |
savedFile = new FileRef( |
U |
125 |
targetFile.getAbsolutePath(), |
|
126 |
targetFile.isDirectory(), |
|
127 |
targetFile.isHidden(), |
|
128 |
targetFile.lastModified(), |
|
129 |
targetFile.length()); |
e5ff42
|
130 |
} |
U |
131 |
} |
|
132 |
} catch (IOException ex) { |
|
133 |
logger.log(Level.SEVERE, null, ex); |
|
134 |
} |
ea7193
|
135 |
return savedFile; |
U |
136 |
} |
|
137 |
|
8e51b7
|
138 |
private File getWebappsDir() { |
8931b7
|
139 |
File cfile = new File(this.getClass().getResource( |
U |
140 |
this.getClass().getSimpleName() + ".class").getFile()); |
8e51b7
|
141 |
String path = cfile.getAbsolutePath(); |
8931b7
|
142 |
return new File(path.substring(0, path.indexOf(getRequest().getContextPath()))); |
c7c502
|
143 |
} |
U |
144 |
|
|
145 |
} |