Benjamin Renard commited on 2013-08-22 09:57:43
              Showing 2 changed files, with 69 additions and 0 deletions.
            
| ... | ... | 
                      @@ -0,0 +1,26 @@  | 
                  
| 1 | 
                        +DirectDownload plugin for ViewGit  | 
                    |
| 2 | 
                        +=================================  | 
                    |
| 3 | 
                        +  | 
                    |
| 4 | 
                        +This plugin permit direct download of project's files using URL like :  | 
                    |
| 5 | 
                        +  | 
                    |
| 6 | 
                        +http://my.viewgit.com/?a=directdownload&p=myproject&h=HEAD&f=path/to/my/poject/file  | 
                    |
| 7 | 
                        +  | 
                    |
| 8 | 
                        +Install  | 
                    |
| 9 | 
                        +-------  | 
                    |
| 10 | 
                        +  | 
                    |
| 11 | 
                        +Put directdownload directory in plugins directory  | 
                    |
| 12 | 
                        +  | 
                    |
| 13 | 
                        +Configure rewrite for nicer URL  | 
                    |
| 14 | 
                        +-------------------------------  | 
                    |
| 15 | 
                        +  | 
                    |
| 16 | 
                        +To have nicer URL, you could configure your apache with mod_rewrite.  | 
                    |
| 17 | 
                        +Add this following line in .htaccess file :  | 
                    |
| 18 | 
                        +  | 
                    |
| 19 | 
                        +RewriteEngine On  | 
                    |
| 20 | 
                        +RewriteCond %{REQUEST_FILENAME} !-f
                       | 
                    |
| 21 | 
                        +RewriteCond %{REQUEST_FILENAME} !-d
                       | 
                    |
| 22 | 
                        +RewriteRule ^download/([^/]+)/(.+)$ /index.php?a=directdownload&p=$1&h=HEAD&f=$2 [QSA,L]  | 
                    |
| 23 | 
                        +  | 
                    |
| 24 | 
                        +You could now access to direct download feature with URL like :  | 
                    |
| 25 | 
                        +  | 
                    |
| 26 | 
                        +http://my.viewgit.com/download/myproject/path/to/my/poject/file  | 
                    
| ... | ... | 
                      @@ -0,0 +1,43 @@  | 
                  
| 1 | 
                        +<?php  | 
                    |
| 2 | 
                        +/**  | 
                    |
| 3 | 
                        + * Direct Download plugin for ViewGit.  | 
                    |
| 4 | 
                        + *  | 
                    |
| 5 | 
                        + * This plugin permit direct download of project file using URL like  | 
                    |
| 6 | 
                        + *  | 
                    |
| 7 | 
                        + * http://my.viewgit.com/?a=directdownload&p=myproject&h=HEAD&f=path/to/my/poject/file  | 
                    |
| 8 | 
                        + *  | 
                    |
| 9 | 
                        + * @author Benjamin Renard <brenard@zionetrix.net>  | 
                    |
| 10 | 
                        + */  | 
                    |
| 11 | 
                        +class DirectDownloadPlugin extends VGPlugin  | 
                    |
| 12 | 
                        +{
                       | 
                    |
| 13 | 
                        +	function __construct() {
                       | 
                    |
| 14 | 
                        + global $conf;  | 
                    |
| 15 | 
                        +		$this->register_action('directdownload');
                       | 
                    |
| 16 | 
                        + }  | 
                    |
| 17 | 
                        +  | 
                    |
| 18 | 
                        +	function action($action) {
                       | 
                    |
| 19 | 
                        +		if ($action === 'directdownload') {
                       | 
                    |
| 20 | 
                        + $project = validate_project($_REQUEST['p']);  | 
                    |
| 21 | 
                        + $hash = validate_hash($_REQUEST['h']);  | 
                    |
| 22 | 
                        +			if (isset($_REQUEST['f'])) {
                       | 
                    |
| 23 | 
                        + $path = $_REQUEST['f'];  | 
                    |
| 24 | 
                        + $infos=git_get_path_info($project,$hash,$path);  | 
                    |
| 25 | 
                        +				if (count($infos)!=1) {
                       | 
                    |
| 26 | 
                        +					die('Invalid path');
                       | 
                    |
| 27 | 
                        + }  | 
                    |
| 28 | 
                        + $hash=$infos[0]['hash'];  | 
                    |
| 29 | 
                        + $name=$infos[0]['name'];  | 
                    |
| 30 | 
                        + }  | 
                    |
| 31 | 
                        +			else {
                       | 
                    |
| 32 | 
                        + $name = $_REQUEST['n'];  | 
                    |
| 33 | 
                        + }  | 
                    |
| 34 | 
                        +  | 
                    |
| 35 | 
                        +			header('Content-type: application/octet-stream');
                       | 
                    |
| 36 | 
                        +			header("Content-Disposition: attachment; filename=$name");
                       | 
                    |
| 37 | 
                        +  | 
                    |
| 38 | 
                        + run_git_passthru($project, "cat-file blob $hash");  | 
                    |
| 39 | 
                        + die();  | 
                    |
| 40 | 
                        + }  | 
                    |
| 41 | 
                        + }  | 
                    |
| 42 | 
                        +}  | 
                    |
| 43 | 
                        +  | 
                    |
| 0 | 44 |