Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
TemporaryFileService.php
1<?php
2
4
5use Bitrix\Disk\File;
6use Bitrix\Disk\Internals\AttachedObjectTable;
7use Bitrix\Disk\SystemUser;
12
14{
15 public const TASK_SOURCE = 'TASK';
16
18 protected const LIMIT_SELECT_UNATTACHED_FILES = 200;
19 protected const EXPIRY_INTERVAL = '-12 hours';
20
22
23 public function __construct()
24 {
25 $this->dateExpired = (new DateTime())->add(self::EXPIRY_INTERVAL);
26 }
27
28 public static function cleanAgent(): string
29 {
30 (new self())->clean();
31
32 return __METHOD__. '();';
33 }
34
35 public function clean(): void
36 {
37 if (!Loader::includeModule('disk'))
38 {
39 return;
40 }
41
42 $this->deleteUnattachedFiles();
43 $this->cleanExpired();
44 }
45
46 protected function deleteUnattachedFiles(): void
47 {
48 $subQuery = AttachedObjectTable::query()
49 ->setSelect(['ID'])
50 ->where('OBJECT_ID', new \Bitrix\Main\DB\SqlExpression('%s'))
51 ;
52
53 $unattachedFiles = FileTemporaryTable::query()
54 ->setSelect(['DISK_FILE_ID'])
55 ->where('DATE_CREATE', '<', $this->dateExpired)
56 ->whereIn('SOURCE', self::SOURCE_MUST_BE_ATTACHED)
57 ->whereExpr("NOT EXISTS ({$subQuery->getQuery()})", ['DISK_FILE_ID'])
58 ->setLimit(self::LIMIT_SELECT_UNATTACHED_FILES)
59 ->fetchAll()
60 ;
61
62 $diskFilesIds = array_column($unattachedFiles, 'DISK_FILE_ID');
63
64 if (empty($diskFilesIds))
65 {
66 return;
67 }
68
69 $diskFiles = File::getModelList(['filter' => Query::filter()->whereIn('ID', $diskFilesIds)]);
70
71 foreach ($diskFiles as $diskFile)
72 {
73 $diskFile->delete(SystemUser::SYSTEM_USER_ID);
74 }
75 }
76
77 protected function cleanExpired(): void
78 {
79 FileTemporaryTable::deleteByFilter(['<DATE_CREATE' => $this->dateExpired]);
80 }
81}