Automation init...2

This commit is contained in:
최준흠 2024-09-08 18:45:08 +09:00
parent eaf7986815
commit 2ab886919c
9 changed files with 116 additions and 67 deletions

View File

@ -16,4 +16,14 @@ class FileEntity extends ParentEntity
{
return $this->attributes['pid'];
}
//한게시물에 여러개가 있을경우 번호
final public function getSequence(): int
{
return $this->attributes['file_sequence'];
}
public function setSequence(int $file_sequence): void
{
$this->attributes['file_sequence'] = $file_sequence;
}
}

View File

@ -3,16 +3,18 @@
namespace App\Libraries\Mangboard;
use App\Libraries\MyStorage\FileLibrary as ParentLibrary;
use App\Models\Mangboard\FileModel;
use App\Libraries\MyStorage\FileLibrary as ParentLibrary;
use App\Entities\Mangboard\UserEntity;
use App\Entities\Mangboard\FileEntity;
use App\Entities\Mangboard\BoardEntity;
class FileLibrary extends ParentLibrary
{
private $_user = null;
private $_boardName = "";
private $_boardTable = "";
private $_boardLevel = 1;
private $_model = null;
public function __construct(string $path)
{
@ -69,24 +71,46 @@ class FileLibrary extends ParentLibrary
{
$this->_boardTable = $boardTable;
}
public function getBoardLevel(): int
{
if ($this->_boardLevel === null) {
throw new \Exception("BoardModel Level이 지정되지 않았습니다.");
}
return $this->_boardLevel;
}
public function setBoardLevel(string $boardLevel): void
{
$this->_boardLevel = $boardLevel;
}
public function save($content): null|FileEntity
public function save(string $fileName, string $mediaType, string $content, int $file_sequence)
{
//망보드 파일관리 table에 등록
try {
$entity = parent::save($content);
if ($entity === null) {
$entity = parent::save($fileName, $mediaType, $content, $file_sequence);
if (!$entity) {
return null;
}
$entity = $this->getEntity();
$entity->user_pid = $this->getUser()->getPK();
$entity->user_name = $this->getUser()->getTitle();
$entity->board_name = $this->getBoardName();
$entity->table_name = $this->getBoardTable();
$entity->reg_date = date("Y-m-d H:i:s");
return $this->getModel()->create($entity);
$entity->file_description = "Filedata";
$entity->setSequence($file_sequence);
$entity = $this->getModel()->create($entity);
return $entity;
} catch (\Exception $e) {
log_message("error", $e->getMessage());
return null;
}
}
public function updateFileEntityBoardPK(FileEntity $entity, BoardEntity $boardEntity): FileEntity
{
$entity->board_pid = $boardEntity->getPK();
return $this->getModel()->modify($entity);
}
}

View File

@ -4,6 +4,7 @@ namespace App\Libraries\MyCrawler;
use Symfony\Component\DomCrawler\Crawler;
use App\Libraries\CommonLibrary;
use App\Entities\Mangboard\FileEntity;
abstract class MyCrawlerLibrary extends CommonLibrary
{
@ -35,57 +36,27 @@ abstract class MyCrawlerLibrary extends CommonLibrary
);
return $nodes;
}
protected function getMediaTag(string $mediaType): string
//Download한 파일 저장후 추가작업시 사용
protected function download_process($entity, int $file_sequence)
{
$mediaTag = "";
switch ($mediaType) {
case "image":
$mediaTag = sprintf(
"<img src=\"/%s/%s/%s\" alt=\"%s\">",
$this->getMyStorage()->getUploadPath(),
$this->getMyStorage()->getEntity()->getPath(),
$this->getMyStorage()->getEntity()->getTitle(),
$this->getMyStorage()->getEntity()->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>",
$this->getMyStorage()->getEntity()->getTitle(),
$this->getMyStorage()->getUploadPath(),
$this->getMyStorage()->getEntity()->getPath(),
$this->getMyStorage()->getEntity()->getTitle(),
$this->getMyStorage()->getEntity()->getMimeType(),
);
break;
}
return $mediaTag;
return $entity;
}
final protected function download(Crawler $crawler, array $options): array
final protected function download(string $mediaType, Crawler $crawler, array $options, array $entitys = []): array
{
$downloadInfos = [];
$nodes = $this->getNodes($crawler, $options);
$file_sequence = 1;
foreach ($nodes as $node) {
$url = $node->attr($options["attr"]);
$downloadInfos[] = $this->getMySocket()->download($url);
}
return $downloadInfos;
}
protected function save(string $mediaType, array $downloadInfos, $fileEntitys = []): array
{
foreach ($downloadInfos as $downloadInfo) {
$entity = $this->getMyStorage()->getEntity();
$entity->setTitle($downloadInfo['fileName']);
if (!$this->getMyStorage()->save($downloadInfo['content'])) {
list($fileName, $content) = $this->getMySocket()->download($node->attr($options["attr"]));
$entity = $this->getMyStorage()->save($fileName, $mediaType, $content, $file_sequence);
if ($entity === null) {
continue;
}
$entity->setMediaHTML($this->getMediaTag($mediaType));
$fileEntitys[] = $entity;
$entitys[] = $this->download_process($entity, $file_sequence);
$file_sequence++;
}
return $fileEntitys;
return $entitys;
}
}

View File

@ -34,8 +34,10 @@ class YamapLibrary extends MyCrawlerLibrary
{
if ($this->_myStorage === null) {
$this->_myStorage = new FileLibrary(getenv('yamap.storage.upload.path'));
//원래는 mb_board에서 해당Board정보를 읽어서 처리해아함
$this->_myStorage->setBoardName(getenv('yamap.storage.board.name'));
$this->_myStorage->setBoardTable($this->getBoardModel()->getTable());
$this->_myStorage->setBoardlevel(getenv('yamap.storage.board.level'));
}
return $this->_myStorage;
}
@ -77,11 +79,9 @@ class YamapLibrary extends MyCrawlerLibrary
{
$crawler = $this->getContent($url, getenv("yamap.view.content.tag"));
//3. Image 처리
$downloadInfos = $this->download($crawler, ["tag" => "img", "attr" => "src"]);
$fileEntitys = $this->save("image", $downloadInfos);
$fileEntitys = $this->download("image", $crawler, ["tag" => "img", "attr" => "src"]);
//4. Video(mp4) 처리
$downloadInfos = $this->download($crawler, ["tag" => "video", "attr" => "src"]);
return $this->save("video", $downloadInfos, $fileEntitys);
return $this->download("video", $crawler, ["tag" => "video", "attr" => "src"], $fileEntitys);
}
private function createBoard(array $item, array $fileEntitys): BoardEntity
@ -95,6 +95,7 @@ class YamapLibrary extends MyCrawlerLibrary
$entity->user_pid = $this->getMyStorage()->getUser()->getPK();
$entity->user_id = $this->getMyStorage()->getUser()->getID();
$entity->user_name = $item["nickname"] != "" ? $item["nickname"] : $this->getMyStorage()->getUser()->getTitle();
$entity->level = $this->getMyStorage()->getBoardLevel();
$entity->hit = $item['hit'];
$entity->reg_date = date("Y-m-d H:i:s", strtotime($item['date']));
$entity->data_type = "html";
@ -107,8 +108,14 @@ class YamapLibrary extends MyCrawlerLibrary
// echo $entity->hit . "\n";
// echo $entity->reg_date . "\n";
// exit;
//망보드에 넣기
return $board->create($entity);
//망보드 게시판에 등록
$entity = $board->create($entity);
//망보드 파일관리툴에 등록된 파일게시물에 등록한 게시판번호 수정하기
foreach ($fileEntitys as $fileEntity) {
$this->getMyStorage()->updateFileEntityBoardPK($fileEntity, $entity);
}
return $entity;
}
public function execute(): void

