Feature #22915 ยป pdf_thumbnails.patch
| app/models/attachment.rb | ||
|---|---|---|
|
end
|
||
|
def image?
|
||
|
!!(self.filename =~ /\.(bmp|gif|jpg|jpe|jpeg|png)$/i)
|
||
|
!!(self.filename =~ /\.(bmp|gif|jpg|jpe|jpeg|png|pdf|tiff|tif)$/i)
|
||
|
end
|
||
|
def thumbnailable?
|
||
| ... | ... | |
|
size = Setting.thumbnails_size.to_i
|
||
|
end
|
||
|
size = 100 unless size > 0
|
||
|
target = File.join(self.class.thumbnails_storage_path, "#{id}_#{digest}_#{size}.thumb")
|
||
|
target = File.join(self.class.thumbnails_storage_path, "#{id}_#{digest}_#{size}.thumb.png")
|
||
|
begin
|
||
|
Redmine::Thumbnail.generate(self.diskfile, target, size)
|
||
| ... | ... | |
|
# Deletes all thumbnails
|
||
|
def self.clear_thumbnails
|
||
|
Dir.glob(File.join(thumbnails_storage_path, "*.thumb")).each do |file|
|
||
|
Dir.glob(File.join(thumbnails_storage_path, "*.thumb.png")).each do |file|
|
||
|
File.delete file
|
||
|
end
|
||
|
end
|
||
| lib/redmine/thumbnail.rb | ||
|---|---|---|
|
return nil unless convert_available?
|
||
|
unless File.exists?(target)
|
||
|
# Make sure we only invoke Imagemagick if this is actually an image
|
||
|
unless File.open(source) {|f| MimeMagic.by_magic(f).try(:image?)}
|
||
|
unless File.open(source) {|f| MimeMagic.by_magic(f).try(:image?) || MimeMagic.by_magic(f).child_of?('application/pdf')}
|
||
|
return nil
|
||
|
end
|
||
|
directory = File.dirname(target)
|
||
| ... | ... | |
|
FileUtils.mkdir_p directory
|
||
|
end
|
||
|
size_option = "#{size}x#{size}>"
|
||
|
cmd = "#{shell_quote CONVERT_BIN} #{shell_quote source} -thumbnail #{shell_quote size_option} #{shell_quote target}"
|
||
|
cmd = "#{shell_quote CONVERT_BIN} #{shell_quote source}[0] -thumbnail #{shell_quote size_option} #{shell_quote target}"
|
||
|
unless system(cmd)
|
||
|
logger.error("Creating thumbnail failed (#{$?}):\nCommand: #{cmd}")
|
||
|
return nil
|
||
| test/fixtures/attachments.yml | ||
|---|---|---|
|
filename: root_attachment.txt
|
||
|
filesize: 54
|
||
|
author_id: 2
|
||
|
attachments_021:
|
||
|
content_type: application/pdf
|
||
|
downloads: 0
|
||
|
created_on: 2016-05-27 15:23:14 +09:00
|
||
|
disk_filename: 146435549450_testfile_1.pdf
|
||
|
disk_directory: "2016/05"
|
||
|
container_id: 14
|
||
|
digest: ab39db5ed28060b91c9d9b086473d65a
|
||
|
id: 21
|
||
|
container_type: Issue
|
||
|
description: ""
|
||
|
filename: testfile.pdf
|
||
|
filesize: 7870
|
||
|
author_id: 2
|
||
| test/unit/attachment_test.rb | ||
|---|---|---|
|
assert_equal true, Attachment.new(:filename => 'test.jpg').thumbnailable?
|
||
|
end
|
||
|
def test_thumbnailable_should_be_true_for_pdfs
|
||
|
assert_equal true, Attachment.new(:filename => 'test.pdf').thumbnailable?
|
||
|
end
|
||
|
def test_thumbnailable_should_be_true_for_non_images
|
||
|
assert_equal false, Attachment.new(:filename => 'test.txt').thumbnailable?
|
||
|
end
|
||
| ... | ... | |
|
attachment = Attachment.find(16)
|
||
|
Attachment.clear_thumbnails
|
||
|
assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size" do
|
||
|
assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb.png')).size" do
|
||
|
thumbnail = attachment.thumbnail
|
||
|
assert_equal "16_8e0294de2441577c529f170b6fb8f638_100.thumb.png", File.basename(thumbnail)
|
||
|
assert File.exists?(thumbnail)
|
||
|
end
|
||
|
end
|
||
|
def test_thumbnail_should_generate_the_thumbnail_from_pdf
|
||
|
set_fixtures_attachments_directory
|
||
|
attachment = Attachment.find(21)
|
||
|
Attachment.clear_thumbnails
|
||
|
assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb.png')).size" do
|
||
|
thumbnail = attachment.thumbnail
|
||
|
assert_equal "16_8e0294de2441577c529f170b6fb8f638_100.thumb", File.basename(thumbnail)
|
||
|
assert_equal "21_ab39db5ed28060b91c9d9b086473d65a_100.thumb.png", File.basename(thumbnail)
|
||
|
assert File.exists?(thumbnail)
|
||
|
end
|
||
|
end
|
||