From 952b495c4dd1ff3a44aa23ac2910b48ff6b00ca6 Mon Sep 17 00:00:00 2001 From: vanyauhalin Date: Sun, 11 Jun 2023 23:14:20 +0400 Subject: [PATCH] ruby: support for configuration via environment variables --- web/documentserver-example/ruby/.gitignore | 1 + .../ruby/app/configuration.rb | 192 ++++++++++++++++++ .../ruby/app/controllers/home_controller.rb | 13 +- .../ruby/app/models/document_helper.rb | 56 ++--- .../ruby/app/models/file_model.rb | 6 +- .../ruby/app/models/jwt_helper.rb | 7 +- .../ruby/app/models/service_converter.rb | 15 +- .../ruby/app/models/track_helper.rb | 18 +- .../ruby/app/views/home/editor.html.erb | 2 +- .../ruby/app/views/home/index.html.erb | 4 +- .../ruby/config/application.rb | 68 ------- 11 files changed, 258 insertions(+), 124 deletions(-) create mode 100644 web/documentserver-example/ruby/app/configuration.rb diff --git a/web/documentserver-example/ruby/.gitignore b/web/documentserver-example/ruby/.gitignore index 763468b9..93e83f59 100644 --- a/web/documentserver-example/ruby/.gitignore +++ b/web/documentserver-example/ruby/.gitignore @@ -1 +1,2 @@ sorbet +storage diff --git a/web/documentserver-example/ruby/app/configuration.rb b/web/documentserver-example/ruby/app/configuration.rb new file mode 100644 index 00000000..a89b4643 --- /dev/null +++ b/web/documentserver-example/ruby/app/configuration.rb @@ -0,0 +1,192 @@ +# +# (c) Copyright Ascensio System SIA 2023 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# frozen_string_literal: true +# typed: true + +require 'uri' + +class ConfigurationManager + extend T::Sig + + sig { returns(String) } + attr_reader :version + + sig { void } + def initialize + @version = '1.6.0' + end + + sig { returns(T.nilable(URI::Generic)) } + def example_uri + url = ENV['EXAMPLE_URL'] + return nil if url.nil? + URI(url) + end + + sig { returns(URI::Generic) } + def document_server_uri + url = ENV['DOCUMENT_SERVER_URL'] || 'http://document-server/' + URI(url) + end + + sig { returns(URI::Generic) } + def document_server_api_uri + path = + ENV['DOCUMENT_SERVER_API_PATH'] || + 'web-apps/apps/api/documents/api.js' + URI.join(document_server_uri, path) + end + + sig { returns(URI::Generic) } + def document_server_preloader_uri + path = + ENV['DOCUMENT_SERVER_PRELOADER_PATH'] || + 'web-apps/apps/api/documents/cache-scripts.html' + URI.join(document_server_uri, path) + end + + sig { returns(URI::Generic) } + def document_server_command_uri + path = + ENV['DOCUMENT_SERVER_COMMAND_PATH'] || + 'coauthoring/CommandService.ashx' + URI.join(document_server_uri, path) + end + + sig { returns(URI::Generic) } + def document_server_converter_uri + path = + ENV['DOCUMENT_SERVER_CONVERTER_PATH'] || + 'ConvertService.ashx' + URI.join(document_server_uri, path) + end + + sig { returns(String) } + def jwt_secret + ENV['JWT_SECRET'] || '' + end + + sig { returns(String) } + def jwt_header + ENV['JWT_HEADER'] || 'Authorization' + end + + sig { returns(T::Boolean) } + def jwt_use_for_request + env = ENV['JWT_USE_FOR_REQUEST'] + return ActiveModel::Type::Boolean.new.cast(env) if env + true + end + + sig { returns(T::Boolean) } + def ssl_verify_peer_mode_enabled + env = ENV['SSL_VERIFY_PEER_MODE_ENABLED'] + return ActiveModel::Type::Boolean.new.cast(env) if env + false + end + + sig { returns(Pathname) } + def storage_path + storage_path = ENV['STORAGE_PATH'] || 'storage' + storage_directory = Pathname(storage_path) + return storage_directory if storage_directory.absolute? + current_directory = Pathname(File.expand_path(__dir__)) + current_directory.join('..', storage_directory) + end + + sig { returns(Numeric) } + def maximum_file_size + env = ENV['MAXIMUM_FILE_SIZE'] + return env.to_i if env + 5 * 1024 * 1024 + end + + sig { returns(Numeric) } + def convertation_timeout + 120 + end + + sig { returns(T::Array[String]) } + def fillable_file_extensions + '.docx|.oform' + .split('|') + end + + sig { returns(T::Array[String]) } + def viewable_file_extensions + '.djvu|.oxps|.pdf|.xps' + .split('|') + end + + sig { returns(T::Array[String]) } + def editable_file_extensions + '.csv|.docm|.docx|.docxf|.dotm|.dotx|.epub|.fb2|.html|.odp|.ods|.odt|.otp|.ots|.ott|.potm|.potx|.ppsm|.ppsx|.pptm|.pptx|.rtf|.txt|.xlsm|.xlsx|.xltm|.xltx' + .split('|') + end + + sig { returns(T::Array[String]) } + def convertable_file_extensions + '.doc|.dot|.dps|.dpt|.epub|.et|.ett|.fb2|.fodp|.fods|.fodt|.htm|.html|.mht|.mhtml|.odp|.ods|.odt|.otp|.ots|.ott|.pot|.pps|.ppt|.rtf|.stw|.sxc|.sxi|.sxw|.wps|.wpt|.xls|.xlsb|.xlt|.xml' + .split('|') + end + + sig { returns(T::Hash[String, String]) } + def languages + { + 'en' => 'English', + 'hy' => 'Armenian', + 'az' => 'Azerbaijani', + 'eu' => 'Basque', + 'be' => 'Belarusian', + 'bg' => 'Bulgarian', + 'ca' => 'Catalan', + 'zh' => 'Chinese (Simplified)', + 'zh-TW' => 'Chinese (Traditional)', + 'cs' => 'Czech', + 'da' => 'Danish', + 'nl' => 'Dutch', + 'fi' => 'Finnish', + 'fr' => 'French', + 'gl' => 'Galego', + 'de' => 'German', + 'el' => 'Greek', + 'hu' => 'Hungarian', + 'id' => 'Indonesian', + 'it' => 'Italian', + 'ja' => 'Japanese', + 'ko' => 'Korean', + 'lo' => 'Lao', + 'lv' => 'Latvian', + 'ms' => 'Malay (Malaysia)', + 'no' => 'Norwegian', + 'pl' => 'Polish', + 'pt' => 'Portuguese (Brazil)', + 'pt-PT' => 'Portuguese (Portugal)', + 'ro' => 'Romanian', + 'ru' => 'Russian', + 'si' => 'Sinhala (Sri Lanka)', + 'sk' => 'Slovak', + 'sl' => 'Slovenian', + 'es' => 'Spanish', + 'sv' => 'Swedish', + 'tr' => 'Turkish', + 'uk' => 'Ukrainian', + 'vi' => 'Vietnamese', + 'aa-AA' => 'Test Language' + } + end +end diff --git a/web/documentserver-example/ruby/app/controllers/home_controller.rb b/web/documentserver-example/ruby/app/controllers/home_controller.rb index 6073c019..24b02e1b 100755 --- a/web/documentserver-example/ruby/app/controllers/home_controller.rb +++ b/web/documentserver-example/ruby/app/controllers/home_controller.rb @@ -16,8 +16,15 @@ require 'net/http' require 'mimemagic' +require_relative '../configuration' class HomeController < ApplicationController + @config_manager = ConfigurationManager.new + + class << self + attr_reader :config_manager + end + def index end @@ -159,7 +166,7 @@ class HomeController < ApplicationController isEmbedded = params[:dmode] if JwtHelper.is_enabled && JwtHelper.use_for_request - jwtHeader = Rails.configuration.header.empty? ? "Authorization" : Rails.configuration.header; + jwtHeader = HomeController.config_manage.jwt_header; if request.headers[jwtHeader] hdr = request.headers[jwtHeader] hdr.slice!(0, "Bearer ".length) @@ -278,7 +285,7 @@ class HomeController < ApplicationController isEmbedded = params[:dmode] if JwtHelper.is_enabled && isEmbedded == nil && user_address != nil && JwtHelper.use_for_request - jwtHeader = Rails.configuration.header.empty? ? "Authorization" : Rails.configuration.header; + jwtHeader = HomeController.config_manage.jwt_header; if request.headers[jwtHeader] hdr = request.headers[jwtHeader] hdr.slice!(0, "Bearer ".length) @@ -330,7 +337,7 @@ class HomeController < ApplicationController res = http.request(req) data = res.body - if data.size <= 0 || data.size > Rails.configuration.fileSizeMax + if data.size <= 0 || data.size > HomeController.config_manager.maximum_file_size render plain: '{"error": "File size is incorrect"}' return end diff --git a/web/documentserver-example/ruby/app/models/document_helper.rb b/web/documentserver-example/ruby/app/models/document_helper.rb index 024025da..d57d9131 100755 --- a/web/documentserver-example/ruby/app/models/document_helper.rb +++ b/web/documentserver-example/ruby/app/models/document_helper.rb @@ -14,7 +14,14 @@ # limitations under the License. # +require_relative '../configuration' + class DocumentHelper + @config_manager = ConfigurationManager.new + + class << self + attr_reader :config_manager + end @@runtime_cache = {} @@remote_ip = nil @@ -29,11 +36,7 @@ class DocumentHelper # define max file size def file_size_max - if Rails.configuration.fileSizeMax == nil - 5 * 1024 * 1024 - else - Rails.configuration.fileSizeMax # or get it from the config - end + DocumentHelper.config_manager.maximum_file_size end # all the supported file extensions @@ -42,38 +45,22 @@ class DocumentHelper end def fill_forms_exts - if Rails.configuration.fillDocs.empty? - [] - else - Rails.configuration.fillDocs.split("|") - end + DocumentHelper.config_manager.fillable_file_extensions end # file extensions that can be viewed def viewed_exts - if Rails.configuration.viewedDocs.empty? - [] - else - Rails.configuration.viewedDocs.split("|") - end + DocumentHelper.config_manager.viewable_file_extensions end # file extensions that can be edited def edited_exts - if Rails.configuration.editedDocs.empty? - [] - else - Rails.configuration.editedDocs.split("|") - end + DocumentHelper.config_manager.editable_file_extensions end # file extensions that can be converted def convert_exts - if Rails.configuration.convertDocs.empty? - [] - else - Rails.configuration.convertDocs.split("|") - end + DocumentHelper.config_manager.convertable_file_extensions end # get current user host address @@ -83,8 +70,7 @@ class DocumentHelper # get the storage path of the given file def storage_path(file_name, user_address) - directory = File.absolute_path?(Rails.configuration.storagePath) ? Rails.configuration.storagePath - : Rails.root.join('public', Rails.configuration.storagePath, cur_user_host_address(user_address)) # get the path to the directory for the host address + directory = DocumentHelper.config_manager.storage_path.join(cur_user_host_address(user_address)) # create a new directory if it doesn't exist unless File.directory?(directory) @@ -97,8 +83,7 @@ class DocumentHelper # get the path to the forcesaved file version def forcesave_path(file_name, user_address, create) - directory = File.absolute_path?(Rails.configuration.storagePath) ? Rails.configuration.storagePath - : Rails.root.join('public', Rails.configuration.storagePath, cur_user_host_address(user_address)) + directory = DocumentHelper.config_manager.storage_path.join(cur_user_host_address(user_address)) # the directory with host address doesn't exist unless File.directory?(directory) @@ -176,8 +161,7 @@ class DocumentHelper # get all the stored files from the folder def get_stored_files(user_address) - directory = File.absolute_path?(Rails.configuration.storagePath) ? Rails.configuration.storagePath - : Rails.root.join('public', Rails.configuration.storagePath, cur_user_host_address(user_address)) + directory = DocumentHelper.config_manager.storage_path.join(cur_user_host_address(user_address)) arr = []; @@ -230,7 +214,7 @@ class DocumentHelper # get file url def get_file_uri(file_name, for_document_server) - uri = get_server_url(for_document_server) + '/' + Rails.configuration.storagePath + '/' + cur_user_host_address(nil) + '/' + ERB::Util.url_encode(file_name) + uri = get_server_url(for_document_server) + '/' + DocumentHelper.config_manager.storage_path.to_s + '/' + cur_user_host_address(nil) + '/' + ERB::Util.url_encode(file_name) return uri end @@ -245,11 +229,11 @@ class DocumentHelper # get server url def get_server_url(for_document_server) - if for_document_server && !Rails.configuration.urlExample.empty? - return Rails.configuration.urlExample + if for_document_server && DocumentHelper.config_manager.example_uri + return DocumentHelper.config_manager.example_uri.to_s else return @@base_url - end + end end # get callback url @@ -344,7 +328,7 @@ class DocumentHelper end # enable ignore certificate def verify_ssl(file_uri, http) - if file_uri.start_with?('https') && Rails.configuration.verify_peer_off.eql?('true') + if file_uri.start_with?('https') && DocumentHelper.config_manager.ssl_verify_peer_mode_enabled http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE # set the flags for the server certificate verification at the beginning of SSL session end diff --git a/web/documentserver-example/ruby/app/models/file_model.rb b/web/documentserver-example/ruby/app/models/file_model.rb index 2d57ea1f..d23226cd 100755 --- a/web/documentserver-example/ruby/app/models/file_model.rb +++ b/web/documentserver-example/ruby/app/models/file_model.rb @@ -14,9 +14,12 @@ # limitations under the License. # +require_relative '../configuration' + class FileModel attr_accessor :file_name, :mode, :type, :user_ip, :lang, :user, :action_data, :direct_url + attr_reader :config_manager # set file parameters def initialize(attributes = {}) @@ -28,6 +31,7 @@ class FileModel @user = attributes[:user] @action_data = attributes[:action_data] @direct_url = attributes[:direct_url] + @config_manager = ConfigurationManager.new end def type @@ -46,7 +50,7 @@ class FileModel # get file uri for document server def file_uri_user - File.absolute_path?(Rails.configuration.storagePath) ? download_url + "&dmode=emb" : DocumentHelper.get_file_uri(@file_name, false) + @config_manager.storage_path.absolute? ? download_url + "&dmode=emb" : DocumentHelper.get_file_uri(@file_name, false) end # get document type from its name (word, cell or slide) diff --git a/web/documentserver-example/ruby/app/models/jwt_helper.rb b/web/documentserver-example/ruby/app/models/jwt_helper.rb index d4a21a8e..df8231ed 100644 --- a/web/documentserver-example/ruby/app/models/jwt_helper.rb +++ b/web/documentserver-example/ruby/app/models/jwt_helper.rb @@ -15,12 +15,13 @@ # require 'jwt' +require_relative '../configuration' class JwtHelper - @jwt_secret = Rails.configuration.jwtSecret - @token_use_for_request = Rails.configuration.token_use_for_request - + @jwt_secret = ConfigurationManager.new.jwt_secret + @token_use_for_request = ConfigurationManager.new.jwt_use_for_request + class << self # check if a secret key to generate token exists or not def is_enabled diff --git a/web/documentserver-example/ruby/app/models/service_converter.rb b/web/documentserver-example/ruby/app/models/service_converter.rb index edd2b828..086a9edf 100755 --- a/web/documentserver-example/ruby/app/models/service_converter.rb +++ b/web/documentserver-example/ruby/app/models/service_converter.rb @@ -14,10 +14,17 @@ # limitations under the License. # -class ServiceConverter +require_relative '../configuration' - @@convert_timeout = Rails.configuration.timeout # get the convertion timeout from the config - @@document_converter_url = Rails.configuration.urlSite + Rails.configuration.urlConverter # get the converter url from the config +class ServiceConverter + @config_manager = ConfigurationManager.new + + class << self + attr_reader :config_manager + end + + @@convert_timeout = ServiceConverter.config_manager.convertation_timeout + @@document_converter_url = ServiceConverter.config_manager.document_server_converter_uri.to_s class << self @@ -61,7 +68,7 @@ class ServiceConverter if JwtHelper.is_enabled && JwtHelper.use_for_request # if the signature is enabled payload["token"] = JwtHelper.encode(payload) # get token and save it to the payload - jwtHeader = Rails.configuration.header.empty? ? "Authorization" : Rails.configuration.header; # get signature authorization header + jwtHeader = ServiceConverter.config_manager.jwt_header; # get signature authorization header req.add_field(jwtHeader, "Bearer #{JwtHelper.encode({ :payload => payload })}") # set it to the request with the Bearer prefix end diff --git a/web/documentserver-example/ruby/app/models/track_helper.rb b/web/documentserver-example/ruby/app/models/track_helper.rb index 2e0fef3b..37e03e45 100755 --- a/web/documentserver-example/ruby/app/models/track_helper.rb +++ b/web/documentserver-example/ruby/app/models/track_helper.rb @@ -15,8 +15,16 @@ # require 'net/http' +require_relative '../configuration' class TrackHelper + @config_manager = ConfigurationManager.new + + class << self + attr_reader :config_manager + end + + @@document_command_url = TrackHelper.config_manager.document_server_command_uri.to_s class << self @@ -34,7 +42,7 @@ class TrackHelper if JwtHelper.is_enabled && JwtHelper.use_for_request inHeader = false token = nil - jwtHeader = Rails.configuration.header.empty? ? "Authorization" : Rails.configuration.header; # get the authorization header from the config + jwtHeader = TrackHelper.config_manager.jwt_header; # get the authorization header from the config if file_data["token"] # if the token is in the body token = JwtHelper.decode(file_data["token"]) # decode a token into a payload object using a secret key elsif request.headers[jwtHeader] # if the token is in the header @@ -211,8 +219,6 @@ class TrackHelper # send the command request def command_request(method, key, meta = nil) - document_command_url = Rails.configuration.urlSite + Rails.configuration.commandUrl # get the document command url - # create a payload object with the method and key payload = { :c => method, @@ -226,17 +232,17 @@ class TrackHelper data = nil begin - uri = URI.parse(document_command_url) # parse the document command url + uri = URI.parse(@@document_command_url) # parse the document command url http = Net::HTTP.new(uri.host, uri.port) # create a connection to the http server - DocumentHelper.verify_ssl(document_command_url, http) + DocumentHelper.verify_ssl(@@document_command_url, http) req = Net::HTTP::Post.new(uri.request_uri) # create the post request req.add_field("Content-Type", "application/json") # set headers if JwtHelper.is_enabled && JwtHelper.use_for_request # if the signature is enabled payload["token"] = JwtHelper.encode(payload) # get token and save it to the payload - jwtHeader = Rails.configuration.header.empty? ? "Authorization" : Rails.configuration.header; # get signature authorization header + jwtHeader = TrackHelper.config_manager.jwt_header; # get signature authorization header req.add_field(jwtHeader, "Bearer #{JwtHelper.encode({ :payload => payload })}") # set it to the request with the Bearer prefix end diff --git a/web/documentserver-example/ruby/app/views/home/editor.html.erb b/web/documentserver-example/ruby/app/views/home/editor.html.erb index 4497c2f9..7f990a9f 100755 --- a/web/documentserver-example/ruby/app/views/home/editor.html.erb +++ b/web/documentserver-example/ruby/app/views/home/editor.html.erb @@ -21,7 +21,7 @@ - +