Update CoreLibs with phpstan level 8, add qqFileUploader implementation base, add base Test class for testing
This commit is contained in:
32
www/lib/FileUpload/Core/qqUploadedFile.php
Normal file
32
www/lib/FileUpload/Core/qqUploadedFile.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace FileUpload\Core;
|
||||
|
||||
interface qqUploadedFile // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
|
||||
{
|
||||
/**
|
||||
* Save the file to the specified path
|
||||
*
|
||||
* @param string $path
|
||||
* @return boolean TRUE on success
|
||||
*/
|
||||
public function save(string $path): bool;
|
||||
|
||||
/**
|
||||
* get qqfile name from _GET array
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string;
|
||||
|
||||
/**
|
||||
* Get file size from _SERVERa array, throws an error if not possible
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getSize(): int;
|
||||
}
|
||||
|
||||
// __END__
|
||||
@@ -5,26 +5,40 @@ namespace FileUpload\Core;
|
||||
/**
|
||||
* Handle file uploads via regular form post (uses the $_FILES array)
|
||||
*/
|
||||
class qqUploadedFileForm
|
||||
class qqUploadedFileForm implements qqUploadedFile // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
|
||||
{
|
||||
/**
|
||||
* Save the file to the specified path
|
||||
*
|
||||
* @param string $path
|
||||
* @return boolean TRUE on success
|
||||
*/
|
||||
public function save($path)
|
||||
public function save(string $path): bool
|
||||
{
|
||||
if (!move_uploaded_file($_FILES['qqfile']['tmp_name'], $path)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public function getName()
|
||||
|
||||
/**
|
||||
* get qqfile name from _FILES array
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $_FILES['qqfile']['name'];
|
||||
return $_FILES['qqfile']['name'] ?? '';
|
||||
}
|
||||
public function getSize()
|
||||
|
||||
/**
|
||||
* get files size from _FILES array
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getSize(): int
|
||||
{
|
||||
return $_FILES['qqfile']['size'];
|
||||
return (int)$_FILES['qqfile']['size'] ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,16 +5,22 @@ namespace FileUpload\Core;
|
||||
/**
|
||||
* Handle file uploads via XMLHttpRequest
|
||||
*/
|
||||
class qqUploadedFileXhr
|
||||
class qqUploadedFileXhr implements qqUploadedFile // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
|
||||
{
|
||||
/**
|
||||
* Save the file to the specified path
|
||||
*
|
||||
* @param string $path
|
||||
* @return boolean TRUE on success
|
||||
*/
|
||||
public function save($path)
|
||||
public function save(string $path): bool
|
||||
{
|
||||
$input = fopen("php://input", "r");
|
||||
$temp = tmpfile();
|
||||
// abort if not resources
|
||||
if (!is_resource($input) || !is_resource($temp)) {
|
||||
return false;
|
||||
}
|
||||
$realSize = stream_copy_to_stream($input, $temp);
|
||||
fclose($input);
|
||||
|
||||
@@ -23,17 +29,34 @@ class qqUploadedFileXhr
|
||||
}
|
||||
|
||||
$target = fopen($path, "w");
|
||||
if (!is_resource($target)) {
|
||||
return false;
|
||||
}
|
||||
fseek($temp, 0, SEEK_SET);
|
||||
stream_copy_to_stream($temp, $target);
|
||||
fclose($target);
|
||||
|
||||
return true;
|
||||
}
|
||||
public function getName()
|
||||
|
||||
/**
|
||||
* get qqfile name from _GET array
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $_GET['qqfile'];
|
||||
return $_GET['qqfile'] ?? '';
|
||||
}
|
||||
public function getSize()
|
||||
|
||||
/**
|
||||
* Get file size from _SERVERa array, throws an error if not possible
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getSize(): int
|
||||
{
|
||||
if (isset($_SERVER['CONTENT_LENGTH'])) {
|
||||
return (int)$_SERVER['CONTENT_LENGTH'];
|
||||
|
||||
Reference in New Issue
Block a user