Продолжение разработки 23.10.31

Добавлен .gitignore, скрыты несколько нинужных файлов, в целом продолжен запил основных частей функционала, начат микрорефакторинг (теперь концентрация индусского кода будет чуть меньше).
This commit is contained in:
2023-10-31 21:57:17 +03:00
parent e487ed79c4
commit 12143c148d
15 changed files with 608 additions and 166 deletions

View File

@@ -1,25 +1,96 @@
<?php // Create new post
// Includes
require_once("../_auth.php");
require_once("../_utils.php");
require_once("../_types.php");
require_once("../user/index.php");
// Create single publication
function Post_Create ($author, $tags, $pic_path, $title = null, $prev_path = null, $comms_enabled = false, $edit_lock = false) {
/*
* FUNCTION
* Check if image size properties are valid
*/
function ImageSizeIsValid ($x, $y) {
global $Config;
return ($x <= $Config["media"]["max_pic_res"]["x"])
&& ($y <= $Config["media"]["max_pic_res"]["y"])
&& (GetRatio($x, $y) <= $Config["media"]["max_pic_res"]["ratio"]);
}
/*
* FUNCTION
* Create preview version of image
*/
function Post_CreatePreviewFromImage ($src, $dst) {
$img = null;
// Reading image from source path
switch (mime_content_type($src)) {
case "image/jpeg":
$img = imagecreatefromjpeg($src);
break;
case "image/png":
$img = imagecreatefrompng($src);
break;
default:
throw new Exception("invalid mime type");
}
// Saving it as LQ JPEG
imagejpeg($img, $dst, 30);
}
/*
* FUNCTION
* Store image
*/
function Post_StoreImage ($path, $preview = true) {
global $Config;
// Paths
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
$fileName = strval(time()) . "_" . GenerateRandomString(4);
$targetDir = "../../" . $Config["media"]["pics_path"];
$targetPath = JoinPaths($targetDir, $fileName . "." . $ext);
// Creating preview file
if ($Config["media"]["previews_enabled"] && $preview) {
$previewDir = "../../" . $Config["media"]["prevs_path"];
$previewPath = JoinPaths($previewDir, $fileName . ".jpg");
Post_CreatePreviewFromImage($path, $previewPath);
}
move_uploaded_file($path, $targetPath);
}
/*
* METHOD
* Create single publication
*/
function Post_Create ($author, $tags, $pic_path, $title = null, $prev_path = null, $comms_enabled = false, $edit_lock = false): ReturnT {
global $db;
// $s = $db->prepare("INSERT ...");
// $s->bind_param("s", $author);
// $s->execute();
// $d = $s->get_result()->fetch_assoc();
//
// if (!(bool)$d) {
// return null;
// }
$result = null;
return $result;
// Check post params
// Author ID should exist
if (!User_IDExist($author))
// TODO
// Performing SQL query
$s = $db->prepare("INSERT INTO posts (author_id,comment_section_id,tags,title,pic_path,preview_path,comments_enabled,edit_lock) VALUES (?,?,?,?,?,?,?,?)");
$s->bind_param("ssssssss", $author, null, $tags, $title, $pic_path, $prev_path, $comms_enabled, $edit_lock);
if ($s->execute() !== true) {
return new ReturnT(null, 601, "failed to create post record in DB");
}
return new ReturnT($result);
}
@@ -54,8 +125,16 @@ if (ThisFileIsRequested(__FILE__)) {
// Check if resolution is bigger than allowed or have unacceptable aspect ratio
list($SzX, $SzY, $Type, $Attr) = getimagesize($TmpFilePath);
if ($SzX > $Config["media"]["max_pic_res"]["x"] || $SzY > $Config["media"]["max_pic_res"]["y"] || (GetAspectRatio($SzX, $SzY) > $Config["media"]["max_pic_res"]["ratio"]))
if (!Post_ImageIsValid($SzX, $SzY))
ReturnJSONError($Err_DP_ImageWrongRes, "image with that resolution or aspect ratio cant be accepted");
// TODO: delete image if unacceptable
// Copy picture to storage folder
Post_StoreImage($TmpFilePath, $Config)
// Create post
//$success = Post_Create(
}
?>