Dateiverwaltung für die WebBox
ulrich
2017-03-29 6e1a290ebaddb441ff2a4fe911f1cdfc62070519
commit | author | age
b7475d 1 /*
8931b7 2
U 3     Dateiverwaltung - File management in your browser
4     Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU Affero General Public License as
8     published by the Free Software Foundation, either version 3 of the
9     License, or (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU Affero General Public License for more details.
15
16     You should have received a copy of the GNU Affero General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
b7475d 20
U 21 package de.uhilger.filecms.api;
22
6e1a29 23 import de.uhilger.transit.web.WebKontext;
ae9140 24 import de.uhilger.wbx.api.ApiBase;
8eb306 25 import de.uhilger.wbx.data.FileRef;
72e43d 26 import java.io.File;
U 27 import java.util.logging.Logger;
6e1a29 28 import javax.servlet.ServletContext;
b7475d 29
U 30 /**
31  *
32  */
6e1a29 33 public abstract class Api extends ApiBase implements WebKontext {
U 34   
35   protected ServletContext ctx;  
b7475d 36   
72e43d 37   private static final Logger logger = Logger.getLogger(Api.class.getName());
U 38
39   public static final String WBX_DATA_PATH = "daten/";
40   public static final String PUB_DIR_PATH = "www/";
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";
44   public static final String HOME_DIR_NAME = "Persoenlich";
45   public static final String WBX_ADMIN_ROLE = "wbxAdmin";
46   
47   public static final String WBX_BASE = "$basis";
48   public static final String WBX_DATA = "$daten";
ae9140 49     
72e43d 50   /**
U 51    * Einen relativen Pfad in einen absoluten Pfad der WebBox 
52    * aufloesen.
53    * 
54    * Nur die absoluten Pfade zu PUB_DIR_NAME, HOME_DIR_NAME 
55    * sowie WBX_BASE und WBX_DATA werden ausgegeben. Letztere 
56    * beiden nur fuer Nutzer mit der Rolle WBX_ADMIN_ROLE.
57    * 
58    * D.h., es werden nur Pfade aufgeloest, die sich innerhalb 
59    * des Ordners der WeBox befinden.
60    * 
61    * @param relPath
62    * @return 
63    */
64   protected File getTargetDir(String relPath) {
65     logger.fine(relPath);
66     File targetDir;
67     String targetPath = null;
68     if(relPath.startsWith(PUB_DIR_NAME)) {
69       targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length());
70       targetDir = new File(getBase().getAbsolutePath(), targetPath);
71     } else if(relPath.startsWith(HOME_DIR_NAME)) {
72       targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length());
73       targetDir = new File(getBase().getAbsolutePath(), targetPath);
74     } else if(getRequest().isUserInRole(WBX_ADMIN_ROLE)) {
75       logger.fine("in admin role");
76       if(relPath.startsWith(WBX_BASE)) {
77         logger.fine("is base");
6e1a29 78         targetPath = getCatalinaBase(ctx);
72e43d 79         targetDir = new File(targetPath, relPath.substring(WBX_BASE.length()));
U 80       } else if(relPath.startsWith(WBX_DATA)) {
6e1a29 81         targetPath = getWbxDataDir(ctx);
72e43d 82         logger.fine("is data, combine " + targetPath + ' ' + relPath.substring(WBX_DATA.length()));
U 83         targetDir = new File(targetPath, relPath.substring(WBX_DATA.length()));
84       } else {
85         targetDir = getDefaultDir(relPath);
86       }
87     } else {
88       // kann eigentlich nicht sein..
89       targetDir = getDefaultDir(relPath);
90     }
91     logger.fine("returning targetDir " + targetDir.getAbsolutePath());
92     //File targetDir = new File(getBase().getAbsolutePath(), targetPath);
93     return targetDir;
94   }
95   
96   protected File getDefaultDir(String relPath) {
97     String targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length());
98     return new File(getBase().getAbsolutePath(), targetPath);
99   }
100   
101   protected FileRef getBase() {
102     FileRef base = null;
6e1a29 103     Object o = getFileBase(ctx);
ae9140 104     if(o instanceof File) {
U 105       File file = (File) o;
72e43d 106       base = new FileRef(file.getAbsolutePath(), file.isDirectory());
U 107     }
108     return base;
b7475d 109   }
6e1a29 110
U 111   
112   /* ------------- Implementierung WebKontext ------------- */
113
114   @Override
115   public ServletContext getServletContext() {
116     return ctx;
117   }
118
119   @Override
120   public void setServletContext(ServletContext servletContext) {
121     this.ctx = servletContext;
122   }
b7475d 123   
U 124 }