FriendsOfFlarum / upload

The file upload extension with insane intelligence for your Flarum forum.
https://discuss.flarum.org/d/4154
MIT License
176 stars 96 forks source link

how to change the url that input #282

Closed liaosunny123 closed 2 weeks ago

liaosunny123 commented 3 years ago

How to change the url that input. For example: I use IMGUR and upload a image , the edit will display [upl-image-preview url=//xxx.jpeg] but I want it display this automatically: [upl-image-preview url=//asdawde.com/url?=https://xxx.jpeg] How can I do that? There is a settings which is above the IMGUR Client ID that I can change the prefix but it dos not work I put : //asdawde.com/url?= in that place

clarkwinkelmann commented 3 years ago

That's not something that's supported out of the box.

You could use a Custom Template that implements your feature.

If you want to preserve the other features of the Image Preview Template, you can return a variation of '[upl-image-preview url={URL}]' like the original https://github.com/FriendsOfFlarum/upload/blob/master/src/Templates/ImagePreviewTemplate.php

However if you re-use the existing bbcode, you should not extend the original template but just create a new one that implements Template. If you extend the original one, Flarum will attempt to register the same bbcode two times and it will probably fail. However as long as the original template is still present, the same bbcode can be used from another template as well.

liaosunny123 commented 3 years ago

Thank you a lot. But I changed my extend.php and it goes wrong. This is my code. `<?php use Flarum\Extend; use Flarum\Foundation\AbstractServiceProvider; use Flarum\Foundation\Application; use FoF\Upload\Contracts\Template; use FoF\Upload\File; use FoF\Upload\Helpers\Settings;

class EpicMoImgur implements Template { public function tag(): string { return 'EpicMoImgur'; }

public function name(): string
{
    return 'EpicMoImgur';
}

public function description(): string
{
    return 'Use a url change to change the imgur links.';
}

public function preview(File $file): string
{
    return '[upl-image-preview url=//example.epicmo.cn?url=https:{URL}]' . $file->url;
}

}

class MyServiceProvider extends AbstractServiceProvider { public function register() { $this->app->make(Settings::class)->addRenderTemplate($this->app->make(MyTemplate::class)); } }

return [ new Extend\Compat(function (Application $app) { $app->register(MyServiceProvider::class); }), ]; ` And the Flarum gets its wrong. There is something wrong in my code? I put it into the extend.php in the local dir.

clarkwinkelmann commented 3 years ago

Is there any error message?

The service provider can now be registered using the Extend\ServiceProvider Flarum extender. We should update the wiki with that. Actually we should make a custom extender for this feature.

I'm not 100% sure what's allowed or not inside the preview function, it might need to meet some TextFormatter requirements since it's used as part of the preview logic.

liaosunny123 commented 3 years ago

the logs show that it cannot admit my code and goes wrong. [2021-05-03 00:02:30] flarum.ERROR: ReflectionException: Class FoF\Upload\Helpers\Settings does not exist in /www/wwwroot/mysite/vendor/illuminate/container/Container.php:833 Stack trace: mysite refers to the local dir of the website.

liaosunny123 commented 3 years ago

`[2021-05-03 00:02:30] flarum.ERROR: ReflectionException: Class FoF\Upload\Helpers\Settings does not exist in /www/wwwroot/mysite/vendor/illuminate/container/Container.php:833 Stack trace:

0 /www/wwwroot/mysite/vendor/illuminate/container/Container.php(833): ReflectionClass->__construct('FoF\Upload\Help...')

1 /www/wwwroot/mysite/vendor/illuminate/container/Container.php(714): Illuminate\Container\Container->build('FoF\Upload\Help...')

2 /www/wwwroot/mysite/vendor/illuminate/container/Container.php(652): Illuminate\Container\Container->resolve('FoF\Upload\Help...', Array)

3 /www/wwwroot/mysite/extend.php(43): Illuminate\Container\Container->make('FoF\Upload\Help...')

4 /www/wwwroot/mysite/vendor/flarum/core/src/Foundation/Application.php(183): MyServiceProvider->register()

5 /www/wwwroot/mysite/extend.php(49): Flarum\Foundation\Application->register(Object(MyServiceProvider))

6 /www/wwwroot/mysite/vendor/illuminate/container/BoundMethod.php(36): Flarum\Foundation\Site::{closure}(Object(Flarum\Foundation\Application))

7 /www/wwwroot/mysite/vendor/illuminate/container/Util.php(40): Illuminate\Container\BoundMethod::Illuminate\Container{closure}()

8 /www/wwwroot/mysite/vendor/illuminate/container/BoundMethod.php(81): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))

9 /www/wwwroot/mysite/vendor/illuminate/container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Container\Container), Object(Closure), Object(Closure))

10 /www/wwwroot/mysite/vendor/illuminate/container/Container.php(611): Illuminate\Container\BoundMethod::call(Object(Illuminate\Container\Container), Object(Closure), Array, NULL)

11 /www/wwwroot/mysite/vendor/flarum/core/src/Extend/Compat.php(35): Illuminate\Container\Container->call(Object(Closure))

12 /www/wwwroot/mysite/vendor/flarum/core/src/Foundation/InstalledSite.php(146): Flarum\Extend\Compat->extend(Object(Illuminate\Container\Container))

13 [internal function]: Flarum\Foundation\InstalledSite->Flarum\Foundation{closure}(Object(Flarum\Foundation\Application))

14 /www/wwwroot/mysite/vendor/flarum/core/src/Foundation/Application.php(328): call_user_func(Object(Closure), Object(Flarum\Foundation\Application))

15 /www/wwwroot/mysite/vendor/flarum/core/src/Foundation/Application.php(269): Flarum\Foundation\Application->fireAppCallbacks(Array)

16 /www/wwwroot/mysite/vendor/flarum/core/src/Foundation/InstalledSite.php(150): Flarum\Foundation\Application->boot()

17 /www/wwwroot/mysite/vendor/flarum/core/src/Foundation/InstalledSite.php(82): Flarum\Foundation\InstalledSite->bootLaravel()

18 /www/wwwroot/mysite/vendor/flarum/core/src/Http/Server.php(58): Flarum\Foundation\InstalledSite->bootApp()

19 /www/wwwroot/mysite/vendor/flarum/core/src/Http/Server.php(35): Flarum\Http\Server->safelyBootAndGetHandler()

20 /www/wwwroot/mysite/public/index.php(26): Flarum\Http\Server->listen()

21 {main}`

liaosunny123 commented 3 years ago

I thinks whether can i edit the local config? upload/src/templates/imagepreview and i try to changed the code public function bbcode(): string { return '[upl-image-preview url={URL}]'; } } into public function bbcode(): string { return '[upl-image-preview url=//hdd.dj/?url=https:{URL}]'; } } But it never changed.

