Modified: branches/packs/app/models/content_blob.rb (3557 => 3558)
--- branches/packs/app/models/content_blob.rb 2013-05-22 13:27:47 UTC (rev 3557)
+++ branches/packs/app/models/content_blob.rb 2013-05-22 13:42:17 UTC (rev 3558)
@@ -1,26 +1,35 @@
# myExperiment: app/models/content_blob.rb
#
-# Copyright (c) 2009 University of Manchester and the University of Southampton.
-# See license.txt for details.
+# Copyright (c) 2007-2013 The University of Manchester, the University of
+# Oxford, and the University of Southampton. See license.txt for details.
require 'digest/md5'
require 'digest/sha1'
class ContentBlob < ActiveRecord::Base
+ before_save :update_metadata
+
+ # validates_presence_of uses a function that assumes UTF-8 encoding and thus
+ # has issues with other encodings. The following validation provides similar
+ # functionality to validates_presence_of on the content blob data.
+
validate do |record|
if record.data.nil? || record.data.length == 0
record.errors.add(:data, 'cannot be empty.')
end
end
- before_save do |blob|
- blob.update_checksums
- end
+ def update_metadata
- def update_checksums
self.md5 = Digest::MD5.hexdigest(data)
self.sha1 = Digest::SHA1.hexdigest(data)
+
+ case self.data
+ when StringIO
+ self.size = self.data.size
+ when String
+ self.size = self.data.bytesize
+ end
end
-
end
Added: branches/packs/db/migrate/20130520145852_add_size_to_content_blobs.rb (0 => 3558)
--- branches/packs/db/migrate/20130520145852_add_size_to_content_blobs.rb (rev 0)
+++ branches/packs/db/migrate/20130520145852_add_size_to_content_blobs.rb 2013-05-22 13:42:17 UTC (rev 3558)
@@ -0,0 +1,10 @@
+class AddSizeToContentBlobs < ActiveRecord::Migration
+ def self.up
+ add_column :content_blobs, :size, :integer
+ execute "UPDATE content_blobs SET size = LENGTH(data);"
+ end
+
+ def self.down
+ drop_column :content_blobs, :size
+ end
+end