Skip to content

Instantly share code, notes, and snippets.

@derwiki
Created December 27, 2020 23:23
Show Gist options
  • Save derwiki/fd6aa6bbcd2f3e22cc9e80ebf90233ff to your computer and use it in GitHub Desktop.
Save derwiki/fd6aa6bbcd2f3e22cc9e80ebf90233ff to your computer and use it in GitHub Desktop.
diff --git a/Procfile b/Procfile
new file mode 100644
index 0000000..c2c566e
--- /dev/null
+++ b/Procfile
@@ -0,0 +1 @@
+web: bundle exec puma -C config/puma.rb
diff --git a/app/controllers/upload_controller.rb b/app/controllers/upload_controller.rb
new file mode 100644
index 0000000..2157acf
--- /dev/null
+++ b/app/controllers/upload_controller.rb
@@ -0,0 +1,50 @@
+require 'image_enhancer'
+require 'uri'
+require 'net/http'
+
+
+CONFIG_JSON_URL = "https://cambe.s3.us-east-2.amazonaws.com/config.json"
+
+
+class UploadController < ApplicationController
+ skip_before_action :verify_authenticity_token
+ before_action :authenticate
+
+ def upload
+ Rails.logger.info("config: #{live_config.to_s}")
+ fail unless live_config[:upload]
+ fail unless params[:host].present?
+ key = "#{params[:host]}.jpg"
+ upload_to_s3(key, params[:image].tempfile)
+ url = bucket.object(key).public_url
+ render plain: url
+ end
+
+ protected
+
+ def authenticate
+ authenticate_or_request_with_http_basic do |email, password|
+ User
+ .find_by_email(URI.decode(email))
+ .valid_password?(password)
+ end
+ end
+
+ def s3
+ @s3 ||= Aws::S3::Resource.new(region: 'us-east-2')
+ end
+
+ def bucket
+ @bucket ||= s3.bucket('cambe')
+ end
+
+ def upload_to_s3(key, image_file)
+ bucket.object(key).tap do |obj|
+ obj.upload_file(image_file, acl: 'public-read')
+ end
+ end
+
+ def live_config
+ @config ||= JSON[ Net::HTTP.get(URI(CONFIG_JSON_URL)) ].with_indifferent_access
+ end
+end
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 4ec3dec..f38dd09 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -13,6 +13,9 @@ class UsersController < ApplicationController
redirect_to root_path, :alert => "Access denied."
end
end
+
+ host = @user.email.split('@').first
+ @image_url = "https://cambe.s3.us-east-2.amazonaws.com/#{host}.jpg"
end
def update
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb
index 8d0767f..a9badb2 100644
--- a/app/views/users/show.html.erb
+++ b/app/views/users/show.html.erb
@@ -1,3 +1,5 @@
<h3>User</h3>
<p>Name: <%= @user.name if @user.name %></p>
<p>Email: <%= @user.email if @user.email %></p>
+
+<img src="<%= @image_url %>" />
diff --git a/config/routes.rb b/config/routes.rb
index 77d8ee3..b8e756c 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
+ post '/upload' => 'upload#upload'
root to: 'visitors#index'
devise_for :users
resources :users
diff --git a/config/storage.yml b/config/storage.yml
index d32f76e..7bcc6a3 100644
--- a/config/storage.yml
+++ b/config/storage.yml
@@ -7,12 +7,12 @@ local:
root: <%= Rails.root.join("storage") %>
# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
-# amazon:
-# service: S3
-# access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
-# secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
-# region: us-east-1
-# bucket: your_own_bucket
+amazon:
+ service: S3
+ access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
+ secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
+ region: us-east-2
+ bucket: cambe
# Remember not to checkin your GCS keyfile to a repository
# google:
diff --git a/lib/image_enhancer.rb b/lib/image_enhancer.rb
new file mode 100644
index 0000000..aab0787
--- /dev/null
+++ b/lib/image_enhancer.rb
@@ -0,0 +1,20 @@
+class ImageEnhancer
+ attr_accessor :url, :width
+
+ def initialize(url, width: nil)
+ self.url = url
+ self.width = width
+ end
+
+ def perform
+ image = MiniMagick::Image.open(self.url)
+ image.resize "#{width}x" if self.width
+ image.quality "60"
+ image.channel "rgb"
+ image.auto_level
+ image.format "jpg"
+ tempfile = Tempfile.new
+ image.write(tempfile.path)
+ tempfile
+ end
+end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment