操作
バグ #7
完了RedmineのWiki記法でthumbnailマクロを使うと、使用した添付ファイル画像のリンクが壊れる
ステータス:
終了
優先度:
通常
担当者:
-
カテゴリ:
-
対象バージョン:
-
開始日:
2014/03/12
期日:
進捗率:
100%
予定工数:
重要度:
ファイル
高橋 徹 さんが約11年前に更新
添付ファイルのリンクを生成していると思われる箇所
app/views/issues/show.html.erb
85 <%= link_to_attachments @issue, :thumbnails => true %>
link_to_attachmentsを定義している箇所を捜索
app/helpers/attachments_helper.rb
25 def link_to_attachments(container, options = {}) 26 options.assert_valid_keys(:author, :thumbnails) 27 28 if container.attachments.any? 29 options = {:deletable => container.attachments_deletable?, :author => true}.merge(options) 30 render :partial => 'attachments/links', 31 :locals => {:attachments => container.attachments, :options => options, :thumbnails => (options[:thumbnails] && Setting.thumbnails_enabled?)} 32 end 33 end
30行目のrender :partialで、リンクのレンダリングはapp/views/attachments/_links.html.erbに委譲している。
1 <div class="attachments"> 2 <% for attachment in attachments %> 3 <p><%= link_to_attachment attachment, :class => 'icon icon-attachment', :download => true -%> 4 <% if attachment.is_text? %> 5 <%= link_to image_tag('magnifier.png'), 6 :controller => 'attachments', :action => 'show', 7 :id => attachment, :filename => attachment.filename %> 8 <% end %> 9 <%= h(" - #{attachment.description}") unless attachment.description.blank? %> 10 <span class="size">(<%= number_to_human_size attachment.filesize %>)</span> 11 <% if options[:deletable] %> 12 <%= link_to image_tag('delete.png'), attachment_path(attachment), 13 :data => {:confirm => l(:text_are_you_sure)}, 14 :method => :delete, 15 :class => 'delete', 16 :title => l(:button_delete) %> 17 <% end %> 18 <% if options[:author] %> 19 <span class="author"><%= h(attachment.author) %>, <%= format_time(attachment.created_on) %></span> ment.created_on) %></span> 20 <% end %> 21 </p> 22 <% end %> 23 <% if defined?(thumbnails) && thumbnails %> 24 <% images = attachments.select(&:thumbnailable?) %> 25 <% if images.any? %> 26 <div class="thumbnails"> 27 <% images.each do |attachment| %> 28 <div><%= thumbnail_tag(attachment) %></div> 29 <% end %> 30 </div> 31 <% end %> 32 <% end %> 33 </div>
このファイルの中で懸案のリンクを生成しているのは
3 <p><%= link_to_attachment attachment, :class => 'icon icon-attachment', :download => true -%>
link_to_attachmentを定義しているファイルを検索すると、app/helpers/application_helper.rbであった。
94 def link_to_attachment(attachment, options={}) 95 text = options.delete(:text) || attachment.filename 96 route_method = options.delete(:download) ? :download_named_attachment_p ath : :named_attachment_path 97 html_options = options.slice!(:only_path) 98 url = send(route_method, attachment, attachment.filename, options) 99 link_to text, url, html_options 100 end
高橋 徹 さんが約11年前に更新
- app/helpers/application_helper.rb
95 text = options.delete(:text) || attachment.filename
のところでデバッグプリントを埋め込み調べると、QQQ)attachment.filename=片倉小十郎.jpg (戦国BASARAの片倉小十郎、伊達政宗の宰相) QQQ)options.delete(:text)=
となった。これは引数に渡されてくるattachmentの問題だ。呼び出し元は、 - app/views/attachments/_links.html.erb
1 <div class="attachments"> 2 <% for attachment in attachments %> 3 <p><%= link_to_attachment attachment, :class => 'icon icon-attachment', :download => true -%>
で、さらにここを呼び出す元は - app/helpers/attachments_helper.rb
30 render :partial => 'attachments/links', 31 :locals => {:attachments => container.attachments, :options => options, :thumbnails => (options[:thumbnails] && Setting.thumbnails_enabled?)}
と、container.attachments
が指定されている。containerをここの関数link_to_attachmentsに渡している元は、 - app/views/issues/show.html.erb
85 <%= link_to_attachments @issue, :thumbnails => true %>
の@issue部分である。
ここで、container.attachmentsは、モデルのattachmentのリストであろうと検討をつけ、app/models/attachment.rbを眺めてみる。
うむ、さっぱり分からん。app/helpers/application_helper.rbの次のメソッドの引数attachmentを詳細に見ることにした。
94 def link_to_attachment(attachment, options={}) ++ print("QQQ)attachment.inspect=#{attachment.inspect}\n")
実行結果は
QQQ)attachment.inspect=#<Attachment id: 1, container_id: 2, container_type: "WikiPage", filename: "片倉小十郎.jpg (戦国BASARAの片倉小十郎、伊達政宗の宰相)", disk_filename: "140303000143_0606daee01577da6c199ba439c7ef40e.jpg", filesize: 74596, content_type: nil, digest: "08ad4964675bb6f3bb70a6c0fde40af4", downloads: 0, author_id: 1, created_on: "2014-03-02 15:01:43", description: "戦国BASARAの片倉小十郎、伊達政宗の宰相", disk_directory: "2014/03">
となり、filenameが、ファイル名+' '+'('+descriptionの内容+')' となっている。
Wikiページのthumbnailマクロを削除して実行すると
QQQ)attachment.inspect=#<Attachment id: 1, container_id: 2, container_type: "WikiPage", filename: "片倉小十郎.jpg", disk_filename: "140303000143_0606daee01577da6c199ba439c7ef40e.jpg", filesize: 74596, content_type: nil, digest: "08ad4964675bb6f3bb70a6c0fde40af4", downloads: 0, author_id: 1, created_on: "2014-03-02 15:01:43", description: "戦国BASARAの片倉小十郎、伊達政宗の宰相", disk_directory: "2014/03">
となり、filenameが、ファイル名となっている。MySQLのデータベースを調べてもthumbnailマクロの有無に関わらずfilenameは変わらない。
そこで、これは実行時にフックか何かthumbnailの存在によって働きかけているのかと推測し、'thumbnail'の言葉を検索し、app/controllers/attachments_controller.rb が怪しそうと目星をつけて調べてみる。
ちょっとわからない・・・ うむ・・・
操作