File viewer for attached text files.

git-svn-id: http://redmine.rubyforge.org/svn/trunk@1520 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2008-06-09 18:40:59 +00:00
parent aa0beecad0
commit 80a7486f95
14 changed files with 181 additions and 12 deletions

View File

@ -23,7 +23,10 @@ class AttachmentsController < ApplicationController
if @attachment.is_diff? if @attachment.is_diff?
@diff = File.new(@attachment.diskfile, "rb").read @diff = File.new(@attachment.diskfile, "rb").read
render :action => 'diff' render :action => 'diff'
else elsif @attachment.is_text?
@content = File.new(@attachment.diskfile, "rb").read
render :action => 'file'
elsif
download download
end end
end end
@ -38,9 +41,9 @@ class AttachmentsController < ApplicationController
private private
def find_project def find_project
@attachment = Attachment.find(params[:id]) @attachment = Attachment.find(params[:id])
render_404 and return false unless File.readable?(@attachment.diskfile) #render_404 and return false unless File.readable?(@attachment.diskfile)
@project = @attachment.project @project = @attachment.project
rescue #rescue
render_404 # render_404
end end
end end

View File

@ -15,6 +15,9 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'coderay'
require 'coderay/helpers/file_type'
module ApplicationHelper module ApplicationHelper
include Redmine::WikiFormatting::Macros::Definitions include Redmine::WikiFormatting::Macros::Definitions
@ -116,6 +119,11 @@ module ApplicationHelper
l(:actionview_datehelper_select_month_names).split(',')[month-1] l(:actionview_datehelper_select_month_names).split(',')[month-1]
end end
def syntax_highlight(name, content)
type = CodeRay::FileType[name]
type ? CodeRay.scan(content, type).html : h(content)
end
def pagination_links_full(paginator, count=nil, options={}) def pagination_links_full(paginator, count=nil, options={})
page_param = options.delete(:page_param) || :page page_param = options.delete(:page_param) || :page
url_param = params.dup url_param = params.dup

View File

@ -15,16 +15,9 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'coderay'
require 'coderay/helpers/file_type'
require 'iconv' require 'iconv'
module RepositoriesHelper module RepositoriesHelper
def syntax_highlight(name, content)
type = CodeRay::FileType[name]
type ? CodeRay.scan(content, type).html : h(content)
end
def format_revision(txt) def format_revision(txt)
txt.to_s[0,8] txt.to_s[0,8]
end end

View File

@ -88,6 +88,10 @@ class Attachment < ActiveRecord::Base
self.filename =~ /\.(jpe?g|gif|png)$/i self.filename =~ /\.(jpe?g|gif|png)$/i
end end
def is_text?
Redmine::MimeType.is_type?('text', filename)
end
def is_diff? def is_diff?
self.filename =~ /\.(patch|diff)$/i self.filename =~ /\.(patch|diff)$/i
end end

View File

@ -0,0 +1,15 @@
<h2><%=h @attachment.filename %></h2>
<div class="attachments">
<p><%= h("#{@attachment.description} - ") unless @attachment.description.blank? %>
<span class="author"><%= @attachment.author %>, <%= format_time(@attachment.created_on) %></span></p>
<p><%= link_to l(:button_download), {:controller => 'attachments', :action => 'download', :id => @attachment } -%>
<span class="size">(<%= number_to_human_size @attachment.filesize %>)</span></p>
</div>
&nbsp;
<%= render :partial => 'common/file', :locals => {:content => @content, :filename => @attachment.filename} %>
<% content_for :header_tags do -%>
<%= stylesheet_link_tag "scm" -%>
<% end -%>

View File

@ -36,4 +36,40 @@ attachments_003:
filename: logo.gif filename: logo.gif
description: This is a logo description: This is a logo
author_id: 2 author_id: 2
attachments_004:
created_on: 2006-07-19 21:07:27 +02:00
container_type: Issue
container_id: 3
downloads: 0
disk_filename: 060719210727_source.rb
digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
id: 4
filesize: 153
filename: source.rb
author_id: 2
description: This is a Ruby source file
content_type: application/x-ruby
attachments_005:
created_on: 2006-07-19 21:07:27 +02:00
container_type: Issue
container_id: 3
downloads: 0
disk_filename: 060719210727_changeset.diff
digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
id: 5
filesize: 687
filename: changeset.diff
author_id: 2
content_type: text/x-diff
attachments_006:
created_on: 2006-07-19 21:07:27 +02:00
container_type: Issue
container_id: 3
downloads: 0
disk_filename: 060719210727_archive.zip
digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
id: 6
filesize: 157
filename: archive.zip
author_id: 2
content_type: application/octet-stream

