Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
diskfile.php
1<?php
3
11
12class DiskFile extends Controller
13{
14 private const FILE_DOWNLOAD_URL = '/bitrix/services/main/ajax.php?' .
15 'action=landing.api.diskFile.download&' .
16 'fileId=#fileId#&blockId=#blockId#&scope=#scope#';
17
18 public function getDefaultPreFilters(): array
19 {
20 return [];
21 }
22
31 public static function getDownloadLink(string $scope, int $blockId, ?int $fileId = null): string
32 {
33 return str_replace(
34 ['#scope#', '#blockId#', '#fileId#'],
35 [$scope, $blockId, $fileId ?: '#fileId#'],
36 self::FILE_DOWNLOAD_URL . '&ver=' . time()
37 );
38 }
39
48 private function blockContainsFile(string $scope, int $blockId, int $fileId): bool
49 {
50 if (Type::isPublicScope($scope))
51 {
52 return false;
53 }
54
55 Type::setScope($scope);
56 $needed = Connector\Disk::FILE_PREFIX_HREF . $fileId;
57
58 return Block::isContains($blockId, $needed);
59 }
60
69 private function landingContainsFile(string $scope, int $landingId, int $fileId): bool
70 {
71 if (Type::isPublicScope($scope))
72 {
73 return false;
74 }
75
76 Type::setScope($scope);
77 $needed = Connector\Disk::FILE_PREFIX_HREF . $fileId;
78
79 return Block::isContains($landingId, $needed, true);
80 }
81
90 public function downloadAction(string $scope, int $blockId, int $fileId): ?BFile
91 {
92 if ($this->blockContainsFile($scope, $blockId, $fileId))
93 {
94 $fileInfo = \Bitrix\Landing\Connector\Disk::getFileInfo($fileId, false);
95 if ($fileInfo)
96 {
97 return new BFile(\CFile::getFileArray($fileInfo['ID']), $fileInfo['NAME']);
98 }
99 }
100
101 $this->addError(new Error('Access denied.'));
102 return null;
103 }
104
113 public function viewAction(string $scope, int $blockId, int $fileId): ?array
114 {
115 if ($this->blockContainsFile($scope, $blockId, $fileId))
116 {
117 $fileInfo = \Bitrix\Landing\Connector\Disk::getFileInfo($fileId, false);
118 if ($fileInfo)
119 {
120 $urlToDownload = $this->getDownloadLink($scope, $blockId, $fileId);
121 $attributes = Viewer\ItemAttributes::tryBuildByFileId($fileInfo['ID'], $urlToDownload);
122 $attributes->setTitle($fileInfo['NAME']);
123 return $attributes->getAttributes();
124 }
125 }
126
127 $this->addError(new Error('Access denied.'));
128 return null;
129 }
130
137 public function infoAction(int $fileId, string $scope, int $landingId): ?array
138 {
139 if ($this->landingContainsFile($scope, $landingId, $fileId))
140 {
141 return \Bitrix\Landing\Connector\Disk::getFileInfo($fileId, false);
142 }
143
144 return null;
145 }
146}
viewAction(string $scope, int $blockId, int $fileId)
Definition diskfile.php:113
infoAction(int $fileId, string $scope, int $landingId)
Definition diskfile.php:137
downloadAction(string $scope, int $blockId, int $fileId)
Definition diskfile.php:90
static getDownloadLink(string $scope, int $blockId, ?int $fileId=null)
Definition diskfile.php:31