public function testCopyBetweenFilesystems() { $manager = new MountManager(); $fs1 = Mockery::mock('League\\Flysystem\\FilesystemInterface'); $fs2 = Mockery::mock('League\\Flysystem\\FilesystemInterface'); $manager->mountFilesystem('fs1', $fs1); $manager->mountFilesystem('fs2', $fs2); $filename = 'test.txt'; $buffer = tmpfile(); $fs1->shouldReceive('readStream')->once()->with($filename)->andReturn($buffer); $fs2->shouldReceive('writeStream')->once()->with($filename, $buffer)->andReturn(true); $response = $manager->copy("fs1://{$filename}", "fs2://{$filename}"); $this->assertTrue($response); // test failed status $fs1->shouldReceive('readStream')->once()->with($filename)->andReturn(false); $status = $manager->copy("fs1://{$filename}", "fs2://{$filename}"); $this->assertFalse($status); $buffer = tmpfile(); $fs1->shouldReceive('readStream')->once()->with($filename)->andReturn($buffer); $fs2->shouldReceive('writeStream')->once()->with($filename, $buffer)->andReturn(false); $status = $manager->copy("fs1://{$filename}", "fs2://{$filename}"); $this->assertFalse($status); $buffer = tmpfile(); $fs1->shouldReceive('readStream')->once()->with($filename)->andReturn($buffer); $fs2->shouldReceive('writeStream')->once()->with($filename, $buffer)->andReturn(true); $status = $manager->copy("fs1://{$filename}", "fs2://{$filename}"); $this->assertTrue($status); }
/** * Duplicate a file given it's URL * * @param string $url * @return string */ public function duplicate($url) { // Make the destination path $current_path = $this->helpers->path($url); $filename = basename($current_path); $dst_disk = $this->disks ? $this->disks->getFilesystem('dst') : $this->disk; $new_path = $this->storage->makeNestedAndUniquePath($filename, $dst_disk); // Copy, supporting alternative destination disks if ($this->disks) { $this->disks->copy('src://' . $current_path, 'dst://' . $new_path); } else { $this->disk->copy($current_path, $new_path); } // Return the Upchuck URL return $this->helpers->url($new_path); }
/** * @param FileNode $from * @param FileNode $to * * @return FileNode * @throws TransferFailedException */ public function copyTo(FileNode $from, FileNode $to) { $mountManager = new MountManager(['from' => $from->getFilesystem(), 'to' => $to->getFilesystem()]); $this->log(LogLevel::INFO, "Copying file from: '{from}', to: '{to}'", ['from' => $from, 'to' => $to]); if (!$mountManager->copy('from://' . $from->getPath(), 'to://' . $to->getPath())) { throw new TransferFailedException($from, $to); } return $to; }
private function copyormovefile($source, $destination, $domove = true) { $filesystemid = $this->extractFilesystemIdFromPath($source); $path = $this->extractPathFromPath($source); $filesystemid2 = $this->extractFilesystemIdFromPath($destination); $newname = $this->extractPathFromPath($destination); $returndata = array('success' => false); $filesystems = $this->getFilesystems(); if (count($filesystems) == 0) { return $this->throwJsonError('No Filesystem found'); } else { $filesystem = isset($filesystems[$filesystemid]) ? $filesystems[$filesystemid] : null; if (!$filesystem) { return $this->throwJsonError('Filesystem not found'); } $fs = $this->getFs($filesystem); if ($fs->has($path)) { //$metadata = $fs->getMetadata($path); if ($filesystemid == $filesystemid2) { try { $fs->rename($path, $newname); } catch (FileExistsException $e) { return $this->throwJsonError($e->getMessage()); } catch (FileNotFoundException $e) { return $this->throwJsonError($e->getMessage()); } catch (NotSupportedException $e) { return $this->throwJsonError($e->getMessage()); } catch (RootViolationException $e) { return $this->throwJsonError($e->getMessage()); } catch (\Exception $e) { return $this->throwJsonError($e->getMessage()); } } else { $filesystem2 = isset($filesystems[$filesystemid2]) ? $filesystems[$filesystemid2] : null; if (!$filesystem2) { return $this->throwJsonError('Filesystem not found'); } $fs2 = $this->getFs($filesystem2); $manager = new MountManager(['fssource' => $fs, 'fsdestination' => $fs2]); try { if ($domove) { $manager->move('fssource:/' . $path, 'fsdestination:/' . $newname); } else { $manager->copy('fssource:/' . $path, 'fsdestination:/' . $newname); } } catch (FileExistsException $e) { return $this->throwJsonError($e->getMessage()); } catch (FileNotFoundException $e) { return $this->throwJsonError($e->getMessage()); } catch (NotSupportedException $e) { return $this->throwJsonError($e->getMessage()); } catch (RootViolationException $e) { return $this->throwJsonError($e->getMessage()); } catch (\Exception $e) { return $this->throwJsonError($e->getMessage()); } } $returndata = array('success' => true); } } $response = new Response(json_encode($returndata)); $response->headers->set('Content-Type', 'application/json; charset=UTF-8'); return $response; }