Skip to content

Commit

Permalink
UPDATE
Browse files Browse the repository at this point in the history
JAVA
 - support readfile mode
 - remove preview filtering/mapping, but keep/provide the empty wrappers to allow
 for extending and first step hint for implementation, cft. issue psolom#27 and
 psolom#31
  • Loading branch information
Georg Kallidis committed Aug 18, 2016
1 parent f803320 commit ac489a5
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 93 deletions.
4 changes: 2 additions & 2 deletions connectors/jsp/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ unallowed_dirs =
# provide the document root, which is relative to servlet context if it start not with '/'
doc_root =

# provide a preview path, may be relative
preview =
# provide a preview path, may be relative, by default not supported
#preview =


#FEATURED OPTIONS
Expand Down
21 changes: 11 additions & 10 deletions connectors/jsp/filemanager.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@
* - check strictServletCompliance 8/16
* - dynamic content type setting 8/16
*/
//use new rich manager - or not
boolean rich = true;
FileManagerI fm = (rich)? new RichFileManager(getServletContext(), request) : new FileManager(getServletContext(), request);
//use new rich manager - or not
boolean rich = true;
FileManagerI fm = (rich)? new RichFileManager(getServletContext(), request) : new FileManager(getServletContext(), request);
boolean strictServletCompliance = false; // default value is ISO-8859-1.
JSONObject responseData = null;
String mode = "";
boolean putTextarea = false;
if(!auth(request)) {
Expand Down Expand Up @@ -84,9 +78,16 @@
}
else if (mode.equals("getimage")){
if(fm.setGetVar("path", (strictServletCompliance)? qpm.get("path"):request.getParameter("path"))) {
String paramThumbs =request.getParameter("thumbnail");
fm.preview(request, response);
}
} else if (mode.equals("move")){
}
else if (mode.equals("readfile")){
if(fm.setGetVar("path", (strictServletCompliance)? qpm.get("path"):request.getParameter("path"))) {
fm.preview(request, response);
}
}
else if (mode.equals("move")){
if(fm.setGetVar("old", (strictServletCompliance)? qpm.get("old"):request.getParameter("old")) &&
fm.setGetVar("new", (strictServletCompliance)? qpm.get("new"):request.getParameter("new"))
) {
Expand Down
Binary file modified connectors/jsp/libraries/java/bin/com/nartex/AbstractFM.class
Binary file not shown.
Binary file modified connectors/jsp/libraries/java/bin/com/nartex/RichFileManager.class
Binary file not shown.
32 changes: 13 additions & 19 deletions connectors/jsp/libraries/java/src/com/nartex/AbstractFM.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,27 @@ public abstract class AbstractFM implements FileManagerI{
protected Map<String, String> params = new HashMap<String, String>();
protected Path documentRoot; // make it static?
protected Path fileManagerRoot = null; // static?
protected String referer = "";
protected Logger log = LoggerFactory.getLogger("filemanager");
protected JSONObject error = null;
protected SimpleDateFormat dateFormat;
protected List<FileItem> files = null;
protected boolean reload = false;
protected Path previewBasePath = null; //static?
protected boolean previewPathRelative = false; // needed as it is exposed either relative or absolute


public AbstractFM(ServletContext servletContext, HttpServletRequest request) throws IOException {
String contextPath = request.getContextPath();

Path localPath = Paths.get(servletContext.getRealPath("/"));
Path docRoot4FileManager = localPath.toRealPath(LinkOption.NOFOLLOW_LINKS);

this.referer = request.getHeader("referer");
if (referer != null) {
String referer = request.getHeader("referer");
// this is
if (referer != null && referer.indexOf("index.html") > 0 ) {
this.fileManagerRoot = docRoot4FileManager.
resolve(referer.substring(referer.indexOf(contextPath) + 1 + contextPath.length(), referer.indexOf("index.html")));
// last resort and only if already
} else if (this.fileManagerRoot == null && request.getServletPath().indexOf("connectors") > 0) {
resolve(referer.substring(referer.indexOf(contextPath) + 1 + contextPath.length(), referer.indexOf("index.html")));
}
// last resort and only if already
if (this.fileManagerRoot == null && request.getServletPath().indexOf("connectors") > 0) {
this.fileManagerRoot = docRoot4FileManager.
resolve(request.getServletPath().substring(1, request.getServletPath().indexOf("connectors")));
// no pathInfo
Expand Down Expand Up @@ -115,17 +115,11 @@ public AbstractFM(ServletContext servletContext, HttpServletRequest request) thr
}
log.debug("final documentRoot:"+ this.documentRoot);
}
if (reload) {
this.previewBasePath = null;
}
// a relative path
if (this.previewBasePath == null) {
this.previewBasePath = Paths.get(contextPath);
}


dateFormat = new SimpleDateFormat(config.getProperty("date"));

this.setParams();
this.setParams(referer);

loadLanguageFile();

Expand Down Expand Up @@ -250,9 +244,9 @@ protected String getFileExtension(String filename) {
return retval;
}

protected void setParams() {
if (this.referer != null) {
String[] tmp = this.referer.split("\\?");
protected void setParams(String referer) {
if (referer != null) {
String[] tmp = referer.split("\\?");
String[] params_tmp = null;
LinkedHashMap<String, String> params = new LinkedHashMap<String, String>();
if (tmp.length > 1 && tmp[1] != "") {
Expand Down
95 changes: 33 additions & 62 deletions connectors/jsp/libraries/java/src/com/nartex/RichFileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Date;
import java.util.HashMap;
Expand All @@ -45,15 +42,15 @@
* - added mode replace
* - added interface
* - adapted download to new two-step mode
* - optional indirection methods cleanPreview and getPreviewFolder to allow for URL-mapping
*
* @author gkallidis
*
*/
public class RichFileManager extends AbstractFM implements FileManagerI {





/**
*
* @param servletContext
Expand All @@ -64,7 +61,7 @@ public class RichFileManager extends AbstractFM implements FileManagerI {
public RichFileManager(ServletContext servletContext, HttpServletRequest request) throws IOException {

super(servletContext,request);

}

@Override
Expand Down Expand Up @@ -104,7 +101,7 @@ public void preview(HttpServletRequest request, HttpServletResponse resp) {
}

if (this.get.get("path") != null && Files.exists(file)) {
resp.setHeader("Content-type", "image/octet-stream"); // + getFileExtension(file.toFile().getName()));
resp.setHeader("Content-type", "image/"+ getFileExtension(file.toFile().getName())); // octet-stream" + getFileExtension(file.toFile().getName()));
resp.setHeader("Content-Transfer-Encoding", "Binary");
resp.setHeader("Content-length", "" + size);
resp.setHeader("Content-Disposition", "inline; filename=\"" + getFileBaseName(file.toFile().getName()) + "\"");
Expand All @@ -118,11 +115,6 @@ public void preview(HttpServletRequest request, HttpServletResponse resp) {
}
}

private String cleanPreview(String filecontextpath) {
String preview =getPreviewFolder(); // config.getProperty("preview");
return filecontextpath.replace(preview.replaceFirst("^/", ""), "");
}

// expect small filesw
protected void readSmallFile(HttpServletResponse resp, Path file) {
OutputStream os = null;
Expand Down Expand Up @@ -193,7 +185,6 @@ public JSONObject getFolder(HttpServletRequest request) throws JSONException, IO
this.error("JSONObject error");
}
} else if (file.canRead() && (!contains(config.getProperty("unallowed_files"), files[i])) ) {
//this.item = new HashMap();
this.item = new HashMap<String,Object>();
this.item.put("properties", this.properties);
this.getFileInfo(this.get.get("path") + files[i], showThumbs);
Expand All @@ -208,8 +199,6 @@ public JSONObject getFolder(HttpServletRequest request) throws JSONException, IO
data.put("Path", this.item.get("path"));
data.put("Filename", this.item.get("filename"));
data.put("File Type", this.item.get("filetype"));
// data.put("Thumbnail", this.item.get("thumbnail"));
// data.put("Preview", this.item.get("preview"));
data.put("Properties", this.item.get("properties"));
data.put("Error", "");
data.put("Code", 0);
Expand Down Expand Up @@ -259,7 +248,7 @@ protected void getFileInfo(String path, boolean thumbs) throws JSONException {
} else if (isImage(pathTmp)) {

String imagePath = getPreviewFolder() + pathTmp;
if (thumbs) { // not yet implemented
if (thumbs) { // TODO not yet implemented
this.item.put("path", imagePath );
} else {
this.item.put("path", imagePath);
Expand All @@ -282,42 +271,6 @@ protected void getFileInfo(String path, boolean thumbs) throws JSONException {
this.item.put("properties", props);
}

/**
* constructs previewFolder from property preview.
*
* If it starts with / is an absulte URL else it resolves relative to contextpath
*
* Result start with a slash for client call
*
* Better use org.apache.http.client.utils.URIUtils resolve ?
*
* @return previewFolder
*/
protected String getPreviewFolder() {
Path tmpPreviewPath = null;
String previewPath = null;
String preview = config.getProperty("preview");
if (preview != null && preview.startsWith("/")) {
try {
tmpPreviewPath = Paths.get(new URI(preview));
} catch (URISyntaxException e) {
log.error("is not a valid URI preview config:"+ preview);
}
}
if (tmpPreviewPath != null) {
previewPath = tmpPreviewPath.normalize().toString();
} else {
// relative ?
if (previewBasePath != null) {
previewPath = this.previewBasePath.resolve(preview).normalize().toString();
} else {
previewPath = Paths.get(preview).normalize().toString();
}
}
return previewPath.replace("\\", "/") + "/";
}


/* (non-Javadoc)
* @see com.nartex.FileManagerI#download(javax.servlet.http.HttpServletResponse)
*/
Expand Down Expand Up @@ -490,17 +443,16 @@ public JSONObject moveItem() {
String filename = tmp[tmp.length - 1];
int pos = itemName.lastIndexOf("/");

String path = "";
Path fileTo = null;
if (pos > 0) {
path = itemName.substring(0, pos + 1);
fileTo = this.documentRoot.resolve(cleanPreview(path)); // from subfolder, folder should be ..
if (pos > 0 && itemName.contains("..")) {
String path = itemName.substring(0, pos + 1);
fileTo = this.documentRoot.resolve(cleanPreview(path)); // from subfolder, folder could be .. check later in root
} else {
fileTo = this.documentRoot;
}
//String root = this.get.get("root"); // slash at beginning and end
String folder = this.get.get("new");
if (folder.trim().startsWith( "/")) { // absolute path is not allowed
if (folder.trim().startsWith( "/")) { // absolute path is not allowed, this is then just root folder
folder = folder.trim().replaceFirst( "/", "" );
}
Path fileFrom = null;
Expand Down Expand Up @@ -594,13 +546,9 @@ public JSONObject rename() {
if (!error) {
array = new JSONObject();
try {
String prefix = "";
if (!this.get.get("old").startsWith("/")) {
prefix= "/";
}
array.put("Error", "");
array.put("Code", 0);
array.put("Old Path", prefix + this.get.get("old"));
array.put("Old Path", this.get.get("old"));
array.put("Old Name", filename);
array.put("New Path", getPreviewFolder() + path + this.get.get("new"));
array.put("New Name", this.get.get("new"));
Expand Down Expand Up @@ -724,5 +672,28 @@ public void loadLanguageFile() {
}
}
}

/**
* constructs mapping from property preview.
*
* Suspended, due to https://github.com/servocoder/RichFilemanager/issues/27
*
*/
protected String getPreviewFolder() {
// TODO not yet implemented
//if (directURL) { }
return "";
}

/**
* clean mapping
* @param filecontextpath
* @return filecontextpath
*/
protected String cleanPreview(String filecontextpath) {
// TODO not yet implemented
//if (directURL) { }
return filecontextpath;
}

}

0 comments on commit ac489a5

Please sign in to comment.