Class: RailsUploads::Types::File

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_uploads/types/file.rb

Direct Known Subclasses

Image

Instance Method Summary collapse

Constructor Details

#initialize(source, options = {}) ⇒ File

Returns a new instance of File.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rails_uploads/types/file.rb', line 5

def initialize(source, options={})
  if source.is_a? ActionDispatch::Http::UploadedFile or source.is_a? Rack::Test::UploadedFile
    @upload = source          
    @stored = false
    @default = false
    @storage = build_storage(:local)
  elsif source.is_a? String
    @upload = false
    @filename = source
    @stored = true
    @default = false
    @storage = build_storage
  elsif options.has_key? :default
    @upload = false
    @filename = options[:default]
    @stored = true
    @default = true
    @storage = build_storage
  end
  @deleted = false        
  @options = options
end

Instance Method Details

#deleteObject



76
77
78
79
80
81
82
83
84
# File 'lib/rails_uploads/types/file.rb', line 76

def delete
  if not is_default? and is_stored? and exists?
    storage.delete path
    yield if block_given?
    @storage = build_storage(:local) if @upload.present?
    @stored = false
    @deleted = true
  end
end

#exists?(*args) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/rails_uploads/types/file.rb', line 32

def exists?(*args)
  return false if is_deleted?
  storage.exists? path(*args)
end

#extnameObject



46
47
48
49
# File 'lib/rails_uploads/types/file.rb', line 46

def extname
  return nil if is_deleted?
  @extname ||= ::File.extname(filename)
end

#filenameObject



51
52
53
54
# File 'lib/rails_uploads/types/file.rb', line 51

def filename
  return nil if is_deleted?
  @filename ||= "#{(Time.now.to_f * 10000000).to_i}#{::File.extname @upload.original_filename}".downcase
end

#is_default?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/rails_uploads/types/file.rb', line 28

def is_default?
  @default
end

#is_deleted?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/rails_uploads/types/file.rb', line 37

def is_deleted?
  @deleted
end

#path(*args) ⇒ Object



56
57
58
59
# File 'lib/rails_uploads/types/file.rb', line 56

def path(*args)
  return nil if is_deleted?
  is_stored? ? destination_path(*args) : @upload.path
end

#size(*args) ⇒ Object



41
42
43
44
# File 'lib/rails_uploads/types/file.rb', line 41

def size(*args)
  return 0 if is_deleted?
  storage.size path(*args)
end

#storeObject



66
67
68
69
70
71
72
73
74
# File 'lib/rails_uploads/types/file.rb', line 66

def store
  if not is_stored? and is_upload?
    @storage = build_storage
    storage.store @upload, destination_path
    yield if block_given?
    @stored = true
    @deleted = false
  end
end

#url(*args) ⇒ Object



61
62
63
64
# File 'lib/rails_uploads/types/file.rb', line 61

def url(*args)
  return nil if is_deleted? or not is_stored?
  storage.url path(*args)
end