MCPcopy Create free account
hub / github.com/error311/FileRise / bufferSourceToTempStream

Method bufferSourceToTempStream

src/FileRise/Domain/FileModel.php:462–514  ·  view source on GitHub ↗
(StorageAdapterInterface $srcStorage, string $srcPath)

Source from the content-addressed store, hash-verified

460 }
461
462 private static function bufferSourceToTempStream(StorageAdapterInterface $srcStorage, string $srcPath): array
463 {
464 $tmpPath = @tempnam(self::tempTransferDir(), 'frxfer_');
465 if ($tmpPath === false || $tmpPath === '') {
466 return [false, null, ''];
467 }
468
469 $tmp = @fopen($tmpPath, 'w+b');
470 if ($tmp === false) {
471 @unlink($tmpPath);
472 return [false, null, ''];
473 }
474
475 $bytes = 0;
476 $stream = $srcStorage->openReadStream($srcPath);
477 if (is_resource($stream)) {
478 $copied = @stream_copy_to_stream($stream, $tmp);
479 @fclose($stream);
480 if ($copied === false) {
481 @fclose($tmp);
482 @unlink($tmpPath);
483 return [false, null, ''];
484 }
485 $bytes = (int)$copied;
486 } elseif (is_object($stream) && method_exists($stream, 'read')) {
487 while (true) {
488 $chunk = $stream->read(8192);
489 if ($chunk === false || $chunk === '') {
490 break;
491 }
492 $written = @fwrite($tmp, $chunk);
493 if ($written === false) {
494 if (method_exists($stream, 'close')) {
495 $stream->close();
496 }
497 @fclose($tmp);
498 @unlink($tmpPath);
499 return [false, null, ''];
500 }
501 $bytes += $written;
502 }
503 if (method_exists($stream, 'close')) {
504 $stream->close();
505 }
506 } else {
507 @fclose($tmp);
508 @unlink($tmpPath);
509 return [false, null, ''];
510 }
511
512 @rewind($tmp);
513 return [$tmp, $bytes > 0 ? $bytes : null, $tmpPath];
514 }
515
516 public static function copyFilesAcrossSources(string $sourceId, string $destinationId, string $sourceFolder, string $destinationFolder, array $files): array
517 {

Callers 2

Calls 4

tempTransferDirMethod · 0.95
closeMethod · 0.80
openReadStreamMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected