Class: Attachs::Types::File

Inherits:
Object
  • Object
show all
Defined in:
lib/attachs/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/attachs/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

#default?Boolean

Returns:

  • (Boolean)


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

def default?
  @default == true
end

#deleteObject



90
91
92
93
94
95
96
97
98
# File 'lib/attachs/types/file.rb', line 90

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

#deleted?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/attachs/types/file.rb', line 36

def deleted?
  @deleted == true
end

#exists?(*args) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/attachs/types/file.rb', line 40

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

#extnameObject



50
51
52
53
# File 'lib/attachs/types/file.rb', line 50

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

#filenameObject



55
56
57
58
# File 'lib/attachs/types/file.rb', line 55

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

#original_filenameObject



60
61
62
63
# File 'lib/attachs/types/file.rb', line 60

def original_filename
  return nil unless upload.present?
  upload.original_filename
end

#path(*args) ⇒ Object



65
66
67
68
# File 'lib/attachs/types/file.rb', line 65

def path(*args)
  return nil if deleted?
  (stored? ? destination_path(*args) : upload.path).to_s
end

#realpath(*args) ⇒ Object



70
71
72
73
# File 'lib/attachs/types/file.rb', line 70

def realpath(*args)
  return nil if deleted? or Rails.application.config.attachs.storage == :s3
  (stored? ? storage.realpath(path(*args)) : upload.path).to_s
end

#size(*args) ⇒ Object



45
46
47
48
# File 'lib/attachs/types/file.rb', line 45

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

#storeObject



80
81
82
83
84
85
86
87
88
# File 'lib/attachs/types/file.rb', line 80

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

#stored?Boolean

Returns:

  • (Boolean)


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

def stored?
  @stored == true
end

#url(*args) ⇒ Object



75
76
77
78
# File 'lib/attachs/types/file.rb', line 75

def url(*args)
  return nil if deleted? or not stored?
  storage.url(path(*args)).to_s
end