Added method \ncc\Utilities > Functions > downloadGitServiceFile()

Added method \ncc\Utilities > Functions > prepareGitServiceRequest()

https://git.n64.cc/nosial/ncc/-/issues/28
This commit is contained in:
Netkas 2022-12-16 00:40:13 -05:00
parent 2b78bdd959
commit 0da410ad31

View file

@ -4,9 +4,11 @@
use Exception; use Exception;
use ncc\Abstracts\AuthenticationType; use ncc\Abstracts\AuthenticationType;
use ncc\Abstracts\HttpRequestType;
use ncc\Abstracts\Runners; use ncc\Abstracts\Runners;
use ncc\Abstracts\Scopes; use ncc\Abstracts\Scopes;
use ncc\Classes\BashExtension\BashRunner; use ncc\Classes\BashExtension\BashRunner;
use ncc\Classes\HttpClient;
use ncc\Classes\LuaExtension\LuaRunner; use ncc\Classes\LuaExtension\LuaRunner;
use ncc\Classes\PerlExtension\PerlRunner; use ncc\Classes\PerlExtension\PerlRunner;
use ncc\Classes\PhpExtension\PhpRunner; use ncc\Classes\PhpExtension\PhpRunner;
@ -17,6 +19,7 @@
use ncc\Exceptions\AuthenticationException; use ncc\Exceptions\AuthenticationException;
use ncc\Exceptions\FileNotFoundException; use ncc\Exceptions\FileNotFoundException;
use ncc\Exceptions\GitlabServiceException; use ncc\Exceptions\GitlabServiceException;
use ncc\Exceptions\HttpException;
use ncc\Exceptions\InvalidScopeException; use ncc\Exceptions\InvalidScopeException;
use ncc\Exceptions\IOException; use ncc\Exceptions\IOException;
use ncc\Exceptions\MalformedJsonException; use ncc\Exceptions\MalformedJsonException;
@ -32,6 +35,8 @@
use ncc\Objects\Vault\Entry; use ncc\Objects\Vault\Entry;
use ncc\ThirdParty\jelix\Version\Parser; use ncc\ThirdParty\jelix\Version\Parser;
use ncc\ThirdParty\Symfony\Filesystem\Filesystem; use ncc\ThirdParty\Symfony\Filesystem\Filesystem;
use PharData;
use ZipArchive;
/** /**
* @author Zi Xing Narrakas * @author Zi Xing Narrakas
@ -517,11 +522,12 @@
* *
* @param HttpRequest $httpRequest * @param HttpRequest $httpRequest
* @param Entry|null $entry * @param Entry|null $entry
* @param bool $expect_json
* @return HttpRequest * @return HttpRequest
* @throws AuthenticationException * @throws AuthenticationException
* @throws GitlabServiceException * @throws GitlabServiceException
*/ */
public static function prepareGitServiceRequest(HttpRequest $httpRequest, ?Entry $entry=null): HttpRequest public static function prepareGitServiceRequest(HttpRequest $httpRequest, ?Entry $entry=null, bool $expect_json=true): HttpRequest
{ {
if($entry !== null) if($entry !== null)
{ {
@ -538,9 +544,76 @@
} }
} }
$httpRequest->Headers[] = "Accept: application/json"; if($expect_json)
$httpRequest->Headers[] = "Content-Type: application/json"; {
$httpRequest->Headers[] = "Accept: application/json";
$httpRequest->Headers[] = "Content-Type: application/json";
}
return $httpRequest; return $httpRequest;
} }
/**
* Downloads a file from the given URL and saves it to the given path
*
* @param string $url
* @param Entry|null $entry
* @return string
* @throws AuthenticationException
* @throws GitlabServiceException
* @throws InvalidScopeException
* @throws HttpException
*/
public static function downloadGitServiceFile(string $url, ?Entry $entry=null): string
{
$out_path = Functions::getTmpDir() . "/" . basename($url);
$httpRequest = new HttpRequest();
$httpRequest->Url = $url;
$httpRequest->Type = HttpRequestType::GET;
$httpRequest = Functions::prepareGitServiceRequest($httpRequest, $entry, false);
Console::out('Downloading file ' . $url);
HttpClient::download($httpRequest, $out_path);
return $out_path;
}
/**
* @param string $path
* @return string
* @throws Exception
*/
public static function extractArchive(string $path): string
{
$filesystem = new Filesystem();
if(!$filesystem->exists(dirname($path)))
$filesystem->mkdir(dirname($path));
switch(pathinfo($path, PATHINFO_EXTENSION))
{
case 'zip':
$zip = new ZipArchive();
$zip->open($path);
$zip->extractTo(dirname($path));
$zip->close();
break;
case 'tar':
$phar = new PharData($path);
$phar->extractTo(dirname($path));
break;
case 'gz':
$phar = new PharData($path);
$phar->decompress();
break;
default:
throw new Exception('Unsupported archive type');
}
return dirname($path);
}
} }