Class: InlineUploader::Attacher
- Inherits:
-
Object
- Object
- InlineUploader::Attacher
- Defined in:
- lib/inline_uploader.rb
Instance Method Summary collapse
- #attach_fds(params) ⇒ Object
- #attach_previous_uploads(*hashes) ⇒ Object
- #call(env) ⇒ Object
-
#get_valid_tag(val) ⇒ Object
returns a valid tag or nil.
-
#initialize(app, delegate) ⇒ Attacher
constructor
A new instance of Attacher.
- #logger ⇒ Object
Constructor Details
#initialize(app, delegate) ⇒ Attacher
Returns a new instance of Attacher.
45 46 47 48 |
# File 'lib/inline_uploader.rb', line 45 def initialize(app, delegate) @delegate = delegate @app = app end |
Instance Method Details
#attach_fds(params) ⇒ Object
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 |
# File 'lib/inline_uploader.rb', line 79 def attach_fds(params) params.inject({}) do |fds, kv| key, val = kv if val.is_a? Hash fds.merge! attach_fds(params[key]) elsif tag = get_valid_tag(val) upload = File.join(@delegate.upload_dir, tag) if File.exist?(upload) file = YAML.load_file("#{upload}_meta") file[:name] = key file[:tempfile] = File.open(upload, 'rb') params[key] = file fds[tag] = file[:tempfile] logger.info "attaching upload file at #{upload}" else params[key] = nil logger.info "no upload file found at #{upload}" end end fds end end |
#attach_previous_uploads(*hashes) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/inline_uploader.rb', line 60 def attach_previous_uploads(*hashes) return yield unless hashes.inject(false) {|others,h| !h.delete(@delegate.flag_param).nil? or others } logger.info "Attaching Inline Upload Tags." fds = {} begin hashes.each {|h| fds.merge! attach_fds(h) } yield ensure fds.values.each do |f| f.close unless f.closed? FileUtils.rm(f.path) if File.exist?(f.path) end end end |
#call(env) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/inline_uploader.rb', line 50 def call(env) request = Rack::Request.new(env) # we have to look at both get and post as if Rack::Request.params # has been called, GET will also contain all POST params. go figure. attach_previous_uploads request.GET, request.POST do return @app.call(env) end end |
#get_valid_tag(val) ⇒ Object
returns a valid tag or nil.
110 111 112 |
# File 'lib/inline_uploader.rb', line 110 def get_valid_tag(val) val if val.is_a? String and val.length == TAG_LENGTH and val =~ TAG_REGEXP end |
#logger ⇒ Object
114 115 116 |
# File 'lib/inline_uploader.rb', line 114 def logger @delegate.logger end |