Automation/app/Libraries/MyStorage/FileLibrary.php
2024-09-11 15:36:25 +09:00

81 lines
2.7 KiB
PHP

<?php
namespace App\Libraries\MyStorage;
use App\Entities\MyStorage\FileEntity;
use App\Traits\FileTrait;
class FileLibrary extends MyStorageLibrary
{
use FileTrait;
private $_path = "";
private $_imageLibrary = null;
public function __construct(string $path)
{
parent::__construct();
$this->_path = $path;
}
final public function getPath(): string
{
return $this->_path;
}
public function createFileEntity(): FileEntity
{
return new FileEntity();
}
protected function getMediaTag(string $mediaType, FileEntity $entity): string
{
$mediaTag = "";
switch ($mediaType) {
case "image":
$mediaTag = sprintf(
"<img src=\"/%s/%s/%s\" alt=\"%s\">",
$this->getUploadPath(),
$entity->getPath(),
$entity->getTitle(),
$entity->getTitle()
);
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>",
$entity->getTitle(),
$this->getUploadPath(),
$entity->getPath(),
$entity->getTitle(),
$entity->getMimeType(),
);
break;
}
return $mediaTag;
}
public function save(string $fileName, string $mediaType, string $content, int $file_sequence): FileEntity
{
$fullPath = WRITEPATH . $this->getUploadPath() . DIRECTORY_SEPARATOR . $this->getPath();
$this->makeDirectory($fullPath);
$saveFilePath = $fullPath . DIRECTORY_SEPARATOR . $fileName;
if (file_exists($saveFilePath)) {
throw new \Exception(__FUNCTION__ . " 이미 존재하는 파일:{$saveFilePath}");
}
//원본이미지 저장
if (!file_put_contents($saveFilePath, $content)) {
throw new \Exception(__FUNCTION__ . " 파일저장 실패:{$saveFilePath}");
}
$entity = $this->createFileEntity();
$entity->setPath($this->getPath());
$entity->setTitle($fileName);
$entity->setMimeType(mime_content_type($saveFilePath));
$entity->setSize(filesize($saveFilePath));
$entity->setSequence($file_sequence);
$entity->setMediaHTML($this->getMediaTag($mediaType, $entity));
log_message("notice", __FUNCTION__ . " 원본 {$file_sequence}번째 " . $entity->getTitle() . " 작업 완료");
return $entity;
}
}