You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

attachments_helper.rb 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2011 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. module AttachmentsHelper
  18. # Displays view/delete links to the attachments of the given object
  19. # Options:
  20. # :author -- author names are not displayed if set to false
  21. def link_to_attachments(container, options = {})
  22. options.assert_valid_keys(:author)
  23. if container.attachments.any?
  24. options = {:deletable => container.attachments_deletable?, :author => true}.merge(options)
  25. render :partial => 'attachments/links', :locals => {:attachments => container.attachments, :options => options}
  26. end
  27. end
  28. def to_utf8(str)
  29. if str.respond_to?(:force_encoding)
  30. str.force_encoding('UTF-8')
  31. return str if str.valid_encoding?
  32. else
  33. return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
  34. end
  35. begin
  36. Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3]
  37. rescue Iconv::InvalidEncoding
  38. # "UTF-8//IGNORE" is not supported on some OS
  39. str
  40. end
  41. end
  42. def render_api_attachment(attachment, api)
  43. api.attachment do
  44. api.id attachment.id
  45. api.filename attachment.filename
  46. api.filesize attachment.filesize
  47. api.content_type attachment.content_type
  48. api.description attachment.description
  49. api.content_url url_for(:controller => 'attachments', :action => 'download', :id => attachment, :filename => attachment.filename, :only_path => false)
  50. api.author(:id => attachment.author.id, :name => attachment.author.name) if attachment.author
  51. api.created_on attachment.created_on
  52. end
  53. end
  54. end