clarkwinkelmann commented 3 years ago

Sadly there are actually multiple issues.

First the wiki was really out of date, sorry about that. I have now updated it with accurate code.

Unfortunately it seems like the rendering of custom templates is broken in the frontend because of https://github.com/FriendsOfFlarum/upload/issues/281

Until the linked issue is fixed, custom template will no longer work :grimacing:

YiHui-Liu commented 3 years ago

I edit file vendor/fof/upload/src/Templates/ImagePreviewTemplate.php:

public function bbcode(): string
    {
        return '[upl-image-preview url=https://images.weserv.nl/?url={URL}]';
    }

It works well. But if I run command sudo php flarum cache:clear, my website will get error. So I have to undo the changes, reload website and edit file again.

YiHui-Liu commented 3 years ago

I edit file vendor/fof/upload/src/Templates/ImagePreviewTemplate.php:

public function bbcode(): string
    {
        return '[upl-image-preview url=https://images.weserv.nl/?url={URL}]';
    }

It works well. But if I run command sudo php flarum cache:clear, my website will get error. So I have to undo the changes, reload website and edit file again.

But it only works in post page. If i want to use Media manager, the url will not add the prefix.

github-actions[bot] commented 2 weeks ago

This issue has been automatically closed because it received no activity for three months. If you think it was closed by accident, please leave a comment. If you are running into a similar issue on the latest version, please open a new issue. Thank you.