Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace communication_matrix\local\spec\features\matrix;
18
 
19
use communication_matrix\local\command;
20
use GuzzleHttp\Psr7\Response;
21
use GuzzleHttp\Psr7\Utils;
22
 
23
/**
24
 * Matrix API feature to upload content.
25
 *
26
 * https://spec.matrix.org/v1.1/client-server-api/#post_matrixmediav3upload
27
 *
28
 * @package    communication_matrix
29
 * @copyright  2023 Andrew Lyons <andrew@nicols.co.uk>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 * @codeCoverageIgnore
32
 * This code does not warrant being tested. Testing offers no discernible benefit given its usage is tested.
33
 */
34
trait upload_content_v3 {
35
    /**
36
     * Upload the content in the matrix/synapse server.
37
     *
38
     * @param null|\stored_file $content The content to be uploaded
39
     * @param null|string $mediaid The mediaid to associate a file with. Supported for v1.7 API an above only.
40
     * @return Response
41
     */
42
    public function upload_content(
43
        ?\stored_file $content,
44
        ?string $mediaid = null,
45
    ): Response {
46
        $query = [];
47
        if ($content) {
48
            $query['filename'] = $content->get_filename();
49
        }
50
 
51
        if ($mediaid !== null) {
52
            // Specification of the mediaid requires version 1.7 or above of the upload API.
53
            // See https://spec.matrix.org/v1.7/client-server-api/#put_matrixmediav3uploadservernamemediaid.
54
            $this->requires_version('1.7');
55
            $command = new command(
56
                $this,
57
                method: 'PUT',
58
                endpoint: '_matrix/media/v3/upload/:mediaid',
59
                sendasjson: false,
60
                query: $query,
61
                params: [
62
                    ':mediaid' => $mediaid,
63
                ],
64
            );
65
        } else {
66
            $command = new command(
67
                $this,
68
                method: 'POST',
69
                endpoint: '_matrix/media/v3/upload',
70
                sendasjson: false,
71
                query: $query,
72
            );
73
        }
74
 
75
        if ($content) {
76
            // Add the content-type, and header.
77
            $command = $command->withHeader('Content-Type', $content->get_mimetype());
78
            $command = $command->withBody(Utils::streamFor($content->get_content()));
79
        }
80
 
81
        return $this->execute($command);
82
    }
83
}