Skip to content

Commit

Permalink
API refactoring; new error handling flow #270
Browse files Browse the repository at this point in the history
  • Loading branch information
psolom committed Nov 27, 2017
1 parent af60b30 commit de747e8
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 494 deletions.
2 changes: 1 addition & 1 deletion connectors/ashx/filemanager.ashx
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public class filemanager : IHttpHandler
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.Write(Initiate());
break;
case "getfolder":
case "readfolder":
context.Response.ContentType = "plain/text";
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.Write(getInfo(context.Request["path"]));
Expand Down
111 changes: 27 additions & 84 deletions connectors/asp/FileManagerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public IActionResult Index(string mode, string path, string name, List<IFormFile
{
case "initiate":
return Json(Initiate());
case "getfolder":
return Json(GetFolder(path));
case "readfolder":
return Json(ReadFolder(path));
case "addfolder":
return Json(AddFolder(path, name));
case "upload":
Expand All @@ -51,26 +51,16 @@ public IActionResult Index(string mode, string path, string name, List<IFormFile
return Json(Move(old, @new));
case "copy":
return Json(Copy(source, target));
case "editfile":
return Json(EditFile(path));
case "savefile":
return Json(SaveFile(path, content));
case "delete":
return Json(Delete(path));
case "download":
if (Request.Headers["accept"].ToString().Contains("json"))
{
return Json(Download(path));
}
else
{
var file = DownloadFile(path);
return File(file.FileBytes, "application/x-msdownload", file.FileName);
}
return Download(path);
case "getimage":
return GetImage(path, thumbnail);
case "readfile":
break;
return ReadFile(path);
case "summarize":
return Json(Summarize());
}
Expand Down Expand Up @@ -108,10 +98,8 @@ private dynamic Initiate()

}

private dynamic GetFolder(string path)
private dynamic ReadFolder(string path)
{


if (path == null) path = string.Empty;

var rootpath = Path.Combine(_webRootPath, path);
Expand Down Expand Up @@ -612,36 +600,6 @@ private dynamic Copy(string source, string target)
}
}

private dynamic EditFile(string path)
{
var fileName = Path.GetFileName(path);
var fileExtension = Path.GetExtension(path).Replace(".", "");
var filePath = Path.Combine(_webRootPath, path);

var content = System.IO.File.ReadAllText(filePath, Encoding.UTF8);

var result = new
{
Data = new
{
Id = path,
Type = "file",
Attributes = new
{
Name = fileName,
Extension = fileExtension,
Writable = 1,
Readable = 1,
// created vb.
Content = content,
Path = $"/{Path.Combine(path)}"
}
}
};

return result;
}

private dynamic SaveFile(string path, string content)
{
var filePath = Path.Combine(_webRootPath, path);
Expand Down Expand Up @@ -731,56 +689,43 @@ private dynamic Delete(string path)
}
}

private dynamic Download(string path)
private dynamic ReadFile(string path)
{
var fileName = Path.GetFileName(Path.Combine(_webRootPath, path));
var fileExtension = Path.GetExtension(fileName).Replace(".", "");

// undone dosya var m� kontrol�...
var filePath = Path.Combine(_webRootPath, path);
var fileName = Path.GetFileName(filePath);
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);

var result = new
var cd = new ContentDisposition
{
Data = new
{
Id = path,
Type = "file",
Attributes = new
{
Name = fileName,
Extension = fileExtension,
Readable = 1,
Writable = 1,
// created date, size vb.
Modified = DateTime.Now.ToString(CultureInfo.InvariantCulture),
//Path = $"{path}"
}
}
Inline = true,
FileName = fileName
};
Response.AddHeader("Content-Disposition", cd.ToString());

return result;

return File(fileBytes, "application/octet-stream");
}

private dynamic DownloadFile(string path)
private IActionResult GetImage(string path, bool thumbnail)
{
var filepath = Path.Combine(_webRootPath, path);
var fileName = Path.GetFileName(filepath);
byte[] fileBytes = System.IO.File.ReadAllBytes(filepath);
var filePath = Path.Combine(_webRootPath, path);
var fileName = Path.GetFileName(filePath);
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);

