Skip to content

Commit 4eff2e7

Browse files
committed
ruby: fix - search for filename by path
1 parent 4f26ff6 commit 4eff2e7

File tree

2 files changed

+36
-47
lines changed

2 files changed

+36
-47
lines changed

web/documentserver-example/ruby/app/controllers/home_controller.rb

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -366,55 +366,49 @@ def rename
366366

367367
#ReferenceData
368368
def reference
369-
begin
370-
body = JSON.parse(request.body.read)
371-
referenceData = body["referenceData"]
369+
body = JSON.parse(request.body.read)
370+
fileName = ""
371+
372372

373-
if referenceData != nil
374-
instanceId = referenceData["instanceId"]
375-
if instanceId == DocumentHelper.get_server_url(false)
376-
fileKey = JSON.parse(referenceData["fileKey"])
377-
userAddress = fileKey["userAddress"]
378-
if userAddress == DocumentHelper.cur_user_host_address(nil)
379-
fileName = fileKey["fileName"]
380-
end
373+
if body.key?("referenceData")
374+
referenceData = body["referenceData"]
375+
instanceId = referenceData["instanceId"]
376+
if instanceId == DocumentHelper.get_server_url(false)
377+
fileKey = JSON.parse(referenceData["fileKey"])
378+
userAddress = fileKey["userAddress"]
379+
if userAddress == DocumentHelper.cur_user_host_address(nil)
380+
fileName = fileKey["fileName"]
381381
end
382382
end
383+
end
383384

384-
if (!defined?(fileName) && defined?(userAddress))
385-
begin
386-
path = File.basename(body["path"])
387-
if File.exists?(DocumentHelper.storage_path(path, nil))
388-
fileName = path
389-
end
390-
rescue => ex
391-
render plain: '{ "error": "' + ex.message + '"}'
392-
end
385+
if fileName.empty? and body.key?("path")
386+
path = File.basename(body["path"])
387+
if File.exist?(DocumentHelper.storage_path(path, nil))
388+
fileName = path
393389
end
390+
end
394391

395-
if !defined?(fileName)
396-
render plain: '{ "error": "File not found"}'
397-
return
398-
end
392+
if fileName.empty?
393+
render plain: '{ "error": "File not found"}'
394+
return
395+
end
399396

400-
data = {
401-
:fileType => DocumentHelper.get_internal_extension(fileName),
402-
:url => DocumentHelper.get_download_url(fileName),
403-
:directUrl => body["directUrl"] ? DocumentHelper.get_download_url(fileName) : DocumentHelper.get_download_url(fileName,false),
404-
:referenceData => {
405-
:instanceId => DocumentHelper.get_server_url(false),
406-
:fileKey => fileKey.to_json
407-
},
408-
:path => fileName
409-
}
410-
411-
if JwtHelper.is_enabled
412-
data["token"] = JwtHelper.encode(data)
413-
end
397+
data = {
398+
:fileType => DocumentHelper.get_internal_extension(fileName),
399+
:url => DocumentHelper.get_download_url(fileName),
400+
:directUrl => body["directUrl"] ? DocumentHelper.get_download_url(fileName) : DocumentHelper.get_download_url(fileName,false),
401+
:referenceData => {
402+
:instanceId => DocumentHelper.get_server_url(false),
403+
:fileKey => {:fileName => fileName,:userAddress => DocumentHelper.cur_user_host_address(nil)}.to_json
404+
},
405+
:path => fileName
406+
}
414407

415-
render plain: data.to_json
416-
rescue => ex
417-
render plain: '{ "error": "' + ex.message + '"}'
408+
if JwtHelper.is_enabled
409+
data["token"] = JwtHelper.encode(data)
418410
end
411+
412+
render plain: data.to_json
419413
end
420414
end

web/documentserver-example/ruby/app/models/file_model.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,6 @@ def get_config
105105
}
106106
]
107107

108-
fileKey = {
109-
:fileName => @file_name,
110-
:userAddress => DocumentHelper.cur_user_host_address(nil)
111-
}
112-
113108
config = {
114109
:type => type(),
115110
:documentType => document_type,
@@ -141,7 +136,7 @@ def get_config
141136
},
142137
:referenceData => {
143138
:instanceId => DocumentHelper.get_server_url(false),
144-
:fileKey => !@user.id.eql?("uid-0") ? fileKey.to_json : nil
139+
:fileKey => !@user.id.eql?("uid-0") ? {:fileName => @file_name,:userAddress => DocumentHelper.cur_user_host_address(nil)}.to_json : nil
145140
}
146141
},
147142
:editorConfig => {

0 commit comments

Comments
 (0)