View File

@ -97,6 +97,6 @@ class WebLibrary extends MySocketLibrary
}
log_message("notice", "{$fileName} 파일이 다운로드되었습니다!");
}
return ["fileName" => $fileName, "content" => $content];
return array($fileName, $content);
}
}

View File

@ -27,7 +27,37 @@ class FileLibrary extends MyStorageLibrary
return $this->_entity;
}
public function save($content): null|FileEntity
private function getMediaTag(string $mediaType): string
{
$mediaTag = "";
switch ($mediaType) {
case "image":
$mediaTag = sprintf(
"<img src=\"/%s/%s/%s\" alt=\"%s\">",
$this->getUploadPath(),
$this->getEntity()->getPath(),
$this->getEntity()->getTitle(),
$this->getEntity()->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>",
$this->getEntity()->getTitle(),
$this->getUploadPath(),
$this->getEntity()->getPath(),
$this->getEntity()->getTitle(),
$this->getEntity()->getMimeType(),
);
break;
}
return $mediaTag;
}
public function save(string $fileName, string $mediaType, string $content, int $file_sequence)
{
$fullPath = WRITEPATH . $this->getUploadPath() . DIRECTORY_SEPARATOR . $this->getPath();
if (!is_dir($fullPath)) {
@ -35,14 +65,16 @@ class FileLibrary extends MyStorageLibrary
throw new \Exception("Make Directory Error:" . $fullPath);
}
}
$entity = $this->getEntity();
$saveFile = $fullPath . DIRECTORY_SEPARATOR . $entity->getTitle();
log_message("debug", "Storage Save-> " . $saveFile);
if (!file_put_contents($saveFile, $content)) {
return null;
$saveFilePath = $fullPath . DIRECTORY_SEPARATOR . $fileName;
log_message("debug", "Storage Save-> " . $saveFilePath);
if (!file_put_contents($saveFilePath, $content)) {
return false;
}
//File형식에 따른 MimeType 지정
$entity->setMimeType(mime_content_type($saveFile));
$entity = $this->getEntity();
$entity->setTitle($fileName);
$entity->setMimeType(mime_content_type($saveFilePath));
$entity->setMediaHTML($this->getMediaTag($mediaType));
return $entity;
}
}

