comparison app/controllers/attachments_controller.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 2f6e71e31b55 622f24f53b42
children c86dacc2ef0a
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
89 end 89 end
90 90
91 @attachment = Attachment.new(:file => request.raw_post) 91 @attachment = Attachment.new(:file => request.raw_post)
92 @attachment.author = User.current 92 @attachment.author = User.current
93 @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16) 93 @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
94 saved = @attachment.save
94 95
95 if @attachment.save 96 respond_to do |format|
96 respond_to do |format| 97 format.js
97 format.api { render :action => 'upload', :status => :created } 98 format.api {
98 end 99 if saved
99 else 100 render :action => 'upload', :status => :created
100 respond_to do |format| 101 else
101 format.api { render_validation_errors(@attachment) } 102 render_validation_errors(@attachment)
102 end 103 end
104 }
103 end 105 end
104 end 106 end
105 107
106 def destroy 108 def destroy
107 if @attachment.container.respond_to?(:init_journal) 109 if @attachment.container.respond_to?(:init_journal)
108 @attachment.container.init_journal(User.current) 110 @attachment.container.init_journal(User.current)
109 end 111 end
110 # Make sure association callbacks are called 112 if @attachment.container
111 @attachment.container.attachments.delete(@attachment) 113 # Make sure association callbacks are called
112 redirect_to_referer_or project_path(@project) 114 @attachment.container.attachments.delete(@attachment)
115 else
116 @attachment.destroy
117 end
118
119 respond_to do |format|
120 format.html { redirect_to_referer_or project_path(@project) }
121 format.js
122 end
113 end 123 end
114 124
115 def toggle_active 125 def toggle_active
116 @attachment.active = !@attachment.active? 126 @attachment.active = !@attachment.active?
117 @attachment.save! 127 @attachment.save!
130 render_404 140 render_404
131 end 141 end
132 142
133 # Checks that the file exists and is readable 143 # Checks that the file exists and is readable
134 def file_readable 144 def file_readable
135 @attachment.readable? ? true : render_404 145 if @attachment.readable?
146 true
147 else
148 logger.error "Cannot send attachment, #{@attachment.diskfile} does not exist or is unreadable."
149 render_404
150 end
136 end 151 end
137 152
138 def read_authorize 153 def read_authorize
139 @attachment.visible? ? true : deny_access 154 @attachment.visible? ? true : deny_access
140 end 155 end