-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy.rb
More file actions
51 lines (40 loc) · 1.17 KB
/
copy.rb
File metadata and controls
51 lines (40 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
unless DB.table_exists?(:copies)
DB.create_table :copies do
primary_key :id
String :url, :size=>2000, :null=>false #size of url
String :sha1, :size=>40
String :md5, :size=>40, :null=>false
Bignum :size
Time :timestamp
foreign_key :aip_id, :aips, :type=>'integer', :null=>false
index :aip_id, :name=>:index_copies_aip
end
end
class Copy < Sequel::Model(:copies)
plugin :validation_helpers
def validate
super
validates_format %r([a-f0-9]{40}), :sha1
validates_format %r([a-f0-9]{32}), :md5
#validates_format /http/, :url
end
many_to_one :aip
def download f
rs = StorageMaster.new id, url.to_s
rs.download f
if size
unless File.size(f) == self.size
raise "#{url} size is wrong: expected #{size}, actual #{File.size(f)}"
end
actual_sha = Digest::SHA1.file(f).hexdigest
unless actual_sha == self.sha1
raise "#{url} sha1 is wrong: expected #{self.sha1}, actual #{actual_sha1}"
end
end
actual_md5 = Digest::MD5.file(f).hexdigest
unless actual_md5 == self.md5
raise "#{url} md5 is wrong: expected #{self.md5}, actual #{actual_md5}"
end
f
end
end