xref: /dokuwiki/inc/Remote/Response/Media.php (revision 6cce3332fbc12c1e250ec7e6adbad6d4dc2c74e8)
1<?php
2
3namespace dokuwiki\Remote\Response;
4
5/**
6 * Represents a single media revision in the wiki.
7 */
8class Media extends ApiResponse
9{
10    /** @var string The media ID */
11    public $id;
12    /** @var int The media revision aka last modified timestamp */
13    public $revision;
14    /** @var int The page size in bytes */
15    public $size;
16    /** @var int The current user's permissions for this file */
17    public $perms;
18    /** @var bool Wether this is an image file */
19    public $isimage;
20    /** @var string MD5 sum over the file's content (if available and requested) */
21    public $hash;
22
23    /** @inheritdoc */
24    public function __construct($data)
25    {
26        $this->id = cleanID($data['id'] ?? '');
27        if ($this->id === '') {
28            throw new \InvalidArgumentException('Missing id');
29        }
30        if (!media_exists($this->id)) {
31            throw new \InvalidArgumentException('Media does not exist');
32        }
33
34        // FIXME this isn't really managing the difference between old and current revs correctly
35
36        $this->revision = (int)($data['rev'] ?? $data['mtime'] ?? @filemtime(mediaFN($this->id)));
37        $this->size = (int)($data['size'] ?? @filesize(mediaFN($this->id)));
38        $this->perms = $data['perm'] ?? auth_quickaclcheck($this->id);
39        $this->isimage = (bool)($data['isimg'] ?? false);
40        $this->hash = $data['hash'] ?? '';
41    }
42
43    /**
44     * Calculate the hash for this page
45     *
46     * This is a heavy operation and should only be called when needed.
47     */
48    public function calculateHash()
49    {
50        if (!media_exists($this->id)) return;
51        $this->hash = md5(io_readFile(mediaFN($this->id)));
52    }
53}
54