Dateiverwaltung für die WebBox
ulrich
2017-03-05 fab629ffb99008bce86082033d349f42ba13b93a
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   
ea7193 208   public FileRef saveTextFile(String relPath, String fileName, String contents) {
U 209     FileRef savedFile = null;
e5ff42 210     try {
U 211       FileRef datenRef = getBase();
212       Object p = getRequest().getUserPrincipal();
213       if(p instanceof Principal) {
2121cc 214         File targetFile = new File(getTargetDir(relPath), fileName);
7342b1 215         if(targetFile.exists()) {
U 216           /*
217             muss delete() sein?
218             pruefen: ueberschreibt der FileWriter den alteen Inhalt oder 
219             entsteht eine unerwuenschte Mischung aus altem und neuem 
220             Inhalt?
221           */
222           targetFile.delete();
223         } else {
915927 224           targetFile.getParentFile().mkdirs();
e5ff42 225         }
7342b1 226         targetFile.createNewFile();
U 227         FileWriter w = new FileWriter(targetFile);
228         w.write(contents);
229         w.flush();
230         w.close();
231         savedFile = new FileRef(
232                 targetFile.getAbsolutePath(), 
233                 targetFile.isDirectory(), 
234                 targetFile.isHidden(), 
235                 targetFile.lastModified(), 
236                 targetFile.length());
e5ff42 237       }
U 238     } catch (IOException ex) {
239       logger.log(Level.SEVERE, null, ex);
240     }
ea7193 241     return savedFile;
U 242   }
5efd94 243     
U 244   /* ---- Hilfsfunktionen ---- */
ea7193 245   
5efd94 246   private File getTargetDir(String relPath) {
3c4b10 247     logger.fine(relPath);
5efd94 248     String targetPath = null;
U 249     if(relPath.startsWith(PUB_DIR_NAME)) {
9e2964 250       targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length());
5efd94 251     } else if(relPath.startsWith(HOME_DIR_NAME)) {
9e2964 252       targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length());
5efd94 253     } else {
U 254       // kann eigentlich nicht sein..
255     }
3c4b10 256     logger.fine(targetPath);
5efd94 257     File targetDir = new File(getBase().getAbsolutePath(), targetPath);
U 258     return targetDir;
259   }
9e2964 260   
5efd94 261   private FileRef getBase() {
U 262     FileRef base = null;
263     Object o = getServletContext().getAttribute(Initialiser.FILE_BASE);
3c4b10 264     if(o instanceof String) {
U 265       String baseStr = (String) o;
266       logger.fine(baseStr);
267       File file = new File(baseStr);
268       base = new FileRef(file.getAbsolutePath(), file.isDirectory());
5efd94 269     }
U 270     return base;
271   }
272   
273   private String getUserName() {
274     String userName = null;
275     Object p = getRequest().getUserPrincipal();
276     if(p instanceof Principal) {
277       userName = ((Principal) p).getName();
278     }
279     return userName;
547755 280   }    
U 281 }