Class: FileUploader

Inherits:
CarrierWave::Uploader::Base
  • Object
show all
Includes:
Card::Location
Defined in:
mod/05_standard/lib/file_uploader.rb

Direct Known Subclasses

ImageUploader

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Card::Location

#card_path, #card_url, #page_path

Instance Attribute Details

#modObject

Returns the value of attribute mod.



44
45
46
# File 'mod/05_standard/lib/file_uploader.rb', line 44

def mod
  @mod
end

Instance Method Details

#action_idObject



152
153
154
# File 'mod/05_standard/lib/file_uploader.rb', line 152

def action_id
  model.selected_content_action_id
end

#cache_dirObject



100
101
102
# File 'mod/05_standard/lib/file_uploader.rb', line 100

def cache_dir
  Cardio.paths['files'].existent.first + '/cache'
end

#create_versions?(new_file) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'mod/05_standard/lib/file_uploader.rb', line 127

def create_versions? new_file
  model.create_versions?
end

#db_content(opts = {}) ⇒ Object

generate identifier that gets stored in the card’s db_content field



67
68
69
70
71
72
# File 'mod/05_standard/lib/file_uploader.rb', line 67

def db_content opts={}
  if opts[:mod] && !model.load_from_mod
    model.load_from_mod = opts[:mod]
  end
  "%s/%s" % [file_dir, url_filename(opts)]
end

#extensionObject



57
58
59
60
61
62
63
64
# File 'mod/05_standard/lib/file_uploader.rb', line 57

def extension
  case
  when file && file.extension.present? ; ".#{file.extension}"
  when card_content = model.content    ; File.extname(card_content)
  when orig = original_filename        ; File.extname(orig)
  else                                   ''
  end.downcase
end

#file_dirObject



90
91
92
93
94
95
96
97
98
# File 'mod/05_standard/lib/file_uploader.rb', line 90

def file_dir
  if mod_file?
    ":#{model.codename}"
  elsif model.id
    "~#{model.id}"
  else
    "~#{model.upload_cache_card.id}"
  end
end

#filenameObject



49
50
51
52
53
54
55
# File 'mod/05_standard/lib/file_uploader.rb', line 49

def filename
  if mod_file?
    "#{model.type_code}#{extension}"
  else
    "#{action_id}#{extension}"
  end
end

#mod_file?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'mod/05_standard/lib/file_uploader.rb', line 148

def mod_file?
  @mod ||= model.mod_file?
end

#original_filenameObject



136
137
138
# File 'mod/05_standard/lib/file_uploader.rb', line 136

def original_filename
  @original_filename ||= (model.selected_action && model.selected_action.comment)
end

#path(version = nil) ⇒ Object

paperclip compatibility used in type/file.rb#core (base format)



132
133
134
# File 'mod/05_standard/lib/file_uploader.rb', line 132

def path(version=nil)
  version ? versions[version].path : super()
end

#retrieve_dirObject



144
145
146
# File 'mod/05_standard/lib/file_uploader.rb', line 144

def retrieve_dir
  model.retrieve_dir
end

#retrieve_pathObject



116
117
118
# File 'mod/05_standard/lib/file_uploader.rb', line 116

def retrieve_path
  File.join([retrieve_dir, full_filename(filename)].compact)
end

#store_dirObject



140
141
142
# File 'mod/05_standard/lib/file_uploader.rb', line 140

def store_dir
  model.store_dir
end

#store_path(for_file = nil) ⇒ Object

Carrierwave calls store_path without argument when it stores the file and with the identifier from the db when it retrieves the file In our case the first part of our identifier is not part of the path but we can construct the filename from db data. So we don’t need the identifier.



108
109
110
111
112
113
114
# File 'mod/05_standard/lib/file_uploader.rb', line 108

def store_path(for_file=nil) #
  if for_file
    retrieve_path
  else
    File.join([store_dir, full_filename(filename)].compact)
  end
end

#tmp_pathObject



120
121
122
123
124
125
# File 'mod/05_standard/lib/file_uploader.rb', line 120

def tmp_path
  if !Dir.exists? model.tmp_upload_dir
    Dir.mkdir model.tmp_upload_dir
  end
  File.join model.tmp_upload_dir, filename
end

#url(opts = {}) ⇒ Object



86
87
88
# File 'mod/05_standard/lib/file_uploader.rb', line 86

def url opts = {}
  "%s/%s/%s" % [card_path(Card.config.files_web_path), file_dir, full_filename(url_filename(opts))]
end

#url_filename(opts = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'mod/05_standard/lib/file_uploader.rb', line 74

def url_filename opts={}
  if opts[:mod] && !model.load_from_mod
    model.load_from_mod = opts[:mod]
  end

  basename = if (mod = mod_file?)
    "#{mod}#{extension}"
  else
    "#{action_id}#{extension}"
  end
end