Move lib/danbooru, lib/danbooru_image_resizer to app/logical.

This commit is contained in:
evazion
2017-12-18 11:31:48 -06:00
parent 53c351ab71
commit efeaad25ca
12 changed files with 6 additions and 14 deletions

View File

@@ -1,39 +0,0 @@
module Danbooru
module HasBitFlags
extend ActiveSupport::Concern
module ClassMethods
# NOTE: the ordering of attributes has to be fixed#
# new attributes should be appended to the end.
def has_bit_flags(attributes, options = {})
field = options[:field] || :bit_flags
attributes.each.with_index do |attribute, i|
bit_flag = 1 << i
define_singleton_method("flag_value_for") do |key|
index = attributes.index(key)
raise IndexError if index.nil?
1 << index
end
define_method(attribute) do
send(field) & bit_flag > 0
end
define_method("#{attribute}?") do
send(field) & bit_flag > 0
end
define_method("#{attribute}=") do |val|
if val.to_s =~ /t|1|y/
send("#{field}=", send(field) | bit_flag)
else
send("#{field}=", send(field) & ~bit_flag)
end
end
end
end
end
end
end

View File

@@ -1,7 +0,0 @@
require "danbooru/paginator/active_record_extension"
require "danbooru/paginator/numbered_collection_extension"
require "danbooru/paginator/sequential_collection_extension"
require "danbooru/paginator/pagination_error"
ApplicationRecord.__send__(:include, Danbooru::Paginator::ActiveRecordExtension)
Delayed::Job.__send__(:include, Danbooru::Paginator::ActiveRecordExtension)

View File

@@ -1,120 +0,0 @@
require 'active_support/concern'
module Danbooru
module Paginator
module ActiveRecordExtension
extend ActiveSupport::Concern
module ClassMethods
def paginate(page, options = {})
@paginator_options = options
if use_sequential_paginator?(page)
paginate_sequential(page)
else
paginate_numbered(page)
end
end
def use_sequential_paginator?(page)
page =~ /[ab]\d+/i
end
def paginate_sequential(page)
if page =~ /b(\d+)/
paginate_sequential_before($1)
elsif page =~ /a(\d+)/
paginate_sequential_after($1)
else
paginate_sequential_before
end
end
def paginate_sequential_before(before_id = nil)
c = limit(records_per_page + 1)
if before_id.to_i > 0
c = c.where("#{table_name}.id < ?", before_id.to_i)
end
c.reorder("#{table_name}.id desc").tap do |obj|
obj.extend(SequentialCollectionExtension)
obj.sequential_paginator_mode = :before
end
end
def paginate_sequential_after(after_id)
limit(records_per_page + 1).where("#{table_name}.id > ?", after_id.to_i).reorder("#{table_name}.id asc").tap do |obj|
obj.extend(SequentialCollectionExtension)
obj.sequential_paginator_mode = :after
end
end
def paginate_numbered(page)
page = [page.to_i, 1].max
if page > Danbooru.config.max_numbered_pages
raise ::Danbooru::Paginator::PaginationError.new("You cannot go beyond page #{Danbooru.config.max_numbered_pages}. Please narrow your search terms.")
end
limit(records_per_page).offset((page - 1) * records_per_page).tap do |obj|
obj.extend(NumberedCollectionExtension)
if records_per_page > 0
obj.total_pages = (obj.total_count.to_f / records_per_page).ceil
else
obj.total_pages = 1
end
obj.current_page = page
end
end
def records_per_page
option_for(:limit).to_i
end
# When paginating large tables, we want to avoid doing an expensive count query
# when the result won't even be used. So when calling paginate you can pass in
# an optional :search_count key which points to the search params. If these params
# exist, then assume we're doing a search and don't override the default count
# behavior. Otherwise, just return some large number so the paginator skips the
# count.
def option_for(key)
case key
when :limit
limit = @paginator_options.try(:[], :limit) || Danbooru.config.posts_per_page
if limit.to_i > 1_000
limit = 1_000
end
limit
when :count
if @paginator_options.has_key?(:search_count) && @paginator_options[:search_count].blank?
1_000_000
elsif @paginator_options[:count]
@paginator_options[:count]
else
nil
end
end
end
# taken from kaminari (https://github.com/amatsuda/kaminari)
def total_count
return option_for(:count) if option_for(:count)
c = except(:offset, :limit, :order)
c = c.reorder(nil)
c = c.count
c.respond_to?(:count) ? c.count : c
rescue ActiveRecord::StatementInvalid => e
if e.to_s =~ /statement timeout/
1_000_000
else
raise
end
end
end
end
end
end

View File

@@ -1,15 +0,0 @@
module Danbooru
module Paginator
module NumberedCollectionExtension
attr_accessor :current_page, :total_pages
def is_first_page?
current_page == 1
end
def is_last_page?
current_page >= total_pages
end
end
end
end

View File

@@ -1,6 +0,0 @@
module Danbooru
module Paginator
class PaginationError < Exception
end
end
end

View File

@@ -1,31 +0,0 @@
module Danbooru
module Paginator
module SequentialCollectionExtension
attr_accessor :sequential_paginator_mode
def is_first_page?
if sequential_paginator_mode == :before
false
else
size <= records_per_page
end
end
def is_last_page?
if sequential_paginator_mode == :after
false
else
size <= records_per_page
end
end
def to_a
if sequential_paginator_mode == :before
super.first(records_per_page)
else
super.first(records_per_page).reverse
end
end
end
end
end

View File

@@ -1,51 +0,0 @@
module Danbooru
def resize(read_path, write_path, width, height, resize_quality = 90)
image = Magick::Image.read(read_path).first
geometry = "#{width}x>"
if width == Danbooru.config.small_image_width
# wider than it is tall
geometry = "#{Danbooru.config.small_image_width}x#{Danbooru.config.small_image_width}>"
end
image.change_geometry(geometry) do |new_width, new_height, img|
img.resize!(new_width, new_height)
width = new_width
height = new_height
end
image = flatten(image, width, height)
image.strip!
image.write(write_path) do
self.quality = resize_quality
# setting PlaneInterlace enables progressive encoding for JPEGs
self.interlace = Magick::PlaneInterlace
end
image.destroy!
FileUtils.chmod(0664, write_path)
end
def flatten(image, width, height)
if image.alpha?
# since jpeg can't represent transparency, we need to create an image list,
# put a white image on the bottom, then flatten it.
list = Magick::ImageList.new
list.new_image(width, height) do
self.background_color = "#FFFFFF"
end
list << image
flattened_image = list.flatten_images
list.each do |image|
image.destroy!
end
return flattened_image
else
return image
end
end
module_function :resize, :flatten
end