18 package org.turro.elephant.impl.repository;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.TreeSet;
24 import org.apache.commons.fileupload.FileItem;
25 import org.apache.commons.fileupload.FileItemFactory;
26 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
27 import org.apache.commons.fileupload.servlet.ServletFileUpload;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.context.IConstructor;
36 public static final String
38 VALID_PATTERN =
".*\\.(#)$",
39 VALID_IMAGES =
"gif|jpg|png|mpg",
40 VALID_HTML =
"html|htm",
41 VALID_DOCUMENTS =
"txt|cvs|pdf|odt|ott|odm|oth|ods|ots|odg|otg|odp|otp|odi|odf|odc|odb|doc|xls";
44 private String context, subFolder;
45 private boolean selectable =
true;
46 private String roleUpload =
null,
53 private String[] htmlFiles;
72 roleCreateDir = value;
80 String sep =
"", values[] = value.split(
" *, *");
82 for(
int i = 0; i < values.length; i++) {
83 if(values[i].equals(
"images")) {
84 validUploads += sep + VALID_IMAGES;
87 else if(values[i].equals(
"html")) {
88 validUploads += sep + VALID_HTML;
91 if(values[i].equals(
"documents")) {
92 validUploads += sep + VALID_DOCUMENTS;
96 validUploads = VALID_PATTERN.replaceAll(
"#", validUploads);
100 File f =
new File(getRealPath(file));
102 org.amic.util.file.FileUtil.deleteFile(f);
107 File f =
new File(getRealPath(folder));
112 if(!constructor.
isInRole(roleList)) {
113 htmlFiles =
new String[0];
115 if(htmlFiles ==
null) {
116 File f =
new File(getRealPath(
null));
117 File[] children = f.listFiles();
118 TreeSet
set =
new TreeSet();
120 if(subFolder !=
null && subFolder.length() > 0) {
124 "<a class='folder' href='" + folderLink +
"'>..</a>" +
127 for(
int i = 0; children !=
null && i < children.length; i++) {
131 (
isSelectable() ?
"<input type='checkbox' name='check' value ='" + children[i].getName() +
"'/>" :
"") +
132 (children[i].isDirectory() ?
133 "<a class='folder' href='" + folderLink +
"'>" :
134 "<a repository=\"" +
getImageUrl(children[i].getName()) +
"\" class='file'>") +
135 children[i].getName() +
139 htmlFiles = (String[])
set.toArray(
new String[0]);
145 return getRealPath(
null);
149 return fileName.replaceAll(
"[^a-zA-Z0-9\\-\\_\\.]",
"_");
157 this.context = context;
165 this.constructor = constructor;
166 if(ServletFileUpload.isMultipartContent(constructor.
getRequest()) && constructor.
isInRole(roleUpload)) {
167 FileItemFactory factory =
new DiskFileItemFactory();
168 ServletFileUpload upload =
new ServletFileUpload(factory);
169 List items = upload.parseRequest(constructor.
getRequest());
170 Iterator iter = items.iterator();
171 while (iter.hasNext()) {
172 FileItem item = (FileItem) iter.next();
173 if(!item.isFormField()) {
174 String onlyName = item.getName();
175 if(onlyName !=
null && onlyName.length() > 0) {
176 int p = onlyName.lastIndexOf(
'/');
177 if(p == -1) p = onlyName.lastIndexOf(
'\\');
179 if(realName.matches(validUploads)) {
180 File uploadedFile =
new File(realName);
181 item.write(uploadedFile);
188 String[] check = constructor.
getRequest().getParameterValues(
"check");
190 for(
int i = 0; i < check.length; i++) {
201 String folderPath =
"repository(" +
202 (subFolder ==
null || subFolder.length() == 0 ?
"" :
"/" + subFolder);
203 return folderPath +
"/" + file +
")";
206 private String getRealPath(String file) {
207 String folderPath = rootPath +
208 (subFolder ==
null || subFolder.length() == 0 ?
"" :
"/" + subFolder);
222 this.selectable = selectable;
230 this.subFolder = subFolder;
static String getRealPath(String path)
static String getRootWebPath()
void setRoleCreateDir(String value)
void setRoleUpload(String value)
void setSubFolder(String subFolder)
String convertToValidName(String fileName)
void createFolder(String folder)
IConstructor getConstructor()
void setSelectable(boolean selectable)
void setContext(String context)
String getImageUrl(String file)
void setRootPath(String value)
void setValidUploads(String value)
static final String REPOSITORY_JSP
void setRoleDelete(String value)
void setRoleList(String value)
void setConstructor(IConstructor constructor)
void deleteFile(String file)
HttpServletRequest getRequest()
String getParameter(String param)
boolean isInRole(String role)