/vendor/plugins/lico_adva_skin/lib/lib/paperclip_processors/asset_processor.rb

https://github.com/Jabir/Yaron-CMS · Ruby · 138 lines · 95 code · 22 blank · 21 comment · 16 complexity · 92ebdcf20a78ecd25e8274b78bddab7a MD5 · raw file

  1. module Paperclip
  2. class AssetProcessor < Processor
  3. attr_accessor :time_offset, :geometry, :whiny
  4. def initialize(file, options = {})
  5. super
  6. ctype = (`file -Ib #{file.path} 2>/dev/null || file -ib #{file.path} 2>/dev/null`.gsub(/\n/,""))
  7. @is_image = /image/ =~ ctype || /pdf/ =~ ctype
  8. @is_video = !@is_image
  9. @create_thumb = options.include?(:geometry)
  10. @create_flv = !options.include?(:geometry)
  11. #
  12. # Video
  13. #
  14. if @is_video
  15. @file = file
  16. @basename = File.basename(file.path, File.extname(file.path))
  17. if @create_thumb
  18. @time_offset = options[:time_offset] || '-4'
  19. unless options[:geometry].nil? || (@geometry = Geometry.parse(options[:geometry])).nil?
  20. @geometry.width = (@geometry.width / 2.0).floor * 2.0
  21. @geometry.height = (@geometry.height / 2.0).floor * 2.0
  22. @geometry.modifier = ''
  23. end
  24. @whiny = options[:whiny].nil? ? true : options[:whiny]
  25. end
  26. end
  27. #
  28. # Afbeelding
  29. #
  30. if @is_image
  31. @file = file
  32. @current_format = File.extname(@file.path)
  33. @basename = File.basename(@file.path, @current_format)
  34. @format = options[:format]
  35. if @create_thumb
  36. geometry = options[:geometry]
  37. @crop = geometry[-1,1] == '#'
  38. @target_geometry = Geometry.parse geometry
  39. @current_geometry = Geometry.from_file @file
  40. @convert_options = options[:convert_options]
  41. @whiny = options[:whiny].nil? ? true : options[:whiny]
  42. end
  43. end
  44. end
  45. # Returns true if the +target_geometry+ is meant to crop.
  46. def crop?
  47. @crop
  48. end
  49. # Returns true if the image is meant to make use of additional convert options.
  50. def convert_options?
  51. not @convert_options.blank?
  52. end
  53. # Returns the command ImageMagick's +convert+ needs to transform the image
  54. # into the thumbnail.
  55. def transformation_command
  56. scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
  57. trans = "-resize \"#{scale}\""
  58. trans << " -crop \"#{crop}\" +repage" if crop
  59. trans << " #{convert_options}" if convert_options?
  60. trans
  61. end
  62. def make
  63. if @create_thumb
  64. if @is_video
  65. dst = Tempfile.new([ @basename, 'jpg' ].compact.join("."))
  66. dst.binmode
  67. cmd = %Q[-itsoffset #{time_offset} -i "#{File.expand_path(@file.path)}" -y -vcodec mjpeg -vframes 1 -an -f rawvideo ]
  68. cmd << "-s #{geometry.to_s} " unless @geometry.nil?
  69. cmd << %Q["#{File.expand_path(dst.path)}"]
  70. `/usr/local/bin/ffmpeg #{cmd} >> #{RAILS_ROOT}/log/ffmpeg.log 2>> #{RAILS_ROOT}/log/production.log`
  71. # begin
  72. # success = Paperclip.run('ffmpeg', cmd)
  73. # rescue PaperclipCommandLineError
  74. # raise PaperclipError, "There was an error processing the thumbnail for #{@basename} (ffmpeg #{cmd})" if whiny
  75. # end
  76. return dst
  77. end
  78. if @is_image
  79. src = @file
  80. dst = Tempfile.new([@basename, @format, "jpg"].compact.join("."))
  81. dst.binmode
  82. command = <<-end_command
  83. "#{ File.expand_path(src.path) }[0]"
  84. #{ transformation_command }
  85. "#{ File.expand_path(dst.path) }"
  86. end_command
  87. begin
  88. success = Paperclip.run("convert", command.gsub(/\s+/, " "))
  89. rescue PaperclipCommandLineError
  90. raise PaperclipError, "There was an error processing the thumbnail for #{@basename}" if @whiny
  91. end
  92. return dst
  93. end
  94. end
  95. if @create_flv
  96. if @is_video
  97. dst = Tempfile.new([@basename, 'flv'].compact.join("."))
  98. dst.binmode
  99. cmd = %Q[-i "#{File.expand_path(@file.path)}" -y "#{File.expand_path(dst.path)}"]
  100. `/usr/local/bin/ffmpeg #{cmd} >> #{RAILS_ROOT}/log/ffmpeg.log 2>> #{RAILS_ROOT}/log/ffmpeg.log`
  101. # begin
  102. # success = Paperclip.run('ffmpeg', cmd)
  103. # rescue PaperclipCommandLineError
  104. # raise PaperclipError, "There was an error processing the thumbnail for #{@basename} (ffmpeg {#{cmd}})" if whiny
  105. # end
  106. return dst
  107. end
  108. if @is_image
  109. dst = Tempfile.new([@basename, @format].compact.join("."))
  110. File.copy(File.expand_path(@file.path), File.expand_path(dst.path))
  111. return dst
  112. end
  113. end
  114. end
  115. end
  116. end