Skip to content

Commit

Permalink
Merge pull request #86 from luceos/dk/support-filesystem
Browse files Browse the repository at this point in the history
Fix: adds support for cloud filesystem
  • Loading branch information
jaspervriends authored Mar 1, 2024
2 parents cbbe884 + 80dbf65 commit c80eb61
Show file tree
Hide file tree
Showing 9 changed files with 4,356 additions and 9,774 deletions.
2 changes: 1 addition & 1 deletion js/dist/admin.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dist/admin.js.map

Large diffs are not rendered by default.

13,998 changes: 4,298 additions & 9,700 deletions js/package-lock.json

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
"name": "@v17development/flarum-seo",
"version": "0.0.0",
"dependencies": {
"flarum-webpack-config": "^0.1.0-beta.10"
"@flarum/prettier-config": "^1.0.0",
"flarum-tsconfig": "^1.0.2",
"flarum-webpack-config": "^2.0.0",
"webpack": "^5.70.0",
"webpack-cli": "^4.9.2"
},
"scripts": {
"build": "webpack --mode production",
"start": "webpack --mode development --watch"
},
"devDependencies": {
"flarum": "^0.1.0-beta.16",
"webpack": "^4.44.2",
"webpack-cli": "^3.3.12"

}
}
10 changes: 5 additions & 5 deletions js/src/admin/components/Forms/SeoSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Select from 'flarum/components/Select';
export default class SeoSettings extends Component {
oninit(vnode) {
super.oninit(vnode);

this.saving = false;

this.fields = [
Expand All @@ -33,7 +33,7 @@ export default class SeoSettings extends Component {

// Cheat 'seo_social_media_imageUrl'
// Todo: Find a better way
app.forum.data.attributes.seo_social_media_imageUrl = app.forum.attribute('baseUrl') + '/assets/' + app.data.settings.seo_social_media_image_path;
app.forum.data.attributes.seo_social_media_imageUrl = app.data.settings.seo_social_media_image_url;

this.showField = 'all';

Expand Down Expand Up @@ -74,7 +74,7 @@ export default class SeoSettings extends Component {
{"Enter one or more keywords that describes your forum."}
</div>,
<textarea className="FormControl" bidi={this.values.forum_keywords} placeholder="Add a few keywords" />,
<div className="helpText"
<div className="helpText"
style={{
color: countKeywords(this.values.forum_keywords()) == false ? "red" : null
}}>
Expand All @@ -93,7 +93,7 @@ export default class SeoSettings extends Component {
className: this.showField !== 'all' ? 'hidden' : ''
}, [
<div className="helpText">
When your forum is shared on Twitter, it will have an image (if a social media image has been set up). This can be a big card with a big image, or a small card (summary) with a smaller image.
When your forum is shared on Twitter, it will have an image (if a social media image has been set up). This can be a big card with a big image, or a small card (summary) with a smaller image.
</div>,
Select.component({
options: {
Expand Down Expand Up @@ -295,4 +295,4 @@ export default class SeoSettings extends Component {
m.redraw();
});
}
}
}
4 changes: 2 additions & 2 deletions js/src/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import app from 'flarum/app';
import { extend } from 'flarum/extend';
import DashboardPage from 'flarum/components/DashboardPage';
import SeoWidget from "./components/SeoWidget";
import SettingsPage from './pages/SettingsPage';
import SettingsPage from './Pages/SettingsPage';

app.initializers.add('v17development-flarum-seo', () => {
app.extensionData.for('v17development-seo').registerPage(SettingsPage);
Expand All @@ -11,4 +11,4 @@ app.initializers.add('v17development-flarum-seo', () => {
extend(DashboardPage.prototype, 'availableWidgets', widgets => {
widgets.add('seo-widget', <SeoWidget />, 500);
});
});
});
33 changes: 12 additions & 21 deletions src/Api/DeleteSocialMediaImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,37 @@
namespace V17Development\FlarumSeo\Api;

use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Filesystem\Cloud;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Psr\Http\Message\ServerRequestInterface;
use Laminas\Diactoros\Response\EmptyResponse;
use Flarum\Api\Controller\AbstractDeleteController;
use Flarum\Foundation\Paths;

class DeleteSocialMediaImageController extends AbstractDeleteController
{
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
protected SettingsRepositoryInterface $settings;
protected Cloud $disk;

/**
* @var Paths
*/
protected $paths;

/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings, Paths $paths)
public function __construct(SettingsRepositoryInterface $settings, Container $container)
{
$this->settings = $settings;
$this->paths = $paths;
$this->disk = $container->make('filesystem')->disk('flarum-assets');
}
/**
* {@inheritdoc}
*/

protected function delete(ServerRequestInterface $request)
{
$request->getAttribute('actor')->assertAdmin();

$path = $this->settings->get('seo_social_media_image_path');
$this->settings->set('seo_social_media_image_path', null);
$uploadDir = new Filesystem(new Local($this->paths->public.'/assets'));
if ($uploadDir->has($path)) {
$uploadDir->delete($path);
$this->settings->set('seo_social_media_image_url', null);

if ($this->disk->exists($path)) {
$this->disk->delete($path);
}

return new EmptyResponse(204);
}
}
52 changes: 18 additions & 34 deletions src/Api/UploadSocialMediaImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,44 @@
namespace V17Development\FlarumSeo\Api;

use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Filesystem\Cloud;
use Illuminate\Support\Str;
use Illuminate\Support\Arr;
use Intervention\Image\ImageManager;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\MountManager;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UploadedFileInterface;
use Tobscure\JsonApi\Document;
use Flarum\Api\Controller\ShowForumController;
use Flarum\Foundation\Paths;

class UploadSocialMediaImageController extends ShowForumController
{
/**
* @var SettingsRepositoryInterface
*/
protected $settings;

/**
* @var Paths
*/
protected $paths;

/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings, Paths $paths)
protected SettingsRepositoryInterface $settings;
protected Cloud $disk;


public function __construct(SettingsRepositoryInterface $settings, Container $container)
{
$this->settings = $settings;
$this->paths = $paths;
$this->disk = $container->make('filesystem')->disk('flarum-assets');
}
/**
* {@inheritdoc}
*/

public function data(ServerRequestInterface $request, Document $document)
{
$request->getAttribute('actor')->assertAdmin();

/** @var UploadedFileInterface $file */
$file = Arr::get($request->getUploadedFiles(), 'seo_social_media_image');
$tmpFile = tempnam($this->paths->storage.'/tmp', 'site-image');
$file->moveTo($tmpFile);

$mount = new MountManager([
'source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))),
'target' => new Filesystem(new Local($this->paths->public.'/assets')),
]);

if (($path = $this->settings->get('seo_social_media_image_path')) && $mount->has($file = "target://$path")) {
$mount->delete($file);
if (($path = $this->settings->get('seo_social_media_image_path')) && $this->disk->exists($path)) {
$this->disk->delete($path);
}

$uploadName = 'site-image-'.Str::lower(Str::random(8)).'.png';
$mount->move('source://'.pathinfo($tmpFile, PATHINFO_BASENAME), "target://$uploadName");

$this->disk->put($uploadName, $file->getStream()->getContents());

$this->settings->set('seo_social_media_image_path', $uploadName);
$this->settings->set('seo_social_media_image_url', $this->disk->url($uploadName));
return parent::data($request, $document);
}
}
}
19 changes: 13 additions & 6 deletions src/Listeners/PageListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
namespace V17Development\FlarumSeo\Listeners;

// FlarumSEO classes
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Filesystem\Cloud;
use V17Development\FlarumSeo\Managers\Discussion;
use V17Development\FlarumSeo\Managers\Page;
use V17Development\FlarumSeo\Managers\Profile;
Expand Down Expand Up @@ -53,6 +55,8 @@ class PageListener

protected $discussionType = 1; // Special Google results as default, check check readme for different results

protected Cloud $assets;

/**
* PageListener constructor.
*
Expand All @@ -63,7 +67,8 @@ class PageListener
*/
public function __construct(
SettingsRepositoryInterface $settings,
UrlGenerator $url
UrlGenerator $url,
Container $container
) {
// Get Flarum settings
$this->settings = $settings;
Expand All @@ -74,6 +79,8 @@ public function __construct(
// List enabled extensions
$this->enabled_extensions = json_decode($this->settings->get("extensions_enabled"), true);

$this->assets = $container->make('filesystem')->disk('flarum-assets');

// Fancy SEO question-answer?
$this->discussionType = $this->settings->get("seo_post_crawler") === '1' ? 2 : 1;

Expand Down Expand Up @@ -201,17 +208,17 @@ private function setSiteTags()
// Set image
if($applicationSeoSocialMediaImage !== null)
{
$this->setImage($this->applicationUrl . '/assets/' . $applicationSeoSocialMediaImage);
$this->setImage($this->assets->url($applicationSeoSocialMediaImage));
}
// Fallback to the logo
else if($applicationLogo !== null)
{
$this->setImage($this->applicationUrl . '/assets/' . $applicationLogo);
$this->setImage($this->assets->url($applicationLogo));
}
// Fallback to the favicon
else if($applicationFavicon !== null)
{
$this->setImage($this->applicationUrl . '/assets/' . $applicationFavicon);
$this->setImage($this->assets->url($applicationFavicon));
}
}

Expand Down Expand Up @@ -435,7 +442,7 @@ public function setDescription($content)

/**
* Set page keywords
*
*
* @param $keywords
*/
public function setKeywords($keywords)
Expand Down Expand Up @@ -536,7 +543,7 @@ public function setImage($imagePath)
public function setPageTitle($title)
{
$this->flarumDocument->title = $title;

return $this;
}
}

0 comments on commit c80eb61

Please sign in to comment.