include data_size and files list in Collection api response
authorTom Clegg <tom@clinicalfuture.com>
Tue, 5 Feb 2013 03:31:43 +0000 (22:31 -0500)
committerTom Clegg <tom@clinicalfuture.com>
Tue, 5 Feb 2013 06:41:51 +0000 (01:41 -0500)
app/models/collection.rb

index 2d9d9f102fd95233a53cf74f57db27f94bf60b14..03c0a9449555d87cfe2363c9336ea22b1e787aeb 100644 (file)
@@ -4,13 +4,8 @@ class Collection < OrvosModel
   include CommonApiTemplate
 
   api_accessible :superuser, :extend => :common do |t|
-    t.add :locator
-    t.add :portable_data_hash
-    t.add :name
-    t.add :redundancy
-    t.add :redundancy_confirmed_by_client
-    t.add :redundancy_confirmed_at
-    t.add :redundancy_confirmed_as
+    t.add :data_size
+    t.add :files
   end
 
   def redundancy_status
@@ -47,4 +42,46 @@ class Collection < OrvosModel
       false
     end
   end
+
+  def data_size
+    inspect_manifest_text if @data_size.nil? or manifest_text_changed?
+    @data_size
+  end
+
+  def files
+    inspect_manifest_text if @files.nil? or manifest_text_changed?
+    @files
+  end
+
+  def inspect_manifest_text
+    if !manifest_text
+      @data_size = false
+      @files = []
+      return
+    end
+    @data_size = 0
+    @files = []
+    manifest_text.split("\n").each do |stream|
+      toks = stream.split(" ")
+      toks[1..-1].each do |tok|
+        if (re = tok.match /^[0-9a-f]{32}/)
+          blocksize = nil
+          tok.split('+')[1..-1].each do |hint|
+            if !blocksize and hint.match /^\d+$/
+              blocksize = hint.to_i
+            end
+            if (re = hint.match /^GS(\d+)$/)
+              blocksize = re[1].to_i
+            end
+          end
+          @data_size = false if !blocksize
+          @data_size += blocksize if @data_size
+        else
+          if (re = tok.match /^(\d+):(\d+):(\S+)$/)
+            @files << [toks[0], re[3], re[2].to_i]
+          end
+        end
+      end
+    end
+  end
 end