BrightSide Workbench Full Report + Source Code
FileAttach.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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.file.util;
20 
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.List;
24 import org.turro.annotation.ElephantControl;
25 import org.turro.elephant.context.ElephantContext;
26 import org.turro.elephant.context.IConstructor;
27 import org.turro.elephant.impl.repository.Repository;
28 import org.turro.entities.Controls;
29 
34 @ElephantControl(name=Controls.CTRL_FILES)
35 public class FileAttach {
36 
37  private final String entityPath;
38 
39  public FileAttach(String entityPath) {
40  this.entityPath = entityPath;
41  }
42 
43  public String getPublishable() {
44  return "/_internal/files" + entityPath;
45  }
46 
48  return new Repository(constructor, getPublishable());
49  }
50 
51  public String getPrivate() {
52  return "/WEB-INF/files" + entityPath;
53  }
54 
56  return new Repository(constructor, getPrivate());
57  }
58 
59  public String getPublicFile(String file) {
60  return getPublicFile(file, true);
61  }
62 
63  public String getPublicFile(String file, boolean withContext) {
64  return getFile(getPublishable(), file, withContext);
65  }
66 
67  public List<String> getPublicFiles() {
68  return getFiles(getPublishable());
69  }
70 
71  public String getFile(String folder, String file) {
72  return getFile(folder, file, true);
73  }
74 
75  public String getFile(String folder, String file, boolean withContext) {
76  File root = new File(ElephantContext.getRealPath(folder + file));
77  if(root.exists()) {
78  return (withContext ? ElephantContext.getRootWebPath() : "") + folder + file;
79  }
80  return null;
81  }
82 
83  public List<String> getFiles(String folder) {
84  return getFiles(folder, true);
85  }
86 
87  public List<String> getFiles(String folder, boolean withContext) {
88  File root = new File(ElephantContext.getRealPath(folder));
89  List<String> files = new ArrayList<>();
90  if(root.exists()) {
91  for(File f : root.listFiles()) {
92  if(f.isFile()) {
93  files.add((withContext ? ElephantContext.getRootWebPath() : "") + folder + "/" + f.getName());
94  }
95  }
96  }
97  return files;
98  }
99 
100  public List<PairedFile> getImageFiles(String folder) {
101  return getImageFiles(folder, true);
102  }
103 
104  public List<PairedFile> getImageFiles(String folder, boolean withContext) {
105  File root = new File(ElephantContext.getRealPath(folder));
106  List<PairedFile> files = new ArrayList<>();
107  if(root.exists()) {
108  for(File f : root.listFiles()) {
109  if(f.isFile()) {
110  files.add(new PairedFile((withContext ? ElephantContext.getRootWebPath() : "") + folder, f.getName(),
111  (withContext ? ElephantContext.getRootWebPath() : "") + folder + "/thumbs", ".png"));
112  }
113  }
114  }
115  return files;
116  }
117 
118  /* Statics */
119 
120  public static String getPublishable(String entityPath, String file) {
121  FileAttach fa = new FileAttach(entityPath);
122  return fa.getFile(fa.getPublishable(), file);
123  }
124 
125  public static String getPrivate(String entityPath, String file) {
126  FileAttach fa = new FileAttach(entityPath);
127  return fa.getFile(fa.getPrivate(), file);
128  }
129 
130  public static List<String> getPublishableFiles(String entityPath, String file) {
131  FileAttach fa = new FileAttach(entityPath);
132  return fa.getFiles(fa.getPublishable() + file);
133  }
134 
135  public static List<PairedFile> getPublishableImages(String entityPath, String file) {
136  FileAttach fa = new FileAttach(entityPath);
137  return fa.getImageFiles(fa.getPublishable() + file);
138  }
139 
140 }
List< String > getFiles(String folder)
Definition: FileAttach.java:83
List< String > getPublicFiles()
Definition: FileAttach.java:67
String getPublicFile(String file, boolean withContext)
Definition: FileAttach.java:63
List< PairedFile > getImageFiles(String folder)
String getPublicFile(String file)
Definition: FileAttach.java:59
String getFile(String folder, String file, boolean withContext)
Definition: FileAttach.java:75
String getFile(String folder, String file)
Definition: FileAttach.java:71
static List< PairedFile > getPublishableImages(String entityPath, String file)
Repository getPrivateRepository(IConstructor constructor)
Definition: FileAttach.java:55
Repository getPublishableRepository(IConstructor constructor)
Definition: FileAttach.java:47
static String getPublishable(String entityPath, String file)
List< String > getFiles(String folder, boolean withContext)
Definition: FileAttach.java:87
FileAttach(String entityPath)
Definition: FileAttach.java:39
static String getPrivate(String entityPath, String file)
static List< String > getPublishableFiles(String entityPath, String file)
List< PairedFile > getImageFiles(String folder, boolean withContext)