xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / inc / artifacts.inc.php (89853c6949e0fa4776d509d9fa70ee9193310518) (2,899B) (mode 100644) [raw]
<?php
require_once(__DIR__ . '/mime.inc.php');

function rg_artifacts_set_error($str)
{
	global $rg_artifacts_error;
	$rg_artifacts_error = $str;
	rg_log($str);
}

function rg_artifacts_error()
{
	global $rg_artifacts_error;
	return $rg_artifacts_error;
}

/*
 * Load info about a file in the artifacts directory
 */
function rg_artifacts_load_file($uid, $repo_id, $path)
{
	rg_log_enter('artifacts_load_one: path=' . $path);

	$ret = FALSE;
	while (1) {
		if (strstr($path, '..')) {
			rg_artifacts_set_error('invalid path');
			break;
		}

		$apath = rg_repo_artifacts_path_by_id($uid, $repo_id);
		$fpath = $apath . '/' . $path;
		$ret = @rg_unserialize(file_get_contents($fpath . '.rg.meta'));
		if ($ret === FALSE) {
			rg_log('DEBUG: cannot load metadata from ' . $fpath . '.rg.data');
			$ret = array();
		}
		rg_log_ml('DEBUG: data loaded from .rg.meta: ' . print_r($ret, TRUE));

		$ret['full_path'] = $fpath;
		$ret['path'] = dirname($path);
		$ret['path_file'] = $path;
		$ret['e_path_file'] = rg_path2url($ret['path_file']);
		$ret['file'] = basename($path);
		$ret['is_dir'] = 0;
		$ret['size_nice'] = rg_1024($ret['size']);

		if (!isset($ret['content_type']))
			$ret['content_type'] = rg_content_type_by_ext($ret['file']);

		rg_log_ml('ret: ' . print_r($ret, TRUE));
		break;
	}

	rg_log_exit();
	return $ret;
}

/*
 * Load the list of artifacts in a specific dir
 */
function rg_artifacts_load($uid, $repo_id, $dir)
{
	rg_log_enter('artifacts_load');

	$ret = FALSE;
	while (1) {
		if (strstr($dir, '..')) {
			rg_artifacts_set_error('invalid dir');
			break;
		}

		$apath = rg_repo_artifacts_path_by_id($uid, $repo_id);
		$fdir = $apath . '/' . $dir;
		if (!is_dir($fdir)) {
			$ret = array();
			break;
		}

		$d = rg_dir_load($fdir);
		if ($d === FALSE) {
			rg_artifacts_set_error(rg_util_error());
			break;
		}

		$ret = array();
		foreach ($d as $i => $v) {
			if (!is_dir($fdir . '/' . $v)) {
				// ignore meta files
				$last = substr($v, -8);
				if (strcmp($last, '.rg.meta') == 0)
					continue;

				$ret[] = rg_artifacts_load_file($uid, $repo_id, $dir . '/' . $v);
				continue;
			}

			$a = array();
			$a['content_type'] = 'n/a';
			$a['size'] = 'n/a';
			$a['path'] = $dir;
			$a['path_file'] = $dir . '/' . $v;
			$a['e_path_file'] = rg_path2url($a['path_file']);
			$a['file'] = $v;
			$a['original_path'] = 'n/a';
			$a['is_dir'] = 1;
			$a['size_nice'] = 'n/a';
			$ret[] = $a;
		}

		rg_log_ml('ret: ' . print_r($ret, TRUE));
		break;
	}

	rg_log_exit();
	return $ret;
}

/*
 * Add several variables to the list
 */
function rg_artifacts_cosmetic_one(&$a)
{
	if (isset($a['upload_ts']))
		$a['upload_ts_nice'] = gmdate('Y-m-d H:i:s', $a['upload_ts']);
	else
		$a['upload_ts_nice'] = 'n/a';
}

/*
 * Add several variables to the list
 */
function rg_artifacts_cosmetic(&$list)
{
	foreach ($list as $i => &$v)
		rg_artifacts_cosmetic_one($v);
	unset($v);
}

Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master