Debian Codegraph @

plugin/openimg/openimg.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 require_relative 'model/photo'
4
5 module Plugin::Openimg
6 ImageOpener = Data.define(:name, :condition, :open)
7 end
8
9 Plugin.create :openimg do
10 # 画像アップロードサービスの画像URLから実際の画像を得る。
11 # サービスによってはリファラとかCookieで制御してる場合があるので、
12 # "http://twitpic.com/d250g2" みたいなURLから直接画像の内容を返す。
13 # String url 画像URL
14 # String|nil 画像
15 defevent :openimg_raw_image_from_display_url,
16 prototype: [String, tcor(IO, nil)]
17
18 # 画像アップロードサービスの画像URLから画像のPixbufを得る。
19 defevent :openimg_pixbuf_from_display_url,
20 prototype: [String, tcor(:pixbuf, nil), tcor(Thread, nil)]
21
22 # 画像を取得できるURLの条件とその方法を配列で返す
23 defevent :openimg_image_openers,
24 prototype: [Pluggaloid::COLLECT]
25
26 # 画像を新しいウィンドウで開く
27 defevent :openimg_open,
28 priority: :ui_response,
29 prototype: [String, Diva::Model]
30
31 defdsl :defimageopener do |name, condition, &proc|
32 collection :openimg_image_openers do |mutation|
33 mutation.add(Plugin::Openimg::ImageOpener.new(name.freeze, condition, proc))
34 end
35 end
36
37 filter_openimg_raw_image_from_display_url do |display_url, content|
38 unless content
39 content = Plugin.collect(:openimg_image_openers).lazy.select{ |opener|
40 opener.condition === display_url
41 }.map{ |opener|
42 opener.open.(display_url)
43 }.find(&ret_nth)
44 if !content and /\.(?:jpe?g|png|gif|)\z/i.match(display_url)
45 begin
46 uri = Diva::URI(display_url)
47 if uri.scheme == 'file'
48 content = File.open(uri.path, 'rb')
49 else
50 content = URI.open(uri.to_s, 'rb')
51 end
52 rescue => _
53 error _
54 end
55 end
56 end
57 [display_url, content]
58 end
59
60 on_openimg_open do |display_url|
61 Plugin.call(:open, display_url)
62 end
63
64 def addsupport(cond, element_rule = {}, &block); end
65
66 end