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.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Tupper

Returns a new instance of Tupper.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tupper.rb', line 13

def initialize session
  @max_size = DEFAULT_MAX_SIZE
  @session = session
  if @session.has_key?(SESSION_STORE_KEY)
    json = @session.fetch(SESSION_STORE_KEY)
  else
    json = ''
  end

  unless json.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.



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

def file_info
  @file_info
end

#max_sizeObject

Returns the value of attribute max_size.



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

def max_size
  @max_size
end

#temp_dirObject

Returns the value of attribute temp_dir.



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

def temp_dir
  @temp_dir
end

Instance Method Details

#cleanupObject



78
79
80
81
82
# File 'lib/tupper.rb', line 78

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



32
33
34
35
# File 'lib/tupper.rb', line 32

def configure &block
  yield self
  self
end

#has_uploaded_file?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/tupper.rb', line 46

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

#original_fileObject



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

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

#upload(file_info) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tupper.rb', line 50

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



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

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