88 lines
2.7 KiB
PHP
88 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries\MyStorage;
|
|
|
|
use App\Traits\FileTrait;
|
|
|
|
class FileLibrary extends MyStorageLibrary
|
|
{
|
|
use FileTrait;
|
|
private $_uploadPath = "uploads";
|
|
private $_path = "";
|
|
private $_mimeType = "";
|
|
private $_fileSize = 0;
|
|
private $_fileSequence = 0;
|
|
private $_imageLibrary = null;
|
|
public function __construct(string $path)
|
|
{
|
|
parent::__construct();
|
|
$this->_path = $path;
|
|
}
|
|
final public function getUploadPath(): string
|
|
{
|
|
return $this->_uploadPath;
|
|
}
|
|
final public function getPath(): string
|
|
{
|
|
return $this->_path;
|
|
}
|
|
final public function getMimeType(): string
|
|
{
|
|
return $this->_mimeType;
|
|
}
|
|
final public function getFileSize(): int
|
|
{
|
|
return $this->_fileSize;
|
|
}
|
|
final public function getFileSequence(): int
|
|
{
|
|
return $this->_fileSequence;
|
|
}
|
|
final public function getMediaTag(): string
|
|
{
|
|
$mediaTag = "";
|
|
switch ($this->getOrintginType()) {
|
|
case "image":
|
|
$mediaTag = sprintf(
|
|
"<img src=\"/%s/%s/%s\" alt=\"%s\">",
|
|
$this->getUploadPath(),
|
|
$this->getPath(),
|
|
$this->getOriginName(),
|
|
$this->getOriginName()
|
|
);
|
|
break;
|
|
case "video":
|
|
$mediaTag = sprintf(
|
|
"<video alt=\"%s\" controls autoplay>
|
|
<source src=\"/%s/%s/%s\" type=\"%s\">
|
|
Your browser does not support the video tag.
|
|
</video>",
|
|
$this->getOriginName(),
|
|
$this->getUploadPath(),
|
|
$this->getPath(),
|
|
$this->getOriginName(),
|
|
$this->getMimeType(),
|
|
);
|
|
break;
|
|
}
|
|
return $mediaTag;
|
|
}
|
|
public function save(): static
|
|
{
|
|
$fullPath = WRITEPATH . $this->getUploadPath() . DIRECTORY_SEPARATOR . $this->getPath();
|
|
$this->makeDirectory($fullPath);
|
|
$saveFilePath = $fullPath . DIRECTORY_SEPARATOR . $this->getOriginName();
|
|
if (file_exists($saveFilePath)) {
|
|
throw new \Exception(__FUNCTION__ . " 이미 존재하는 파일:{$saveFilePath}");
|
|
}
|
|
//원본이미지 저장
|
|
if (!file_put_contents($saveFilePath, $this->getOriginContent())) {
|
|
throw new \Exception(__FUNCTION__ . " 파일저장 실패:{$saveFilePath}");
|
|
}
|
|
$this->_mimeType = mime_content_type($saveFilePath);
|
|
$this->_fileSize = filesize($saveFilePath);
|
|
log_message("notice", __FUNCTION__ . " 원본파일 {$this->getOriginName()} 작업 완료");
|
|
return $this;
|
|
}
|
|
}
|