disk === 'files') { return $work(Storage::disk('files')->path($file->path)); } $tempPath = tempnam(sys_get_temp_dir(), 'thumb-src-'); if ($tempPath === false) { throw new RuntimeException('Could not create a temp file for '.$file->original_name); } try { $this->copyDown($file, $tempPath); return $work($tempPath); } finally { @unlink($tempPath); } } private function copyDown(File $file, string $tempPath): void { $stream = Storage::disk($file->disk)->readStream($file->path); $out = fopen($tempPath, 'wb'); if ($stream === null || $out === false) { if (is_resource($out)) { fclose($out); } throw new RuntimeException('Could not read '.$file->original_name.' from its storage disk.'); } try { stream_copy_to_stream($stream, $out); } finally { fclose($out); if (is_resource($stream)) { fclose($stream); } } } }