2024-05-03 03:04:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Requests
|
|
|
|
class Bunny
|
|
|
|
include HTTParty
|
|
|
|
|
|
|
|
base_uri "https://storage.bunnycdn.com"
|
|
|
|
|
|
|
|
attr_reader :access_key, :storage_zone_name
|
|
|
|
|
|
|
|
def initialize(access_key:, storage_zone_name:)
|
|
|
|
@access_key = access_key
|
|
|
|
@storage_zone_name = storage_zone_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(path)
|
|
|
|
self.class.delete("/#{storage_zone_name}/#{path}", headers: { "AccessKey" => access_key })
|
|
|
|
end
|
|
|
|
|
|
|
|
def exists?(path)
|
|
|
|
# bunny doesn't support HEAD, for some reason - so we have to download the ENTIRE file to check if it exists
|
|
|
|
# I'm unsure if this counts against us, so it should be used sparingly
|
|
|
|
!get(path).nil?
|
|
|
|
end
|
|
|
|
|
2024-10-13 06:04:55 +00:00
|
|
|
def get(path, &block)
|
2024-05-03 03:04:43 +00:00
|
|
|
r = self.class.get("/#{storage_zone_name}/#{path}", headers: { "AccessKey" => access_key })
|
2024-10-13 06:04:55 +00:00
|
|
|
file = r.success? ? r.body : nil
|
|
|
|
block&.call(file)
|
|
|
|
file
|
2024-05-03 03:04:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def put(path, body)
|
|
|
|
self.class.put("/#{storage_zone_name}/#{path}", body: body, headers: { "AccessKey" => access_key, "Checksum" => Digest::SHA2.hexdigest(body) })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|