summaryrefslogtreecommitdiffstats
path: root/app/controllers/blobs_controller.rb
blob: 46d85ae7bdebbae20c384b97558d5980da01c9b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#--
#   Copyright (C) 2008 Johan Sørensen <johan@johansorensen.com>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#++

class BlobsController < ApplicationController
  before_filter :find_project_and_repository
  before_filter :check_repository_for_commits

  def show
    @git = @repository.git
    @ref, @path = branch_and_path(params[:branch_and_path], @git)
    @commit = @git.commit(@ref)
    unless @commit
      redirect_to project_repository_blob_path(@project, @repository, 
                    branch_with_tree("HEAD", @path)) and return
    end
    @blob = @git.tree(@commit.tree.id, ["#{@path.join("/")}"]).contents.first
    head = @git.get_head(@ref) || Grit::Head.new(@commit.id_abbrev, @commit)
    @root = Breadcrumb::Blob.new(:paths => @path, :head => head, 
                :repository => @repository, :name => @blob.basename)
    render_not_found and return unless @blob
    unless @blob.respond_to?(:data) # it's a tree
      redirect_to project_repository_tree_path(@project, @repository, @commit.id, @path)
    end
    cache_for 2.minutes
  end

  def raw
    @git = @repository.git
    @ref, @path = branch_and_path(params[:branch_and_path], @git)
    @commit = @git.commit(@ref)
    unless @commit
      redirect_to project_repository_raw_blob_path(@project, @repository, 
                    branch_with_tree("HEAD", @path)) and return
    end
    @blob = @git.tree(@commit.tree.id, ["#{@path.join("/")}"]).contents.first
    render_not_found and return unless @blob
    if @blob.size > 500.kilobytes
      flash[:error] = I18n.t "blogs_controller.raw_error"
      redirect_to project_repository_path(@project, @repository) and return
    end
    cache_for 2.minutes
    render :text => @blob.data, :content_type => @blob.mime_type
  end
  
  # def text
  # end
end