<commit_before><?php

namespace Lorry\Presenter\Publish\Api;

use Lorry\ApiPresenter;
use Lorry\Exception;
use Lorry\Exception\ForbiddenException;
use Lorry\Service\ConfigService;
use Lorry\Model\Addon;
use Lorry\Model\Release;
use Symfony\Component\Finder\Finder;

class QueryFile extends ApiPresenter {

	public static function sanitizeFilename($supplied) {
		$file_name = basename($supplied);
		if(preg_match('/^[-0-9A-Z_\.]/i', $file_name)) {
			return $file_name;
		}
		throw new Exception(gettext('invalid filename'));
	}

	public static function getType() {
		$type = filter_input(INPUT_GET, 'type');
		if($type == 'asset') {
			throw new Lorry\Exception\NotImplementedException;
		}
		if(in_array($type, array('asset', 'data'))) {
			return $type;
		}
		throw new Exception('unknown type');
	}

	public static function getAssetDirectory(ConfigService $config, Addon $addon, Release $release) {
		$base = $config->get('upload/assets');
		if(is_dir($base) && is_writable($base)) {
			return $base.'/'.$release->getAssetSecret();
		}
		throw new \Exception('asset directory does not exist or is not writeable');
	}

	public static function getDataDirectory(ConfigService $config, Addon $addon, Release $release) {
		$base = $config->get('upload/data');
		if(is_dir($base) && is_writable($base)) {
			return $base.'/addon'.$addon->getId().'/release'.$release->getId();
		}
		throw new \Exception('data directory does not exist or is not writeable');
	}

	public function get($id, $version) {
		if(!$this->session->authenticated()) {
			throw new ForbiddenException();
		}

		$user = $this->session->getUser();
		$addon = \Lorry\Presenter\Publish\Edit::getAddon($id, $user);
		$release = \Lorry\Presenter\Publish\Release::getRelease($addon->getId(), $version);

		$finder = new Finder();
		$found = $finder->files()->depth('== 0')->in(QueryFile::getDataDirectory($this->config, $addon, $release));

		$files = array();
		foreach($found as $file) {
			$files[] = array('uniqueIdentifier' => count($files), 'fileName' => $file->getRelativePathname());
		}

		$this->display(array('files' => $files));
	}

}<commit_msg>fix release file upload showing error if no file has ever been uploaded before<commit_after><?php

namespace Lorry\Presenter\Publish\Api;

use Lorry\ApiPresenter;
use Lorry\Exception;
use Lorry\Exception\ForbiddenException;
use Lorry\Service\ConfigService;
use Lorry\Model\Addon;
use Lorry\Model\Release;
use Symfony\Component\Finder\Finder;

class QueryFile extends ApiPresenter {

	public static function sanitizeFilename($supplied) {
		$file_name = basename($supplied);
		if(preg_match('/^[-0-9A-Z_\.]/i', $file_name)) {
			return $file_name;
		}
		throw new Exception(gettext('invalid filename'));
	}

	public static function getType() {
		$type = filter_input(INPUT_GET, 'type');
		if($type == 'asset') {
			throw new Lorry\Exception\NotImplementedException;
		}
		if(in_array($type, array('asset', 'data'))) {
			return $type;
		}
		throw new Exception('unknown type');
	}

	public static function getAssetDirectory(ConfigService $config, Addon $addon, Release $release) {
		$base = $config->get('upload/assets');
		if(is_dir($base) && is_writable($base)) {
			return $base.'/'.$release->getAssetSecret();
		}
		throw new \Exception('asset directory does not exist or is not writeable');
	}

	public static function getDataDirectory(ConfigService $config, Addon $addon, Release $release) {
		$base = $config->get('upload/data');
		if(is_dir($base) && is_writable($base)) {
			return $base.'/addon'.$addon->getId().'/release'.$release->getId();
		}
		throw new \Exception('data directory does not exist or is not writeable');
	}

	public function get($id, $version) {
		if(!$this->session->authenticated()) {
			throw new ForbiddenException();
		}

		$user = $this->session->getUser();
		$addon = \Lorry\Presenter\Publish\Edit::getAddon($id, $user);
		$release = \Lorry\Presenter\Publish\Release::getRelease($addon->getId(), $version);

		$files = array();

		$directory = QueryFile::getDataDirectory($this->config, $addon, $release);
		if(is_dir($directory)) {

			$finder = new Finder();
			$found = $finder->files()->depth('== 0')->in($directory);

			foreach($found as $file) {
				$files[] = array('uniqueIdentifier' => count($files), 'fileName' => $file->getRelativePathname());
			}
		}

		$this->display(array('files' => $files));
	}

}
{'fraction_non_alphanumeric': 0.1742216117216117, 'fraction_numerical': 0.0013736263736263737, 'mean_word_length': 4.96043656207367, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6069343', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1439, 'n_words': 304}