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