iqdb changes
This commit is contained in:
29
app/logical/iqdb/command.rb
Normal file
29
app/logical/iqdb/command.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
module Iqdb
|
||||
class Command
|
||||
attr_reader :database
|
||||
|
||||
def initialize(database)
|
||||
@database = database
|
||||
end
|
||||
|
||||
def process(&block)
|
||||
IO.popen("iqdb #{database}", "w", &block)
|
||||
end
|
||||
|
||||
def add(post)
|
||||
hex = post.id.to_s(16)
|
||||
process do |io|
|
||||
io.puts "add 0 #{hex} :#{post.preview_file_path}"
|
||||
io.puts "quit"
|
||||
end
|
||||
end
|
||||
|
||||
def remove(post_id)
|
||||
hex = post_id.to_s(16)
|
||||
process do |io|
|
||||
io.puts "remove 0 #{hex}"
|
||||
io.puts "quit"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -5,26 +5,7 @@ module Iqdb
|
||||
FLAG_WIDTH_AS_SET = 0x08
|
||||
FLAG_DISCARD_COMMON_COEFFS = 0x16
|
||||
|
||||
attr_reader :hostname, :port
|
||||
|
||||
def self.import(database)
|
||||
IO.popen("iqdb #{database}", "w") do |io|
|
||||
Post.find_each do |post|
|
||||
puts "Adding #{post.id}"
|
||||
io.puts "#{post.id.to_s(16)} :#{post.preview_file_path}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.add(database, image_id, filename)
|
||||
image_id_hex = image_id.to_s(16)
|
||||
`iqdb add #{database} #{image_id_hex} :#{filename}`
|
||||
end
|
||||
|
||||
def self.remove(database, image_id)
|
||||
image_id_hex = image_id.to_s(16)
|
||||
`iqdb remove 0 #{image_id_hex} #{database}`
|
||||
end
|
||||
attr_reader :hostname, :port, :socket
|
||||
|
||||
def initialize(hostname, port)
|
||||
@hostname = hostname
|
||||
@@ -36,7 +17,7 @@ module Iqdb
|
||||
end
|
||||
|
||||
def close
|
||||
@socket.close
|
||||
socket.close
|
||||
end
|
||||
|
||||
def request
|
||||
@@ -46,9 +27,23 @@ module Iqdb
|
||||
close
|
||||
end
|
||||
|
||||
def query(dbid, results, filename, flags = FLAG_DISCARD_COMMON_COEFFS)
|
||||
def add(post)
|
||||
request do
|
||||
@socket.puts "query #{dbid} #{flags} #{results} #{filename}"
|
||||
hex = post.id.to_s(16)
|
||||
socket.puts "add 0 #{hex} :#{post.preview_file_path}"
|
||||
end
|
||||
end
|
||||
|
||||
def remove(post_id)
|
||||
request do
|
||||
hext = post_id.to_s(16)
|
||||
socket.puts "remove 0 #{hex}"
|
||||
end
|
||||
end
|
||||
|
||||
def query(results, filename, flags = FLAG_DISCARD_COMMON_COEFFS)
|
||||
request do
|
||||
socket.puts "query 0 #{flags} #{results} #{filename}"
|
||||
responses = Responses::Collection.new(@socket.read)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -9,8 +9,8 @@ class Post < ActiveRecord::Base
|
||||
after_save :create_version
|
||||
after_save :update_parent_on_save
|
||||
after_save :apply_post_metatags, :on => :create
|
||||
# after_save :update_iqdb, :on => :create
|
||||
# after_destroy :remove_iqdb
|
||||
# after_save :update_iqdb_async, :on => :create
|
||||
# after_destroy :remove_iqdb_async
|
||||
before_save :merge_old_changes
|
||||
before_save :normalize_tags
|
||||
before_save :update_tag_post_counts
|
||||
@@ -1272,17 +1272,31 @@ class Post < ActiveRecord::Base
|
||||
end
|
||||
|
||||
module IqdbMethods
|
||||
def update_iqdb
|
||||
Danbooru.config.all_server_hosts.each do |host|
|
||||
Iqdb::Server.delay(:queue => host).add(Danbooru.config.iqdb_file, id, preview_file_path)
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
module ClassMethods
|
||||
def remove_iqdb(post_id)
|
||||
Iqdb::Server.new(*Danbooru.config.iqdb_hostname_and_port).remove(post_id)
|
||||
Iqdb::Command.new(Danbooru.config.iqdb_file).remove(post_id)
|
||||
end
|
||||
end
|
||||
|
||||
def remove_iqdb
|
||||
def update_iqdb_async
|
||||
Danbooru.config.all_server_hosts.each do |host|
|
||||
Iqdb::Server.delay(:queue => host).remove(Danbooru.config.iqdb_file, id)
|
||||
delay(:queue => host).update_iqdb
|
||||
end
|
||||
end
|
||||
|
||||
def remove_iqdb_async
|
||||
Danbooru.config.all_server_hosts.each do |host|
|
||||
Post.delay(:queue => host).remove_iqdb(id)
|
||||
end
|
||||
end
|
||||
|
||||
def update_iqdb
|
||||
Iqdb::Server.new(*Danbooru.config.iqdb_hostname_and_port).add(self)
|
||||
Iqdb::Command.new(Danbooru.config.iqdb_file).add(self)
|
||||
end
|
||||
end
|
||||
|
||||
include FileMethods
|
||||
@@ -1303,6 +1317,7 @@ class Post < ActiveRecord::Base
|
||||
include ApiMethods
|
||||
extend SearchMethods
|
||||
include PixivMethods
|
||||
include IqdbMethods
|
||||
|
||||
def visible?
|
||||
return false if !Danbooru.config.can_user_see_post?(CurrentUser.user, self)
|
||||
|
||||
Reference in New Issue
Block a user