comparison app/controllers/attachments_controller.rb @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents 433d4f72a19b
children 4f746d8966dd e248c7af89ec
comparison
equal deleted inserted replaced
1294:3e4c3460b6ca 1295:622f24f53b42
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.
83 end 83 end
84 84
85 @attachment = Attachment.new(:file => request.raw_post) 85 @attachment = Attachment.new(:file => request.raw_post)
86 @attachment.author = User.current 86 @attachment.author = User.current
87 @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16) 87 @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
88 saved = @attachment.save
88 89
89 if @attachment.save 90 respond_to do |format|
90 respond_to do |format| 91 format.js
91 format.api { render :action => 'upload', :status => :created } 92 format.api {
92 end 93 if saved
93 else 94 render :action => 'upload', :status => :created
94 respond_to do |format| 95 else
95 format.api { render_validation_errors(@attachment) } 96 render_validation_errors(@attachment)
96 end 97 end
98 }
97 end 99 end
98 end 100 end
99 101
100 def destroy 102 def destroy
101 if @attachment.container.respond_to?(:init_journal) 103 if @attachment.container.respond_to?(:init_journal)
102 @attachment.container.init_journal(User.current) 104 @attachment.container.init_journal(User.current)
103 end 105 end
104 # Make sure association callbacks are called 106 if @attachment.container
105 @attachment.container.attachments.delete(@attachment) 107 # Make sure association callbacks are called
106 redirect_to_referer_or project_path(@project) 108 @attachment.container.attachments.delete(@attachment)
109 else
110 @attachment.destroy
111 end
112
113 respond_to do |format|
114 format.html { redirect_to_referer_or project_path(@project) }
115 format.js
116 end
107 end 117 end
108 118
109 private 119 private
110 def find_project 120 def find_project
111 @attachment = Attachment.find(params[:id]) 121 @attachment = Attachment.find(params[:id])
116 render_404 126 render_404
117 end 127 end
118 128
119 # Checks that the file exists and is readable 129 # Checks that the file exists and is readable
120 def file_readable 130 def file_readable
121 @attachment.readable? ? true : render_404 131 if @attachment.readable?
132 true
133 else
134 logger.error "Cannot send attachment, #{@attachment.diskfile} does not exist or is unreadable."
135 render_404
136 end
122 end 137 end
123 138
124 def read_authorize 139 def read_authorize
125 @attachment.visible? ? true : deny_access 140 @attachment.visible? ? true : deny_access
126 end 141 end