BrightSide Workbench Full Report + Source Code
RepositoryFile.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
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 published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (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 org.turro.elephant.impl.repository;
20 
21 import java.io.File;
22 import java.io.FileFilter;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.text.DateFormat;
28 import java.util.Date;
29 import java.util.Properties;
30 import java.util.Set;
31 import java.util.TreeSet;
32 import org.amic.util.date.DateFormats;
33 import org.amic.util.file.FileUtil;
34 import org.turro.formatter.BytesFormatter;
35 import org.turro.string.Strings;
36 import org.turro.elephant.context.ElephantContext;
37 import org.turro.elephant.context.IConstructor;
38 import org.turro.html.FontAwesome;
39 import org.turro.i18n.I_;
40 import org.turro.util.CompareUtil;
41 
46 public class RepositoryFile implements Comparable<RepositoryFile> {
47 
48  private final File file;
49  private final IConstructor constructor;
50  private Properties properties;
51 
52  public RepositoryFile(File file, IConstructor constructor) {
53  this.file = file;
54  this.constructor = constructor;
55  }
56 
57  public boolean hasContent() {
58  return file.isDirectory() && file.list().length > 0;
59  }
60 
61  public File getFile() {
62  return file;
63  }
64 
65  public boolean isFolder() {
66  return file.isDirectory();
67  }
68 
69  public String getName() {
70  return file.getName();
71  }
72 
73  public String getWebPath() {
74  return file.getAbsolutePath().replace(
77  }
78 
79  public String getThumbWebPath() {
80  StringBuilder webPath = new StringBuilder(getWebPath());
81  webPath.insert(webPath.lastIndexOf("/"), "/thumbs");
82  return webPath.toString();
83  }
84 
85  public String getMimeImage() {
86  return FontAwesome.getMimeIcon(FileUtil.getExtension(file));
87 // return ElephantContext.getRootWebPath() +
88 // "/_internal/system/mime/" +
89 // FileUtil.getExtension(file) + ".png";
90  }
91 
92  public String getByteString() {
93  return new BytesFormatter(file.length()).toString();
94  }
95 
96  public String getDateString() {
97  return DateFormats.format(new Date(file.lastModified()),
98  DateFormat.SHORT, I_.api().used());
99  }
100 
101  public Properties getProperties() {
102  if(properties == null) {
103  properties = new Properties();
104  File fprop = new File(FileUtil.getBasePath(file) + ".properties");
105  if(fprop.exists()) {
106  try (InputStream is = new FileInputStream(fprop)) {
107  properties.load(new InputStreamReader(is, ElephantContext.getEncoding()));
108  } catch (IOException ex) {
109  }
110  }
111  }
112  return properties;
113  }
114 
115  @Override
116  public int compareTo(RepositoryFile o) {
117  int result = CompareUtil.compare(file.isFile(), o.getFile().isFile());
118  if(result == 0) {
119  result = CompareUtil.compare(file.getName(), o.getFile().getName());
120  }
121  return result;
122  }
123 
124  public File[] getFiles(String pattern) {
125  File repository = file;
126  final String patterns[] = pattern.split(",");
127  return repository.listFiles(new FileFilter() {
128  @Override
129  public boolean accept(File pathname) {
130  if(pathname.isFile() && !pathname.isHidden()) {
131  for(String p : patterns) {
132  if(Strings.compareIgnoreCase(pathname.getName(), p)) {
133  return true;
134  }
135  }
136  }
137  return false;
138  }
139  });
140  }
141 
142  public File[] getFolders(String pattern) {
143  File repository = file;
144  final String patterns[] = pattern.split(",");
145  return repository.listFiles(new FileFilter() {
146  @Override
147  public boolean accept(File pathname) {
148  if(pathname.isDirectory() && !pathname.isHidden()) {
149  for(String p : patterns) {
150  if(Strings.compareIgnoreCase(pathname.getName(), p)) {
151  return true;
152  }
153  }
154  }
155  return false;
156  }
157  });
158  }
159 
160  public Set<RepositoryFile> getRepositoryFiles(String pattern) {
161  File[] files = getFiles(pattern);
162  TreeSet<RepositoryFile> set = new TreeSet<>();
163  if(files != null) {
164  for(File f : files) {
165  set.add(new RepositoryFile(f, constructor));
166  }
167  }
168  return set;
169  }
170 
171  public Set<RepositoryFile> getRepositoryFolders(String pattern) {
172  File[] files = getFolders(pattern);
173  TreeSet<RepositoryFile> set = new TreeSet<>();
174  if(files != null) {
175  for(File f : files) {
176  set.add(new RepositoryFile(f, constructor));
177  }
178  }
179  return set;
180  }
181 
182 }
Set< RepositoryFile > getRepositoryFiles(String pattern)
RepositoryFile(File file, IConstructor constructor)
Set< RepositoryFile > getRepositoryFolders(String pattern)
static I18nApiWrapper api()
Definition: I_.java:65