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.

files_controller.rb 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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. class FilesController < ApplicationController
  18. menu_item :files
  19. before_action :find_project_by_project_id
  20. before_action :authorize
  21. accept_api_auth :index, :create
  22. helper :attachments
  23. helper :sort
  24. include SortHelper
  25. def index
  26. sort_init 'filename', 'asc'
  27. sort_update 'filename' => "#{Attachment.table_name}.filename",
  28. 'created_on' => "#{Attachment.table_name}.created_on",
  29. 'size' => "#{Attachment.table_name}.filesize",
  30. 'downloads' => "#{Attachment.table_name}.downloads"
  31. @containers = [Project.includes(:attachments).
  32. references(:attachments).reorder(sort_clause).find(@project.id)]
  33. @containers += @project.versions.includes(:attachments).
  34. references(:attachments).reorder(sort_clause).to_a.sort.reverse
  35. respond_to do |format|
  36. format.html { render :layout => !request.xhr? }
  37. format.api
  38. end
  39. end
  40. def new
  41. @versions = @project.versions.sorted
  42. end
  43. def create
  44. version_id = params[:version_id] || (params[:file] && params[:file][:version_id])
  45. container = version_id.blank? ? @project : @project.versions.find_by_id(version_id)
  46. attachments = Attachment.attach_files(container, (params[:attachments] || (params[:file] && params[:file][:token] && params)))
  47. render_attachment_warning_if_needed(container)
  48. if attachments[:files].present?
  49. if Setting.notified_events.include?('file_added')
  50. Mailer.deliver_attachments_added(attachments[:files])
  51. end
  52. respond_to do |format|
  53. format.html {
  54. flash[:notice] = l(:label_file_added)
  55. redirect_to project_files_path(@project) }
  56. format.api { render_api_ok }
  57. end
  58. else
  59. respond_to do |format|
  60. format.html {
  61. flash.now[:error] = l(:label_attachment) + " " + l('activerecord.errors.messages.invalid')
  62. new
  63. render :action => 'new' }
  64. format.api { render :status => :bad_request }
  65. end
  66. end
  67. end
  68. end