Skip to content

Commit

Permalink
simplified workflow of the "download" API method #248 #265
Browse files Browse the repository at this point in the history
  • Loading branch information
psolom committed Nov 26, 2017
1 parent c985ae4 commit af60b30
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -440,50 +440,44 @@ public JSONObject actionDownload(HttpServletRequest request, HttpServletResponse
}
}

// Ajax
if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) {
return new JSONObject().put("data", new JSONObject(getFileInfo(path)));
} else {

try {
response.setHeader("Content-Description", "File Transfer");
if (file.isFile()) {
String fileExt = filename.substring(filename.lastIndexOf(".") + 1);
String mimeType = (!StringUtils.isEmpty(FileManagerUtils.mimetypes.get(fileExt))) ? FileManagerUtils.mimetypes.get(fileExt) : "application/octet-stream";
response.setContentLength((int) file.length());
response.setContentType(mimeType);
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
response.setContentLength((int) file.length());

FileUtils.copy(new BufferedInputStream(new FileInputStream(file)), response.getOutputStream());
} else {
String[] files = file.list();

if (files == null || files.length == 0) {
return getErrorResponse(String.format(dictionnary.getProperty("DIRECTORY_EMPTY"), file.getName()));
}
try {
response.setHeader("Content-Description", "File Transfer");
if (file.isFile()) {
String fileExt = filename.substring(filename.lastIndexOf(".") + 1);
String mimeType = (!StringUtils.isEmpty(FileManagerUtils.mimetypes.get(fileExt))) ? FileManagerUtils.mimetypes.get(fileExt) : "application/octet-stream";
response.setContentLength((int) file.length());
response.setContentType(mimeType);
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
response.setContentLength((int) file.length());

FileUtils.copy(new BufferedInputStream(new FileInputStream(file)), response.getOutputStream());
} else {
String[] files = file.list();

String zipFileName = FileUtils.getBaseName(path.substring(0, path.length() - 1)) + ".zip";
String mimType = FileManagerUtils.mimetypes.get("zip");
response.setContentType(mimType);
response.setHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + "\"");
byte[] zipFileByteArray;
try {
zipFileByteArray = ZipUtils.zipFolder(file);
} catch (IOException e) {
throw new FMIOException("Exception during ZipFiles", e);
}
response.setContentLength(zipFileByteArray.length);
if (files == null || files.length == 0) {
return getErrorResponse(String.format(dictionnary.getProperty("DIRECTORY_EMPTY"), file.getName()));
}

FileUtils.copy(new ByteArrayInputStream(zipFileByteArray), response.getOutputStream());
String zipFileName = FileUtils.getBaseName(path.substring(0, path.length() - 1)) + ".zip";
String mimType = FileManagerUtils.mimetypes.get("zip");
response.setContentType(mimType);
response.setHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + "\"");
byte[] zipFileByteArray;
try {
zipFileByteArray = ZipUtils.zipFolder(file);
} catch (IOException e) {
throw new FMIOException("Exception during ZipFiles", e);
}
response.setContentLength(zipFileByteArray.length);

} catch (IOException e) {
throw new FMIOException("Download error: " + file.getName(), e);
FileUtils.copy(new ByteArrayInputStream(zipFileByteArray), response.getOutputStream());
}

return null;
} catch (IOException e) {
throw new FMIOException("Download error: " + file.getName(), e);
}

return null;
}

@Override
Expand Down
39 changes: 15 additions & 24 deletions connectors/jsp/libraries/java/src/edu/fuberlin/RichFileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,30 +300,21 @@ private String getDynamicPath(String path) {
public JSONObject download(HttpServletRequest request, HttpServletResponse resp) throws JSONException, FileManagerException {
File file = this.documentRoot.resolve(this.get.get("path")).toFile();
if (this.get.get("path") != null && file.exists()) {

// Ajax
if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) {
return new JSONObject().put("data", new JSONObject(getFileInfo(
this.get.get("path")
)));
} else {

resp.setHeader("Content-Description", "File Transfer");
//resp.setHeader("Content-Type", "application/force-download");
//resp.setHeader("Content-Disposition", "inline;filename=\"" + documentRoot.resolve(this.get.get("path")).toString() + "\"");
resp.setHeader("Content-Transfer-Encoding", "Binary");
resp.setHeader("Content-Length", "" + file.length());
resp.setHeader("Content-Type", "application/octet-stream");
resp.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
// handle caching
resp.setHeader("Pragma", "public");
resp.setHeader("Expires", "0");
resp.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
this.error = null;
readFile(resp, file);
log("file downloaded \""+ file.getAbsolutePath() + "\"");
return null;
}
resp.setHeader("Content-Description", "File Transfer");
//resp.setHeader("Content-Type", "application/force-download");
//resp.setHeader("Content-Disposition", "inline;filename=\"" + documentRoot.resolve(this.get.get("path")).toString() + "\"");
resp.setHeader("Content-Transfer-Encoding", "Binary");
resp.setHeader("Content-Length", "" + file.length());
resp.setHeader("Content-Type", "application/octet-stream");
resp.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
// handle caching
resp.setHeader("Pragma", "public");
resp.setHeader("Expires", "0");
resp.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
this.error = null;
readFile(resp, file);
log("file downloaded \""+ file.getAbsolutePath() + "\"");
return null;
} else {
return getErrorResponse(sprintf(lang("FILE_DOES_NOT_EXIST"), this.get.get("path")));
}
Expand Down
10 changes: 0 additions & 10 deletions connectors/nodejs/filemanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,16 +387,6 @@ case 'readfile':
});// parsePath
break;
case 'download':
// to make it works, you need to change filemanager.js downloadItem function, like this:
/*
var downloadItem = function(resourceObject) {
var queryParams = {mode: 'download', path: resourceObject.id};
var link = document.createElement("a");
link.download = resourceObject && resourceObject.attributes && resourceObject.attributes.name;
link.href = buildConnectorUrl(queryParams);
link.click();
};
*/
parsePath(path, (pp) => {
getinfo(pp, (result) => {
res.setHeader('content-type', 'text/html; charset=UTF-8');
Expand Down
13 changes: 6 additions & 7 deletions scripts/filemanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3943,13 +3943,12 @@ $.richFilemanagerPlugin = function(element, pluginOptions)
path: resourceObject.id
};

return buildAjaxRequest('GET', queryParams).done(function(response) {
if(response.data) {
//window.location = buildConnectorUrl(queryParams);
$.fileDownload(buildConnectorUrl(queryParams));
}
handleAjaxResponseErrors(response);
}).fail(handleAjaxError);
$.fileDownload(buildConnectorUrl(queryParams), {
failCallback: function (responseHtml, url, error) {
var response = $.parseJSON(responseHtml);
handleAjaxResponseErrors(response);
}
});
};

// Perform "editfile" API request
Expand Down
2 changes: 1 addition & 1 deletion scripts/filemanager.min.js

Large diffs are not rendered by default.

0 comments on commit af60b30

Please sign in to comment.