hcportal-dev/Aiko/Libs/Storage.php

141 lines
4.0 KiB
PHP

<?php
namespace Aiko;
use ErrorException;
use Exception;
use Helper;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class Storage
{
private $isProduction;
private $registry;
private $baseStorage;
public function __construct($registry)
{
$this->registry = $registry;
$this->isProduction = $this->registry->config->environment === 'production';
$this->baseStorage = $this->isProduction ? $this->registry->config->base_storage : __SITE_PATH . '/hcportal_docs/';
}
public function fromException($e, $action)
{
return Helper::handleException($this->registry, $e, "storage", $action, false, "Storage");
}
/** @param UploadedFile $file */
public function validateFile($file, $maxSize = 2048, $allowedMime = [])
{
if ($file instanceof UploadedFile) {
$mime = $file->getMimeType();
if (in_array($mime, $allowedMime)) return true;
return false;
}
return false;
}
//
//
// @param $options = [
// 'mime' => [],
// 'size' => 0
// ]
public function store($file, $folderName = 'temp', $prefix = '', $options = [])
{
try {
$storage = $this->trimSlashes($this->baseStorage . '/' . $folderName . '/');
$default = $this->generateFilename($file);
$filename = $default;
if ($prefix) {
$filename = $prefix . '_' . $default;
}
$file->move($storage, $filename);
return $filename;
} catch (FileException $e) {
return $this->fromException($e, "store");
} catch (ErrorException $e) {
return $this->fromException($e, "store");
}
}
public function delete($filename, $folderName = 'temp')
{
try {
$link = $this->trimSlashes($this->baseStorage . '/' . $folderName . '/' . $filename);
if (file_exists($link)) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$fileTmp = $link;
$fileTmp = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, $fileTmp);
@unlink($fileTmp);
} else {
unlink($link);
}
}
return true;
} catch (Exception $e) {
return $this->fromException($e, "delete");
}
}
public function exists($filename, $folderName = 'temp')
{
$link = $this->trimSlashes($this->baseStorage . '/' . $folderName . '/' . $filename);
return file_exists($link);
}
public function url($filename, $path = 'temp', $isLocal = false)
{
if (!$filename) {
return null;
}
$filePath = $this->trimSlashes('hcportal_docs/' . $path . '/' . $filename);
$prefix = $this->registry->config->server_address;
if($isLocal){
$prefix = __SITE_PATH.'/';
}
return $prefix . $filePath;
}
public function generateFilename($file)
{
$time = time();
$filename = uniqid() . '_' . $time . '.' . $file->guessExtension();
return $filename;
}
public function isValidImage($mimeType)
{
return $this->validateMime($mimeType, [
'image/jpeg',
'image/png',
'image/jpg'
]);
return true;
}
public function isValidDocument($mimeType)
{
return $this->validateMime($mimeType, [
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.ms-excel',
'application/pdf'
]);
}
private function validateMime($mimeType, $mimeClientAlowed)
{
if (!in_array($mimeType, $mimeClientAlowed)) {
return false;
}
return true;
}
private function trimSlashes($str)
{
return preg_replace('/(\/+)/', '/', $str);
}
}