(string $sourceId, string $destinationId, string $sourceFolder, string $destinationFolder, array $files)
| 661 | } |
| 662 | |
| 663 | public static function moveFilesAcrossSources(string $sourceId, string $destinationId, string $sourceFolder, string $destinationFolder, array $files): array |
| 664 | { |
| 665 | $errors = []; |
| 666 | $srcStorage = StorageRegistry::getAdapter($sourceId); |
| 667 | $dstStorage = StorageRegistry::getAdapter($destinationId); |
| 668 | |
| 669 | $srcRoot = class_exists('SourceContext') ? SourceContext::uploadRootForId($sourceId) : rtrim((string)UPLOAD_DIR, '/\\') . DIRECTORY_SEPARATOR; |
| 670 | $dstRoot = class_exists('SourceContext') ? SourceContext::uploadRootForId($destinationId) : rtrim((string)UPLOAD_DIR, '/\\') . DIRECTORY_SEPARATOR; |
| 671 | |
| 672 | [$sourceDir, $err] = self::resolveFolderPathForAdapter($srcStorage, $srcRoot, $sourceFolder, false); |
| 673 | if ($err) { |
| 674 | return ["error" => $err]; |
| 675 | } |
| 676 | [$destDir, $err] = self::resolveFolderPathForAdapter($dstStorage, $dstRoot, $destinationFolder, true); |
| 677 | if ($err) { |
| 678 | return ["error" => $err]; |
| 679 | } |
| 680 | |
| 681 | $sourceDir .= DIRECTORY_SEPARATOR; |
| 682 | $destDir .= DIRECTORY_SEPARATOR; |
| 683 | |
| 684 | $srcMetaRoot = self::metaRootForId($sourceId); |
| 685 | $destMetaRoot = self::metaRootForId($destinationId); |
| 686 | |
| 687 | $srcMetaFile = self::getMetadataFilePathForRoot($srcMetaRoot, $sourceFolder); |
| 688 | $destMetaFile = self::getMetadataFilePathForRoot($destMetaRoot, $destinationFolder); |
| 689 | |
| 690 | $srcMetadata = is_file($srcMetaFile) ? (json_decode((string)file_get_contents($srcMetaFile), true) ?: []) : []; |
| 691 | $destMetadata = is_file($destMetaFile) ? (json_decode((string)file_get_contents($destMetaFile), true) ?: []) : []; |
| 692 | |
| 693 | $movedFiles = []; |
| 694 | $safeFileNamePattern = REGEX_FILE_NAME; |
| 695 | |
| 696 | foreach ($files as $fileName) { |
| 697 | $originalName = basename(trim((string)$fileName)); |
| 698 | $basename = $originalName; |
| 699 | |
| 700 | if (!preg_match($safeFileNamePattern, $basename)) { |
| 701 | $errors[] = "$basename has invalid characters."; |
| 702 | continue; |
| 703 | } |
| 704 | |
| 705 | $srcPath = $sourceDir . $originalName; |
| 706 | $destPath = $destDir . $basename; |
| 707 | |
| 708 | clearstatcache(); |
| 709 | $stat = $srcStorage->stat($srcPath); |
| 710 | if ($stat === null || ($stat['type'] ?? '') !== 'file') { |
| 711 | $errors[] = "$originalName does not exist in source."; |
| 712 | continue; |
| 713 | } |
| 714 | |
| 715 | if ($dstStorage->stat($destPath) !== null) { |
| 716 | $basename = self::getUniqueFileNameForAdapter($dstStorage, $destDir, $basename); |
| 717 | $destPath = $destDir . $basename; |
| 718 | } |
| 719 | |
| 720 | $stream = $srcStorage->openReadStream($srcPath); |
no test coverage detected