- def self.munge_manifest_locators(manifest)
- # Given a manifest text and a block, yield each locator,
- # and replace it with whatever the block returns.
- manifest.andand.gsub!(/ [[:xdigit:]]{32}(\+[[:digit:]]+)?(\+\S+)/) do |word|
- if loc = Keep::Locator.parse(word.strip)
- " " + yield(loc)
- else
- " " + word
+ def check_manifest_validity
+ begin
+ Keep::Manifest.validate! manifest_text
+ true
+ rescue ArgumentError => e
+ errors.add :manifest_text, e.message
+ false
+ end
+ end
+
+ def signed_manifest_text
+ if has_attribute? :manifest_text
+ token = current_api_client_authorization.andand.api_token
+ exp = [db_current_time.to_i + Rails.configuration.blob_signature_ttl,
+ expires_at].compact.map(&:to_i).min
+ @signed_manifest_text = self.class.sign_manifest manifest_text, token, exp
+ end
+ end
+
+ def self.sign_manifest manifest, token, exp=nil
+ if exp.nil?
+ exp = db_current_time.to_i + Rails.configuration.blob_signature_ttl
+ end
+ signing_opts = {
+ api_token: token,
+ expire: exp,
+ }
+ m = munge_manifest_locators(manifest) do |match|
+ Blob.sign_locator(match[0], signing_opts)
+ end
+ return m
+ end
+
+ def self.munge_manifest_locators manifest
+ # Given a manifest text and a block, yield the regexp MatchData
+ # for each locator. Return a new manifest in which each locator
+ # has been replaced by the block's return value.
+ return nil if !manifest
+ return '' if manifest == ''
+
+ new_lines = []
+ manifest.each_line do |line|
+ line.rstrip!
+ new_words = []
+ line.split(' ').each do |word|
+ if new_words.empty?
+ new_words << word
+ elsif match = Keep::Locator::LOCATOR_REGEXP.match(word)
+ new_words << yield(match)
+ else
+ new_words << word
+ end
+ end
+ new_lines << new_words.join(' ')
+ end
+ new_lines.join("\n") + "\n"
+ end
+
+ def self.each_manifest_locator manifest
+ # Given a manifest text and a block, yield the regexp match object
+ # for each locator.
+ manifest.each_line do |line|
+ # line will have a trailing newline, but the last token is never
+ # a locator, so it's harmless here.
+ line.split(' ').each do |word|
+ if match = Keep::Locator::LOCATOR_REGEXP.match(word)
+ yield(match)
+ end