Ruby: Merge jwt prev + diff

This commit is contained in:
Andrey Yumatov
2022-01-13 18:08:40 +03:00
parent 703281aa07
commit 507a7f3162
4 changed files with 20 additions and 11 deletions

View File

@ -152,9 +152,11 @@ class HomeController < ApplicationController
# downloading a history file from public
def downloadhistory
begin
dataurl = request.original_url.split('/')
file_path = Rails.root.join('public', Rails.configuration.storagePath, dataurl[4], dataurl[5], dataurl[6], dataurl[7])
file_name = File.basename(params[:fileName])
user_address = params[:userAddress]
version = params[:ver]
file = params[:file]
isEmbedded = params[:dmode]
if JwtHelper.is_enabled
jwtHeader = Rails.configuration.header.empty? ? "Authorization" : Rails.configuration.header;
@ -171,6 +173,15 @@ class HomeController < ApplicationController
return
end
end
hist_path = DocumentHelper.storage_path(file_name, user_address) + "-hist" # or to the original document
file_path = File.join(hist_path, version, file)
# add headers to the response to specify the page parameters
response.headers['Content-Length'] = File.size(file_path).to_s
response.headers['Content-Type'] = MimeMagic.by_path(file_path).eql?(nil) ? nil : MimeMagic.by_path(file_path).type
response.headers['Content-Disposition'] = "attachment;filename*=UTF-8\'\'" + URI.escape(file, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
send_file file_path, :x_sendfile => true
rescue => ex
render plain: '{ "error": "File not found"}'

View File

@ -235,11 +235,10 @@ class DocumentHelper
return uri
end
# get path url
def get_path_uri(path)
# uri = get_server_url(true) + '/' + Rails.configuration.storagePath + '/' + cur_user_host_address(nil) + '/' + path
# get history path url
def get_historypath_uri(file_name,version,file)
# for redirection to my link
uri = get_server_url(true) + '/history/' + cur_user_host_address(nil) + '/' + path
uri = get_server_url(true) + '/downloadhistory/?fileName=' + URI::encode(file_name) + '&ver='+ URI::encode(version) + '&file='+ URI::encode(file) + '&userAddress=' + cur_user_host_address(nil)
return uri
end

View File

@ -210,7 +210,7 @@ class FileModel
# get the history data from the previous file version and write key and url information about it
dataObj["fileType"] = file_ext[1..file_ext.length]
dataObj["key"] = cur_key
dataObj["url"] = i == cur_ver ? doc_uri : DocumentHelper.get_path_uri(File.join("#{file_name}-hist", i.to_s, "prev#{file_ext}"))
dataObj["url"] = i == cur_ver ? doc_uri : DocumentHelper.get_historypath_uri(file_name, i.to_s, "prev#{file_ext}")
dataObj["version"] = i
if (i > 1) # check if the version number is greater than 1
@ -236,7 +236,7 @@ class FileModel
}
# write the path to the diff.zip archive with differences in this file version
dataObj["changesUrl"] = DocumentHelper.get_path_uri(File.join("#{file_name}-hist", (i - 1).to_s, "diff.zip"))
dataObj["changesUrl"] = DocumentHelper.get_historypath_uri(file_name, (i - 1).to_s, "diff.zip")
end
if JwtHelper.is_enabled # check if a secret key to generate token exists or not

View File

@ -7,14 +7,13 @@ Rails.application.routes.draw do
match '/upload', to: 'home#upload', via: 'post'
match '/download', to: 'home#download', via: 'get'
match '/downloadhistory', to: 'home#downloadhistory', via: 'get'
match '/convert', to: 'home#convert', via: 'post'
match '/track', to: 'home#track', via: 'post'
match '/csv', to: 'home#csv', via: 'get'
match '/files', to: 'home#files', via: 'get'
match '/saveas', to: 'home#saveas', via: 'post'
match '/history/*other/:version/:filename.:ext', to: 'home#downloadhistory', via: 'get'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".