improve admin user profile + image hosting

This commit is contained in:
xiaomlove
2024-12-29 22:16:41 +08:00
parent f146a654c2
commit 5a9f1f1017
40 changed files with 22110 additions and 456 deletions
+55
View File
@@ -0,0 +1,55 @@
<?php
namespace Nexus\Attachment\Drivers;
use GuzzleHttp\Psr7;
use Nexus\Attachment\Storage;
class Chevereto extends Storage {
function upload(string $filepath): string
{
$api = get_setting("image_hosting_chevereto.upload_api_endpoint");
$token = get_setting("image_hosting_chevereto.upload_token");
$logPrefix = "filepath: $filepath, api: $api, token: $token";
$httpClient = new \GuzzleHttp\Client();
$response = $httpClient->request('POST', $api, [
'headers' => [
'X-API-Key' => sprintf('%s', $token),
],
'multipart' => [
[
'name' => 'source',
'contents' => Psr7\Utils::tryFopen($filepath, 'r')
]
]
]);
$statusCode = $response->getStatusCode();
$logPrefix .= ", status code: $statusCode";
if ($statusCode != 200) {
do_log("$logPrefix, statusCode != 200", "error");
throw new \Exception("Unable to upload file, status code {$statusCode}");
}
$stringBody = (string)$response->getBody();
$logPrefix .= ", body: $stringBody";
$result = json_decode($stringBody, true);
if (!is_array($result)) {
do_log("$logPrefix, can not parse to array", "error");
throw new \Exception("Unable to parse response body");
}
if (!isset($result["image"]["url"])) {
do_log("$logPrefix, no image url", "error");
throw new \Exception("upload fail: " . ($result["error"]["message"] ?? ""));
}
return $result["image"]["url"];
}
function getBaseUrl(): string
{
return get_setting("image_hosting_chevereto.base_url");
}
function getDriverName(): string
{
return static::DRIVER_CHEVERETO;
}
}
+22
View File
@@ -0,0 +1,22 @@
<?php
namespace Nexus\Attachment\Drivers;
use Nexus\Attachment\Storage;
class Local extends Storage {
function upload(string $filepath): string
{
throw new \RuntimeException("Not implemented");
}
function getBaseUrl(): string
{
return sprintf("%s/%s", getSchemeAndHttpHost(), trim(get_setting("attachment.httpdirectory"), '/'));
}
function getDriverName(): string
{
return static::DRIVER_LOCAL;
}
}
+64
View File
@@ -0,0 +1,64 @@
<?php
namespace Nexus\Attachment\Drivers;
use GuzzleHttp\Psr7;
use Nexus\Attachment\Storage;
class Lsky extends Storage {
function upload(string $filepath): string
{
$api = get_setting("image_hosting_lsky.upload_api_endpoint");
$token = get_setting("image_hosting_lsky.upload_token");
$logPrefix = "filepath: $filepath, api: $api, token: $token";
$httpClient = new \GuzzleHttp\Client();
$response = $httpClient->request('POST', $api, [
'headers' => [
'Authorization' => sprintf('Bearer %s', $token),
],
'multipart' => [
[
'name' => 'file',
'contents' => Psr7\Utils::tryFopen($filepath, 'r')
]
]
]);
$statusCode = $response->getStatusCode();
$logPrefix .= ", status code: $statusCode";
if ($statusCode != 200) {
do_log("$logPrefix, statusCode != 200", "error");
throw new \Exception("Unable to upload file, status code {$statusCode}");
}
$stringBody = (string)$response->getBody();
$logPrefix .= ", body: $stringBody";
$result = json_decode($stringBody, true);
if (!is_array($result)) {
do_log("$logPrefix, can not parse to array", "error");
throw new \Exception("Unable to parse response body");
}
if (!isset($result["status"])) {
do_log("$logPrefix, no status", "error");
throw new \Exception("Unable to parse response body, no status");
}
if ($result["status"] !== true) {
do_log("$logPrefix, status != true", "error");
throw new \Exception("upload fail: " . $result["message"]);
}
if (!isset($result["data"]["links"]["url"])) {
do_log("$logPrefix, no links url", "error");
throw new \Exception("upload fail: no links url");
}
return $result["data"]["links"]["url"];
}
function getBaseUrl(): string
{
return get_setting("image_hosting_lsky.base_url");
}
function getDriverName(): string
{
return static::DRIVER_LSKY;
}
}
+65
View File
@@ -0,0 +1,65 @@
<?php
namespace Nexus\Attachment;
use Nexus\Attachment\Drivers\Chevereto;
use Nexus\Attachment\Drivers\Local;
use Nexus\Attachment\Drivers\Lsky;
abstract class Storage {
private static array $drivers = [];
const DRIVER_LOCAL = 'local';
const DRIVER_CHEVERETO = 'chevereto';
const DRIVER_LSKY = 'lsky';
/**
* upload to remote and return full url
*
* @param string $filepath
* @return string
*/
abstract function upload(string $filepath): string;
abstract function getBaseUrl(): string;
abstract function getDriverName(): string;
public function uploadGetLocation(string $filepath): string
{
$url = $this->upload($filepath);
return $this->trimBaseUrl($url);
}
public function getImageUrl(string $location): string
{
return sprintf('%s/%s', trim($this->getBaseUrl(), '/'), trim($location, '/'));
}
protected function trimBaseUrl(string $url): string
{
$baseUrl = trim($this->getBaseUrl(), '/') . "/";
if (str_starts_with($url, $baseUrl)) {
return substr($url, strlen($baseUrl));
}
return $url;
}
public static function getDriver(string $name = null): Storage
{
$driver = $name ?: get_setting("image_hosting.driver");
if (isset(self::$drivers[$driver])) {
return self::$drivers[$driver];
}
$result = null;
if ($driver == self::DRIVER_CHEVERETO) {
$result = new Chevereto();
} else if ($driver == self::DRIVER_LSKY) {
$result = new Lsky();
} else if ($driver == self::DRIVER_LOCAL) {
$result = new Local();
}
if ($result) {
return self::$drivers[$driver] = $result;
}
throw new \Exception("Unsupported driver: $driver");
}
}