1<?php
2/*
3 * vim:set softtabstop=4 shiftwidth=4 expandtab:
4 *
5 *  LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
6 * Copyright 2001 - 2020 Ampache.org
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 *
21 */
22
23declare(strict_types=1);
24
25namespace Ampache\Module\Application\Waveform;
26
27use Ampache\Config\ConfigContainerInterface;
28use Ampache\Config\ConfigurationKeyEnum;
29use Ampache\Module\Application\ApplicationActionInterface;
30use Ampache\Module\Authorization\GuiGatekeeperInterface;
31use Ampache\Module\Util\Waveform;
32use Psr\Http\Message\ResponseFactoryInterface;
33use Psr\Http\Message\ResponseInterface;
34use Psr\Http\Message\ServerRequestInterface;
35use Psr\Http\Message\StreamFactoryInterface;
36
37final class ShowAction implements ApplicationActionInterface
38{
39    public const REQUEST_KEY = 'show';
40
41    private ResponseFactoryInterface $responseFactory;
42
43    private ConfigContainerInterface $configContainer;
44
45    private StreamFactoryInterface $streamFactory;
46
47    public function __construct(
48        ResponseFactoryInterface $responseFactory,
49        ConfigContainerInterface $configContainer,
50        StreamFactoryInterface $streamFactory
51    ) {
52        $this->responseFactory = $responseFactory;
53        $this->configContainer = $configContainer;
54        $this->streamFactory   = $streamFactory;
55    }
56
57    public function run(ServerRequestInterface $request, GuiGatekeeperInterface $gatekeeper): ?ResponseInterface
58    {
59        if ($this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::WAVEFORM) === false) {
60            return null;
61        }
62
63        // Prevent user from aborting script
64        ignore_user_abort(true);
65        set_time_limit(300);
66
67        // Write/close session data to release session lock for this script.
68        // This to allow other pages from the same session to be processed
69        // Warning: Do not change any session variable after this call
70        session_write_close();
71
72        $id       = (int) $_REQUEST['song_id'];
73        $waveform = Waveform::get($id);
74
75        if (!$waveform) {
76            return null;
77        }
78
79        return $this->responseFactory
80            ->createResponse()
81            ->withHeader(
82                'Content-type',
83                'image/png'
84            )
85            ->withBody($this->streamFactory->createStream($waveform));
86    }
87}
88