Dateiverwaltung für die WebBox
ulrich
2017-02-21 5efd94fed0b470d715b9aa4d2e6b0e300ca2ec06
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 {
2121cc 60       String path = getTargetDir(relPath).getAbsolutePath();
U 61       logger.fine(path);
5efd94 62       LocalFileSystem fs = new LocalFileSystem();
2121cc 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);
5dfab6 86   }
U 87   
ea7193 88   public FileRef saveTextFile(String relPath, String fileName, String contents) {
U 89     FileRef savedFile = null;
e5ff42 90     try {
U 91       FileRef datenRef = getBase();
92       File daten = new File(datenRef.getAbsolutePath());
93       Object p = getRequest().getUserPrincipal();
94       if(p instanceof Principal) {
2121cc 95         File targetFile = new File(getTargetDir(relPath), fileName);
7342b1 96         if(targetFile.exists()) {
U 97           /*
98             muss delete() sein?
99             pruefen: ueberschreibt der FileWriter den alteen Inhalt oder 
100             entsteht eine unerwuenschte Mischung aus altem und neuem 
101             Inhalt?
102           */
103           targetFile.delete();
104         } else {
915927 105           targetFile.getParentFile().mkdirs();
e5ff42 106         }
7342b1 107         targetFile.createNewFile();
U 108         FileWriter w = new FileWriter(targetFile);
109         w.write(contents);
110         w.flush();
111         w.close();
112         savedFile = new FileRef(
113                 targetFile.getAbsolutePath(), 
114                 targetFile.isDirectory(), 
115                 targetFile.isHidden(), 
116                 targetFile.lastModified(), 
117                 targetFile.length());
e5ff42 118       }
U 119     } catch (IOException ex) {
120       logger.log(Level.SEVERE, null, ex);
121     }
ea7193 122     return savedFile;
U 123   }
5efd94 124     
U 125   /* ---- Hilfsfunktionen ---- */
ea7193 126   
5efd94 127   private File getTargetDir(String relPath) {
U 128     logger.finer(relPath);
129     String targetPath = null;
130     if(relPath.startsWith(PUB_DIR_NAME)) {
131       targetPath = PUB_DIR_PATH + getUserName() + "/" + relPath.substring(PUB_DIR_NAME.length());
132     } else if(relPath.startsWith(HOME_DIR_NAME)) {
133       targetPath = HOME_DIR_PATH + getUserName() + "/" + relPath.substring(HOME_DIR_NAME.length());
134     } else {
135       // kann eigentlich nicht sein..
136     }
137     logger.finer(targetPath);
138     File targetDir = new File(getBase().getAbsolutePath(), targetPath);
139     return targetDir;
140   }
141   private FileRef getBase() {
142     FileRef base = null;
143     Object o = getServletContext().getAttribute(Initialiser.FILE_BASE);
144     if(o instanceof FileRef) {
145       base = (FileRef) o;
146     }
147     return base;
148   }
149   
150   private String getUserName() {
151     String userName = null;
152     Object p = getRequest().getUserPrincipal();
153     if(p instanceof Principal) {
154       userName = ((Principal) p).getName();
155     }
156     return userName;
157   }
158   
159   /*
8e51b7 160   private File getWebappsDir() {
8931b7 161     File cfile = new File(this.getClass().getResource(
U 162             this.getClass().getSimpleName() + ".class").getFile());
8e51b7 163     String path = cfile.getAbsolutePath();
8931b7 164     return new File(path.substring(0, path.indexOf(getRequest().getContextPath())));
c7c502 165   }
5efd94 166   */
7342b1 167     
c7c502 168 }