Dateiverwaltung für die WebBox
ulrich
2017-03-03 547755bb34e06a77061de6c72f8372dc62015edb
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) {
5bfd34 169     String result = null;
U 170     try {
171       File srcDir = getTargetDir(fromPath);
172       File targetDir = getTargetDir(toPath);
173       Iterator i = fileNames.iterator();
174       while(i.hasNext()) {
175         Object o = i.next();
176         if (o instanceof ArrayList) {
177           ArrayList al = (ArrayList) o;
178           File srcFile = new File(srcDir, al.get(0).toString());
179           if(srcFile.isDirectory()) {
180             logger.fine("copy dir " + srcFile.getAbsolutePath() + " to dir " + targetDir.getAbsolutePath());
181             FileUtils.copyDirectoryToDirectory(srcFile, targetDir);
182           } else {
183             logger.fine("copy srcFile " + srcFile.getAbsolutePath() + " to dir " + targetDir.getAbsolutePath());
184             FileUtils.copyFileToDirectory(srcFile, targetDir);
185           }
186         }
187       }
188     } catch (IOException ex) {
189       logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
190     }
191     return result;
9e2964 192   }
U 193   
194   public String moveFiles(String fromPath, String toPath, List fileNames) {
5bfd34 195     String result = null;
U 196     try {
197       File srcDir = getTargetDir(fromPath);
198       File targetDir = getTargetDir(toPath);
199       Iterator i = fileNames.iterator();
200       while(i.hasNext()) {
201         Object o = i.next();
202         if (o instanceof ArrayList) {
203           ArrayList al = (ArrayList) o;
204           File srcFile = new File(srcDir, al.get(0).toString());
205           if(srcFile.isDirectory()) {
206             FileUtils.moveDirectoryToDirectory(srcFile, targetDir, false);
207           } else {
208             FileUtils.moveFileToDirectory(srcFile, targetDir, false);
209           }
210         }
211       }
212     } catch (IOException ex) {
213       logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
214     }
215     return result;
9e2964 216   }
U 217   
ea7193 218   public FileRef saveTextFile(String relPath, String fileName, String contents) {
U 219     FileRef savedFile = null;
e5ff42 220     try {
U 221       FileRef datenRef = getBase();
222       Object p = getRequest().getUserPrincipal();
223       if(p instanceof Principal) {
2121cc 224         File targetFile = new File(getTargetDir(relPath), fileName);
7342b1 225         if(targetFile.exists()) {
U 226           /*
227             muss delete() sein?
228             pruefen: ueberschreibt der FileWriter den alteen Inhalt oder 
229             entsteht eine unerwuenschte Mischung aus altem und neuem 
230             Inhalt?
231           */
232           targetFile.delete();
233         } else {
915927 234           targetFile.getParentFile().mkdirs();
e5ff42 235         }
7342b1 236         targetFile.createNewFile();
U 237         FileWriter w = new FileWriter(targetFile);
238         w.write(contents);
239         w.flush();
240         w.close();
241         savedFile = new FileRef(
242                 targetFile.getAbsolutePath(), 
243                 targetFile.isDirectory(), 
244                 targetFile.isHidden(), 
245                 targetFile.lastModified(), 
246                 targetFile.length());
e5ff42 247       }
U 248     } catch (IOException ex) {
249       logger.log(Level.SEVERE, null, ex);
250     }
ea7193 251     return savedFile;
U 252   }
5efd94 253     
U 254   /* ---- Hilfsfunktionen ---- */
ea7193 255   
5efd94 256   private File getTargetDir(String relPath) {
3c4b10 257     logger.fine(relPath);
5efd94 258     String targetPath = null;
U 259     if(relPath.startsWith(PUB_DIR_NAME)) {
9e2964 260       targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length());
5efd94 261     } else if(relPath.startsWith(HOME_DIR_NAME)) {
9e2964 262       targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length());
5efd94 263     } else {
U 264       // kann eigentlich nicht sein..
265     }
3c4b10 266     logger.fine(targetPath);
5efd94 267     File targetDir = new File(getBase().getAbsolutePath(), targetPath);
U 268     return targetDir;
269   }
9e2964 270   
5efd94 271   private FileRef getBase() {
U 272     FileRef base = null;
273     Object o = getServletContext().getAttribute(Initialiser.FILE_BASE);
3c4b10 274     if(o instanceof String) {
U 275       String baseStr = (String) o;
276       logger.fine(baseStr);
277       File file = new File(baseStr);
278       base = new FileRef(file.getAbsolutePath(), file.isDirectory());
5efd94 279     }
U 280     return base;
281   }
282   
283   private String getUserName() {
284     String userName = null;
285     Object p = getRequest().getUserPrincipal();
286     if(p instanceof Principal) {
287       userName = ((Principal) p).getName();
288     }
289     return userName;
547755 290   }    
U 291 }