From bf5b8fb6ad99026606411d0ec6c0b00929397aef Mon Sep 17 00:00:00 2001 From: rivexe Date: Fri, 17 Feb 2023 12:03:42 +0300 Subject: [PATCH] java: search for fileName by path added --- .../main/java/controllers/IndexServlet.java | 47 +++++++++++++++---- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/web/documentserver-example/java/src/main/java/controllers/IndexServlet.java b/web/documentserver-example/java/src/main/java/controllers/IndexServlet.java index ae018e6cc..970c18f0c 100755 --- a/web/documentserver-example/java/src/main/java/controllers/IndexServlet.java +++ b/web/documentserver-example/java/src/main/java/controllers/IndexServlet.java @@ -57,10 +57,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; -import java.util.Scanner; +import java.util.*; import static utils.Constants.KILOBYTE_SIZE; @@ -650,15 +647,45 @@ private static void reference(final HttpServletRequest request, JSONParser parser = new JSONParser(); JSONObject body = (JSONObject) parser.parse(bodyString); - String instanceId = (String) body.get("instanceId"); - JSONObject fileKey = (JSONObject) body.get("fileKey"); - String fileKeyValue = gson.toJson(fileKey); + JSONObject referenceDataObj = (JSONObject) body.get("referenceData"); + String instanceId = (String) referenceDataObj.get("instanceId"); - String fileName = (String) fileKey.get("fileName"); - String userAddress = (String) fileKey.get("userAddress"); + String fileKeyValue = ""; + String userAddress = ""; + String fileName = ""; + + if (Objects.equals(instanceId, DocumentManager.getServerUrl(false))) { + JSONObject fileKey = (JSONObject) referenceDataObj.get("fileKey"); + fileKeyValue = gson.toJson(fileKey); + userAddress = (String) fileKey.get("userAddress"); + if (Objects.equals(userAddress, DocumentManager.curUserHostAddress(null))) { + fileName = (String) fileKey.get("fileName"); + } + } + + if (Objects.equals(fileName, "") && !Objects.equals(userAddress, "")) + { + try { + String path = (String) body.get("path"); + path = FileUtility.getFileName(path); + File f = new File(DocumentManager.storagePath(path, userAddress)); + if (f.exists()) + { + fileName = path; + } + } catch (Exception e) { + e.printStackTrace(); + writer.write("{ \"error\" : 1, \"message\" : \"" + e.getMessage() + "\"}"); + } + } + + if (Objects.equals(fileName, "")) { + writer.write("{ \"error\": \"File not found\"}"); + return; + } HashMap referenceData = new HashMap<>(); - referenceData.put("instanceId", DocumentManager.getServerUrl(true)); + referenceData.put("instanceId", DocumentManager.getServerUrl(false)); referenceData.put("fileKey", fileKeyValue); HashMap data = new HashMap<>();