| 
									
										
										
										
											2019-03-17 13:36:03 +00:00
										 |  |  | # frozen_string_literal: true | 
					
						
							| 
									
										
										
										
											2019-03-15 01:32:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-09 16:54:46 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2020-03-03 00:24:10 +00:00
										 |  |  | # Copyright (C) 2006-2020  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2007-05-26 15:42:37 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | # modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | # as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | # of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2011-05-18 07:12:27 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2007-05-26 15:42:37 +00:00
										 |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2011-05-18 07:12:27 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2007-05-26 15:42:37 +00:00
										 |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AttachmentsController < ApplicationController | 
					
						
							| 
									
										
										
										
											2020-03-23 05:52:40 +00:00
										 |  |  |   include ActionView::Helpers::NumberHelper | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-01 13:22:35 +00:00
										 |  |  |   before_action :find_attachment, :only => [:show, :download, :thumbnail, :update, :destroy] | 
					
						
							| 
									
										
										
										
											2020-03-21 05:59:31 +00:00
										 |  |  |   before_action :find_container, :only => [:edit_all, :update_all, :download_all] | 
					
						
							|  |  |  |   before_action :find_downloadable_attachments, :only => :download_all | 
					
						
							| 
									
										
										
										
											2016-10-01 12:46:29 +00:00
										 |  |  |   before_action :find_editable_attachments, :only => [:edit_all, :update_all] | 
					
						
							| 
									
										
										
										
											2016-07-14 07:27:31 +00:00
										 |  |  |   before_action :file_readable, :read_authorize, :only => [:show, :download, :thumbnail] | 
					
						
							| 
									
										
										
										
											2016-10-01 13:22:35 +00:00
										 |  |  |   before_action :update_authorize, :only => :update | 
					
						
							| 
									
										
										
										
											2016-07-14 07:27:31 +00:00
										 |  |  |   before_action :delete_authorize, :only => :destroy | 
					
						
							|  |  |  |   before_action :authorize_global, :only => :upload | 
					
						
							| 
									
										
										
										
											2011-05-18 07:12:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-01 09:24:34 +00:00
										 |  |  |   # Disable check for same origin requests for JS files, i.e. attachments with | 
					
						
							|  |  |  |   # MIME type text/javascript. | 
					
						
							| 
									
										
										
										
											2017-06-01 18:29:18 +00:00
										 |  |  |   skip_after_action :verify_same_origin_request, :only => :download | 
					
						
							| 
									
										
										
										
											2016-10-01 09:24:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-01 12:46:29 +00:00
										 |  |  |   accept_api_auth :show, :download, :thumbnail, :upload, :update, :destroy | 
					
						
							| 
									
										
										
										
											2011-05-18 07:12:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-08 18:26:39 +00:00
										 |  |  |   def show | 
					
						
							| 
									
										
										
										
											2011-07-18 20:53:10 +00:00
										 |  |  |     respond_to do |format| | 
					
						
							|  |  |  |       format.html { | 
					
						
							| 
									
										
										
										
											2018-10-04 13:20:11 +00:00
										 |  |  |         if @attachment.container.respond_to?(:attachments) | 
					
						
							|  |  |  |           @attachments = @attachment.container.attachments.to_a | 
					
						
							|  |  |  |           if index = @attachments.index(@attachment) | 
					
						
							|  |  |  |             @paginator = Redmine::Pagination::Paginator.new( | 
					
						
							|  |  |  |               @attachments.size, 1, index+1
 | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2011-07-18 20:53:10 +00:00
										 |  |  |         if @attachment.is_diff? | 
					
						
							| 
									
										
										
										
											2016-04-03 11:41:41 +00:00
										 |  |  |           @diff = File.read(@attachment.diskfile, :mode => "rb") | 
					
						
							| 
									
										
										
										
											2011-11-20 06:13:26 +00:00
										 |  |  |           @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline' | 
					
						
							|  |  |  |           @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type) | 
					
						
							| 
									
										
										
										
											2012-02-04 06:23:38 +00:00
										 |  |  |           # Save diff type as user preference | 
					
						
							|  |  |  |           if User.current.logged? && @diff_type != User.current.pref[:diff_type] | 
					
						
							|  |  |  |             User.current.pref[:diff_type] = @diff_type | 
					
						
							|  |  |  |             User.current.preference.save | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2011-07-18 20:53:10 +00:00
										 |  |  |           render :action => 'diff' | 
					
						
							|  |  |  |         elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte | 
					
						
							| 
									
										
										
										
											2016-04-03 11:41:41 +00:00
										 |  |  |           @content = File.read(@attachment.diskfile, :mode => "rb") | 
					
						
							| 
									
										
										
										
											2011-07-18 20:53:10 +00:00
										 |  |  |           render :action => 'file' | 
					
						
							| 
									
										
										
										
											2016-04-11 19:17:48 +00:00
										 |  |  |         elsif @attachment.is_image? | 
					
						
							|  |  |  |           render :action => 'image' | 
					
						
							| 
									
										
										
										
											2011-07-18 20:53:10 +00:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2016-05-08 08:04:51 +00:00
										 |  |  |           render :action => 'other' | 
					
						
							| 
									
										
										
										
											2011-07-18 20:53:10 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       format.api | 
					
						
							| 
									
										
										
										
											2008-06-08 18:26:39 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-18 07:12:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-26 15:42:37 +00:00
										 |  |  |   def download | 
					
						
							| 
									
										
										
										
											2008-12-30 13:32:51 +00:00
										 |  |  |     if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project) | 
					
						
							|  |  |  |       @attachment.increment_download | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-05-18 07:12:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-12 14:32:25 +00:00
										 |  |  |     if stale?(:etag => @attachment.digest, :template => false) | 
					
						
							| 
									
										
										
										
											2012-07-08 15:26:40 +00:00
										 |  |  |       # images are sent inline | 
					
						
							|  |  |  |       send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), | 
					
						
							|  |  |  |                                       :type => detect_content_type(@attachment), | 
					
						
							| 
									
										
										
										
											2016-05-09 17:22:23 +00:00
										 |  |  |                                       :disposition => disposition(@attachment) | 
					
						
							| 
									
										
										
										
											2012-07-08 15:26:40 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2007-05-26 15:42:37 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-18 07:12:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-07 13:48:07 +00:00
										 |  |  |   def thumbnail | 
					
						
							| 
									
										
										
										
											2014-06-08 04:23:59 +00:00
										 |  |  |     if @attachment.thumbnailable? && tbnail = @attachment.thumbnail(:size => params[:size]) | 
					
						
							| 
									
										
										
										
											2019-12-12 14:32:25 +00:00
										 |  |  |       if stale?(:etag => tbnail, :template => false) | 
					
						
							| 
									
										
										
										
											2019-11-08 01:37:25 +00:00
										 |  |  |         send_file( | 
					
						
							|  |  |  |           tbnail, | 
					
						
							| 
									
										
										
										
											2012-07-08 15:26:40 +00:00
										 |  |  |           :filename => filename_for_content_disposition(@attachment.filename), | 
					
						
							| 
									
										
										
										
											2019-05-12 03:55:56 +00:00
										 |  |  |           :type => detect_content_type(@attachment, true), | 
					
						
							| 
									
										
										
										
											2019-11-08 01:37:25 +00:00
										 |  |  |           :disposition => 'inline') | 
					
						
							| 
									
										
										
										
											2012-07-08 15:26:40 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2012-07-07 13:48:07 +00:00
										 |  |  |     else | 
					
						
							|  |  |  |       # No thumbnail for the attachment or thumbnail could not be created | 
					
						
							| 
									
										
										
										
											2016-07-17 06:35:28 +00:00
										 |  |  |       head 404
 | 
					
						
							| 
									
										
										
										
											2012-07-07 13:48:07 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-23 10:01:16 +00:00
										 |  |  |   def upload | 
					
						
							|  |  |  |     # Make sure that API users get used to set this content type | 
					
						
							|  |  |  |     # as it won't trigger Rails' automatic parsing of the request body for parameters | 
					
						
							|  |  |  |     unless request.content_type == 'application/octet-stream' | 
					
						
							| 
									
										
										
										
											2016-07-17 06:35:28 +00:00
										 |  |  |       head 406
 | 
					
						
							| 
									
										
										
										
											2012-02-23 10:01:16 +00:00
										 |  |  |       return | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-08 10:11:09 +00:00
										 |  |  |     @attachment = Attachment.new(:file => request.raw_post) | 
					
						
							| 
									
										
										
										
											2012-02-23 10:01:16 +00:00
										 |  |  |     @attachment.author = User.current | 
					
						
							| 
									
										
										
										
											2012-10-18 21:06:35 +00:00
										 |  |  |     @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16) | 
					
						
							| 
									
										
										
										
											2014-12-21 21:07:13 +00:00
										 |  |  |     @attachment.content_type = params[:content_type].presence | 
					
						
							| 
									
										
										
										
											2012-12-10 20:09:41 +00:00
										 |  |  |     saved = @attachment.save | 
					
						
							| 
									
										
										
										
											2012-02-23 10:01:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-10 20:09:41 +00:00
										 |  |  |     respond_to do |format| | 
					
						
							|  |  |  |       format.js | 
					
						
							|  |  |  |       format.api { | 
					
						
							|  |  |  |         if saved | 
					
						
							|  |  |  |           render :action => 'upload', :status => :created | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           render_validation_errors(@attachment) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2012-02-23 10:01:16 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-01 12:46:29 +00:00
										 |  |  |   # Edit all the attachments of a container | 
					
						
							|  |  |  |   def edit_all | 
					
						
							| 
									
										
										
										
											2014-11-29 13:41:53 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-01 12:46:29 +00:00
										 |  |  |   # Update all the attachments of a container | 
					
						
							|  |  |  |   def update_all | 
					
						
							| 
									
										
										
										
											2017-06-03 08:30:32 +00:00
										 |  |  |     if Attachment.update_attachments(@attachments, update_all_params) | 
					
						
							|  |  |  |       redirect_back_or_default home_path | 
					
						
							|  |  |  |       return | 
					
						
							| 
									
										
										
										
											2014-11-29 13:41:53 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-10-01 12:46:29 +00:00
										 |  |  |     render :action => 'edit_all' | 
					
						
							| 
									
										
										
										
											2014-11-29 13:41:53 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-21 05:59:31 +00:00
										 |  |  |   def download_all | 
					
						
							|  |  |  |     Tempfile.create('attachments_zip-', Rails.root.join('tmp')) do |tempfile| | 
					
						
							|  |  |  |       zip_file = Attachment.archive_attachments(tempfile, @attachments) | 
					
						
							|  |  |  |       if zip_file | 
					
						
							|  |  |  |         send_data( | 
					
						
							|  |  |  |           File.read(zip_file.path), | 
					
						
							|  |  |  |           :type => 'application/zip', | 
					
						
							|  |  |  |           :filename => "#{@container.class.to_s.downcase}-#{@container.id}-attachments.zip") | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         render_404 | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-01 13:22:35 +00:00
										 |  |  |   def update | 
					
						
							|  |  |  |     @attachment.safe_attributes = params[:attachment] | 
					
						
							|  |  |  |     saved = @attachment.save | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     respond_to do |format| | 
					
						
							|  |  |  |       format.api { | 
					
						
							|  |  |  |         if saved | 
					
						
							|  |  |  |           render_api_ok | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           render_validation_errors(@attachment) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-09 16:54:46 +00:00
										 |  |  |   def destroy | 
					
						
							| 
									
										
										
										
											2012-03-04 11:41:10 +00:00
										 |  |  |     if @attachment.container.respond_to?(:init_journal) | 
					
						
							|  |  |  |       @attachment.container.init_journal(User.current) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2012-12-10 20:09:41 +00:00
										 |  |  |     if @attachment.container | 
					
						
							|  |  |  |       # Make sure association callbacks are called | 
					
						
							|  |  |  |       @attachment.container.attachments.delete(@attachment) | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       @attachment.destroy | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     respond_to do |format| | 
					
						
							|  |  |  |       format.html { redirect_to_referer_or project_path(@project) } | 
					
						
							|  |  |  |       format.js | 
					
						
							| 
									
										
										
										
											2016-04-12 19:18:34 +00:00
										 |  |  |       format.api { render_api_ok } | 
					
						
							| 
									
										
										
										
											2012-12-10 20:09:41 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2008-12-09 16:54:46 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-18 07:12:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-10 10:48:34 +00:00
										 |  |  |   # Returns the menu item that should be selected when viewing an attachment | 
					
						
							|  |  |  |   def current_menu_item | 
					
						
							| 
									
										
										
										
											2018-12-12 18:51:34 +00:00
										 |  |  |     container = @attachment.try(:container) || @container | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if container | 
					
						
							|  |  |  |       case container | 
					
						
							| 
									
										
										
										
											2017-06-10 10:48:34 +00:00
										 |  |  |       when WikiPage | 
					
						
							|  |  |  |         :wiki | 
					
						
							|  |  |  |       when Message | 
					
						
							|  |  |  |         :boards | 
					
						
							|  |  |  |       when Project, Version | 
					
						
							|  |  |  |         :files | 
					
						
							|  |  |  |       else | 
					
						
							| 
									
										
										
										
											2018-12-12 18:51:34 +00:00
										 |  |  |         container.class.name.pluralize.downcase.to_sym | 
					
						
							| 
									
										
										
										
											2017-06-10 10:48:34 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-29 13:41:53 +00:00
										 |  |  |   private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def find_attachment | 
					
						
							| 
									
										
										
										
											2007-05-26 15:42:37 +00:00
										 |  |  |     @attachment = Attachment.find(params[:id]) | 
					
						
							| 
									
										
										
										
											2008-07-22 17:55:19 +00:00
										 |  |  |     # Show 404 if the filename in the url is wrong | 
					
						
							|  |  |  |     raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename | 
					
						
							| 
									
										
										
										
											2007-05-26 15:42:37 +00:00
										 |  |  |     @project = @attachment.project | 
					
						
							| 
									
										
										
										
											2008-07-22 17:20:02 +00:00
										 |  |  |   rescue ActiveRecord::RecordNotFound | 
					
						
							|  |  |  |     render_404 | 
					
						
							| 
									
										
										
										
											2007-05-26 15:42:37 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-18 07:12:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-29 13:41:53 +00:00
										 |  |  |   def find_editable_attachments | 
					
						
							| 
									
										
										
										
											2020-03-21 05:59:31 +00:00
										 |  |  |     @attachments = @container.attachments.select(&:editable?) | 
					
						
							|  |  |  |     render_404 if @attachments.empty? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def find_container | 
					
						
							| 
									
										
										
										
											2014-11-29 13:41:53 +00:00
										 |  |  |     klass = params[:object_type].to_s.singularize.classify.constantize rescue nil | 
					
						
							|  |  |  |     unless klass && klass.reflect_on_association(:attachments) | 
					
						
							|  |  |  |       render_404 | 
					
						
							|  |  |  |       return | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @container = klass.find(params[:object_id]) | 
					
						
							|  |  |  |     if @container.respond_to?(:visible?) && !@container.visible? | 
					
						
							|  |  |  |       render_403 | 
					
						
							|  |  |  |       return | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     if @container.respond_to?(:project) | 
					
						
							|  |  |  |       @project = @container.project | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   rescue ActiveRecord::RecordNotFound | 
					
						
							|  |  |  |     render_404 | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-21 05:59:31 +00:00
										 |  |  |   def find_downloadable_attachments | 
					
						
							|  |  |  |     @attachments = @container.attachments.select{|a| File.readable?(a.diskfile) } | 
					
						
							|  |  |  |     bulk_download_max_size = Setting.bulk_download_max_size.to_i.kilobytes | 
					
						
							|  |  |  |     if @attachments.sum(&:filesize) > bulk_download_max_size | 
					
						
							|  |  |  |       flash[:error] = l(:error_bulk_download_size_too_big, | 
					
						
							| 
									
										
										
										
											2020-03-23 05:52:40 +00:00
										 |  |  |                         :max_size => number_to_human_size(bulk_download_max_size.to_i)) | 
					
						
							| 
									
										
										
										
											2020-03-21 05:59:31 +00:00
										 |  |  |       redirect_to back_url | 
					
						
							|  |  |  |       return | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-25 09:31:36 +00:00
										 |  |  |   # Checks that the file exists and is readable | 
					
						
							|  |  |  |   def file_readable | 
					
						
							| 
									
										
										
										
											2012-12-26 14:00:10 +00:00
										 |  |  |     if @attachment.readable? | 
					
						
							|  |  |  |       true | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       logger.error "Cannot send attachment, #{@attachment.diskfile} does not exist or is unreadable." | 
					
						
							|  |  |  |       render_404 | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2009-04-25 09:31:36 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-18 07:12:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-09 16:54:46 +00:00
										 |  |  |   def read_authorize | 
					
						
							|  |  |  |     @attachment.visible? ? true : deny_access | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-18 07:12:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-01 13:22:35 +00:00
										 |  |  |   def update_authorize | 
					
						
							|  |  |  |     @attachment.editable? ? true : deny_access | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-09 16:54:46 +00:00
										 |  |  |   def delete_authorize | 
					
						
							|  |  |  |     @attachment.deletable? ? true : deny_access | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-18 07:12:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-12 03:55:56 +00:00
										 |  |  |   def detect_content_type(attachment, is_thumb = false) | 
					
						
							| 
									
										
										
										
											2009-12-29 13:28:30 +00:00
										 |  |  |     content_type = attachment.content_type | 
					
						
							| 
									
										
										
										
											2015-02-19 17:46:24 +00:00
										 |  |  |     if content_type.blank? || content_type == "application/octet-stream" | 
					
						
							| 
									
										
										
										
											2019-06-06 14:47:34 +00:00
										 |  |  |       content_type = | 
					
						
							| 
									
										
										
										
											2019-05-12 03:55:56 +00:00
										 |  |  |         Redmine::MimeType.of(attachment.filename).presence || | 
					
						
							|  |  |  |         "application/octet-stream" | 
					
						
							| 
									
										
										
										
											2009-12-29 13:28:30 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2019-05-12 03:55:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if is_thumb && content_type == "application/pdf" | 
					
						
							|  |  |  |       # PDF previews are stored in PNG format | 
					
						
							|  |  |  |       content_type = "image/png" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     content_type | 
					
						
							| 
									
										
										
										
											2009-12-29 13:28:30 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2016-05-09 17:22:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def disposition(attachment) | 
					
						
							| 
									
										
										
										
											2017-01-29 08:03:26 +00:00
										 |  |  |     if attachment.is_pdf? | 
					
						
							| 
									
										
										
										
											2016-05-09 17:22:23 +00:00
										 |  |  |       'inline' | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       'attachment' | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2017-06-03 08:30:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # Returns attachments param for #update_all | 
					
						
							|  |  |  |   def update_all_params | 
					
						
							|  |  |  |     params.permit(:attachments => [:filename, :description]).require(:attachments) | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2007-05-26 15:42:37 +00:00
										 |  |  | end |