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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class FilesController < ApplicationController
  19. menu_item :files
  20. before_action :find_project_by_project_id
  21. before_action :authorize
  22. accept_api_auth :index, :create
  23. helper :attachments
  24. helper :sort
  25. include SortHelper
  26. def index
  27. sort_init 'filename', 'asc'
  28. sort_update 'filename' => "#{Attachment.table_name}.filename",
  29. 'created_on' => "#{Attachment.table_name}.created_on",
  30. 'size' => "#{Attachment.table_name}.filesize",
  31. 'downloads' => "#{Attachment.table_name}.downloads"
  32. @containers = [Project.includes(:attachments).
  33. references(:attachments).reorder(sort_clause).find(@project.id)]
  34. @containers += @project.versions.includes(:attachments).
  35. references(:attachments).reorder(sort_clause).to_a.sort.reverse
  36. respond_to do |format|
  37. format.html {render :layout => !request.xhr?}
  38. format.api
  39. end
  40. end
  41. def new
  42. @versions = @project.versions.sorted
  43. end
  44. def create
  45. version_id = params[:version_id] || (params[:file] && params[:file][:version_id])
  46. container = version_id.blank? ? @project : @project.versions.find_by_id(version_id)
  47. attachments = Attachment.attach_files(container, (params[:attachments] || (params[:file] && params[:file][:token] && params)))
  48. render_attachment_warning_if_needed(container)
  49. if attachments[:files].present?
  50. if Setting.notified_events.include?('file_added')
  51. Mailer.deliver_attachments_added(attachments[:files])
  52. end
  53. respond_to do |format|
  54. format.html do
  55. flash[:notice] = l(:label_file_added)
  56. redirect_to project_files_path(@project)
  57. end
  58. format.api {render_api_ok}
  59. end
  60. else
  61. respond_to do |format|
  62. format.html do
  63. flash.now[:error] = l(:label_attachment) + " " + l('activerecord.errors.messages.invalid')
  64. new
  65. render :action => 'new'
  66. end
  67. format.api {render :status => :bad_request}
  68. end
  69. end
  70. end
  71. end