]> _ Git - fluidbook-toolbox.git/commitdiff
wip #7868 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 30 Jan 2026 16:14:31 +0000 (17:14 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 30 Jan 2026 16:14:31 +0000 (17:14 +0100)
app/Fluidbook/Compiler/Links.php
app/Fluidbook/LinkShortener.php [deleted file]
app/Models/LinkShortener.php
app/Models/Traits/PublicationSettings.php

index 35e78b02e715cb8ff5d3c9bd75d60548b79551ea..4ee4a8750c8f8ff18424b3def57deeffc8693803 100644 (file)
@@ -2,11 +2,9 @@
 
 namespace App\Fluidbook\Compiler;
 
-use App\Fluidbook\Farm;
 use App\Fluidbook\Link\Link;
 use App\Fluidbook\Link\LinksData;
-use App\Fluidbook\LinkShortener;
-use App\Jobs\OCR;
+use App\Models\LinkShortener;
 use App\SubForms\Link\Base;
 use Cubist\Util\Text;
 use Cubist\Util\Url;
diff --git a/app/Fluidbook/LinkShortener.php b/app/Fluidbook/LinkShortener.php
deleted file mode 100644 (file)
index cc14aec..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-<?php
-
-namespace App\Fluidbook;
-
-use GuzzleHttp\Client;
-
-class LinkShortener
-{
-
-    public static function getAvaiableShorteners()
-    {
-        $res = [];
-        foreach (\App\Models\LinkShortener::all() as $server) {
-            $res[$server->id] = $server->url;
-        }
-        return $res;
-    }
-
-    public static function shorturl($url, $shortener)
-    {
-        return cache()->remember('shorturl_' . $shortener . '_' . $url, 3600, function () use ($url, $shortener) {
-            $res = self::_request('shorturl', ['url' => $url], $shortener);
-            if ($res->shorturl) {
-                return $res->shorturl;
-            }
-        });
-    }
-
-    protected static function _request($action, $data, $shortener, $format = 'json')
-    {
-        if (!isset(self::$shorteners[$shortener])) {
-            throw new \Exception("shortener " . $shortener . " not available");
-        }
-
-        $apiURL = 'https://' . $shortener . '/yourls-api.php';
-        $data['action'] = $action;
-        $data['format'] = $format;
-        $data['hash'] = 'sha512';
-        $data['timestamp'] = time();
-        $data['signature'] = hash($data['hash'], $data['timestamp'] . self::$shorteners[$shortener]);
-
-        $client = new Client();
-        $response = $client->request('POST', $apiURL, ['form_params' => $data, 'http_errors' => false]);
-        return json_decode($response->getBody());
-    }
-}
index c7e2191347e0bdef76b2151dd94e688fd326a651..ce41540f623a69acab4922842974436b2b4e50f8 100644 (file)
@@ -4,19 +4,22 @@ namespace App\Models;
 
 use App\Models\Base\ToolboxModel;
 use Cubist\Backpack\Magic\Fields\Text;
+use GuzzleHttp\Client;
 
 class LinkShortener extends ToolboxModel
 {
     protected $table = 'link_shortener';
     protected $_options = ['name' => 'linkshortener',
-        'singular' => 'serveur de liens courts',
-        'plural' => 'serveurs de liens courts',];
+        'singular' => 'raccourcisseur de liens',
+        'plural' => 'raccourcisseurs de liens',];
 
     protected $_enableRevisions = false;
 
     protected static $_permissionBase = 'linkshortener';
     protected static $_ownerAttribute = 'owner';
 
+    protected static $_cache = [];
+
     public function setFields()
     {
         parent::setFields();
@@ -24,6 +27,56 @@ class LinkShortener extends ToolboxModel
         $this->addField('url', Text::class, __('URL'), ['column' => true]);
         $this->addOwnerField();
         $this->addField('api_key', Text::class, __('ClĂ© d\'API'));
+    }
+
+    public static function getAvaiableShorteners()
+    {
+        $res = [];
+        foreach (self::all() as $server) {
+            $res[$server->id] = $server->url;
+        }
+        return $res;
+    }
+
+    public static function getShortener($id)
+    {
+        if (!isset(self::$_cache[$id])) {
+            $s = self::withoutGlobalScopes()->find($id);
+            if ($s === null) {
+                self::$_cache[$id] = false;
+            } else {
+                self::$_cache[$id] = ['key' => $s->api_key, 'url' => $s->url];
+            }
+        }
+        return self::$_cache[$id];
+    }
+
+    public static function shorturl($url, $shortener)
+    {
+        return cache()->remember('shorturl_' . $shortener . '_' . $url, 3600, function () use ($url, $shortener) {
+            $res = self::_request('shorturl', ['url' => $url], $shortener);
+            if ($res->shorturl) {
+                return $res->shorturl;
+            }
+        });
+    }
+
+    protected static function _request($action, $data, $shortener, $format = 'json')
+    {
+        $s = self::getShortener($shortener);
+        if (!$s) {
+            throw new \Exception("shortener " . $shortener . " not available");
+        }
+
+        $apiURL = 'https://' . $s['url'] . '/yourls-api.php';
+        $data['action'] = $action;
+        $data['format'] = $format;
+        $data['hash'] = 'sha512';
+        $data['timestamp'] = time();
+        $data['signature'] = hash($data['hash'], $data['timestamp'] . $s['key']);
 
+        $client = new Client();
+        $response = $client->request('POST', $apiURL, ['form_params' => $data, 'http_errors' => false]);
+        return json_decode($response->getBody());
     }
 }
index 21595a181d98ed26c1fa880e0a0d8a87d783ec49..650ff5b7f321f5d4db0ba2903949b09f7cac4d7c 100644 (file)
@@ -4,14 +4,12 @@ namespace App\Models\Traits;
 
 use App\Fields\FluidbookDevelopmentVersion;
 use App\Fields\FluidbookFont;
-use App\Fields\FluidbookLocale;
 use App\Fields\FluidbookLocaleCurrent;
 use App\Fields\FluidbookSignature;
 use App\Fields\FluidbookTTSVoice;
 use App\Fields\SCORMVersion;
-use App\Fluidbook\LinkShortener;
-use App\Models\File;
 use App\Models\FluidbookExternalInstallServer;
+use App\Models\LinkShortener;
 use Cubist\Backpack\Magic\Fields\Checkbox;
 use Cubist\Backpack\Magic\Fields\Code;
 use Cubist\Backpack\Magic\Fields\Email;