75 lines
2.5 KiB
PHP
75 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries\MyStorage;
|
|
|
|
use App\Entities\MyStorage\FileEntity;
|
|
|
|
class FileLibrary extends MyStorageLibrary
|
|
{
|
|
private $_path = "";
|
|
public function __construct(string $path)
|
|
{
|
|
parent::__construct();
|
|
$this->_path = $path;
|
|
}
|
|
final public function getPath(): string
|
|
{
|
|
return $this->_path;
|
|
}
|
|
|
|
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();
|
|
if (!is_dir($fullPath)) {
|
|
if (!mkdir($fullPath)) {
|
|
throw new \Exception("디렉토리 생성 실패:{$fullPath}");
|
|
}
|
|
}
|
|
$saveFilePath = $fullPath . DIRECTORY_SEPARATOR . $fileName;
|
|
if (file_exists($saveFilePath)) {
|
|
throw new \Exception("이미 존재하는 파일:{$saveFilePath}");
|
|
}
|
|
if (!file_put_contents($saveFilePath, $content)) {
|
|
throw new \Exception("파일저장 실패:{$saveFilePath}");
|
|
}
|
|
$entity = new FileEntity();
|
|
$entity->setPath($this->getPath());
|
|
$entity->setTitle($fileName);
|
|
$entity->setMimeType(mime_content_type($saveFilePath));
|
|
$entity->setSize(filesize($saveFilePath));
|
|
$entity->setMediaHTML($this->getMediaTag($mediaType, $entity));
|
|
log_message("debug", "{$file_sequence}번째 파일저장 완료->{$entity}");
|
|
return $entity;
|
|
}
|
|
}
|