Class: OmniFiles::ProtectedApp

Inherits:
BaseApp
  • Object
show all
Defined in:
lib/omnifiles/protectedapp.rb

Overview

Protected app for POST request

Instance Method Summary collapse

Methods inherited from BaseApp

sanitize

Instance Method Details

#format_time_str(time) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/omnifiles/protectedapp.rb', line 39

def format_time_str time
  if time
    time.localtime.to_s
  else
    '<i>Not yet</i>'
  end
end

#make_haml_data_from_doc(doc) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/omnifiles/protectedapp.rb', line 47

def make_haml_data_from_doc doc
  {
    shortened: doc['shortened'],
    url: url('/f/' + doc['shortened']),
    original_filename: make_visible_filename(doc['original_filename']),
    mime: doc['mime'],
    access_time: format_time_str(doc['accessed']['time']),
    created_time: format_time_str(doc['created']['time']),
    access_count: doc['accessed']['count']
  }
end

#make_visible_filename(filename) ⇒ Object



35
36
37
# File 'lib/omnifiles/protectedapp.rb', line 35

def make_visible_filename filename
    filename ? URI.unescape(filename) : "<i>Not provided</i>"
end

#store_fileObject

POST handler with form/body handling



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/omnifiles/protectedapp.rb', line 108

def store_file
  logger.info "Route POST store"
  begin
    req = Rack::Request.new(env)
    if !req.POST || req.POST == {}
      logger.info "Saving POST body to temp file"

      original_filename = nil

      # Make a temp file with body content
      temp_file = Tempfile.new("omnifiles-post-")
      File.open(temp_file.path, 'wb') do |ftemp|
        IO.copy_stream(req.body, ftemp)
      end
    else
      logger.info "Using POST form"

      # Use a Rack provided file with content
      post_file = req.POST['file']
      original_filename = URI.escape(File.basename(post_file[:filename]))

      temp_file = post_file[:tempfile]
    end

    store_with_file temp_file.path, original_filename

  ensure
    if temp_file
      temp_file.close
      temp_file.unlink
    end
  end
end

#store_with_file(path, original_filename) ⇒ Object

Save temporary file to storage



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/omnifiles/protectedapp.rb', line 143

def store_with_file path, original_filename
  # Take a sample of file
  sample = Digest::MD5.hexdigest(IO.binread(path, 0x100))

  # Determine file mime and desired url
  mime = FileMagic.mime.file path

  # Short URL is composed from escaped filename from form, mime type and leading file bytes
  shortened = @storage.shorten_file sample, original_filename, mime

  # Save file to storage
  target_path = File.join(Settings.storage_dir, shortened)
  raise "Not so unique id #{shortened}" if File.exists? target_path
  FileUtils.cp path, target_path

  # Put record to storage
  @storage.put_file shortened, original_filename, mime
  short_url = url('/f/'+shortened)

  logger.info "Stored file #{target_path} to shortened #{shortened}, magic '#{mime}'"

  short_url
end