138 public static function addList($bucketId, array $files)
140 $bucketId = intval($bucketId);
141 $connection = \Bitrix\Main\Application::getConnection();
142 $helper = $connection->getSqlHelper();
144 foreach ($files[
'file'] as $i => $file)
146 $fileSize = $files[
'file_size'][$i];
147 $fileMtime = \CCloudUtil::gmtTimeToDateTime($files[
'file_mtime'][$i]);
148 $fileHash = $files[
'file_hash'][$i];
151 .
",'" . $helper->forSql($file) .
"'"
152 .
',' . intval($fileSize)
153 .
",'" . $fileMtime->format(
'Y-m-d h:i:s') .
"'"
154 .
",'" . $helper->forSql($fileHash) .
"'"
159 INSERT INTO ' . static::getTableName() .
'
160 (BUCKET_ID, FILE_PATH, FILE_SIZE, FILE_MTIME, FILE_HASH)
162 ' . implode(
",\n", $values) .
'
164 return $connection->query($sql);
178 public static function syncList($bucketId, $path, array $files, $prevLastKey)
181 $bucketId = intval($bucketId);
182 $connection = \Bitrix\Main\Application::getConnection();
183 $helper = $connection->getSqlHelper();
186 foreach ($files[
'file'] as $i => $filePath)
188 $index[$path . $filePath] = $i;
192 '=BUCKET_ID' => $bucketId,
196 $filter[
'>FILE_PATH'] = $path . $prevLastKey;
198 if ($files[
'last_key'])
200 $filter[
'<=FILE_PATH'] = $path . $files[
'last_key'];
202 $fileList = static::getList([
203 'select' => [
'ID',
'FILE_PATH',
'FILE_SIZE',
'FILE_HASH'],
206 while ($fileInfo = $fileList->fetch())
209 array_key_exists($fileInfo[
'FILE_PATH'], $index)
210 && ($files[
'file_size'][$index[$fileInfo[
'FILE_PATH']]] == $fileInfo[
'FILE_SIZE'])
211 && ($files[
'file_hash'][$index[$fileInfo[
'FILE_PATH']]] == $fileInfo[
'FILE_HASH'])
214 unset($files[
'file'][$index[$fileInfo[
'FILE_PATH']]]);
218 $deleteResult = static::delete($fileInfo[
'ID']);
223 foreach ($files[
'file'] as $i => $file)
225 $fileSize = $files[
'file_size'][$i];
226 $fileMtime = \CCloudUtil::gmtTimeToDateTime($files[
'file_mtime'][$i]);
227 $fileHash = $files[
'file_hash'][$i];
230 .
",'" . $helper->forSql($path . $file) .
"'"
231 .
',' . intval($fileSize)
232 .
",'" . $fileMtime->format(
'Y-m-d h:i:s') .
"'"
233 .
",'" . $helper->forSql($fileHash) .
"'"
242 INSERT INTO ' . static::getTableName() .
'
243 (BUCKET_ID, FILE_PATH, FILE_SIZE, FILE_MTIME, FILE_HASH)
245 ' . implode(
",\n", array_splice($values, 0, $insertSize)) .
'
247 $result = $connection->query($sql);
263 public static function syncEnd($bucketId, $path, $prevLastKey)
265 $bucketId = intval($bucketId);
266 $connection = \Bitrix\Main\Application::getConnection();
267 $sqlHelper = $connection->getSqlHelper();
269 DELETE from ' . static::getTableName() .
'
270 WHERE BUCKET_ID = ' . $bucketId .
'
271 AND FILE_PATH like \'' . $sqlHelper->forSql($path) .
'%\'
272 AND FILE_PATH > \'' . $sqlHelper->forSql($path . $prevLastKey) .
'\'
274 $result = $connection->query($delete);
339 public static function deleteByFilePath($bucketId, $filePath)
341 $bucketId = intval($bucketId);
342 $connection = \Bitrix\Main\Application::getConnection();
343 $sqlHelper = $connection->getSqlHelper();
345 DELETE from
' . static::getTableName() . '
346 WHERE BUCKET_ID =
' . $bucketId . "
347 AND FILE_PATH = '" . $sqlHelper->forSql($filePath) . "'
349 $result = $connection->query($delete);
363 public static function dirList($bucketId, $path, $order, $filter)
365 $connection = \Bitrix\Main\Application::getConnection();
366 $sqlHelper = $connection->getSqlHelper();
368 $query = \Bitrix\Clouds\FileHashTable::query();
372 'if(locate(\
'/\', substring(%s, length(\'' . $sqlHelper->forSql($path) .
'\')+1)) > 0, \
'D\', \'F\')',
377 'substring_index(substring(%s, length(\'' . $sqlHelper->forSql($path) .
'\')+1), \
'/\', 1)',
396 $filter[
'=BUCKET_ID'] = $bucketId;
397 $filter[
'%=FILE_PATH'] = $path .
'%';
398 $query->setFilter($filter);
399 $query->setGroup([
'FILE_TYPE',
'NAME']);
400 $query->setOrder($order);
402 $sql = $query->getQuery();
404 return $connection->query($sql);
417 public static function duplicateList($bucketId, $filter, $order, $limit = 0)
419 $connection = \Bitrix\Main\Application::getConnection();
421 $query = \Bitrix\Main\File\Internal\FileHashTable::query();
427 'COUNT(distinct %s, %s)',
428 [
'FILE.SUBDIR',
'FILE.FILE_NAME']
442 $filter[
'=FILE.HANDLER_ID'] = $bucketId;
443 $filter[
'>FILE_COUNT'] = 1;
444 $query->setFilter($filter);
445 $query->setGroup([
'FILE_HASH',
'FILE_SIZE']);
446 $query->setOrder($order);
449 $query->setLimit($limit);
452 $sql = $query->getQuery();
454 return $connection->query($sql);
468 $query = \Bitrix\Main\File\Internal\FileHashTable::getList([
473 '=FILE.HANDLER_ID' => $bucketId,
474 '=FILE_HASH' => $fileHash,
475 '=FILE_SIZE' => $fileSize,
483 while ($fileDuplicate = $query->fetch())
485 $result[] = $fileDuplicate[
'FILE_ID'];
533 $lastKey = (int)$lastKey;
534 $pageSize = (int)$pageSize;
538 ,b_clouds_file_hash.FILE_SIZE as FILE_SIZE
539 ,b_clouds_file_hash.FILE_HASH as FILE_HASH
542 INNER JOIN b_clouds_file_hash ON
543 b_clouds_file_hash.BUCKET_ID = b_file.HANDLER_ID
544 AND b_clouds_file_hash.FILE_PATH = concat('/', b_file.SUBDIR, '/', b_file.FILE_NAME)
545 LEFT JOIN b_file_duplicate ON
546 b_file_duplicate.DUPLICATE_ID = b_file.ID
548 b_file.ID > " . $lastKey .
"
549 AND b_file_duplicate.DUPLICATE_ID is null
551 LIMIT " . $pageSize .
"
554 $connection = \Bitrix\Main\Application::getConnection();
555 $fileIds = $connection->query(
'
557 min(FILE_ID) as FILE_ID_MIN
558 ,max(FILE_ID) as FILE_ID_MAX
559 ,count(FILE_ID) FILE_ID_CNT
560 FROM (' . $sql .
') t
563 if ($fileIds[
'FILE_ID_CNT'] > 0)
565 $connection->queryExecute(
'INSERT IGNORE INTO b_file_hash '.$sql);
584 $duplicates = \Bitrix\Main\File\Internal\FileDuplicateTable::getList([
585 'select' => [
'DUPLICATE_ID'],
587 '=DUPLICATE_ID' => $fileIds,
590 while ($duplicate = $duplicates->fetch())
593 $p = array_search($duplicate[
'DUPLICATE_ID'], $fileIds);
604 $originals = \Bitrix\Main\File\Internal\FileDuplicateTable::getList([
605 'select' => [
'ORIGINAL_ID'],
607 '=ORIGINAL_ID' => $fileIds,
609 'order' => [
'ORIGINAL_ID' =>
'ASC'],
611 while ($original = $originals->fetch())
614 if ($originalId ===
false)
616 $originalId = $original[
'ORIGINAL_ID'];
620 $p = array_search($original[
'ORIGINAL_ID'], $fileIds);
627 if ($originalId ===
false)
629 $originalId = array_shift($fileIds);