mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-24 12:07:23 +08:00
improve admin user profile + image hosting
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user