Class: XmtFroala::AssetUploader

Inherits:
CarrierWave::Uploader::Base
  • Object
show all
Defined in:
app/models/xmt_froala/asset_uploader.rb

Constant Summary collapse

EXT_NAMES =
{:image => XmtFroala.upload_image_ext,
:flash => XmtFroala.upload_flash_ext,
:media => XmtFroala.upload_media_ext,
:file  => XmtFroala.upload_file_ext}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_asset_model(name) ⇒ Object



92
93
94
95
96
97
# File 'app/models/xmt_froala/asset_uploader.rb', line 92

def self.create_asset_model(name)
  return nil unless %w(asset file flash image media).include?(name.to_s)
  class_name = "XmtFroala::#{name.camelize}"
  return Object.const_get(class_name) if Object.const_defined?(class_name)
  nil
end

.create_uploader(name) ⇒ Object



99
100
101
102
103
104
# File 'app/models/xmt_froala/asset_uploader.rb', line 99

def self.create_uploader(name)
  return nil unless %w(asset file flash image media).include?(name.to_s)
  class_name = "XmtFroala::#{name.camelize}Uploader"
  return Object.const_get(class_name) if Object.const_defined?(class_name)
  nil
end

.has_asset?(name) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/models/xmt_froala/asset_uploader.rb', line 83

def self.has_asset?(name)
  EXT_NAMES.keys.include?(name.to_sym)
end

.is_image?(extname) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'app/models/xmt_froala/asset_uploader.rb', line 87

def self.is_image?(extname)
  EXT_NAMES[:image].include?(extname)
end

Instance Method Details

#cache_dirObject



27
28
29
# File 'app/models/xmt_froala/asset_uploader.rb', line 27

def cache_dir
  "#{Rails.root}/tmp/uploads"
end

#delete_tmp_dir(new_file) ⇒ Object



61
62
63
64
65
66
# File 'app/models/xmt_froala/asset_uploader.rb', line 61

def delete_tmp_dir(new_file)
  # make sure we don't delete other things accidentally by checking the name pattern
  if @cache_id_was.present? && @cache_id_was =~ /\A[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}\z/
    FileUtils.rm_rf(File.join(cache_dir, @cache_id_was))
  end
end

#filenameObject



68
69
70
71
72
73
# File 'app/models/xmt_froala/asset_uploader.rb', line 68

def filename
  if original_filename
    @name ||= Digest::MD5.hexdigest(current_path)
    "#{@name}.#{file.extension}"
  end
end

#move_to_cacheObject



75
76
77
# File 'app/models/xmt_froala/asset_uploader.rb', line 75

def move_to_cache
  false
end

#move_to_storeObject



79
80
81
# File 'app/models/xmt_froala/asset_uploader.rb', line 79

def move_to_store
  true
end

#remember_cache_id(new_file) ⇒ Object

store! nil’s the cache_id after it finishes so we need to remember it for deletition



57
58
59
# File 'app/models/xmt_froala/asset_uploader.rb', line 57

def remember_cache_id(new_file)
  @cache_id_was = cache_id
end

#store_dirObject

Override the directory where uploaded files will be stored. This is a sensible default for uploaders that are meant to be mounted:



19
20
21
22
23
24
25
# File 'app/models/xmt_froala/asset_uploader.rb', line 19

def store_dir
  if model
    "#{XmtFroala.upload_store_dir}/#{model.asset_type.to_s.underscore.gsub(/(xmt_froala\/)|(_uploader)/, '')}/#{model.created_at.strftime("%Y%m")}"
  else
    "#{XmtFroala.upload_store_dir}/#{self.class.to_s.underscore.gsub(/(xmt_froala\/)|(_uploader)/, '')}/#{Time.now.strftime("%Y%m")}"
  end
end