Dateiverwaltung für die WebBox
ulrich
2018-03-03 3d0c6d50d341c393890f54704543810563ea5eda
commit | author | age
fafe1b 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
19 package de.uhilger.filecms.pub;
20
21 import de.uhilger.filecms.api.Api;
22 import de.uhilger.filecms.data.Bild;
23 import de.uhilger.filecms.data.FileRef;
24 import java.io.File;
25 import java.util.ArrayList;
5410ce 26 import java.util.Arrays;
fafe1b 27 import java.util.List;
U 28 import java.util.logging.Logger;
29
30 /**
31  * Methoden zur Arbeit mit Inhalten in der oeffentlichen 
32  * Ablage einer WebBox
33  */
34 public class Catalog extends Api {
35
36   private static final Logger logger = Logger.getLogger(Catalog.class.getName());
37   
38   /**
39    * Inhalte der WebBox listen. Hier wird nur ausgegeben was mit 
40    * einer relativen Pfadangabe ausgehend von www, der oeffentlichen 
41    * Ablage einer WeBox, erreichbar ist. 
42    * 
43    * Andere Inhalte werden nicht ausgegeben.
44    * 
45    * @param relPath der relative Pfad, der von www aus gelistet werden soll, ohne www
46    * @return Liste der Dateien und Ordner am Ort relPath
47    */
48   public List<FileRef> list(String relPath) {
301e44 49     return listInt(relPath, null, null);
fafe1b 50   }
U 51   
5410ce 52   /**
U 53    * Inhalte der WebBox listen. Hier wird nur ausgegeben was mit 
54    * einer relativen Pfadangabe ausgehend von www, der oeffentlichen 
55    * Ablage einer WeBox, erreichbar ist. 
56    * 
57    * Andere Inhalte werden nicht ausgegeben.
58    * 
59    * @param relPath der relative Pfad, der von www aus gelistet werden soll, ohne www
60    * @return Liste der Dateien und Ordner am Ort relPath
61    */
62   public List<FileRef> listOrdered(String relPath, String orderBy, String order) {
301e44 63     return listInt(relPath, orderBy, order);
U 64   }
65   
66   private List<FileRef> listInt(String relPath, String orderBy, String order) {
5410ce 67     List<FileRef> files = new ArrayList();
3d0c6d 68     if(!relPath.startsWith(".")) {
U 69       Bild bild = new Bild();
70       StringBuffer path = new StringBuffer(PUB_DIR_PATH); // www/
71       path.append(relPath);
72       File dir = new File(getBase().getAbsolutePath(), path.toString());
73       if(dir.exists()) {
74         File[] fileArray = dir.listFiles();
75         if(orderBy != null && orderBy.equalsIgnoreCase("date")) {
76           Arrays.sort(fileArray, new FileDateComparator(order));
5410ce 77         }
3d0c6d 78         for(int i = 0; i < fileArray.length; i++) {
U 79           logger.fine(fileArray[i].toURI().toString());
80           String fname = fileArray[i].toURI().toString().replace("file:/", "");
81           if(fileArray[i].isDirectory()) {
82             fname = fname.substring(0, fname.length() - 1);
83           }
84           logger.fine(fname);
85           FileRef ref = new FileRef(fname, fileArray[i].isDirectory());
86           ref.setMimetype(bild.getMimeType(fileArray[i]));
87           files.add(ref);
88         }
5410ce 89       }
U 90     }
91     return files;
301e44 92  }
5410ce 93   
fafe1b 94 }