Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
FileInfo.php
1<?php
2
4
6
7class FileInfo extends FileData implements \JsonSerializable
8{
9 protected $id;
10 protected int $fileId = 0;
11 protected bool $treatImageAsFile = false;
12 protected ?string $downloadUrl = null;
13 protected ?string $previewUrl = null;
14 protected int $previewWidth = 0;
15 protected int $previewHeight = 0;
16 protected ?Dictionary $customData = null;
17
24 public function __construct($id, string $name, string $contentType, int $size)
25 {
26 parent::__construct($name, $contentType, $size);
27 $this->id = $id;
28 }
29
30 public static function createFromBFile(int $id): ?FileInfo
31 {
32 $file = \CFile::getFileArray($id);
33 if (!is_array($file))
34 {
35 return null;
36 }
37
38 $fileName = !empty($file['ORIGINAL_NAME']) ? $file['ORIGINAL_NAME'] : $file['FILE_NAME'];
39 $fileInfo = new static($id, $fileName, $file['CONTENT_TYPE'], (int)$file['FILE_SIZE']);
40 $fileInfo->setWidth($file['WIDTH']);
41 $fileInfo->setHeight($file['HEIGHT']);
42 $fileInfo->setFileId($id);
43
44 return $fileInfo;
45 }
46
47 public static function createFromTempFile(string $tempFileId): ?FileInfo
48 {
49 [$guid, $signature] = explode('.', $tempFileId);
50
51 $tempFile = TempFileTable::getList([
52 'filter' => [
53 '=GUID' => $guid,
54 '=UPLOADED' => true,
55 ],
56 ])->fetchObject();
57
58 if (!$tempFile)
59 {
60 return null;
61 }
62
63 $fileInfo = new static($tempFileId, $tempFile->getFilename(), $tempFile->getMimetype(), $tempFile->getSize());
64 $fileInfo->setWidth($tempFile->getWidth());
65 $fileInfo->setHeight($tempFile->getHeight());
66 $fileInfo->setFileId($tempFile->getFileId());
67
68 return $fileInfo;
69 }
70
74 public function getId()
75 {
76 return $this->id;
77 }
78
79 public function setId($id): void
80 {
81 if (is_int($id) || is_string($id))
82 {
83 $this->id = $id;
84 }
85 }
86
87 public function getFileId(): int
88 {
89 return $this->fileId;
90 }
91
92 public function setFileId(int $fileId): void
93 {
94 $this->fileId = $fileId;
95 }
96
97 public function shouldTreatImageAsFile(): bool
98 {
100 }
101
102 public function setTreatImageAsFile(bool $flag): void
103 {
104 $this->treatImageAsFile = $flag;
105 }
106
107 public function getDownloadUrl(): ?string
108 {
109 return $this->downloadUrl;
110 }
111
112 public function setDownloadUrl(string $downloadUrl): void
113 {
114 $this->downloadUrl = $downloadUrl;
115 }
116
117 public function getPreviewUrl(): ?string
118 {
119 return $this->previewUrl;
120 }
121
122 public function setPreviewUrl(string $previewUrl, int $previewWidth, int $previewHeight): void
123 {
124 $this->previewUrl = $previewUrl;
125 $this->previewWidth = $previewWidth;
126 $this->previewHeight = $previewHeight;
127 }
128
129 public function getPreviewWidth(): int
130 {
131 return $this->previewWidth;
132 }
133
134 public function getPreviewHeight(): int
135 {
137 }
138
139 public function setCustomData(array $customData): self
140 {
141 $this->getCustomData()->setValues($customData);
142
143 return $this;
144 }
145
149 public function getCustomData(): Dictionary
150 {
151 if ($this->customData === null)
152 {
153 $this->customData = new Dictionary();
154 }
155
156 return $this->customData;
157 }
158
159 public function jsonSerialize(): array
160 {
161 return [
162 'serverFileId' => $this->getId(),
163 'serverId' => $this->getId(), // compatibility
164 'type' => $this->getContentType(),
165 'name' => $this->getName(),
166 'size' => $this->getSize(),
167 'width' => $this->getWidth(),
168 'height' => $this->getHeight(),
169 'treatImageAsFile' => $this->isImage() && $this->shouldTreatImageAsFile(),
170 'downloadUrl' => $this->getDownloadUrl(),
171 'serverPreviewUrl' => $this->getPreviewUrl(),
172 'serverPreviewWidth' => $this->getPreviewWidth(),
173 'serverPreviewHeight' => $this->getPreviewHeight(),
174 'customData' => $this->customData !== null ? $this->getCustomData()->getValues() : [],
175 ];
176 }
177}
setDownloadUrl(string $downloadUrl)
Definition FileInfo.php:112
__construct($id, string $name, string $contentType, int $size)
Definition FileInfo.php:24
static createFromTempFile(string $tempFileId)
Definition FileInfo.php:47
static createFromBFile(int $id)
Definition FileInfo.php:30
setCustomData(array $customData)
Definition FileInfo.php:139
setPreviewUrl(string $previewUrl, int $previewWidth, int $previewHeight)
Definition FileInfo.php:122