Skip to content

Instantly share code, notes, and snippets.

@S64
Last active June 24, 2023 16:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save S64/c26cf23a7d743e162fa5b7c924f43f8e to your computer and use it in GitHub Desktop.
prototype of `kiganix/common-cloudflareimages-manager`

kiganix/common-cloudflareimages-manager

bundle install
bundle exec rake
bundle exec rake upload
# frozen_string_literal: true
source 'https://rubygems.org'
gem 'rake', '~> 13.0', '>= 13.0.6'
gem 'rspec-expectations', '~> 3.12', '>= 3.12.3'
gem 'faraday', '~> 2.7', '>= 2.7.7'
gem 'faraday-multipart', '~> 1.0', '>= 1.0.4'
gem 'marcel', '~> 1.0', '>= 1.0.2'
require 'rspec/expectations'
include RSpec::Matchers
require 'faraday'
require 'faraday/multipart'
require 'marcel'
SOURCE_DIR = 'blobs'
SOURCES = FileList.new("#{SOURCE_DIR}/**/*.{png,jpg,jpeg,gif}").pathmap("%{^#{SOURCE_DIR}/,}p")
ID_PREFIX = 'kiganix-ccim'
ID_FORMAT = "#{ID_PREFIX}/%s"
CF_ACCOUNT_ID = "#{ENV['CF_ACCOUNT_ID']}"
CF_API_TOKEN = "#{ENV['CF_API_TOKEN']}"
CF_IMAGES_ENDPOINT_FORMAT = 'https://api.cloudflare.com/client/v4/accounts/%s/images/v1'
task :default => [:check, SOURCE_DIR]
namespace :check do
task :prerequirements do
puts "#{RUBY_VERSION}"
expect(File.read('.ruby-version').strip).to eq "#{RUBY_VERSION}"
puts CF_ACCOUNT_ID
expect(CF_ACCOUNT_ID).not_to be_empty
puts CF_API_TOKEN
expect(CF_API_TOKEN).not_to be_empty
end
task :filename do
SOURCES.each do |path|
id = ID_FORMAT % path
puts "#{id}"
expect(id).to have_attributes(size: (be < 1024))
end
end
end
task :check => [
:'check:prerequirements',
:'check:filename',
]
task :upload => [:check, SOURCE_DIR] do
conn = Faraday.new(
url: CF_IMAGES_ENDPOINT_FORMAT % CF_ACCOUNT_ID,
headers: {
'Authorization': "Bearer #{CF_API_TOKEN}",
},
) do |c|
c.request :multipart
c.request :url_encoded
end
SOURCES.each do |path|
File.open("#{SOURCE_DIR}/#{path}", 'r') do |file|
conn.post('', {
file: Faraday::Multipart::FilePart.new(
file,
"#{Marcel::MimeType.for file}"
),
id: "#{ID_FORMAT % path}"
})
end
end
end
directory SOURCE_DIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment