Project

General

Profile

Feature #306 » 0003-store-fulltext-in-the-attachment-model-and-make-it-s.patch

Jens Krämer, 2017-06-21 11:13

View differences:

app/models/attachment.rb
after_rollback :delete_from_disk, :on => :create
after_commit :delete_from_disk, :on => :destroy
after_commit :reuse_existing_file_if_possible, :on => :create
after_commit :extract_fulltext, :on => :create
safe_attributes 'filename', 'content_type', 'description'
......
digest.size < 64 ? "MD5" : "SHA256" if digest.present?
end
def extract_fulltext
if Redmine::Configuration['enable_fulltext_search'] and
readable? and
text = Redmine::TextExtractor.new(self).text
update_column :fulltext, text
end
end
private
def reuse_existing_file_if_possible
......
time.strftime("%Y/%m")
end
def self.extract_fulltext
if Redmine::Configuration['enable_fulltext_search']
Attachment.where(fulltext: nil).find_in_batches do |group|
group.each{|a| a.extract_fulltext}
end
else
logger.info "fulltext search is disabled, check configuration.yml"
end
end
# Returns an ASCII or hashed filename that do not
# exists yet in the given subdirectory
def self.disk_filename(filename, directory=nil)
config/configuration.yml.example
# allowed values: :memory, :file, :memcache
#openid_authentication_store: :memory
# Enable fulltext extraction and fulltext search in attachments.
# To make existing attachments fulltext searchable, run
# rake redmine:attachments:extract_fulltext
#
# Enabled by default.
#
# enable_fulltext_search: false
# Text extraction helper programs.
#
# commands should write the resulting plain text to STDOUT. Use __FILE__ as
db/migrate/20170613064930_add_fulltext_to_attachments.rb
class AddFulltextToAttachments < ActiveRecord::Migration
def change
add_column :attachments, :fulltext, :text, :limit => 4.megabytes # room for at least 1 million characters / approx. 80 pages of english text
end
end
lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb
end
if searchable_options[:search_attachments] && (options[:titles_only] ? options[:attachments] == 'only' : options[:attachments] != '0')
attachment_columns = ["#{Attachment.table_name}.filename", "#{Attachment.table_name}.description"]
if Redmine::Configuration['enable_fulltext_search']
attachment_columns << "#{Attachment.table_name}.fulltext"
end
r |= fetch_ranks_and_ids(
search_scope(user, projects, options).
joins(:attachments).
where(search_tokens_condition(["#{Attachment.table_name}.filename", "#{Attachment.table_name}.description"], tokens, options[:all_words])),
where(search_tokens_condition(attachment_columns, tokens, options[:all_words])),
options[:limit]
)
queries += 1
lib/redmine/configuration.rb
# Configuration default values
@defaults = {
'email_delivery' => nil,
'enable_fulltext_search' => true,
'max_concurrent_ajax_uploads' => 2
}
lib/tasks/redmine.rake
task :update_digests => :environment do
Attachment.update_digests_to_sha256
end
desc 'Makes existing attachments fulltext searchable'
task :extract_fulltext => :environment do
Attachment.extract_fulltext
end
end
namespace :tokens do
test/unit/attachment_test.rb
puts '(ImageMagick convert not available)'
end
def test_should_extract_fulltext
a = Attachment.create(
:container => Issue.find(1),
:file => uploaded_test_file("testfile.txt", "text/plain"),
:author => User.find(1),
:content_type => 'text/plain')
a.reload
assert a.fulltext.include?("this is a text file for upload tests\r\nwith multiple lines")
end
end
(2-2/11)