Class: ElFinderS3::Connector
- Inherits:
-
Object
- Object
- ElFinderS3::Connector
- Defined in:
- lib/el_finder_s3/connector.rb
Overview
Represents ElFinder connector on Rails side.
Constant Summary collapse
- VALID_COMMANDS =
Valid commands to run.
%w[archive duplicate put file ls tree extract mkdir mkfile open paste ping get rename resize rm tmb upload]
- DEFAULT_OPTIONS =
Default options for instances.
{ :mime_handler => ElFinderS3::MimeType, :image_handler => ElFinderS3::Image, :original_filename_method => lambda { |file| file.original_filename.respond_to?(:force_encoding) ? file.original_filename.force_encoding('utf-8') : file.original_filename }, :disabled_commands => %w(archive duplicate extract resize tmb), :allow_dot_files => true, :upload_max_size => '50M', :name_validator => lambda { |name| name.strip != '.' && name =~ /^[^\x00-\x1f\\?*:"><|\/]+$/ }, :upload_file_mode => 0644, :archivers => {}, :extractors => {}, :home => 'Home', :default_perms => {:read => true, :write => true, :locked => false, :hidden => false}, :perms => [], :thumbs => false, :thumbs_directory => '.thumbs', :thumbs_size => 48, :thumbs_at_once => 5, }
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#options ⇒ Hash
writeonly
Options setter.
Class Method Summary collapse
Instance Method Summary collapse
- #from_hash(hash) ⇒ Object
-
#initialize(options) ⇒ Connector
constructor
Initializes new instance.
-
#run(params) ⇒ Object
Runs request-response cycle.
- #to_hash(pathname) ⇒ Object
Constructor Details
#initialize(options) ⇒ Connector
Initializes new instance.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/el_finder_s3/connector.rb', line 42 def initialize() @options = DEFAULT_OPTIONS.merge() raise(ArgumentError, 'Missing required :url option') unless @options.key?(:url) raise(ArgumentError, 'Missing required :server option') unless @options.key?(:server) raise(ArgumentError, 'Mime Handler is invalid') unless mime_handler.respond_to?(:for) raise(ArgumentError, 'Image Handler is invalid') unless image_handler.nil? || ([:size, :resize, :thumbnail].all? { |m| image_handler.respond_to?(m) }) raise(ArgumentError, 'Missing required :region option') unless @options[:server].key?(:region) raise(ArgumentError, 'Missing required :access_key_id option') unless @options[:server].key?(:access_key_id) raise(ArgumentError, 'Missing required :secret_access_key option') unless @options[:server].key?(:secret_access_key) raise(ArgumentError, 'Missing required :bucket_name option') unless @options[:server].key?(:bucket_name) @options[:url] = 'https://' + @options[:server][:bucket] + '.s3.amazonaws.com' unless @options.key?(:url) @headers = {} @response = {} end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
13 14 15 |
# File 'lib/el_finder_s3/connector.rb', line 13 def adapter @adapter end |
#options=(value) ⇒ Hash (writeonly)
Options setter.
154 155 156 157 158 159 |
# File 'lib/el_finder_s3/connector.rb', line 154 def (value = {}) value.each_pair do |k, v| @options[k.to_sym] = v end @options end |
Class Method Details
.logger ⇒ Object
65 66 67 |
# File 'lib/el_finder_s3/connector.rb', line 65 def logger @logger ||= Logger.new(STDOUT) end |
.logger=(val) ⇒ Object
69 70 71 |
# File 'lib/el_finder_s3/connector.rb', line 69 def logger=(val) @logger = val end |
Instance Method Details
#from_hash(hash) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/el_finder_s3/connector.rb', line 132 def from_hash(hash) # restore missing '=' len = hash.length % 4 hash += '==' if len == 1 or len == 2 hash += '=' if len == 3 decoded_hash = Base64.urlsafe_decode64(hash) decoded_hash = decoded_hash.respond_to?(:force_encoding) ? decoded_hash.force_encoding('utf-8') : decoded_hash pathname = @root + decoded_hash rescue ArgumentError => e if e. != 'invalid base64' raise end nil end |
#run(params) ⇒ Object
Runs request-response cycle.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/el_finder_s3/connector.rb', line 78 def run(params) @adapter = ElFinderS3::Adapter.new(@options[:server]) # @adapter = ElFinderS3::FtpAdapter.new(@options[:server]) @root = ElFinderS3::Pathname.new(adapter) begin @params = params.dup @headers = {} @response = {} @response[:errorData] = {} if VALID_COMMANDS.include?(@params[:cmd]) if @options[:thumbs] @thumb_directory = @root + @options[:thumbs_directory] @thumb_directory.mkdir unless @thumb_directory.exist? raise(RuntimeError, "Unable to create thumbs directory") unless @thumb_directory.directory? end @current = @params[:current] ? from_hash(@params[:current]) : nil @target = (@params[:target] and !@params[:target].empty?) ? from_hash(@params[:target]) : nil if params[:targets] @targets = @params[:targets].map { |t| from_hash(t) } end begin send("_#{@params[:cmd]}") rescue Net::FTPPermError @response[:error] = 'Access Denied' end else invalid_request end @response.delete(:errorData) if @response[:errorData].empty? return @headers, @response ensure adapter.close end end |
#to_hash(pathname) ⇒ Object
124 125 126 127 |
# File 'lib/el_finder_s3/connector.rb', line 124 def to_hash(pathname) # note that '=' are removed Base64.urlsafe_encode64(pathname.path.to_s).chomp.tr("=\n", "") end |