Class: Medium
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Medium
- Defined in:
- lib/has_media/models/medium.rb
Constant Summary collapse
- ENCODE_WAIT =
0
- ENCODE_ENCODING =
1
- ENCODE_SUCCESS =
2
- ENCODE_FAILURE =
3
- ENCODE_NOT_READY =
4
- NO_ENCODING =
5
Constants included from HasMedia
Class Method Summary collapse
Instance Method Summary collapse
-
#directory_path ⇒ Object
System directory to store files.
-
#directory_uri ⇒ Object
http uri of directory which stores media.
-
#encoded_file_name(version = nil) ⇒ Object
encoded_file_name Return the encoded file name for a medium This use the HasMedia.encoded_extensions configuration.
-
#encoding? ⇒ Boolean
Is this medium encoding?.
-
#failed? ⇒ Boolean
Has the encoding failed for this medium.
-
#file_exists?(thumbnail = nil) ⇒ Boolean
file_exists? Is the medium for the current format exists.
-
#file_extension ⇒ String
file_extension Return the file extension for the current medium type This use the HasMedia.encoded_extensions configuration.
-
#file_path(version = nil) ⇒ Object
system path for a medium.
-
#file_uri(version = nil) ⇒ Object
http uri for a medium.
-
#original_file_path ⇒ Object
Public path to the file originally uploaded.
-
#original_file_uri ⇒ Object
Http uri to the originally file.
-
#ready? ⇒ Boolean
Is this medium ready?.
-
#unlink_files ⇒ Object
Delete media file(s) from disk.
Methods included from HasMedia
directory_path, directory_path=, directory_uri, directory_uri=, encoded_extensions, encoded_extensions=, errors_messages, errors_messages=, included, medium_types, medium_types=, #remove_old_media, sanitize_file_name
Class Method Details
.new_from_value(object, value, context, encode, only) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/has_media/models/medium.rb', line 25 def self.new_from_value(object, value, context, encode, only) if value.respond_to?(:content_type) mime_type = value.content_type else mime_type = MIME::Types.type_for(value.path).first.content_type end only ||= "" medium_types = HasMedia.medium_types.keys.collect{|c| Kernel.const_get(c)} if only != "" and klass = Kernel.const_get(only.camelize) medium_types = [klass] end klass = medium_types.find do |k| HasMedia.medium_types[k.to_s].empty? || HasMedia.medium_types[k.to_s].include?(mime_type) end if klass.nil? object.media_errors = [HasMedia.[:type_error]] return end medium = klass.new if value.respond_to?(:original_filename) medium.filename = HasMedia.sanitize_file_name(value.original_filename) else medium.filename = HasMedia.sanitize_file_name(File.basename(value.path)) end medium.file = value medium.content_type = mime_type medium.context = context medium.encode_status = (encode == "false" ? NO_ENCODING : ENCODE_WAIT) medium.save medium end |
Instance Method Details
#directory_path ⇒ Object
System directory to store files
97 98 99 |
# File 'lib/has_media/models/medium.rb', line 97 def directory_path self.file.store_dir end |
#directory_uri ⇒ Object
http uri of directory which stores media
125 126 127 128 129 |
# File 'lib/has_media/models/medium.rb', line 125 def directory_uri File.join(HasMedia.directory_uri, ActiveSupport::Inflector.underscore(self.type), self.id.to_s) end |
#encoded_file_name(version = nil) ⇒ Object
encoded_file_name Return the encoded file name for a medium This use the HasMedia.encoded_extensions configuration
FIXME duplicate with HasMedia::sanitize_file_name
117 118 119 120 121 122 |
# File 'lib/has_media/models/medium.rb', line 117 def encoded_file_name(version = nil) # remove original extension and add the encoded extension final_name = filename.gsub(/\.[^.]{1,4}$/, "") + '.' + file_extension final_name[-4,0] = "_#{version}" if version final_name end |
#encoding? ⇒ Boolean
Is this medium encoding?
60 61 62 |
# File 'lib/has_media/models/medium.rb', line 60 def encoding? encode_status == ENCODE_ENCODING end |
#failed? ⇒ Boolean
Has the encoding failed for this medium
74 75 76 |
# File 'lib/has_media/models/medium.rb', line 74 def failed? encode_status == ENCODE_FAILURE end |
#file_exists?(thumbnail = nil) ⇒ Boolean
file_exists? Is the medium for the current format exists
139 140 141 |
# File 'lib/has_media/models/medium.rb', line 139 def file_exists?(thumbnail = nil) File.exist?(File.join(Rails.root, 'public', file_uri(thumbnail))) end |
#file_extension ⇒ String
file_extension Return the file extension for the current medium type This use the HasMedia.encoded_extensions configuration
150 151 152 153 154 155 156 |
# File 'lib/has_media/models/medium.rb', line 150 def file_extension sym = type.underscore.to_sym unless HasMedia.encoded_extensions.keys.include?(sym) raise Exception.new("You need to add encoded extension configuration for :#{sym}") end HasMedia.encoded_extensions[sym] end |
#file_path(version = nil) ⇒ Object
system path for a medium
101 102 103 |
# File 'lib/has_media/models/medium.rb', line 101 def file_path(version = nil) File.join(directory_path, encoded_file_name(version)) end |
#file_uri(version = nil) ⇒ Object
http uri for a medium
106 107 108 |
# File 'lib/has_media/models/medium.rb', line 106 def file_uri(version = nil) File.join(directory_uri, encoded_file_name(version)) end |
#original_file_path ⇒ Object
Public path to the file originally uploaded.
87 88 89 |
# File 'lib/has_media/models/medium.rb', line 87 def original_file_path File.join(directory_path, self.filename) end |
#original_file_uri ⇒ Object
Http uri to the originally file
92 93 94 |
# File 'lib/has_media/models/medium.rb', line 92 def original_file_uri File.join(directory_uri, self.filename) end |
#ready? ⇒ Boolean
Is this medium ready?
67 68 69 |
# File 'lib/has_media/models/medium.rb', line 67 def ready? encode_status == ENCODE_SUCCESS end |
#unlink_files ⇒ Object
Delete media file(s) from disk
81 82 83 84 |
# File 'lib/has_media/models/medium.rb', line 81 def unlink_files file = self.full_filename File.unlink(file) if File.exists? file end |