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