-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial work for generating images with transparent background
- Loading branch information
1 parent
2faebce
commit 354fddd
Showing
10 changed files
with
146 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module UserScoped | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_action :set_user | ||
end | ||
|
||
private | ||
|
||
def set_user | ||
@user = User.find_by!(username: params[:user_username]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Images::CalendarsController < ApplicationController | ||
include UserScoped | ||
|
||
def show | ||
if small? | ||
redirect_to @user.calendar_image.variant(:small) | ||
else | ||
redirect_to @user.calendar_image.variant(:large) | ||
end | ||
end | ||
|
||
private | ||
|
||
def small? | ||
params[:variant] == "small" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,7 @@ | ||
class SignaturesController < ApplicationController | ||
before_action :set_user | ||
include UserScoped | ||
|
||
def show | ||
redirect_to url_for(@user.signature) | ||
end | ||
|
||
private | ||
|
||
def set_user | ||
@user = User.with_attached_signature.find_by!(username: params[:user_username]) | ||
redirect_to @user.signature | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module User::CalendarImageable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
has_one_attached :calendar_image do |attachable| | ||
attachable.variant :large, resize_to_limit: [1200, 180], preprocessed: true | ||
attachable.variant :small, resize_to_limit: [600, 150], preprocessed: true | ||
end | ||
|
||
after_create_commit :enqueue_calendar_images_generation | ||
after_update_commit :enqueue_calendar_images_generation, if: -> { calendar_images.obsolete? } | ||
end | ||
|
||
def calendar_images | ||
User::CalendarImages.new(self) | ||
end | ||
|
||
private | ||
|
||
def enqueue_calendar_images_generation | ||
calendar_images.generate_later | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
require "image_processing/vips" | ||
|
||
class User::CalendarImages | ||
def initialize(user) | ||
super() | ||
@user = user | ||
end | ||
|
||
def generate | ||
Instrumentation.instrument(title: "#{self.class.name}#generate") do | ||
user.with_time_zone { generate_from_activities } | ||
end | ||
end | ||
|
||
def generate_later | ||
User::CalendarImages::GenerateJob.perform_later(user) | ||
end | ||
|
||
def obsolete? | ||
return true unless user.calendar_image.attached? | ||
return true if user.saved_change_to_checksum? | ||
|
||
user.calendar_image.blob.created_at.in_time_zone.to_date != Time.zone.today | ||
end | ||
|
||
class GenerateJob < ApplicationJob | ||
retry_on(*BrowserSession::RETRYABLE_ERRORS, attempts: 10, wait: :polynomially_longer) | ||
|
||
limits_concurrency to: 1, key: :screenshots, duration: 2.hours | ||
|
||
queue_as :screenshots | ||
|
||
def perform(user) | ||
user.calendar_images.generate | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :user | ||
|
||
def generate_from_activities | ||
calendar_html = render_calendar_html | ||
|
||
capture_html_screenshot(calendar_html) | ||
end | ||
|
||
def render_calendar_html | ||
activities_amount_per_day = user.calendars.current_year.activities_amount_sum_per_day | ||
|
||
ApplicationController.render("users/signature", locals: { activities_amount_per_day: }, layout: nil) | ||
end | ||
|
||
def capture_html_screenshot(html_page) | ||
BrowserSession.fetch_page do |page| | ||
page.go_to("data:text/html;base64,#{Base64.strict_encode64(html_page)}") | ||
|
||
image = page | ||
.screenshot( | ||
encoding: :binary, | ||
selector: ".signature", | ||
format: :png, | ||
background_color: Ferrum::RGBA.new(0, 0, 0, 0.0) | ||
) | ||
|
||
user.calendar_image.attach( | ||
io: StringIO.new(image), | ||
filename: "#{user.username}_calendar.png", | ||
content_type: "application/png" | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters