Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
archiveentry.php
1<?php
3
8use CFile;
9
15{
20 protected $name;
21
26 protected $path;
27
32 protected $size;
33
38 protected $crc32;
39
43 protected function __construct()
44 {}
45
50 public function getName()
51 {
52 return $this->name;
53 }
54
60 public function setName($name)
61 {
62 $this->name = $name;
63
64 return $this;
65 }
66
71 public function getPath()
72 {
73 return $this->path;
74 }
75
80 public function getSize()
81 {
82 return $this->size;
83 }
84
88 public function getCrc32()
89 {
90 return $this->crc32;
91 }
92
99 public static function createFromFilePath($filePath, $name = null)
100 {
101 $fileArray = CFile::MakeFileArray($filePath);
102
103 if (\is_array($fileArray))
104 {
105 return self::createFromFile([
106 'ID' => 0,
107 'ORIGINAL_NAME' => $fileArray['name'],
108 'FILE_SIZE' => $fileArray['size'],
109 'SRC' => mb_substr($fileArray['tmp_name'], mb_strlen(self::getDocRoot())),
110 ], $name);
111 }
112
113 return null;
114 }
115
122 public static function createFromFileId($fileId, $moduleId = null)
123 {
124 $fileArray = CFile::getFileArray($fileId);
125
126 // check file exists
127 if (!\is_array($fileArray) || empty($fileArray['SRC']))
128 {
129 return null;
130 }
131
132 // check module restriction
133 if (
134 $moduleId !== null &&
135 (
136 !isset($fileArray['MODULE_ID']) ||
137 $fileArray['MODULE_ID'] !== $moduleId
138 )
139 )
140 {
141 return null;
142 }
143
144 return self::createFromFile($fileArray);
145 }
146
153 protected static function createFromFile(array $fileArray, $name = null)
154 {
155 $zipEntry = new static();
156 $zipEntry->setName($name?: $fileArray['ORIGINAL_NAME']);
157 $zipEntry->size = (int)$fileArray['FILE_SIZE'];
158
159 if (empty($fileArray['SRC']))
160 {
161 $fileArray['SRC'] = CFile::getFileSrc($fileArray);
162 }
163
164 $fromClouds = false;
165 $filename = $fileArray['SRC'];
166 if (isset($fileArray['HANDLER_ID']) && !empty($fileArray['HANDLER_ID']))
167 {
168 $fromClouds = true;
169 }
170
171 if ($fromClouds)
172 {
173 $filename = preg_replace('~^(http[s]?)(\://)~i', '\\1.' , $filename);
174 $cloudUploadPath = Option::get(
175 'main',
176 'bx_cloud_upload',
177 '/upload/bx_cloud_upload/'
178 );
179 $zipEntry->path = $cloudUploadPath . $filename;
180 }
181 else
182 {
183 $zipEntry->path = $filename;
184 }
185 $zipEntry->path = Uri::urnEncode($zipEntry->path, 'UTF-8');
186
187 return $zipEntry;
188 }
189
194 protected static function getDocRoot()
195 {
196 static $docRoot = null;
197
198 if ($docRoot === null)
199 {
200 $context = Application::getInstance()->getContext();
201 $server = $context->getServer();
202 $docRoot = $server->getDocumentRoot();
203 }
204
205 return $docRoot;
206 }
207
212 public function __toString()
213 {
214 $crc32 = $this->getCrc32()?: '-';
215 $name = Encoding::convertEncoding(
216 $this->getName(),
217 LANG_CHARSET,
218 'UTF-8'
219 );
220
221 return "{$crc32} {$this->getSize()} {$this->getPath()} {$name}";
222 }
223}
__construct()
$path
static getDocRoot()
setName($name)
getPath()
getName()
getCrc32()
$crc32
static createFromFile(array $fileArray, $name=null)
__toString()
getSize()
static createFromFilePath($filePath, $name=null)
$name
$size
static createFromFileId($fileId, $moduleId=null)