Sha256: 85699b2143a198377b725005d89e2f356f72bfeee10f2268ad1966075ba72b67

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

require "openssl"
require "pathname"

module Cloudflared
  class ImagesResource < Resource
    # ONE_DAY = 60 * 60 * 24
    # ONE_HOUR = 60 * 60
    # THIRTY_MINUTES = 60 * 30
    FIFTEEN_MINUTES = 60 * 15
    IMAGE_DELIVERY_URL = "https://imagedelivery.net"

    def delete(file_id:, **params)
      delete_request("images/v1/#{file_id}", params: params)
      true
    end

    def details(file_id:, **params)
      Image.new get_request("images/v1/#{file_id}", params: params).body.dig("result")
    end

    def direct_upload_url(**params)
      post_request("images/v1/direct_upload", body: params).body.dig("result", "uploadURL")
    end

    def download(file_id:, **params)
      get_request("images/v1/#{file_id}/blob", params: params).body
    end

    def list(**params)
      Collection.from_response get_request("images/v1", params: params), key: "images", type: Image
    end

    def public_url(path)
      "#{IMAGE_DELIVERY_URL}/#{@client.images_hash}/#{path}"
    end

    def signed_url(path, expiry_seconds: FIFTEEN_MINUTES)
      # The path uses the image + the file_id (and a variant if passed through)
      path = path[1..] if path[0] == "/"
      path = "#{@client.images_hash}/#{path}"

      # Calculate the hexdigest with the  leading slash
      sig = OpenSSL::HMAC.hexdigest("SHA256", @client.images_default_key, "/#{path}")

      # Calculate the seconds since the epoch in the future
      exp = Time.new.to_i + expiry_seconds

      # Respond with the url
      qry = path.include?("?") ? "&" : "?"
      "#{IMAGE_DELIVERY_URL}/#{path}#{qry}sig=#{sig}&exp=#{exp}"
    end

    def stats(**params)
      get_request("images/v1/stats", params: params).body.dig("result")
    end

    def update(file_id:, **params)
      Image.new patch_request("images/v1/#{file_id}", body: params).body.dig("result")
    end

    def upload(file:, **params)
      Image.new upload_request("images/v1", file: file, params: params).body.dig("result")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cloudflared-0.1.1 lib/cloudflared/resources/images.rb