<commit_before>#!/usr/bin/ruby
require 'rubygems'
require 'json'
require 'open-uri'
require 'digest'
require 'dalli'

module Cantaloupe

  def self.authorized?(identifier, full_size, operations, resulting_size,
                       output_format, request_uri, request_headers, client_ip,
                       cookies)
    true 
  end

  module AmazonS3Resolver
    ##
    # @param identifier [String] Image identifier
    # @param context [Hash] Context for this request
    # @return [String,Hash<String,Object>,nil] Object key of the image
    #         corresponding to the given identifier;
    #                   or Hash including `bucket` and `key` keys;
    #                   or nil if not found.
    #
    def self.get_object_key(identifier, context)
      #cache options and client
      options = { :namespace => "cantaloupe_cache", :compress => true }
      dc = Dalli::Client.new('localhost:11211', options)
      static='static/error.png'
      if(identifier != nil && identifier=~ /::/)
        item, *image = identifier.split(/::/)
        response = dc.get(item)
      else
        return static;
      end
      if(response!=nil)
        if (response=='non-public')
          return static;
        end
      else
        url =  'http://purl.bdrc.io/query/Item_basicInfo?R_RES='+item+'&jsonOut'
        response = open(url).read
      end      
      
      if(JSON.parse(response.to_s)["results"].length>0)
        access= JSON.parse(response.to_s)["results"][0]["bindings"]["access"]["value"]
        if(access =='http://purl.bdrc.io/resource/AccessOpen')
          work= JSON.parse(response.to_s)["results"][0]["bindings"]["work"]["value"]
          workShort = work[29..-1]
          key=image[0].split(".").first
          s3= 'Works/'+(Digest::MD5.hexdigest workShort)[0..1]+"/"+workShort+"/images/"+workShort+"-"+key[0,key.length-4]+"/"+image[0]
        else
          #we should have an error key pointing to a 'restricted warning' image 
          dc.set(item, 'non-public')
          return static;
        end
      else
        return static;
      end
      return s3;
    end
  end 
end
#puts Cantaloupe::authorized?('bdr:I29329::I1KG150430315.jpg', 'full', 'operations', 'resulting_size','output_format', 'request_uri', 'request_headers', 'client_ip','cookies')
#puts Cantaloupe::AmazonS3Resolver::get_object_key('bdr:<KEY>', 'full')<commit_msg>cleanup and better cantaloupe script<commit_after>#!/usr/bin/ruby
require 'rubygems'
require 'json'
require 'open-uri'
require 'digest'
require 'dalli'
require "net/http"
require 'net/https'
require "uri"

module Cantaloupe

  def self.authorized?(identifier, full_size, operations, resulting_size,
                       output_format, request_uri, request_headers, client_ip,
                       cookies)
    true 
  end

  module AmazonS3Resolver
    ##
    # @param identifier [String] Image identifier
    # @param context [Hash] Context for this request
    # @return [String,Hash<String,Object>,nil] Object key of the image
    #         corresponding to the given identifier;
    #                   or Hash including `bucket` and `key` keys;
    #                   or nil if not found.
    #
    def self.get_object_key(identifier, context)

      #cache options and client
      options = { :namespace => "cantaloupe_cache", :compress => true }
      dc = Dalli::Client.new('localhost:11211', options)
      static='static/error.png'
      if(identifier != nil && identifier=~ /::/)
        item, *image = identifier.split(/::/)
        response = dc.get(item)
      else
        return static;
      end
      if(response!=nil)
        if (response=='non-public')
          return static;
        end
      else
        uri = URI.parse("http://purl.bdrc.io/query/Item_basicInfo")
        http = Net::HTTP.new(uri.host,uri.port)
        req = Net::HTTP::Post.new(uri.path)
        payload = {"R_RES" => item }
        req.set_form_data(payload)
        response = http.request(req).body.to_s
        dc.set(item, response)
      end      
      
      responsej = JSON.parse(response)
      if(responsej["results"].length>0)
        access= responsej["results"][0]["bindings"]["access"]["value"]
        if(access =='http://purl.bdrc.io/resource/AccessOpen')
          work= responsej["results"][0]["bindings"]["work"]["value"]
          workShort = work[29..-1]
          key=image[0].split(".").first
          s3= 'Works/'+(Digest::MD5.hexdigest workShort)[0..1]+"/"+workShort+"/images/"+workShort+"-"+key[0,key.length-4]+"/"+image[0]
        else
          #we should have an error key pointing to a 'restricted warning' image 
          dc.set(item, 'non-public')
          return static;
        end
      else
        return static;
      end
      return s3;
    end
  end 
end

#puts Cantaloupe::authorized?('bdr:I29329::I1KG150430315.jpg', 'full', 'operations', 'resulting_size','output_format', 'request_uri', 'request_headers', 'client_ip','cookies')
#puts Cantaloupe::AmazonS3Resolver::get_object_key('bdr:I29329::I1KG150430315.jpg', 'full')
{'fraction_non_alphanumeric': 0.14311163895486936, 'fraction_numerical': 0.017616785431512273, 'mean_word_length': 2.630028735632184, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4795357', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1561, 'n_words': 329}