Dateiverwaltung für die WebBox
ulrich
2017-03-06 47e9d48f23fb2803f9c6cc80e48b2b3884d4841f
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
5bfd34 21 import de.uhilger.filecms.data.FileRef;
7342b1 22 import de.uhilger.filecms.web.Initialiser;
8e51b7 23 import java.io.File;
3ad4db 24 import java.io.FileNotFoundException;
U 25 import java.io.FileReader;
e5ff42 26 import java.io.FileWriter;
U 27 import java.io.IOException;
28 import java.security.Principal;
7342b1 29 import java.util.ArrayList;
5bfd34 30 import java.util.Iterator;
7342b1 31 import java.util.List;
e5ff42 32 import java.util.logging.Level;
8e51b7 33 import java.util.logging.Logger;
5bfd34 34 import org.apache.commons.io.FileUtils;
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   
9e2964 48   public static final int OP_COPY = 1;
U 49   public static final int OP_MOVE = 2;
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();
7342b1 57     if(relPath.length() == 0) {
2121cc 58       FileRef namedPublicFolder = new FileRef(PUB_DIR_NAME, true);
5dfab6 59       logger.finer(namedPublicFolder.getAbsolutePath());
2121cc 60       FileRef namedHomeFolder = new FileRef(HOME_DIR_NAME, true);
5dfab6 61       logger.finer(namedHomeFolder.getAbsolutePath());
2121cc 62       files = new ArrayList();
7342b1 63       files.add(namedHomeFolder);
U 64       files.add(namedPublicFolder);
ea7193 65     } else {
2121cc 66       String path = getTargetDir(relPath).getAbsolutePath();
5bfd34 67       logger.fine("listing path: " + path);
U 68       File dir = new File(path);
69       if(dir.exists()) {
70         File[] fileArray = dir.listFiles();
71         for(int i = 0; i < fileArray.length; i++) {
72           logger.fine(fileArray[i].toURI().toString());
73           String fname = fileArray[i].toURI().toString().replace("file:/", "");
74           if(fileArray[i].isDirectory()) {
75             fname = fname.substring(0, fname.length() - 1);
76           }
77           logger.fine(fname);
78           files.add(new FileRef(fname, fileArray[i].isDirectory()));
79         }
5dfab6 80       }
c509a0 81     }    
7342b1 82     return files;
2121cc 83   }
U 84   
c509a0 85   public FileRef newFolder(String relPath, String folderName) {
U 86     logger.finer(relPath);
87     String targetPath = null;
88     if(relPath.startsWith(PUB_DIR_NAME)) {
89       targetPath = PUB_DIR_PATH + getUserName() + "/" + relPath.substring(PUB_DIR_NAME.length()) + "/" + folderName;
90     } else if(relPath.startsWith(HOME_DIR_NAME)) {
91       targetPath = HOME_DIR_PATH + getUserName() + "/" + relPath.substring(HOME_DIR_NAME.length()) + "/" + folderName;
92     } else {
93       // kann eigentlich nicht sein..
94     }
95     logger.finer(targetPath);
96     File targetDir = new File(getBase().getAbsolutePath(), targetPath);
97     targetDir.mkdirs();
98     return new FileRef(targetDir.getAbsolutePath(), true);
5dfab6 99   }
U 100   
3ad4db 101   public String getCode(String relPath, String fileName) {
U 102     String code = null;
103     
104     Object p = getRequest().getUserPrincipal();
105     if(p instanceof Principal) {
106       FileReader reader = null;
107       try {
108         File targetFile = new File(getTargetDir(relPath), fileName);
109         reader = new FileReader(targetFile);
110         StringBuffer buf = new StringBuffer();
111         char[] readBuffer = new char[1024];
112         int charsRead = reader.read(readBuffer);
113         while(charsRead > -1) {
114           buf.append(readBuffer, 0, charsRead);
115           charsRead = reader.read(readBuffer);
116         }
117         code = buf.toString();
118       } catch (FileNotFoundException ex) {
119         Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex);
120       } catch (IOException ex) {
121         Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex);
122       } finally {
123         try {
124           reader.close();
125         } catch (IOException ex) {
126           Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex);
127         }
128       }
129       
130     }    
131     
132     return code;
133   }
134   
663ee9 135   public String renameFile(String relPath, String fname, String newName) {
U 136     File targetDir = getTargetDir(relPath);
137     File file = new File(targetDir, fname);
138     file.renameTo(new File(targetDir, newName));
139     return fname + " umbenannt zu " + newName;
140   }
141   
fc1897 142   public String deleteFiles(String relPath, List fileNames) {
U 143     String result = null;
144     try {
145       logger.fine(fileNames.toString());
146       File targetDir = getTargetDir(relPath);
147       for(int i=0; i < fileNames.size(); i++) {
148         Object o = fileNames.get(i);
149         if(o instanceof ArrayList) {
150           ArrayList al = (ArrayList) o;
151           logger.fine(al.get(0).toString());
152           File targetFile = new File(targetDir, al.get(0).toString());
153           logger.fine(targetFile.getAbsolutePath());
5bfd34 154           if(targetFile.isDirectory()) {
U 155             FileUtils.deleteDirectory(targetFile);
156           } else {
157             targetFile.delete();
158           }
fc1897 159         }
U 160       }
161       result = "deleted";
162     } catch (Throwable ex) {
163       logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
164     }
165     return result;
166   }
167   
9e2964 168   public String copyFiles(String fromPath, String toPath, List fileNames) {
fab629 169     return copyOrMoveFiles(fromPath, toPath, fileNames, OP_COPY);
9e2964 170   }
U 171   
172   public String moveFiles(String fromPath, String toPath, List fileNames) {
fab629 173     return copyOrMoveFiles(fromPath, toPath, fileNames, OP_MOVE);
U 174   }
175   
176   private String copyOrMoveFiles(String fromPath, String toPath, List fileNames, int operation) {
5bfd34 177     String result = null;
U 178     try {
179       File srcDir = getTargetDir(fromPath);
180       File targetDir = getTargetDir(toPath);
181       Iterator i = fileNames.iterator();
182       while(i.hasNext()) {
183         Object o = i.next();
184         if (o instanceof ArrayList) {
185           ArrayList al = (ArrayList) o;
186           File srcFile = new File(srcDir, al.get(0).toString());
187           if(srcFile.isDirectory()) {
fab629 188             if(operation == OP_MOVE) {
U 189               FileUtils.moveDirectoryToDirectory(srcFile, targetDir, false);
190             } else {
191               FileUtils.copyDirectoryToDirectory(srcFile, targetDir);
192             }
5bfd34 193           } else {
fab629 194             if(operation == OP_MOVE) {
U 195               FileUtils.moveFileToDirectory(srcFile, targetDir, false);
196             } else {
197               FileUtils.copyFileToDirectory(srcFile, targetDir);              
198             }
5bfd34 199           }
U 200         }
201       }
202     } catch (IOException ex) {
203       logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
204     }
205     return result;
9e2964 206   }
U 207   
47e9d4 208   public FileRef saveTextFileAs(String relPath, String fileName, String contents) {
U 209     FileRef savedFile = null;
210     logger.fine(relPath + " " + fileName);
211     //FileRef datenRef = getBase();
212     Object p = getRequest().getUserPrincipal();
213     if(p instanceof Principal) {
214       File targetFile = new File(getTargetDir(relPath), fileName);
215       if(targetFile.exists()) {
216         targetFile = getNewFileName(targetFile);
217       } else {
218         targetFile.getParentFile().mkdirs();
219       }
220       saveToFile(targetFile, contents);
221     }
222     return savedFile;
223   }
224   
225   private File getNewFileName(File file) {
226     File dir = file.getParentFile();
227     String targetName = file.getName();
228     logger.fine("targetName: " + targetName);
229     String ext = "";
230     int dotpos = targetName.indexOf(".");
231     if(dotpos > -1) {
232       ext = targetName.substring(dotpos);
233       targetName = targetName.substring(0, dotpos);
234     }
235     logger.fine("targetName: " + targetName + ", ext: " + ext);
236     int i = 1;
237     while(file.exists()) {
238       StringBuffer buf = new StringBuffer();
239       buf.append(targetName);
240       buf.append("-");
241       buf.append(i);
242       if(ext.length() > 0) {
243         buf.append(ext);
244       }
245       file = new File(dir, buf.toString());
246       i++;
247     }
248     logger.fine("new file: " + file.getName());
249     return file;
250   }
251   
252   private FileRef saveToFile(File targetFile, String contents) {
ea7193 253     FileRef savedFile = null;
e5ff42 254     try {
47e9d4 255       targetFile.createNewFile();
U 256       FileWriter w = new FileWriter(targetFile);
257       w.write(contents);
258       w.flush();
259       w.close();
260       savedFile = new FileRef(
261               targetFile.getAbsolutePath(),
262               targetFile.isDirectory(),
263               targetFile.isHidden(),
264               targetFile.lastModified(),
265               targetFile.length());
e5ff42 266     } catch (IOException ex) {
47e9d4 267       logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
U 268     }
269     return savedFile;
270   }
271   
272   public FileRef saveTextFile(String relPath, String fileName, String contents) {
273     FileRef savedFile = null;
274     logger.fine(relPath + " " + fileName);
275     //FileRef datenRef = getBase();
276     Object p = getRequest().getUserPrincipal();
277     if(p instanceof Principal) {
278       File targetFile = new File(getTargetDir(relPath), fileName);
279       if(targetFile.exists()) {
280         /*
281           muss delete() sein?
282           pruefen: ueberschreibt der FileWriter den alteen Inhalt oder 
283           entsteht eine unerwuenschte Mischung aus altem und neuem 
284           Inhalt?
285         */
286         targetFile.delete();
287       } else {
288         targetFile.getParentFile().mkdirs();
289       }
290       saveToFile(targetFile, contents);
e5ff42 291     }
ea7193 292     return savedFile;
U 293   }
5efd94 294     
U 295   /* ---- Hilfsfunktionen ---- */
ea7193 296   
5efd94 297   private File getTargetDir(String relPath) {
3c4b10 298     logger.fine(relPath);
5efd94 299     String targetPath = null;
U 300     if(relPath.startsWith(PUB_DIR_NAME)) {
9e2964 301       targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length());
5efd94 302     } else if(relPath.startsWith(HOME_DIR_NAME)) {
9e2964 303       targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length());
5efd94 304     } else {
U 305       // kann eigentlich nicht sein..
306     }
3c4b10 307     logger.fine(targetPath);
5efd94 308     File targetDir = new File(getBase().getAbsolutePath(), targetPath);
U 309     return targetDir;
310   }
9e2964 311   
5efd94 312   private FileRef getBase() {
U 313     FileRef base = null;
314     Object o = getServletContext().getAttribute(Initialiser.FILE_BASE);
3c4b10 315     if(o instanceof String) {
U 316       String baseStr = (String) o;
317       logger.fine(baseStr);
318       File file = new File(baseStr);
319       base = new FileRef(file.getAbsolutePath(), file.isDirectory());
5efd94 320     }
U 321     return base;
322   }
323   
324   private String getUserName() {
325     String userName = null;
326     Object p = getRequest().getUserPrincipal();
327     if(p instanceof Principal) {
328       userName = ((Principal) p).getName();
329     }
330     return userName;
547755 331   }    
U 332 }