Automation init...3

This commit is contained in:
최준흠 2024-09-11 00:09:09 +09:00
parent 24bb1fca5d
commit d1f462bce3
7 changed files with 100 additions and 58 deletions

View File

@ -12,17 +12,18 @@ class YamapController extends CommonController
{
try {
$fullPath = WRITEPATH . "uploads" . DIRECTORY_SEPARATOR . getenv('yamap.storage.upload.path');
$image = new ImageLibrary(
$fullPath,
$fullPath . DIRECTORY_SEPARATOR . "small"
);
$image = new ImageLibrary();
if (in_array("debug", $params)) {
$image->setDebug(true);
}
//디렉토리 생성
$image->makeDirectory($image->getDestinationPath());
$image->setSourcePath($fullPath);
$image->setDestinationPath($fullPath);
foreach ($image->getFilesByExtentionType($image->getSourcePath()) as $file) {
$image->make_small_image($file);
//저장파일명
$fileInfos = pathinfo($image->getDestinationPath() . DIRECTORY_SEPARATOR . $file, PATHINFO_ALL);
$dstFile = $fileInfos['filename'] . "_small." . $fileInfos['extension'];
$image->setDestinationFile($dstFile);
$image->create($file);
}
log_message("notice", "Crawler->" . __FUNCTION__ . " 작업이 완료되었습니다.");
return "완료되었습니다.";

View File

@ -44,6 +44,7 @@ class WebLibrary extends MySocketLibrary
if (!($this->isContainsHttpOrHttps($url))) {
$url = $this->gethost() . $url;
}
log_message("debug", "Socket URL-> " . $url);
$response = $this->getClient()->get($url, $options);
if ($response->getStatusCode() != 200) {
throw new \Exception("error", "{$url} 접속실패: " . $response->getStatusCode());
@ -80,7 +81,6 @@ class WebLibrary extends MySocketLibrary
if (!is_array($fileNames) || !count($fileNames)) {
throw new \Exception("Socket URL Error:" . $this->getHost() . $url);
}
log_message("debug", "Socket URL-> " . $this->getHost() . $url);
$fileName = array_pop($fileNames);
if (!$this->getDebug()) {
$content = $this->getContent($url, [

View File

@ -9,6 +9,7 @@ class FileLibrary extends MyStorageLibrary
{
use FileTrait;
private $_path = "";
private $_imageLibrary = null;
public function __construct(string $path)
{
parent::__construct();
@ -60,10 +61,11 @@ class FileLibrary extends MyStorageLibrary
$this->makeDirectory($fullPath);
$saveFilePath = $fullPath . DIRECTORY_SEPARATOR . $fileName;
if (file_exists($saveFilePath)) {
throw new \Exception(__FUNCTION__ . "이미 존재하는 파일:{$saveFilePath}");
throw new \Exception(__FUNCTION__ . " 이미 존재하는 파일:{$saveFilePath}");
}
//원본이미지 저장
if (!file_put_contents($saveFilePath, $content)) {
throw new \Exception(__FUNCTION__ . "파일저장 실패:{$saveFilePath}");
throw new \Exception(__FUNCTION__ . " 파일저장 실패:{$saveFilePath}");
}
$entity = $this->createFileEntity();
$entity->setPath($this->getPath());

View File

@ -2,12 +2,11 @@
namespace App\Libraries\MyStorage;
use App\Entities\Mangboard\FileEntity;
use App\Entities\Mangboard\UserEntity;
use App\Libraries\MyStorage\FileLibrary;
use App\Libraries\MyUtil\ImageLibrary;
use App\Libraries\MyStorage\FileLibrary;
use App\Models\Mangboard\FileModel;
use App\Entities\Mangboard\UserEntity;
use App\Entities\Mangboard\FileEntity;
class MangboardLibrary extends FileLibrary
{
@ -77,25 +76,37 @@ class MangboardLibrary extends FileLibrary
{
return new FileEntity();
}
private function create_small_image(FileEntity $entity, int $file_sequence): FileEntity
private function create_small_image(FileEntity $entity, string $file, int $width = 480, int $height = 319): FileEntity
{
$fullPath = WRITEPATH . $this->getUploadPath() . DIRECTORY_SEPARATOR . $this->getPath();
$image = new ImageLibrary(
$fullPath,
$fullPath . DIRECTORY_SEPARATOR . "small"
);
$image = new ImageLibrary();
$image->setDebug($this->getDebug());
//Small 디렉토리 생성
$image->makeDirectory($image->getDestinationPath());
if (!$image->make_small_image($entity->getTitle())) {
log_message("notice", __FUNCTION__ . " {$file_sequence}번째 " . $entity->getTitle() . " 작업 완료");
}
$image->setSourcePath($fullPath);
$image->setDestinationPath($fullPath);
//저장파일명
$fileInfos = pathinfo($image->getDestinationPath() . DIRECTORY_SEPARATOR . $file, PATHINFO_ALL);
$dstFile = $fileInfos['filename'] . "_small." . $fileInfos['extension'];
$image->setDestinationFile($dstFile);
//작은이미지 생성후 목적지 Path와 파일명을 다시 file_path에 넣는다.
$result = $image->create($file, $width, $height);
// if ($result) {
// //URL이 되기때문에 /로 넣어야함
// $entity->setPath(sprintf(
// "/%s/%s/%s",
// $this->getUploadPath(),
// $entity->getPath(),
// $image->getDestinationFile()
// ));
// }
return $entity;
}
//망보드 파일관리 table에 등록
private function create_db(FileEntity $entity, int $file_sequence): FileEntity
{
// log_message("debug", $this->getModel()->getTable() . " Table에 {$file_sequence}번째 등록 준비->{$entity->getTitle()}|{$entity->getMimeType()}");
// log_message("notice", __FUNCTION__ . " {$file_sequence}번째 작업 시작");
$entity->user_pid = $this->getUser()->getPK();
$entity->user_name = $this->getUser()->getTitle();
$entity->board_name = $this->getBoardName();
@ -103,9 +114,8 @@ class MangboardLibrary extends FileLibrary
$entity->reg_date = date("Y-m-d H:i:s");
$entity->file_caption = $entity->getTitle();
$entity->file_alt = $entity->getTitle();
//mb_files에서 file_path가 망보드 게시판 파일관리에서 image로 표시되어 file_path+file_name로 설정
$entity->file_path = $entity->getPath() . DIRECTORY_SEPARATOR . $entity->getTitle();
$entity->file_description = "Filedata";
// log_message("debug", "\n-----Entity Value-----\n" . var_export($entity->toArray(), true) . "\n---------------------------\n");
$entity = $this->getModel()->create($entity);
log_message("notice", __FUNCTION__ . " {$file_sequence}번째 작업 완료");
return $entity;
@ -113,9 +123,18 @@ class MangboardLibrary extends FileLibrary
public function save(string $fileName, string $mediaType, string $content, int $file_sequence): FileEntity
{
$entity = parent::save($fileName, $mediaType, $content, $file_sequence);
$entity = $this->create_db($$entity, $file_sequence);
$entity = $this->create_small_image($entity, $file_sequence);
return $entity;
//mb_files에서 file_path가 망보드 게시판 파일관리에서 image로 표시되어 file_path+file_name로 설정
//원본이미지 생성후 목적지 Path와 파일명을 다시 file_path에 넣는다.
//URL이 되기때문에 /로 넣어야함
$entity->setPath(sprintf(
"/%s/%s/%s",
$this->getUploadPath(),
$entity->getPath(),
$entity->getTitle()
));
//작은이미지 생성
$entity = $this->create_small_image($entity, $fileName);
return $this->create_db($entity, $file_sequence);
}
public function setBoardPID(array $fileEntitys, int $board_pid)
{
@ -123,7 +142,7 @@ class MangboardLibrary extends FileLibrary
foreach ($fileEntitys as $fileEntity) {
$fileEntity->board_pid = $board_pid;
//수정시 mb_files에서 file_path가 변경하지 못하게 하기위함
$this->getModel()->setFields($this->getModel()->getFields(['file_path']));
$this->getModel()->setFields($this->getModel()->getFields("except", ['file_path']));
$this->getModel()->modify($fileEntity);
}
log_message("notice", __FUNCTION__ . " 작업 완료");

View File

@ -10,23 +10,39 @@ class ImageLibrary extends MyUtilLibrary
use FileTrait;
private $_srcPath = "";
private $_dstPath = "";
private $_dstFile = "";
private $_image;
private $_imageType;
public function __construct(string $srcPath, string $dstPath)
public function __construct()
{
parent::__construct();
$this->_srcPath = $srcPath;
$this->_dstPath = $dstPath;
}
// 이미지의 현재 너비를 반환하는 메소드
public function getSourcePath(): string
{
return $this->_srcPath;
}
public function setSourcePath(string $srcPath): void
{
$this->_srcPath = $srcPath;
}
public function getDestinationPath(): string
{
return $this->_dstPath;
}
public function setDestinationPath(string $dstPath): void
{
$this->_dstPath = $dstPath;
}
public function getDestinationFile(): string
{
return $this->_dstFile;
}
public function setDestinationFile(string $dstFile): void
{
$this->_dstFile = $dstFile;
}
public function getWidth()
{
return imagesx($this->_image);
@ -107,21 +123,23 @@ class ImageLibrary extends MyUtilLibrary
imagedestroy($this->_image);
}
public function make_small_image(string $file, int $width = 480, int $height = 319): bool
public function create(string $file, int $width = 480, int $height = 319): bool
{
if (!$this->isFileType($this->getSourcePath(), $file)) {
log_message("warning", "{$file} Image 형식파일이 아닙니다.");
return false;
}
//소스파일
$srcfile = $this->getSourcePath() . DIRECTORY_SEPARATOR . $file;
$fileInfos = pathinfo($srcfile, PATHINFO_ALL);
//저장파일
$dstfile = $this->getDestinationPath() . DIRECTORY_SEPARATOR . $fileInfos['filename'] . "_small." . $fileInfos['extension'];
$this->load($srcfile); // 이미지 파일 로드
$this->resize($width, $height); // 200x200으로 이미지 크기 조정
$this->save($dstfile); // 저장
$this->destroy(); // 메모리 해제
log_message("notice", __FUNCTION__ . " 작업 완료");
//저장할 디렉토리 생성
$this->makeDirectory($this->getDestinationPath());
// 이미지 파일 로드
$this->load($this->getSourcePath() . DIRECTORY_SEPARATOR . $file);
// 200x200으로 이미지 크기 조정
$this->resize($width, $height);
// 파일 저장
$this->save($this->getDestinationPath() . DIRECTORY_SEPARATOR . $this->getDestinationFile());
// 메모리 해제
$this->destroy();
log_message("notice", __FUNCTION__ . " Image 작업 완료");
return true;
}
}

View File

@ -65,19 +65,26 @@ abstract class CommonModel extends Model
{
return $this->_action = $action;
}
final public function getFields(array $options = []): array
final public function getFields(string $key = "", $fields = []): array
{
if (isset($options['except'])) {
return array_diff_key($this->allowedFields, array_flip($options['except']));
} elseif (isset($options['only'])) {
return array_intersect_key($this->allowedFields, array_flip($options['only']));
}
$this->allowedFields = array_filter(
$this->allowedFields,
function ($value) use (&$key, &$fields) {
if ($key == 'except') {
return !in_array($value, $fields);
} elseif ($key == 'only') {
return in_array($value, $fields);
} else {
return $value;
}
}
);
return $this->allowedFields;
}
final public function setFields(array $fields): void
{
$this->allowedFields = $fields;
//array_unshift는 array의 맨앞에 넣기위함
//수동입력인경우 PrimaryKey추가 (array_unshift는 array의 맨앞에 넣기위함)
if (!$this->useAutoIncrement) {
array_unshift($this->allowedFields, $this->getPKField());
}
@ -188,11 +195,9 @@ abstract class CommonModel extends Model
$this->setAction(DB_ACTION['CREATE']);
//Field에 맞는 Validation Rule 재정의
$this->setValidationRules($this->getFieldRules($this->getFields()));
//primaryKey가 수동입력이면
//수동입력인경우
$pkField = $this->getPKField();
if (!$this->useAutoIncrement && $entity->$pkField === null) {
//PrimayKey Field를 allowedFields의 맨앞에 넣기 -> array_unshif
array_unshift($this->allowedFields, $this->getPKField());
$entity = $this->convertEntityData($entity, $this->getPKField());
}
$entity = $this->save_process($entity);

View File

@ -107,9 +107,6 @@ class FileModel extends CommonModel
return $entity;
}
switch ($field) {
case "file_path":
$entity->$field .= DIRECTORY_SEPARATOR . $entity->getTitle();
break;
default:
$entity = parent::convertEntityData($entity, $field);
break;