summaryrefslogtreecommitdiffstats
path: root/lib/Entity/MediaAttachment.php
blob: cdb67ede5ac8def108ea990d5101c3e1c06e572d (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
declare(strict_types=1);

// Nextcloud - Social Support
// SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu>
// SPDX-License-Identifier: AGPL-3.0-or-later

namespace OCA\Social\Entity;

use Doctrine\ORM\Mapping as ORM;
use OCP\IURLGenerator;

/**
 * @ORM\Entity
 * @ORM\Table(name="social_media_attachment")
 */
class MediaAttachment  {
	const TYPE_IMAGE = 1;

	public const IMAGE_MIME_TYPES = [
		'image/png',
		'image/jpeg',
		'image/jpg',
		'image/gif',
		'image/x-xbitmap',
		'image/x-ms-bmp',
		'image/bmp',
		'image/svg+xml',
		'image/webp',
	];

	public const IMAGE_MIME_TYPES_CONVERSATION = [
		'image/png' => 'png',
		'image/jpeg' => 'jpg',
		'image/jpg' => 'jpg',
		'image/gif' => 'gif',
		'image/x-xbitmap' => 'bmp',
		'image/x-ms-bmp' => 'bmp',
		'image/bmp' => 'bmp',
		'image/svg+xml' => 'svg',
		'image/webp' => 'webp',
	];

	/**
	 * @ORM\Id
	 * @ORM\Column(type="bigint")
	 * @ORM\GeneratedValue
	 */
	private ?string $id = '-1';

	/**
	 * @ORM\ManyToOne
	 */
	private ?Status $status = null;

	/**
	 * @ORM\Column(name="remote_url", nullable=false)
	 */
	private string $remoteUrl = "";

	/**
	 * @ORM\Column(name="created_at", type="datetime", nullable=false)
	 */
	private \DateTime $createdAt;

	/**
	 * @ORM\Column(name="updated_at", type="datetime", nullable=false)
	 */
	private \DateTime $updatedAt;

	/**
	 * @ORM\Column
	 */
	private ?string $shortcode = null;

	/**
	 * @ORM\Column
	 */
	private string $mimetype = 'image/png';

	/**
	 * @ORM\Column(type="text")
	 */
	private string $description = '';

	/**
	 * @ORM\Column
	 */
	private int $type = self::TYPE_IMAGE;

	/**
	 * @ORM\Column(type="json")
	 */
	private ?array $meta;

	/**
	 * @ORM\ManyToOne
	 */
	private ?Account $account = null;

	/**
	 * @ORM\Column
	 */
	private string $blurhash = '';

	public function __construct() {
		$this->updatedAt = new \DateTime();
		$this->createdAt = new \DateTime();
		$this->meta = [];
	}

	static public function create(): self {
		$attachement = new MediaAttachment();
		$length = 14;
		$length = ($length < 4) ? 4 : $length;
		$attachement->setShortcode(bin2hex(random_bytes(($length - ($length % 2)) / 2)));
		return $attachement;
	}

	public function getId(): string {
		return $this->id;
	}

	public function setId(?string $id): void {
		$this->id = $id;
	}

	public function getStatus(): ?Status {
		return $this->status;
	}

	public function setStatus(?Status $status): void {
		$this->status = $status;
	}

	public function getRemoteUrl(): string {
		return $this->remoteUrl;
	}

	public function setRemoteUrl(string $remoteUrl): void {
		$this->remoteUrl = $remoteUrl;
	}

	public function getCreatedAt(): \DateTime {
		return $this->createdAt;
	}

	public function setCreatedAt(\DateTime $createdAt): void {
		$this->createdAt = $createdAt;
	}

	public function getUpdatedAt(): \DateTime {
		return $this->updatedAt;
	}

	public function setUpdatedAt(\DateTime $updatedAt): void {
		$this->updatedAt = $updatedAt;
	}

	public function getShortcode(): ?string {
		return $this->shortcode;
	}

	public function setShortcode(?string $shortcode): void {
		$this->shortcode = $shortcode;
	}

	public function getType(): int {
		return $this->type;
	}

	public function setType(int $type): void {
		$this->type = $type;
	}

	public function getMeta(): ?array {
		return $this->meta;
	}

	public function setMeta(?array $meta): void {
		$this->meta = $meta;
	}

	public function getAccount(): ?Account {
		return $this->account;
	}

	public function setAccount(?Account $account): void {
		$this->account = $account;
	}

	public function getBlurhash(): ?string {
		return $this->blurhash;
	}

	public function setBlurhash(?string $blurhash): void {
		$this->blurhash = $blurhash;
	}

	public function getDescription(): ?string {
		return $this->description;
	}

	public function setDescription(?string $description): void {
		$this->description = $description;
	}

	public function getMimetype(): string {
		return $this->mimetype;
	}

	public function setMimetype(string $mimetype): void {
		$this->mimetype = $mimetype;
	}

	function toMastodonApi(IURLGenerator $generator) {
		return [
			'id' => $this->getId(),
			'url' => $generator->getAbsoluteURL('/apps/social/media/' . $this->getShortcode() . '.'),
			'preview_url' => $generator->getAbsoluteURL('/apps/social/media/' . $this->getShortcode() . '.' . self::IMAGE_MIME_TYPES_CONVERSATION[$this->getMimetype()]),
			'remote_url' => null,
			'text_url' => $generator->getAbsoluteURL('/apps/social/media/' . $this->getShortcode()),
			'description' => $this->getDescription(),
			'meta' => $this->getMeta(),
		];
	}
}