Class: Lifter::FileUpload

Inherits:
Object
  • Object
show all
Defined in:
lib/lifter/file_upload.rb

Constant Summary collapse

DEFAULT_PROLOGUE_SIZE =
10 * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, opts = {}) ⇒ FileUpload

Returns a new instance of FileUpload.

[View source]

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

def initialize(file, opts = {})
  @mutex = Mutex.new

  @file = file
  @path = file.path

  @authorized = false
  @pending_authorization = false

  @hash = setup_hash(opts[:hash_method])

  @prologue_limit = opts[:prologue_size] || DEFAULT_PROLOGUE_SIZE
  @prologue = ''

  @original_request = opts[:original_request]
  @original_name = opts[:original_name]
  @param = opts[:param]
end

Instance Attribute Details

#original_nameObject (readonly)

Returns the value of attribute original_name.


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

def original_name
  @original_name
end

#original_requestObject (readonly)

Returns the value of attribute original_request.


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

def original_request
  @original_request
end

#paramObject (readonly)

Returns the value of attribute param.


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

def param
  @param
end

#prologueObject (readonly)

Returns the value of attribute prologue.


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

def prologue
  @prologue
end

Instance Method Details

#authorizeObject

[View source]

67
68
69
70
71
# File 'lib/lifter/file_upload.rb', line 67

def authorize
  @mutex.synchronize do
    @authorized = true
  end
end

#authorized?Boolean

Returns:

  • (Boolean)
[View source]

89
90
91
92
93
94
95
96
97
# File 'lib/lifter/file_upload.rb', line 89

def authorized?
  authorized = false

  @mutex.synchronize do
    authorized = @authorized == true
  end

  authorized
end

#closeObject

[View source]

45
46
47
# File 'lib/lifter/file_upload.rb', line 45

def close
  @file.close if !@file.closed?
end

#flushObject

[View source]

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

def flush
  @file.flush
end

#full_pathObject

[View source]

59
60
61
# File 'lib/lifter/file_upload.rb', line 59

def full_path
  File.expand_path(@path)
end

#hashObject

[View source]

63
64
65
# File 'lib/lifter/file_upload.rb', line 63

def hash
  @hash.hexdigest
end

#mv(new_path) ⇒ Object

[View source]

54
55
56
57
# File 'lib/lifter/file_upload.rb', line 54

def mv(new_path)
  FileUtils.mv(full_path, new_path)
  @path = new_path
end

#pending_authorizationObject

[View source]

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

def pending_authorization
  @mutex.synchronize do
    @pending_authorization = true
  end
end

#pending_authorization?Boolean

Returns:

  • (Boolean)
[View source]

79
80
81
82
83
84
85
86
87
# File 'lib/lifter/file_upload.rb', line 79

def pending_authorization?
  pending = false

  @mutex.synchronize do
    pending = @pending_authorization == true
  end

  pending
end

#rmObject

[View source]

49
50
51
52
# File 'lib/lifter/file_upload.rb', line 49

def rm
  FileUtils.rm(full_path)
  @path = nil
end

#write(data) ⇒ Object

[View source]

31
32
33
34
35
36
37
38
39
# File 'lib/lifter/file_upload.rb', line 31

def write(data)
  @file.write(data)

  @hash << data

  if @prologue.length < @prologue_limit
    @prologue << data[0, @prologue_limit - @prologue.length]
  end
end