aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Model/NoteAttachment.php
blob: 8c6fb1f2f21adc8e8d06610f3d28d3eef1ca6997 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace Digitigrade\Model;

use Digitigrade\Model;

class NoteAttachment extends Model {
    public ?int $noteId;
    public int $index;
    public string $type;
    public string $href;
    public ?string $description;

    public static function fromStorageObject(StorageObject $obj, int $index, ?string $description = null) {
        $a = new self();
        $a->index = $index;
        $a->type = $obj->mimetype;
        $a->href = $obj->getUrl();
        $a->description = $description;
        return $a;
    }

    public function setOwnerId(int $id) {
        $this->noteId = $id;
    }

    protected function getUpdateWhereClause($db): ?string {
        if (self::findWhere('note_id = ? AND index = ?', [$this->noteId, $this->index]) != null) {
            return "note_id = $this->noteId AND index = $this->index";
        }
        return null;
    }

    /**
     * @return bool if this attachment is a supported image file
     */
    public function isImage(): bool {
        return in_array($this->type, [
            'image/apng',
            'image/avif',
            'image/gif',
            'image/jpeg',
            'image/png',
            'image/svg+xml',
            'image/webp',
        ]);
    }

    /**
     * @return bool if this attachment is a supported audio file
     */
    public function isAudio(): bool {
        // TODO: be a bit more careful about what counts as supported
        return str_starts_with($this->type, 'audio/');
    }

    /**
     * @return bool if this attachment is a supported video file
     */
    public function isVideo(): bool {
        // TODO: be a bit more careful about what counts as supported
        return str_starts_with($this->type, 'video/');
    }
}