Merge remote-tracking branch 'official/master' into saltation
[gitorious:taladars-gitorious-saltation.git] / app / controllers / trees_controller.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2010 Marko Peltola <marko@markopeltola.com>
4 #   Copyright (C) 2010 Tero Hänninen <tero.j.hanninen@jyu.fi>
5 #   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
6 #   Copyright (C) 2008 Johan Sørensen <johan@johansorensen.com>
7 #   Copyright (C) 2008 Tor Arne Vestbø <tavestbo@trolltech.com>
8 #   Copyright (C) 2009 Fabio Akita <fabio.akita@gmail.com>
9 #
10 #   This program is free software: you can redistribute it and/or modify
11 #   it under the terms of the GNU Affero General Public License as published by
12 #   the Free Software Foundation, either version 3 of the License, or
13 #   (at your option) any later version.
14 #
15 #   This program is distributed in the hope that it will be useful,
16 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 #   GNU Affero General Public License for more details.
19 #
20 #   You should have received a copy of the GNU Affero General Public License
21 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 #++
23
24 class TreesController < ApplicationController
25   include ActiveMessaging::MessageSender
26   before_filter :find_project_and_repository
27   before_filter :check_repository_for_commits
28   before_filter :require_view_right_to_repository
29   renders_in_site_specific_context
30   
31   def index
32     redirect_to repo_owner_path(@repository, :project_repository_tree_path, 
33                   @project, @repository, branch_with_tree(@repository.head_candidate_name, []))
34   end
35   
36   def show
37     @git = @repository.git
38     @ref, @path = branch_and_path(params[:branch_and_path], @git)
39     unless @commit = @git.commit(@ref)
40       handle_missing_tree_sha and return
41     end
42     if stale_conditional?(Digest::SHA1.hexdigest(@commit.id + params[:branch_and_path].join), 
43                           @commit.committed_date.utc)
44       head = @git.get_head(@ref) || Grit::Head.new(@commit.id_abbrev, @commit)
45       @root = Breadcrumb::Folder.new({:paths => @path, :head => head, 
46                                       :repository => @repository})
47       path = @path.blank? ? [] : ["#{@path.join("/")}/"] # FIXME: meh, this sux
48       @tree = @git.tree(@commit.tree.id, path)
49       expires_in 30.seconds
50     end
51   end
52   
53   def archive
54     @git = @repository.git
55     # FIXME: update route when we've fixed rails bug #1939
56     @ref = desplat_path(params[:branch])
57     ext = params[:archive_format]
58     unless @commit = @git.commit(@ref)
59       handle_missing_tree_sha and return
60     end
61     
62     if !@commit
63       flash[:error] = I18n.t "trees_controller.archive_error"
64       redirect_to project_repository_path(@project, @repository) and return
65     end
66     
67     user_path = "#{@repository.project_or_owner.to_param}-#{@repository.to_param}-#{@ref}.#{ext}"
68     disk_path = "#{@repository.hashed_path.gsub(/\//,'-')}-#{@commit.id}.#{ext}"
69     if File.exist?(File.join(GitoriousConfig["archive_cache_dir"], disk_path))
70       respond_to do |format|
71         format.html {
72           set_xsendfile_headers(disk_path, user_path)
73           head(:ok) and return
74         }
75         format.js {
76           render :partial => "archive_ready"
77         }
78       end
79     else
80       # enqueue the creation of the tarball, and send an accepted response
81       if !File.exist?(File.join(GitoriousConfig["archive_work_dir"], disk_path))
82         publish_archive_message(@repository, disk_path, @commit)
83       end
84       
85       respond_to do |format|
86         format.html {
87           # FIXME: This doesn't fly with wget/curl/etc type clients
88           render :text => "The archive is currently being generated, try again later",
89             :status => :accepted, :content_type => "text/plain" and return
90         }
91         format.js {
92           render :partial => "archive_generating"
93         }
94       end
95     end
96   end
97   
98   protected
99     def set_xsendfile_headers(real_path, user_path, content_type = "application/x-gzip")
100       response.headers["X-Sendfile"] = File.join(GitoriousConfig["archive_cache_dir"], real_path)
101       response.headers["Content-Type"] = content_type
102       user_path = user_path.gsub("/", "_").gsub('"', '\"')
103       response.headers["Content-Disposition"] = "Content-Disposition: attachment; filename=\"#{user_path}\""
104     end
105     
106     def publish_archive_message(repo, disk_path, commit)
107       payload = {
108         :full_repository_path => repo.full_repository_path,
109         :output_path => File.join(GitoriousConfig["archive_cache_dir"], disk_path),
110         :work_path => File.join(GitoriousConfig["archive_work_dir"], disk_path),
111         :commit_sha => commit.id,
112         :name => (repo.project.slug + "-" + repo.name),
113         :format => "tar.gz",
114       }
115       publish :archive_repo, payload.to_json
116     end
117     
118     def handle_missing_tree_sha
119       flash[:error] = "No such tree SHA1 was found"
120       redirect_to project_repository_tree_path(@project, @repository, 
121                       branch_with_tree("HEAD", @path || []))
122     end
123 end