Dateiverwaltung für die WebBox
ulrich
2017-02-20 8931b77df172c4a2a106d471a566479ac9c5f6b0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 
    Dateiverwaltung - File management in your browser
    Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 
package de.uhilger.filecms.api;
 
import de.uhilger.filesystem.FileRef;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.security.Principal;
import java.util.logging.Level;
import java.util.logging.Logger;
 
/**
 *
 * @author ulrich
 */
public class FileMgr extends Api {
  private static final Logger logger = Logger.getLogger(FileMgr.class.getName());
  
  public static final String FILE_BASE = "fileBase";
  
  public String hallo() {
    return "Hallo Welt!";
  }
  
  // http://localhost:8097/file-cms/rpc/de.uhilger.filecms.api.FileMgr/getBase/JSONNICE
  public FileRef getBase() {
    FileRef ref = null;
    //String fileBase = getServletContext().getInitParameter(FILE_BASE);
    // -Dfilecms.base=/pfad/zu/daten
    //String fileBase = System.getProperty("filecms.base");
    //File file = new File(fileBase);
    //logger.info(file.getAbsolutePath());
    //logger.info(getWebappsDir().getAbsolutePath());
    
    /* 
        file = new File("."); liefert 
        /home/ulrich/dev/lib/java/tomcat/tomcat2-8.5.9/bin/.
    
        ..auf der WebBox aber
        /home/ulrich/srv/wbx_probe/.
        ..weil das Startskript dort liegt
    
        der Tomcat der WebBox ist unter
        sys/jrs/tomcat/bin
    
        also z.B.
        /home/ulrich/srv/wbx_probe/sys/jrs/tomcat/bin
 
        das Datenverzeichnis ist z.B. auf
        /home/ulrich/srv/wbx_probe/daten
    
        dann ist das Datenverzeichnis unter
        ../../../daten
    
        Der Ausdruck file = new File("."); liefert stets den 
        Ort von dem aus der Java-Prozess gestartet wurde.
    
        Die unten folgende Bestimmung des Datenverzeichnisses 
        ist beschraenkt auf das Datenverzeichnis der WebBox, 
        entweder relativ zum Startskript der WebBox oder 
        dem Startskript von Tomcat, wie es aus Netbeans heraus 
        waehrend der Entwicklung genutzt wird.
    
        Besser ware vielleicht eine Bestimmung ueber einen 
        Systemparameter -Dfilecms.base=... wie weiter oben 
        auskommentiert. Damit liesse sich das file-cms auch 
        ohne WebBox einsetzen. Allerdings muss dann das 
        Datenverzeichnis im Start-Skript gebildet werden, 
        also ausserhalb von Java, wenn es dynamisch aus 
        einem Pfad relativ zum Start-Skript erzeugt werden 
        soll.
    */
    
    File file = new File(".");
    String path = file.getAbsolutePath();
    path = path.substring(0, path.length() - 1);
    file = new File(path);
    if(path.endsWith("bin")) {
      file = file.getParentFile().getParentFile().getParentFile();
    } else {
      
    }
    file = new File(file, "daten/");
    ref = new FileRef(file.getAbsolutePath(), file.isDirectory());
    return ref;
  }
  
  public FileRef saveTextFile(String relPath, String fileName, String contents) {
    FileRef savedFile = null;
    try {
      FileRef datenRef = getBase();
      File daten = new File(datenRef.getAbsolutePath());
      Object p = getRequest().getUserPrincipal();
      if(p instanceof Principal) {
        File userDir = new File(daten, "www/" + ((Principal) p).getName());
        File saveDir = new File(userDir, relPath);
        File targetFile = new File(saveDir, fileName);
        if(!targetFile.exists()) {
          targetFile.getParentFile().mkdirs();
          targetFile.createNewFile();
          FileWriter w = new FileWriter(targetFile);
          w.write(contents);
          w.flush();
          w.close();
          savedFile = new FileRef(
                  targetFile.getAbsolutePath(), 
                  targetFile.isDirectory(), 
                  targetFile.isHidden(), 
                  targetFile.lastModified(), 
                  targetFile.length());
        }
      }
    } catch (IOException ex) {
      logger.log(Level.SEVERE, null, ex);
    }
    return savedFile;
  }
  
  private File getWebappsDir() {
    File cfile = new File(this.getClass().getResource(
            this.getClass().getSimpleName() + ".class").getFile());
    String path = cfile.getAbsolutePath();
    return new File(path.substring(0, path.indexOf(getRequest().getContextPath())));
  }
  
}