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; |
8e51b7
|
23 |
import java.io.File; |
e5ff42
|
24 |
import java.io.FileWriter; |
U |
25 |
import java.io.IOException; |
|
26 |
import java.security.Principal; |
7342b1
|
27 |
import java.util.ArrayList; |
U |
28 |
import java.util.List; |
e5ff42
|
29 |
import java.util.logging.Level; |
8e51b7
|
30 |
import java.util.logging.Logger; |
c7c502
|
31 |
|
U |
32 |
/** |
|
33 |
* |
|
34 |
* @author ulrich |
|
35 |
*/ |
8931b7
|
36 |
public class FileMgr extends Api { |
8e51b7
|
37 |
private static final Logger logger = Logger.getLogger(FileMgr.class.getName()); |
c7c502
|
38 |
|
7342b1
|
39 |
public static final String PUB_DIR_PATH = "www/"; |
U |
40 |
public static final String HOME_DIR_PATH = "home/"; |
|
41 |
public static final String PUB_DIR_NAME = "Oeffentlich"; |
|
42 |
public static final String HOME_DIR_NAME = "Persoenlicher Ordner"; |
c7c502
|
43 |
|
7342b1
|
44 |
private FileRef homeFolder; |
U |
45 |
private FileRef publicFolder; |
|
46 |
private FileRef namedHomeFolder; |
|
47 |
private FileRef namedPublicFolder; |
|
48 |
|
|
49 |
|
ea7193
|
50 |
public String hallo() { |
U |
51 |
return "Hallo Welt!"; |
|
52 |
} |
|
53 |
|
7342b1
|
54 |
public List<FileRef> list(String relPath) { |
U |
55 |
List<FileRef> files = null; |
ea7193
|
56 |
|
7342b1
|
57 |
if(relPath.length() == 0) { |
U |
58 |
publicFolder = new FileRef(getUserPubDir().getAbsolutePath(), true); |
|
59 |
logger.info(publicFolder.getAbsolutePath()); |
|
60 |
homeFolder = new FileRef(getUserHomeDir().getAbsolutePath(), true); |
|
61 |
logger.info(homeFolder.getAbsolutePath()); |
ea7193
|
62 |
|
7342b1
|
63 |
namedPublicFolder = new FileRef(PUB_DIR_NAME, true); |
U |
64 |
logger.info(namedPublicFolder.getAbsolutePath()); |
|
65 |
namedHomeFolder = new FileRef(HOME_DIR_NAME, true); |
|
66 |
logger.info(namedHomeFolder.getAbsolutePath()); |
|
67 |
files = new ArrayList<FileRef>(); |
|
68 |
files.add(namedHomeFolder); |
|
69 |
files.add(namedPublicFolder); |
ea7193
|
70 |
} else { |
U |
71 |
|
|
72 |
} |
7342b1
|
73 |
|
U |
74 |
return files; |
|
75 |
} |
|
76 |
|
|
77 |
private FileRef getBase() { |
|
78 |
FileRef base = null; |
|
79 |
Object o = getServletContext().getAttribute(Initialiser.FILE_BASE); |
|
80 |
if(o instanceof FileRef) { |
|
81 |
base = (FileRef) o; |
|
82 |
} |
|
83 |
return base; |
|
84 |
} |
|
85 |
|
|
86 |
private File getUserPubDir() { |
|
87 |
File userDir = null; |
|
88 |
File daten = new File(getBase().getAbsolutePath()); |
|
89 |
Object p = getRequest().getUserPrincipal(); |
|
90 |
if(p instanceof Principal) { |
|
91 |
userDir = new File(daten, PUB_DIR_PATH + ((Principal) p).getName()); |
|
92 |
} |
|
93 |
return userDir; |
|
94 |
} |
|
95 |
|
|
96 |
private File getUserHomeDir() { |
|
97 |
File userDir = null; |
|
98 |
File daten = new File(getBase().getAbsolutePath()); |
|
99 |
Object p = getRequest().getUserPrincipal(); |
|
100 |
if(p instanceof Principal) { |
|
101 |
userDir = new File(daten, HOME_DIR_PATH + ((Principal) p).getName()); |
|
102 |
} |
|
103 |
return userDir; |
8e51b7
|
104 |
} |
U |
105 |
|
ea7193
|
106 |
public FileRef saveTextFile(String relPath, String fileName, String contents) { |
U |
107 |
FileRef savedFile = null; |
e5ff42
|
108 |
try { |
U |
109 |
FileRef datenRef = getBase(); |
|
110 |
File daten = new File(datenRef.getAbsolutePath()); |
|
111 |
Object p = getRequest().getUserPrincipal(); |
|
112 |
if(p instanceof Principal) { |
|
113 |
File userDir = new File(daten, "www/" + ((Principal) p).getName()); |
|
114 |
File saveDir = new File(userDir, relPath); |
|
115 |
File targetFile = new File(saveDir, fileName); |
7342b1
|
116 |
if(targetFile.exists()) { |
U |
117 |
/* |
|
118 |
muss delete() sein? |
|
119 |
pruefen: ueberschreibt der FileWriter den alteen Inhalt oder |
|
120 |
entsteht eine unerwuenschte Mischung aus altem und neuem |
|
121 |
Inhalt? |
|
122 |
*/ |
|
123 |
targetFile.delete(); |
|
124 |
} else { |
915927
|
125 |
targetFile.getParentFile().mkdirs(); |
e5ff42
|
126 |
} |
7342b1
|
127 |
targetFile.createNewFile(); |
U |
128 |
FileWriter w = new FileWriter(targetFile); |
|
129 |
w.write(contents); |
|
130 |
w.flush(); |
|
131 |
w.close(); |
|
132 |
savedFile = new FileRef( |
|
133 |
targetFile.getAbsolutePath(), |
|
134 |
targetFile.isDirectory(), |
|
135 |
targetFile.isHidden(), |
|
136 |
targetFile.lastModified(), |
|
137 |
targetFile.length()); |
e5ff42
|
138 |
} |
U |
139 |
} catch (IOException ex) { |
|
140 |
logger.log(Level.SEVERE, null, ex); |
|
141 |
} |
ea7193
|
142 |
return savedFile; |
U |
143 |
} |
|
144 |
|
8e51b7
|
145 |
private File getWebappsDir() { |
8931b7
|
146 |
File cfile = new File(this.getClass().getResource( |
U |
147 |
this.getClass().getSimpleName() + ".class").getFile()); |
8e51b7
|
148 |
String path = cfile.getAbsolutePath(); |
8931b7
|
149 |
return new File(path.substring(0, path.indexOf(getRequest().getContextPath()))); |
c7c502
|
150 |
} |
7342b1
|
151 |
|
c7c502
|
152 |
} |