Binary file not shown.

View File

@ -0,0 +1,13 @@
Index: trunk/app/controllers/issues_controller.rb
===================================================================
--- trunk/app/controllers/issues_controller.rb (rvision 1483)
+++ trunk/app/controllers/issues_controller.rb (rvision 1484)
@@ -149,7 +149,7 @@
attach_files(@issue, params[:attachments])
flash[:notice] = l(:notice_successful_create)
Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added')
- redirect_to :controller => 'issues', :action => 'show', :id => @issue, :project_id => @project
+ redirect_to :controller => 'issues', :action => 'show', :id => @issue
return
end
end

View File

@ -0,0 +1,10 @@
# The Greeter class
class Greeter
def initialize(name)
@name = name.capitalize
end
def salute
puts "Hello #{@name}!"
end
end

View File

@ -0,0 +1,59 @@
# redMine - project management software
# Copyright (C) 2006-2008 Jean-Philippe Lang
#
# 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.
#
# 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.
#
# 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.
require File.dirname(__FILE__) + '/../test_helper'
require 'attachments_controller'
# Re-raise errors caught by the controller.
class AttachmentsController; def rescue_action(e) raise e end; end
class AttachmentsControllerTest < Test::Unit::TestCase
fixtures :users, :projects, :issues, :attachments
def setup
@controller = AttachmentsController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
Attachment.storage_path = "#{RAILS_ROOT}/test/fixtures/files"
User.current = nil
end
def test_show_diff
get :show, :id => 5
assert_response :success
assert_template 'diff'
end
def test_show_text_file
get :show, :id => 4
assert_response :success
assert_template 'file'
end
def test_show_other
get :show, :id => 6
assert_response :success
assert_equal 'application/octet-stream', @response.content_type
end
def test_download_text_file
get :download, :id => 4
assert_response :success
assert_equal 'application/x-ruby', @response.content_type
end
end

View File

@ -40,6 +40,8 @@ class DocumentsControllerTest < Test::Unit::TestCase
def test_new_with_one_attachment def test_new_with_one_attachment
@request.session[:user_id] = 2 @request.session[:user_id] = 2
set_tmp_attachments_directory
post :new, :project_id => 'ecookbook', post :new, :project_id => 'ecookbook',
:document => { :title => 'DocumentsControllerTest#test_post_new', :document => { :title => 'DocumentsControllerTest#test_post_new',
:description => 'This is a new document', :description => 'This is a new document',

View File

@ -361,6 +361,8 @@ class IssuesControllerTest < Test::Unit::TestCase
end end
def test_post_edit_with_attachment_only def test_post_edit_with_attachment_only
set_tmp_attachments_directory
# anonymous user # anonymous user
post :edit, post :edit,
:id => 1, :id => 1,

View File

@ -1,3 +1,20 @@
# redMine - project management software
# Copyright (C) 2006-2008 Jean-Philippe Lang
#
# 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.
#
# 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.
#
# 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.
require "#{File.dirname(__FILE__)}/../test_helper" require "#{File.dirname(__FILE__)}/../test_helper"
class IssuesTest < ActionController::IntegrationTest class IssuesTest < ActionController::IntegrationTest
@ -47,6 +64,7 @@ class IssuesTest < ActionController::IntegrationTest
# add then remove 2 attachments to an issue # add then remove 2 attachments to an issue
def test_issue_attachements def test_issue_attachements
log_user('jsmith', 'jsmith') log_user('jsmith', 'jsmith')
set_tmp_attachments_directory
post 'issues/edit/1', post 'issues/edit/1',
:notes => 'Some notes', :notes => 'Some notes',

View File

@ -57,6 +57,12 @@ class Test::Unit::TestCase
def test_uploaded_file(name, mime) def test_uploaded_file(name, mime)
ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + "/files/#{name}", mime) ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + "/files/#{name}", mime)
end end
# Use a temporary directory for attachment related tests
def set_tmp_attachments_directory
Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments")
Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
end
end end