File: //proc/self/cwd/finance/source/Support/Upload.php
<?php
namespace Source\Support;
use CoffeeCode\Uploader\File;
use CoffeeCode\Uploader\Image;
use CoffeeCode\Uploader\Media;
use Source\Models\Gallery;
/**
* FSPHP | Class Upload
*
* @author Robson V. Leite <cursos@upinside.com.br>
* @package Source\Support
*/
class Upload
{
/** @var Message */
private $message;
/**
* Upload constructor.
*/
public function __construct()
{
$this->message = new Message();
}
/**
* @return Message
*/
public function message(): Message
{
return $this->message;
}
/**
* @param array $image
* @param string $name
* @param int $width
* @return null|string
* @throws \Exception
*/
public function image(array $image, string $name, int $width = CONF_IMAGE_SIZE): ?string
{
$upload = new Image(CONF_UPLOAD_DIR, CONF_UPLOAD_IMAGE_DIR);
if (empty($image['type']) || !in_array($image['type'], $upload::isAllowed())) {
$this->message->error("Você não selecionou uma imagem válida");
return null;
}
return str_replace(CONF_UPLOAD_DIR . "/", "", $upload->upload($image, $name, $width, CONF_IMAGE_QUALITY));
}
public function gallery(array $images, string $name, string $width, string $entity, string $galleryLink) : ?Bool
{
$gbFiles = array();
$gbCount = count($images['tmp_name']);
$gbKeys = array_keys($images);
for ($gb = 0; $gb < $gbCount; $gb++):
foreach ($gbKeys as $Keys):
$gbFiles[$gb][$Keys] = $images[$Keys][$gb];
endforeach;
endfor;
$i=0;
foreach ($gbFiles as $gbUpload):
$i++;
$imgName = "{$name}-gb-" . (substr(md5(time() + $i), 0, 5));
$finalName = $this->image($gbUpload, $imgName, $width);
if(isset($finalName) AND $finalName != ""){
$gal = new Gallery();
$gal->gallery_link = $galleryLink;
$gal->tabela_base = $entity;
$gal->gallery = $finalName;
if(!$gal->save()){
$this->message->error("Problema ao enviar galeria");
return false;
}
}else{
$this->message->error("Problema ao enviar galeria");
return false;
}
endforeach;
return true;
}
/**
* @param array $file
* @param string $name
* @return null|string
* @throws \Exception
*/
public function file(array $file, string $name): ?string
{
$upload = new File(CONF_UPLOAD_DIR, CONF_UPLOAD_FILE_DIR);
if (empty($file['type']) || !in_array($file['type'], $upload::isAllowed())) {
$this->message->error("Você não selecionou um arquivo válido");
return null;
}
return str_replace(CONF_UPLOAD_DIR . "/", "", $upload->upload($file, $name));
}
/**
* @param array $media
* @param string $name
* @return null|string
* @throws \Exception
*/
public function media(array $media, string $name): ?string
{
$upload = new Media(CONF_UPLOAD_DIR, CONF_UPLOAD_MEDIA_DIR);
if (empty($media['type']) || !in_array($media['type'], $upload::isAllowed())) {
$this->message->error("Você não selecionou uma mídia válida");
return null;
}
return str_replace(CONF_UPLOAD_DIR . "/", "", $upload->upload($media, $name));
}
/**
* @param string $filePath
*/
public function remove(string $filePath): void
{
if (file_exists($filePath) && is_file($filePath)) {
unlink($filePath);
}
}
}