var file = new
var cd = new ContentDisposition
{
FileName = fileName,
FileBytes = fileBytes
Inline = true,
FileName = fileName
};
Response.AddHeader("Content-Disposition", cd.ToString());

return file;
return File(fileBytes, "image/*");
}

private IActionResult GetImage(string path, bool thumbnail)
private dynamic Download(string path)
{
var filepath = Path.Combine(_webRootPath, path);
var fileName = Path.GetFileName(filepath);
byte[] fileBytes = System.IO.File.ReadAllBytes(filepath);
var filePath = Path.Combine(_webRootPath, path);
var fileName = Path.GetFileName(filePath);
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);

return File(fileBytes, "application/x-msdownload", fileName);
}
Expand All @@ -793,8 +738,6 @@ private dynamic Summarize()
var files = directoryInfo.GetFiles("*", SearchOption.AllDirectories);
var allSize = files.Select(f => f.Length).Sum();



var result = new
{
Data = new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ public final void handleRequest(HttpServletRequest request, HttpServletResponse
case "initiate":
responseData = actionInitiate(request);
break;
case "getfile":
case "getinfo":
if (!StringUtils.isEmpty(request.getParameter("path"))) {
responseData = actionGetFile(request);
responseData = actionGetInfo(request);
}
break;
case "getfolder":
case "readfolder":
if (!StringUtils.isEmpty(request.getParameter("path"))) {
responseData = actionGetFolder(request);
responseData = actionReadFolder(request);
}
break;
case "rename":
Expand Down Expand Up @@ -303,12 +303,12 @@ protected final boolean hasPermission(String action){
}

@Override
public JSONObject actionGetFile(HttpServletRequest request) throws FileManagerException {
public JSONObject actionGetInfo(HttpServletRequest request) throws FileManagerException {
throw new UnsupportedOperationException();
}

@Override
public JSONObject actionGetFolder(HttpServletRequest request) throws FileManagerException {
public JSONObject actionReadFolder(HttpServletRequest request) throws FileManagerException {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public interface IFileManager {

JSONObject actionInitiate(HttpServletRequest request) throws FileManagerException;

JSONObject actionGetFile(HttpServletRequest request) throws FileManagerException;
JSONObject actionGetInfo(HttpServletRequest request) throws FileManagerException;

JSONObject actionGetFolder(HttpServletRequest request) throws FileManagerException;
JSONObject actionReadFolder(HttpServletRequest request) throws FileManagerException;

JSONObject actionReadFile(HttpServletRequest request, HttpServletResponse response) throws FileManagerException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public LocalFileManager(Locale locale, Map<String,String> options) throws FMInit
}

@Override
public JSONObject actionGetFolder(HttpServletRequest request) throws FileManagerException {
public JSONObject actionReadFolder(HttpServletRequest request) throws FileManagerException {

String path = getPath(request, "path");
String type = request.getParameter("type");
Expand Down Expand Up @@ -109,15 +109,11 @@ public JSONObject actionGetFolder(HttpServletRequest request) throws FileManager
}

@Override
public JSONObject actionGetFile(HttpServletRequest request) throws FileManagerException {
public JSONObject actionGetInfo(HttpServletRequest request) throws FileManagerException {
String path = getPath(request, "path");

File file = new File(docRoot.getPath() + path);

if (file.isDirectory()) {
return getErrorResponse(dictionnary.getProperty("FORBIDDEN_ACTION_DIR"));
}

// check if the name is not in "excluded" list
String filename = file.getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ editRestrictions=txt,csv,md
# Filter section
# ------------------------

# File types that are filtered out from the output list based on the type of filter ('getfolder' request)
# File types that are filtered out from the output list based on the type of filter ('readfolder' request)
outputFilter_images=jpg,jpe,jpeg,gif,png,svg,bmp

# ------------------------
Expand Down
2 changes: 1 addition & 1 deletion connectors/jsp/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ icons-default = default.png
# Filter section
# ------------------------

# File types that are filtered out from the output list based on the type of filter ('getfolder' request)
# File types that are filtered out from the output list based on the type of filter ('readfolder' request)
outputFilter_images=jpg,jpe,jpeg,gif,png,svg,bmp

# ------------------------
Expand Down
7 changes: 3 additions & 4 deletions connectors/jsp/filemanager.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,17 @@
}
}
try {
// renamed getinfo to getfile
if (mode.equals("getinfo") || mode.equals("getfile")){
if (mode.equals("getinfo")){
if(fm.setGetVar("path", (strictServletCompliance)? qpm.get("path"): request.getParameter("path"))) {
responseData = fm.getInfo();
}
}
else if (mode.equals("initiate")){
responseData = fm.initiate(request);
}
else if (mode.equals("getfolder")){
else if (mode.equals("readfolder")){
if(fm.setGetVar("path", (strictServletCompliance)? qpm.get("path"):request.getParameter("path"))) {
responseData = fm.getFolder(request);
responseData = fm.readFolder(request);
}
}
else if (mode.equals("rename")){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public interface FileManagerI {

JSONObject getInfo() throws JSONException, FileManagerException;

JSONObject getFolder(HttpServletRequest request) throws JSONException, IOException, FileManagerException;
JSONObject readFolder(HttpServletRequest request) throws JSONException, IOException, FileManagerException;

JSONObject rename() throws JSONException, FileManagerException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected JSONObject readSmallFile(HttpServletResponse resp, Path file) throws J


@Override
public JSONObject getFolder(HttpServletRequest request) throws JSONException, IOException, FileManagerException {
public JSONObject readFolder(HttpServletRequest request) throws JSONException, IOException, FileManagerException {
JSONArray array = new JSONArray();

boolean showThumbs = false;
Expand Down Expand Up @@ -224,10 +224,6 @@ private Map getFileInfo(String path) throws FileManagerException {
// get file
File file = getFile(path);

if(file.isDirectory() && !path.endsWith("/")){
throw new FMIOException("Error reading the file (file as directory not allowed): " + file.getAbsolutePath());
}

BasicFileAttributes attr;
try {
attr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
Expand Down
27 changes: 7 additions & 20 deletions connectors/nodejs/filemanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ module.exports = (__appRoot) => { // eslint-disable-line max-statements
});// parsePath
}// getIndividualFileInfo

function getfolder(pp, callback) {
function readfolder(pp, callback) {
fs.readdir(pp.osFullPath, (err, files) => {
if (err) {
console.log('err -> ', err); // eslint-disable-line no-console
Expand Down Expand Up @@ -356,24 +356,11 @@ module.exports = (__appRoot) => { // eslint-disable-line max-statements
});// getinfo
});// parsePath
break;
case 'getfolder':
case 'readFolder':
parsePath(path, (pp) => {
getfolder(pp, (result) => {
readfolder(pp, (result) => {
respond(res, {data: result});
});// getfolder
});// parsePath
break;
case 'editfile':
parsePath(path, (pp) => {
getinfo(pp, (result) => {
fs.readFile(paths.resolve(pp.osFullPath), (err, f) => {
if (err) {
res.status(500).send(err);
}
result.attributes.content = f.toString();
respond(res, {data: result});
});
});// getinfo
});// readfolder
});// parsePath
break;
case 'getimage':
Expand All @@ -388,10 +375,10 @@ case 'readfile':
break;
case 'download':
parsePath(path, (pp) => {
getinfo(pp, (result) => {
res.setHeader('content-type', 'text/html; charset=UTF-8');
res.send(JSON.stringify({data: result}));
});// getinfo
res.setHeader('content-description', 'File Transfer');
res.setHeader('content-disposition', 'attachment; filename="' + pp.filename + '"');
res.sendFile(paths.resolve(pp.osFullPath));
});// parsePath
break;
case 'addfolder':
Expand Down
2 changes: 1 addition & 1 deletion connectors/php/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

/**
* Event listener on after "getfolder" API method successfully executed.
* Event listener on after "readfolder" API method successfully executed.
*
* @param \RFM\Event\Api\AfterFolderReadEvent $event
*/
Expand Down
Loading

0 comments on commit de747e8

Please sign in to comment.