init.rb

init.rb of - Terence Mill, 2010-11-29 11:01

Download (1.55 KB)

 
1
require 'redmine'
2
require 'base64'
3
require 'dispatcher'
4

    
5
Redmine::Plugin.register :redmine_screenshot_paste do
6
  name 'Screenshot Paste'
7
  author 'Jean-Philippe Lang'
8
  description 'Allow pasting a screenshot from the clipboard on the issue form.'
9
  version '1.1.0'
10
end
11

    
12
class UploadedScreenshot
13
  def initialize(content, name)
14
    @raw = StringIO.new(Base64.decode64(content.to_s))
15
    @name = name.to_s.strip
16
    @name = "screenshot" if @name.blank?
17
    @name << ".png"
18
  end
19
  
20
  def size
21
    @raw.size
22
  end
23
  
24
  def original_filename
25
    @name
26
  end
27
  
28
  def content_type
29
    "image/png"
30
  end
31
  
32
  def read(*args)
33
    @raw.read(*args)
34
  end
35
end
36

    
37
module RedmineScreenshotPaste
38
  def self.included(base)
39
    base.send(:extend, ClassMethods)
40
    base.class_eval do
41
      class << self
42
        alias_method_chain :attach_files, :screenshot
43
      end
44
    end
45
  end
46

    
47
  module ClassMethods
48
    def attach_files_with_screenshot(obj, attachments)
49
      screenshot = attachments['screenshot']
50
      if screenshot.is_a?(Hash)
51
        file = UploadedScreenshot.new(screenshot.delete('content'),
52
                                      screenshot.delete('name'))
53
        screenshot['file'] = file
54
      end
55
      attach_files_without_screenshot(obj, attachments)
56
    end
57
  end
58
end
59

    
60

    
61
class RedmineScreenshotPasteHook < Redmine::Hook::ViewListener
62
  render_on :view_issues_form_details_bottom, :partial => 'screenshot'
63
end
64

    
65
Dispatcher.to_prepare :redmine_screenshot_paste do
66
  Attachment.send :include, RedmineScreenshotPaste
67
end