Skip to content

Commit

Permalink
java: fix - search for filename by path
Browse files Browse the repository at this point in the history
  • Loading branch information
rivexe committed Feb 22, 2023
1 parent 4eff2e7 commit 21d1fd9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -645,31 +645,39 @@ private static void reference(final HttpServletRequest request,
scanner.useDelimiter("\\A");
String bodyString = scanner.hasNext() ? scanner.next() : "";
scanner.close();
Gson gson = new GsonBuilder().disableHtmlEscaping().create();

JSONParser parser = new JSONParser();
JSONObject body = (JSONObject) parser.parse(bodyString);
JSONObject referenceDataObj = (JSONObject) body.get("referenceData");
String instanceId = (String) referenceDataObj.get("instanceId");

String fileKeyValue = "";
String userAddress = "";
String fileName = "";
boolean incorrectFileKey = false;

JSONParser parser = new JSONParser();
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
JSONObject body = (JSONObject) parser.parse(bodyString);

if (instanceId.equals(DocumentManager.getServerUrl(false))) {
JSONObject fileKey = (JSONObject) referenceDataObj.get("fileKey");
fileKeyValue = gson.toJson(fileKey);
userAddress = (String) fileKey.get("userAddress");
if (userAddress.equals(DocumentManager.curUserHostAddress(null))) {
fileName = (String) fileKey.get("fileName");
if (body.containsKey("referenceData")) {
JSONObject referenceDataObj = (JSONObject) body.get("referenceData");
String instanceId = (String) referenceDataObj.get("instanceId");

if (instanceId.equals(DocumentManager.getServerUrl(false))) {
try {
JSONObject fileKey = (JSONObject) parser.parse((String) referenceDataObj.get("fileKey"));
userAddress = (String) fileKey.get("userAddress");

if (userAddress.equals(DocumentManager.curUserHostAddress(null))) {
fileName = (String) fileKey.get("fileName");
}
} catch (Exception e) {
incorrectFileKey = true; //data from DocEditor can give incorrect fileKey param in java Example
}
}
}

if (fileName.equals("") && !userAddress.equals("")) {
if (fileName.equals("")) {
try {
String path = (String) body.get("path");
path = FileUtility.getFileName(path);
File f = new File(DocumentManager.storagePath(path, userAddress));
File f = new File(DocumentManager.storagePath(path, null));
if (f.exists()) {
fileName = path;
}
Expand All @@ -684,16 +692,20 @@ private static void reference(final HttpServletRequest request,
return;
}

HashMap<String, Object> fileKey = new HashMap<>();
fileKey.put("fileName", fileName);
fileKey.put("userAddress", DocumentManager.curUserHostAddress(null));

HashMap<String, Object> referenceData = new HashMap<>();
referenceData.put("instanceId", DocumentManager.getServerUrl(false));
referenceData.put("fileKey", fileKeyValue);
referenceData.put("fileKey", gson.toJson(fileKey));

HashMap<String, Object> data = new HashMap<>();
data.put("fileType", FileUtility.getFileExtension(fileName));
data.put("url", DocumentManager.getDownloadUrl(fileName, true));
data.put("directUrl", DocumentManager.getDownloadUrl(fileName, true));
data.put("referenceData", referenceData);
data.put("path", referenceData);
data.put("path", fileName);

if (DocumentManager.tokenEnabled()) {
String token = DocumentManager.createToken(data);
Expand Down
1 change: 0 additions & 1 deletion web/documentserver-example/java/src/main/webapp/editor.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@
};
var onRequestReferenceData = function(event) { // user refresh external data source
event.data.directUrl = !!config.document.directUrl;
let xhr = new XMLHttpRequest();
xhr.open("POST", "IndexServlet?type=reference");
Expand Down

0 comments on commit 21d1fd9

Please sign in to comment.