Add clean urls for snapshot
Add clean urls for snapshot

--- a/include/Config.class.php
+++ b/include/Config.class.php
@@ -1,24 +1,4 @@
 <?php
-/**
- * Constant for tar archive
- */
-define('GITPHP_COMPRESS_TAR', 'tar');
-
-/**
- * Constant for bz2 archive
- */
-define('GITPHP_COMPRESS_BZ2', 'tbz2');
-
-/**
- * Constant for gz archive
- */
-define('GITPHP_COMPRESS_GZ', 'tgz');
-
-/**
- * Constant for zip archive
- */
-define('GITPHP_COMPRESS_ZIP', 'zip');
-
 /**
  * Configfile reader class
  *

--- a/include/Router.class.php
+++ b/include/Router.class.php
@@ -353,6 +353,24 @@
 				$params['g'] = ltrim($regs[2], "/");
 		}
 
+		if (preg_match('@^projects/([^/\?]+)/snapshot(/[^/\?]+)?$@', $url, $regs)) {
+			$params['p'] = rawurldecode($regs[1]);
+			$params['a'] = 'snapshot';
+			if (!empty($regs[2]))
+				$params['h'] = ltrim($regs[2], "/");
+		}
+
+		$formats = GitPHP_Archive::SupportedFormats();
+		foreach ($formats as $format => $extension) {
+			if (preg_match('@^projects/([^/\?]+)/' . $format . '(/[^/\?]+)?$@', $url, $regs)) {
+				$params['p'] = rawurldecode($regs[1]);
+				$params['a'] = 'snapshot';
+				$params['fmt'] = $format;
+				if (!empty($regs[2]))
+					$params['h'] = ltrim($regs[2], "/");
+			}
+		}
+
 		if (preg_match('@^projects/([^/\?]+)/atom$@', $url, $regs)) {
 			$params['p'] = rawurldecode($regs[1]);
 			$params['a'] = 'atom';
@@ -536,6 +554,20 @@
 					if (!empty($params['graphtype'])) {
 						$baseurl .= '/' . $params['graphtype'];
 						$exclude[] = 'graphtype';
+					}
+					$exclude[] = 'action';
+					break;
+
+				case 'snapshot':
+					if (!empty($params['format'])) {
+						$baseurl .= '/' . $params['format'];
+						$exclude[] = 'format';
+					} else {
+						$baseurl .= '/snapshot';
+					}
+					if (!empty($params['hash'])) {
+						$baseurl .= '/' . GitPHP_Router::GetHash($params['hash'], $abbreviate);
+						$exclude[] = 'hash';
 					}
 					$exclude[] = 'action';
 					break;
@@ -649,14 +681,14 @@
 
 
 			case 'snapshot':
-				if (!empty($params['hash']))
+				if (!(empty($params['hash']) || in_array('hash', $exclude)))
 					$query['h'] = GitPHP_Router::GetHash($params['hash'], $abbreviate);
 				if (!empty($params['path']))
 					$query['f'] = rawurlencode($params['path']);
 				if (!empty($params['prefix']))
 					$query['prefix'] = $params['prefix'];
-				if (!empty($params['fmt']))
-					$query['fmt'] = $params['fmt'];
+				if (!(empty($params['format']) || in_array('format', $exclude)))
+					$query['fmt'] = $params['format'];
 				break;
 
 

file:a/index.php -> file:b/index.php
--- a/index.php
+++ b/index.php
@@ -33,6 +33,12 @@
 define('GITPHP_LIBDIR', GITPHP_BASEDIR . 'lib/');
 define('GITPHP_SMARTYDIR', GITPHP_LIBDIR . 'smarty/libs/');
 define('GITPHP_GESHIDIR', GITPHP_LIBDIR . 'geshi/');
+
+define('GITPHP_COMPRESS_TAR', 'tar');
+define('GITPHP_COMPRESS_BZ2', 'tbz2');
+define('GITPHP_COMPRESS_GZ', 'tgz');
+define('GITPHP_COMPRESS_ZIP', 'zip');
+
 
 include(GITPHP_INCLUDEDIR . 'version.php');
 

--- a/js/modules/tooltip.snapshot.js
+++ b/js/modules/tooltip.snapshot.js
@@ -16,12 +16,18 @@
 			var content = '<div>' + GitPHP.Resources.Snapshot + ': ';
 			var first = true;
 			var formats = GitPHP.Snapshot.Formats
+			var cleanurl = href.indexOf('/snapshot') != -1;
 			for (var type in formats) {
 				if (formats.hasOwnProperty(type)) {
 					if (!first) {
 						content += ' | ';
 					}
-					content += '<a href="' + href + '&fmt=' + type + '">' + formats[type] + '</a>';
+					if (cleanurl) {
+						var newhref = href.replace("/snapshot", "/" + type);
+						content += '<a href="' + newhref + '">' + formats[type] + '</a>';
+					} else {
+						content += '<a href="' + href + '&fmt=' + type + '">' + formats[type] + '</a>';
+					}
 					first = false;
 				}
 			}

--- a/test/bootstrap.php
+++ b/test/bootstrap.php
@@ -6,6 +6,11 @@
 define('GITPHP_BASEDIR', dirname(__FILE__) . '/../');
 define('GITPHP_CACHEDIR', GITPHP_BASEDIR . 'cache/');
 
+define('GITPHP_COMPRESS_TAR', 'tar');
+define('GITPHP_COMPRESS_BZ2', 'tbz2');
+define('GITPHP_COMPRESS_GZ', 'tgz');
+define('GITPHP_COMPRESS_ZIP', 'zip');
+
 define('GITPHP_TEST_RESOURCES', dirname(__FILE__) . '/resources');
 define('GITPHP_TEST_PROJECTROOT', GITPHP_TEST_RESOURCES . '/testprojectroot');
 

comments