1<?php
2
3
4namespace ComboStrap;
5
6use ComboStrap\Web\Url;
7
8/**
9 * Class ThirdMediaLink
10 * @package ComboStrap
11 *
12 */
13class ThirdMediaLink extends MediaLink
14{
15
16
17    public function renderMediaTag(): string
18    {
19
20        $mediaMarkup = $this->mediaMarkup;
21        $tagAttributes = $this->mediaMarkup->getExtraMediaTagAttributes();
22
23        $urlString = $mediaMarkup->getFetchUrl()->toString();
24        $path = $mediaMarkup->getPath();
25        $tagAttributes->addOutputAttributeValue("href", $urlString);
26
27        try {
28            $label = $mediaMarkup->getLabel();
29        } catch (ExceptionNotFound $e) {
30            $label = $path->getLastName();
31        }
32        $tagAttributes->addOutputAttributeValue("title", $label);
33
34        // dokuwiki class
35        $tagAttributes
36            ->addClassName("media")
37            ->addClassName("mediafile")
38            ->addClassName("wikilink2");
39        try {
40            // dokuwiki icon
41            $extension = FileSystems::getMime($path);
42            $tagAttributes->addClassName("mf_$extension");
43        } catch (ExceptionNotFound $e) {
44            LogUtility::warning("No icon could be added to the media link. Error: {$e->getMessage()}");
45        }
46
47        if (!FileSystems::exists($path)) {
48            $tagAttributes->addClassName(LinkMarkup::getHtmlClassNotExist());
49        }
50
51        return $tagAttributes->toHtmlEnterTag("a") . $label . "</a>";
52
53    }
54
55
56    /**
57     */
58    public function getFetchUrl(): Url
59    {
60
61
62        $path = $this->mediaMarkup->getPath();
63        if(!$path instanceof WikiPath){
64            return $this->mediaMarkup->getFetchUrl();
65        }
66
67        try {
68            $mime = FileSystems::getMime($path);
69        } catch (ExceptionNotFound $e) {
70            return parent::getFetchUrl();
71        }
72
73        switch ($mime->toString()) {
74            case Mime::PDF:
75                try {
76                    return (new FetcherPdf())
77                        ->buildFromUrl($this->mediaMarkup->getFetchUrl())
78                        ->getFetchUrl();
79                } catch (ExceptionBadArgument $e) {
80                    LogUtility::internalError($e->getMessage());
81                    return FetcherRawLocalPath::createFromPath($path)
82                        ->getFetchUrl();
83                }
84            default:
85                return FetcherRawLocalPath::createFromPath($path)
86                    ->getFetchUrl();
87        }
88
89
90    }
91
92
93}
94