View File

@ -3,7 +3,6 @@
namespace App\Libraries\MyStorage;
use App\Libraries\CommonLibrary;
use App\Entities\MyStorage\FileEntity;
abstract class MyStorageLibrary extends CommonLibrary
{
@ -13,7 +12,7 @@ abstract class MyStorageLibrary extends CommonLibrary
parent::__construct();
}
abstract public function save($content): null|FileEntity;
abstract public function save(string $fileName, string $mediaType, string $content, int $file_sequence);
final public function getUploadPath(): string
{
return $this->_uploadPath;

View File

@ -75,7 +75,7 @@ class BoardModel extends CommonModel
public function __construct(string $table)
{
$this->table = $table;
$fields = ["title", "user_pid", "user_id", "user_name", "data_type", "editor_type", "reg_date", "hit", "content"];
$fields = ["title", "user_pid", "user_id", "user_name", "level", "data_type", "editor_type", "reg_date", "hit", "content"];
parent::__construct($fields);
}
public function getTitleField(): string
@ -101,6 +101,7 @@ class BoardModel extends CommonModel
$rules[$field] = "required|trim|string";
break;
case 'hit':
case 'level':
case 'user_pid':
$rules[$field] = "if_exist|numeric";
break;

View File

@ -38,7 +38,7 @@ class FileModel extends CommonModel
public function __construct()
{
$fields = ["user_pid", "user_name", "board_name", "table_name", "file_name", "file_path", "file_type", "reg_date"];
$fields = ["user_pid", "user_name", "board_pid", "board_name", "table_name", "file_name", "file_path", "file_type", "file_description", "file_sequence", "reg_date"];
parent::__construct($fields);
}
public function getTitleField(): string
@ -48,7 +48,9 @@ class FileModel extends CommonModel
public function getFieldRule(string $field, array $rules): array
{
switch ($field) {
case "board_pid":
case "user_pid":
case "file_sequence":
$rules[$field] = "if_exist|numeric";
break;
case "board_name":
@ -58,6 +60,9 @@ class FileModel extends CommonModel
case "file_type":
$rules[$field] = "required|trim|string";
break;
case "file_description":
$rules[$field] = "if_exist|trim|string";
break;
case "reg_date":
$rules[$field] = "if_exist|valid_date";
break;