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.



39
40
41
# File 'mod/05_standard/lib/file_uploader.rb', line 39

def mod
  @mod
end

Instance Method Details

#action_idObject



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

def action_id
  model.selected_content_action_id
end

#cache_dirObject



93
94
95
# File 'mod/05_standard/lib/file_uploader.rb', line 93

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

#create_versions?(_new_file) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'mod/05_standard/lib/file_uploader.rb', line 119

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



62
63
64
65
66
# File 'mod/05_standard/lib/file_uploader.rb', line 62

def db_content opts={}
  return '' unless file.present?
  model.load_from_mod = opts[:mod] if opts[:mod] && !model.load_from_mod
  '%s/%s' % [file_dir, url_filename(opts)]
end

#extensionObject



52
53
54
55
56
57
58
59
# File 'mod/05_standard/lib/file_uploader.rb', line 52

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

#file_dirObject



83
84
85
86
87
88
89
90
91
# File 'mod/05_standard/lib/file_uploader.rb', line 83

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

#filenameObject



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

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

#mod_file?Boolean

Returns:

  • (Boolean)


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

def mod_file?
  model.mod_file?
end

#original_filenameObject



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

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)



124
125
126
# File 'mod/05_standard/lib/file_uploader.rb', line 124

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

#retrieve_dirObject



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

def retrieve_dir
  model.retrieve_dir
end

#retrieve_pathObject



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

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

#store_dirObject



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

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.



102
103
104
105
106
107
108
# File 'mod/05_standard/lib/file_uploader.rb', line 102

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

#tmp_pathObject



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

def tmp_path
  Dir.mkdir model.tmp_upload_dir unless Dir.exist? model.tmp_upload_dir
  File.join model.tmp_upload_dir, filename
end

#url(opts = {}) ⇒ Object



78
79
80
81
# File 'mod/05_standard/lib/file_uploader.rb', line 78

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



68
69
70
71
72
73
74
75
76
# File 'mod/05_standard/lib/file_uploader.rb', line 68

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

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