Project

General

Profile

Feature #22915 ยป pdf_thumbnails.patch

kay rus, 2016-05-27 16:28

View differences:

app/models/attachment.rb
193 193
  end
194 194

  
195 195
  def image?
196
    !!(self.filename =~ /\.(bmp|gif|jpg|jpe|jpeg|png)$/i)
196
    !!(self.filename =~ /\.(bmp|gif|jpg|jpe|jpeg|png|pdf|tiff|tif)$/i)
197 197
  end
198 198

  
199 199
  def thumbnailable?
......
214 214
        size = Setting.thumbnails_size.to_i
215 215
      end
216 216
      size = 100 unless size > 0
217
      target = File.join(self.class.thumbnails_storage_path, "#{id}_#{digest}_#{size}.thumb")
217
      target = File.join(self.class.thumbnails_storage_path, "#{id}_#{digest}_#{size}.thumb.png")
218 218

  
219 219
      begin
220 220
        Redmine::Thumbnail.generate(self.diskfile, target, size)
......
227 227

  
228 228
  # Deletes all thumbnails
229 229
  def self.clear_thumbnails
230
    Dir.glob(File.join(thumbnails_storage_path, "*.thumb")).each do |file|
230
    Dir.glob(File.join(thumbnails_storage_path, "*.thumb.png")).each do |file|
231 231
      File.delete file
232 232
    end
233 233
  end
lib/redmine/thumbnail.rb
29 29
      return nil unless convert_available?
30 30
      unless File.exists?(target)
31 31
        # Make sure we only invoke Imagemagick if this is actually an image
32
        unless File.open(source) {|f| MimeMagic.by_magic(f).try(:image?)}
32
        unless File.open(source) {|f| MimeMagic.by_magic(f).try(:image?) || MimeMagic.by_magic(f).child_of?('application/pdf')}
33 33
          return nil
34 34
        end
35 35
        directory = File.dirname(target)
......
37 37
          FileUtils.mkdir_p directory
38 38
        end
39 39
        size_option = "#{size}x#{size}>"
40
        cmd = "#{shell_quote CONVERT_BIN} #{shell_quote source} -thumbnail #{shell_quote size_option} #{shell_quote target}"
40
        cmd = "#{shell_quote CONVERT_BIN} #{shell_quote source}[0] -thumbnail #{shell_quote size_option} #{shell_quote target}"
41 41
        unless system(cmd)
42 42
          logger.error("Creating thumbnail failed (#{$?}):\nCommand: #{cmd}")
43 43
          return nil
test/fixtures/attachments.yml
267 267
  filename: root_attachment.txt
268 268
  filesize: 54
269 269
  author_id: 2
270
attachments_021:
271
  content_type: application/pdf
272
  downloads: 0
273
  created_on: 2016-05-27 15:23:14 +09:00
274
  disk_filename: 146435549450_testfile_1.pdf
275
  disk_directory: "2016/05"
276
  container_id: 14
277
  digest: ab39db5ed28060b91c9d9b086473d65a
278
  id: 21
279
  container_type: Issue
280
  description: ""
281
  filename: testfile.pdf
282
  filesize: 7870
283
  author_id: 2
test/unit/attachment_test.rb
392 392
    assert_equal true, Attachment.new(:filename => 'test.jpg').thumbnailable?
393 393
  end
394 394

  
395
  def test_thumbnailable_should_be_true_for_pdfs
396
    assert_equal true, Attachment.new(:filename => 'test.pdf').thumbnailable?
397
  end
398

  
395 399
  def test_thumbnailable_should_be_true_for_non_images
396 400
    assert_equal false, Attachment.new(:filename => 'test.txt').thumbnailable?
397 401
  end
......
402 406
      attachment = Attachment.find(16)
403 407
      Attachment.clear_thumbnails
404 408

  
405
      assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size" do
409
      assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb.png')).size" do
410
        thumbnail = attachment.thumbnail
411
        assert_equal "16_8e0294de2441577c529f170b6fb8f638_100.thumb.png", File.basename(thumbnail)
412
        assert File.exists?(thumbnail)
413
      end
414
    end
415

  
416
    def test_thumbnail_should_generate_the_thumbnail_from_pdf
417
      set_fixtures_attachments_directory
418
      attachment = Attachment.find(21)
419
      Attachment.clear_thumbnails
420

  
421
      assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb.png')).size" do
406 422
        thumbnail = attachment.thumbnail
407
        assert_equal "16_8e0294de2441577c529f170b6fb8f638_100.thumb", File.basename(thumbnail)
423
        assert_equal "21_ab39db5ed28060b91c9d9b086473d65a_100.thumb.png", File.basename(thumbnail)
408 424
        assert File.exists?(thumbnail)
409 425
      end
410 426
    end
    (1-1/1)