Class: Tupper

Inherits:
Object
  • Object
show all
Defined in:
lib/tupper.rb,
lib/tupper/errors.rb,
lib/tupper/version.rb

Defined Under Namespace

Classes: FileSizeError, SessionError

Constant Summary collapse

DEFAULT_TMP_DIR =
File.join(%w{ / tmp tupper })
DEFAULT_MAX_SIZE =

MB

8
SESSION_STORE_KEY =
'tupper_file_info'
VERSION =
"1.1.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Tupper

Returns a new instance of Tupper.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tupper.rb', line 14

def initialize session
  @max_size = DEFAULT_MAX_SIZE
  @session  = session
  unless ((json = (@session[SESSION_STORE_KEY] || '')).empty?)
    begin
      @file_info = JSON.parse(json)
    rescue
      @session.delete(SESSION_STORE_KEY)
      raise Tupper::SessionError.new('invalid session data')
    end
  end
end

Instance Attribute Details

#file_infoObject (readonly)

Returns the value of attribute file_info.



11
12
13
# File 'lib/tupper.rb', line 11

def file_info
  @file_info
end

#max_sizeObject

Returns the value of attribute max_size.



12
13
14
# File 'lib/tupper.rb', line 12

def max_size
  @max_size
end

#temp_dirObject

Returns the value of attribute temp_dir.



11
12
13
# File 'lib/tupper.rb', line 11

def temp_dir
  @temp_dir
end

Instance Method Details

#cleanupObject



73
74
75
76
77
# File 'lib/tupper.rb', line 73

def cleanup
  File.unlink uploaded_file if has_uploaded_file?
  @session.delete SESSION_STORE_KEY
  @file_info = nil
end

#configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Tupper)

    the object that the method was called on



27
28
29
30
# File 'lib/tupper.rb', line 27

def configure &block
  yield self
  self
end

#has_uploaded_file?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/tupper.rb', line 41

def has_uploaded_file?
  @file_info && File.exists?(@file_info.fetch("uploaded_file", ''))
end

#original_fileObject



69
70
71
# File 'lib/tupper.rb', line 69

def original_file
  (@file_info || {}).fetch('original_file', nil)
end

#upload(file_info) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tupper.rb', line 45

def upload file_info
  unless @temp_dir
    self.temp_dir = DEFAULT_TMP_DIR
  end

  if (file_size = File.size(file_info[:tempfile])) > max_size * 1024 * 1024
    cleanup
    raise FileSizeError.new("Uploaded file was too large. uploaded_size: #{file_size} bytes / max_size: #{max_size} MB")
  end

  file_hash = "#{Time.now.to_i}_#{Digest::MD5.hexdigest(file_info[:filename]).slice(0, 8)}"
  uploaded_file = File.join(temp_dir, file_hash + File.extname(file_info[:filename]))
  FileUtils.cp(file_info[:tempfile], uploaded_file)
  @file_info = {
    'uploaded_file' => uploaded_file,
    'original_file' => file_info[:filename],
  }
  @session[SESSION_STORE_KEY] = @file_info.to_json
end

#uploaded_fileObject



65
66
67
# File 'lib/tupper.rb', line 65

def uploaded_file
  (@file_info || {}).fetch('uploaded_file', nil)
end