Debian Codegraph @

plugin/photo/model/photo.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 require_relative 'photo_variant'
3
4 module Plugin::Photo
5 # 1種類の画像を扱うModel。
6 # 同じ画像の複数のサイズ、別形式(Photo Variant)を含むことができ、それらを自動的に使い分ける。
7 class Photo < Diva::Model
8 include Diva::Model::PhotoInterface
9 register :photo, name: Plugin[:photo]._('画像')
10
11 field.has :variants, [Diva::Model], required: true
12 field.has :original, InnerPhoto
13 field.uri :perma_link, required: true
14
15 def self.photos
16 @photos ||= TimeLimitedStorage.new(Integer, self)
17 end
18
19 # URIからPhoto Modelを得る。
20 # _uri_ がDiva::Modelだった場合はそれを返すので、PhotoかURIかわからないものをPhotoに変換するのに使える。
21 # サードパーティプラグインはこれを呼ばず、以下のページを参考にすること。
22 # https://reference.mikutter.hachune.net/model/2016/11/30/photo-model.html
23 def self.[](uri)
24 case uri
25 when Diva::Model
26 uri
27 when URI, Addressable::URI, Diva::URI, String
28 wrapped_uri = Diva::URI(uri)
29 photos[wrapped_uri.to_s.hash] ||= wrap(wrapped_uri)
30 end
31 end
32
33 # _perma_link_ のURIをもつPhotoが既にある場合、それを返す。
34 # ない場合は、 _seeds_ の内容を元に作る。
35 # ==== Args
36 # [seeds] variant情報のEnumerator(後述)
37 # [perma_link:] variantの代表となるパーマリンク
38 # ===== seedsについて
39 # 有限個のHashについて繰り返すEnumeratorで、 _perma_link_ に対応するPhotoがまだ作られていない場合にだけ利用される。
40 # 各Hashは以下のキーを持つ。
41 # :name :: そのvariantの名前。Photoの管理上特に利用されないので、重複していても良い(Symbol)
42 # :width :: そのvariantの画像の幅(px)
43 # :height :: そのvariantの画像の高さ(px)
44 # :policy :: オリジナルからどのようにリサイズされたかを示す値(Symbol)
45 # :photo :: そのvariantの画像を示すPhoto Model又は画像のURL
46 # ===== seedsのpolicyキーについて
47 # policyは以下のいずれかの値。
48 # [:original] オリジナル画像。一つだけ含まれていること。
49 # [その他] Plugin::Photo::PhotoVariantを参照
50 def self.generate(seeds, perma_link:)
51 cached = photos[perma_link.to_s.hash]
52 return cached if cached
53 orig, other = seeds.partition{|s| s[:policy] == :original }
54 new(variants: other.map{|s|
55 PhotoVariant.new(s.merge(photo: InnerPhoto[s[:photo]]))
56 },
57 perma_link: perma_link,
58 original: InnerPhoto[orig.first[:photo]])
59 end
60
61 def self.wrap(model_or_uri)
62 inner_photo = InnerPhoto[model_or_uri]
63 new(variants: [],
64 perma_link: inner_photo.perma_link,
65 original: inner_photo)
66 end
67
68 def initialize(*params)
69 super
70 each_photos do |photo|
71 self.class.photos[photo.uri.to_s.hash] = self
72 end
73 self.class.photos[uri.to_s.hash] = self
74 end
75
76 # variantが保持している各Photo Modelを引数にしてblockを呼び出す。
77 # blockを指定しなかった場合は、Enumeratorを返す
78 # ==== Return
79 # [self] ブロックを渡した場合
80 # [Enumerator] Photo Modelを列挙するEnumerator
81 def each_photos(&block)
82 if block_given?
83 variants.each{|pv| block.(pv.photo) }
84 self
85 else
86 variants.lazy.map(&:photo)
87 end
88 end
89
90 def download(width: nil, height: nil, &partial_callback)
91 if width && height
92 larger_than(width: width, height: height)
93 .download(width: width, height: height, &partial_callback)
94 else
95 maximum_original.download(width: width, height: height, &partial_callback)
96 end
97 end
98
99 # PhotoInterfaceをgtkプラグインが拡張し、内部でこのメソッドを呼ぶ。
100 # このModelでは必要ないため空のメソッドを定義しておく。
101 def increase_read_count
102 end
103
104 def blob
105 maximum_original.blob
106 end
107
108 # 指定された幅と高さを上回るvariantを返す。
109 # 既に画像がダウンロードされているvariantのうち、最小のものを使う。
110 # 引数に指定された条件を満たす中で既にダウンロードされている画像がない場合は、条件を満たす最小のvariantを返す。
111 # ==== Args
112 # [width:] (Integer)
113 # [height:] (Integer)
114 # ==== Return
115 # [Photo Model] 最適なPhoto Model
116 def larger_than(width:, height:)
117 largers = variants.select{|pv| pv.policy.to_sym == :fit && pv.width >= width && pv.height >= height }.sort_by(&:width)
118 if largers.empty?
119 maximum_original
120 else
121 (largers.find{|pv| pv.photo.completed? } || largers.first).photo
122 end
123 end
124
125 # 最大サイズのPhotoを返す。
126 # originalがあればそれを返すが、なければfit policyのvariantのうち最大のものを返す。
127 # ==== Return
128 # [Photo Model] 最大のPhoto
129 def maximum
130 self.class.wrap(maximum_original)
131 end
132
133 private
134
135 def maximum_original
136 if original
137 original
138 else
139 variant = variants.find{|v| v.policy.to_sym == :fit }
140 if variant
141 variant.photo
142 else
143 variants.sample.photo
144 end
145 end
146 end
147 end
148 end
149