(string $sourceId, string $destinationId, string $sourceFolder, string $destinationFolder, array $files)
| 514 | } |
| 515 | |
| 516 | public static function copyFilesAcrossSources(string $sourceId, string $destinationId, string $sourceFolder, string $destinationFolder, array $files): array |
| 517 | { |
| 518 | $errors = []; |
| 519 | $srcStorage = StorageRegistry::getAdapter($sourceId); |
| 520 | $dstStorage = StorageRegistry::getAdapter($destinationId); |
| 521 | |
| 522 | $srcRoot = class_exists('SourceContext') ? SourceContext::uploadRootForId($sourceId) : rtrim((string)UPLOAD_DIR, '/\\') . DIRECTORY_SEPARATOR; |
| 523 | $dstRoot = class_exists('SourceContext') ? SourceContext::uploadRootForId($destinationId) : rtrim((string)UPLOAD_DIR, '/\\') . DIRECTORY_SEPARATOR; |
| 524 | |
| 525 | [$sourceDir, $err] = self::resolveFolderPathForAdapter($srcStorage, $srcRoot, $sourceFolder, false); |
| 526 | if ($err) { |
| 527 | return ["error" => $err]; |
| 528 | } |
| 529 | [$destDir, $err] = self::resolveFolderPathForAdapter($dstStorage, $dstRoot, $destinationFolder, true); |
| 530 | if ($err) { |
| 531 | return ["error" => $err]; |
| 532 | } |
| 533 | |
| 534 | $sourceDir .= DIRECTORY_SEPARATOR; |
| 535 | $destDir .= DIRECTORY_SEPARATOR; |
| 536 | |
| 537 | $srcMetaRoot = self::metaRootForId($sourceId); |
| 538 | $destMetaRoot = self::metaRootForId($destinationId); |
| 539 | |
| 540 | $srcMetaFile = self::getMetadataFilePathForRoot($srcMetaRoot, $sourceFolder); |
| 541 | $destMetaFile = self::getMetadataFilePathForRoot($destMetaRoot, $destinationFolder); |
| 542 | |
| 543 | $srcMetadata = is_file($srcMetaFile) ? (json_decode((string)file_get_contents($srcMetaFile), true) ?: []) : []; |
| 544 | $destMetadata = is_file($destMetaFile) ? (json_decode((string)file_get_contents($destMetaFile), true) ?: []) : []; |
| 545 | |
| 546 | $safeFileNamePattern = REGEX_FILE_NAME; |
| 547 | $actor = $_SESSION['username'] ?? 'Unknown'; |
| 548 | $now = date(DATE_TIME_FORMAT); |
| 549 | |
| 550 | foreach ($files as $fileName) { |
| 551 | $originalName = basename(trim((string)$fileName)); |
| 552 | $basename = $originalName; |
| 553 | |
| 554 | if (!preg_match($safeFileNamePattern, $basename)) { |
| 555 | $errors[] = "$basename has an invalid name."; |
| 556 | continue; |
| 557 | } |
| 558 | |
| 559 | $srcPath = $sourceDir . $originalName; |
| 560 | $destPath = $destDir . $basename; |
| 561 | |
| 562 | clearstatcache(); |
| 563 | $stat = $srcStorage->stat($srcPath); |
| 564 | if ($stat === null || ($stat['type'] ?? '') !== 'file') { |
| 565 | $errors[] = "$originalName does not exist in source."; |
| 566 | continue; |
| 567 | } |
| 568 | |
| 569 | if ($dstStorage->stat($destPath) !== null) { |
| 570 | $basename = self::getUniqueFileNameForAdapter($dstStorage, $destDir, $basename); |
| 571 | $destPath = $destDir . $basename; |
| 572 | } |
| 573 |
no test coverage detected