refactor: ♻️ refactor out post-type specific code
This commit is contained in:
parent
9e855fba36
commit
38a5056ff0
3 changed files with 129 additions and 139 deletions
101
www/post_adapter.php
Normal file
101
www/post_adapter.php
Normal file
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
require_once 'mysql_adapter.php';
|
||||
|
||||
use Spatie\YamlFrontMatter\YamlFrontMatter;
|
||||
|
||||
class PostHandler extends MySQLAdapter {
|
||||
public $data_directory;
|
||||
|
||||
function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
$this->data_directory = 'raw';
|
||||
}
|
||||
|
||||
function _normalize_post_data($post_data) {
|
||||
$post_data = parent::_normalize_post_data($post_data);
|
||||
|
||||
$post_data["post_basename"] = basename($post_data["post_path"]);
|
||||
|
||||
$post_meta = $post_data['post_metadata'];
|
||||
|
||||
$post_meta["title"] ??= basename($post_data["post_path"]);
|
||||
|
||||
if(!isset($post_meta['type'])) {
|
||||
$type = null;
|
||||
|
||||
$ext = pathinfo($post_data['post_basename'], PATHINFO_EXTENSION);
|
||||
|
||||
$ext_mapping = [
|
||||
'' => 'directory',
|
||||
'md' => 'text/markdown',
|
||||
'png' => 'image',
|
||||
];
|
||||
|
||||
if(isset($ext_mapping[$ext])) {
|
||||
$post_meta['type'] = $ext_mapping[$ext];
|
||||
}
|
||||
}
|
||||
|
||||
$post_data["post_file_dir"] = '/' . $this->data_directory . $post_data["post_path"];
|
||||
|
||||
$post_data['post_metadata'] = $post_meta;
|
||||
|
||||
return $post_data;
|
||||
}
|
||||
|
||||
function make_post_directory($directory) {
|
||||
$data_directory = $this->data_directory . $directory;
|
||||
|
||||
is_dir($data_directory) || mkdir($data_directory, 0777, true);
|
||||
|
||||
parent::make_post_directory($directory);
|
||||
}
|
||||
|
||||
function save_file($post_path, $file_path) {
|
||||
$this->bump_post($post_path);
|
||||
copy($file_path, $this->data_directory . $post_path);
|
||||
}
|
||||
|
||||
function save_markdown_post($post_path, $post_data) {
|
||||
$frontmatter_post = YamlFrontMatter::parse($post_data);
|
||||
$post_path = chop($post_path, '/');
|
||||
|
||||
$post_content = $frontmatter_post->body();
|
||||
$post_metadata = $frontmatter_post->matter();
|
||||
|
||||
if(basename($post_path) == "README.md") {
|
||||
|
||||
$readme_metadata = [];
|
||||
if(isset($post_metadata['directory_data'])) {
|
||||
$readme_metadata = $post_metadata['directory_data'];
|
||||
}
|
||||
|
||||
$this->update_or_create_post(dirname($post_path),
|
||||
$readme_metadata, $post_content);
|
||||
}
|
||||
|
||||
$this->update_or_create_post($post_path, $post_metadata, $post_content);
|
||||
}
|
||||
|
||||
function handle_upload($post_path, $file_path) {
|
||||
$ext = pathinfo($post_path, PATHINFO_EXTENSION);
|
||||
|
||||
switch($ext) {
|
||||
case "md":
|
||||
$this->save_markdown_post($post_path, file_get_contents($file_path));
|
||||
|
||||
file_put_contents($this->data_directory . $post_path,
|
||||
$post_data);
|
||||
break;
|
||||
case "mddesc":
|
||||
$this->save_markdown_post(chop($post_path, '.mddesc'), file_get_contents($file_path));
|
||||
break;
|
||||
default:
|
||||
$this->save_file($post_path, $file_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue