summaryrefslogtreecommitdiff
path: root/app/models/generic_file.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/generic_file.rb')
-rw-r--r--app/models/generic_file.rb62
1 files changed, 62 insertions, 0 deletions
diff --git a/app/models/generic_file.rb b/app/models/generic_file.rb
new file mode 100644
index 0000000..0e4dce5
--- /dev/null
+++ b/app/models/generic_file.rb
@@ -0,0 +1,62 @@
+class GenericFile < ActiveRecord::Base
+ FILE_TYPES = %w(pdf ps jpg gif png tif wav mp3)
+ CATEGORIES = %w(file document image greeting recording)
+
+ attr_accessible :name, :file, :file_type, :category, :owner_id, :owner_type
+
+ mount_uploader :file, GenericFileUploader
+
+ belongs_to :owner, :polymorphic => true
+
+ validates :name,
+ :presence => true,
+ :uniqueness => {:scope => [:owner_id, :owner_type]}
+
+ validates :file,
+ :presence => true
+
+ before_save :determine_file_type
+
+ def to_s
+ self.name
+ end
+
+ def store_dir
+ "/var/opt/gemeinschaft/generic_files/#{self.id.to_i}"
+ end
+
+ def mime_type
+ return GenericFile.mime_type(self.file.to_s)
+ end
+
+ def self.mime_type(file_name)
+ mime_type = `file -b --mime-type "#{file_name}"`.strip
+ if mime_type.blank?
+ mime_type = MIME::Types.type_for(file_name).first.to_s
+ end
+
+ return mime_type
+ end
+
+ def file_size
+ if self.file
+ return File.size(self.file.to_s)
+ else
+ return 0
+ end
+ end
+
+ def file_extension
+ mime_type = Mime::LOOKUP[self.file_type]
+ if mime_type.class == Mime::Type
+ return mime_type.symbol
+ end
+ end
+
+ private
+ def determine_file_type
+ if self.file_changed?
+ self.file_type = self.mime_type
+ end
+ end
+end