summaryrefslogtreecommitdiff
path: root/app/models/generic_file.rb
diff options
context:
space:
mode:
authorPeter Kozak <spag@golwen.net>2013-04-11 10:51:28 +0200
committerPeter Kozak <spag@golwen.net>2013-04-11 10:51:28 +0200
commit78ea4bcd397a4d8f39ad3c08066fe918a8fc89ad (patch)
treea22d63d7fcaa2c2c32d2905424777a1ff1a8a21d /app/models/generic_file.rb
parent145d774adece71282020603b699ab684537cc1b3 (diff)
file uploader
Diffstat (limited to 'app/models/generic_file.rb')
-rw-r--r--app/models/generic_file.rb44
1 files changed, 44 insertions, 0 deletions
diff --git a/app/models/generic_file.rb b/app/models/generic_file.rb
new file mode 100644
index 0000000..b6b3ede
--- /dev/null
+++ b/app/models/generic_file.rb
@@ -0,0 +1,44 @@
+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
+
+ before_save :determine_file_type
+
+ 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
+
+ private
+ def determine_file_type
+ if self.file_changed?
+ self.file_type = self.mime_type
+ end
+ end
+end