public function testUnlinkErrorIsDir() { $filename = vfsStream::newDirectory('test')->at($this->_root)->url(); try { FileObject::unlink($filename); $this->fail('Expected exception was not thrown'); } catch (\RuntimeException $e) { $this->assertEquals("Error deleting file '{$filename}'", $e->getMessage()); $this->assertFileExists($filename); } }
/** * Write Package content * * @param array $data Package data * @param string $file Source file * @param bool $deleteSource Delete source file * @return integer Number of fragments */ public function writeContent($data, $file, $deleteSource) { $id = $data['Id']; $baseName = $this->getPath($id) . "/{$id}-"; $fileSize = @$data['Size']; $maxFragmentSize = @$data['MaxFragmentSize'] * 1024; // Kilobytes => Bytes if (!$data['FileLocation']) { // No file $numFragments = 0; } elseif ($fileSize == 0 or $maxFragmentSize == 0 or $fileSize <= $maxFragmentSize) { // Don't split, just copy/move/rename the file if ($deleteSource) { \Library\FileObject::rename($file, $baseName . '1'); } else { \Library\FileObject::copy($file, $baseName . '1'); } $numFragments = 1; } else { // Split file into fragments of nearly identical size no bigger than $maxFragmentSize. $fragmentSize = ceil($fileSize / ceil($fileSize / $maxFragmentSize)); // Determine number of fragments by files actually written $numFragments = 0; $input = new \Library\FileObject($file, 'rb'); while ($fragment = $input->fread($fragmentSize)) { $numFragments++; \Library\FileObject::filePutContents($baseName . $numFragments, $fragment); } unset($input); // Close file before eventually trying to delete it if ($deleteSource) { \Library\FileObject::unlink($file); } } return $numFragments; }
/** * Create a platform-specific archive if the source file is not already an * archive of the correct type * * This is currently only supported for the 'windows' platform which expects * a ZIP archive. If the Zip extension is not available, the source file is * assumed to be a ZIP archive and a warning is generated. * * The return value is the path to the archive file - either the source file * or a generated archive. * * @param array $data Package data * @param string $path Path where a new archive will be created * @param bool $deleteSource Delete source file after successfully creating an archive * @return string Path to archive file * @throws RuntimeException if an error occurs */ public function autoArchive($data, $path, $deleteSource) { $source = $data['FileLocation']; if (!$source) { return $source; } $archiveManager = $this->_serviceManager->get('Library\\ArchiveManager'); switch ($data['Platform']) { case 'windows': $type = \Library\ArchiveManager::ZIP; break; default: // other platforms not implemented yet return $source; } if (!$archiveManager->isSupported($type)) { trigger_error("Support for archive type '{$type}' not available. Assuming archive.", E_USER_NOTICE); return $source; } if ($archiveManager->isArchive($type, $source)) { // Already an archive of reqired type. Do nothing. return $source; } try { $filename = "{$path}/archive"; $archive = $archiveManager->createArchive($type, $filename); $archiveManager->addFile($archive, $source, $data['FileName']); $archiveManager->closeArchive($archive); if ($deleteSource) { \Library\FileObject::unlink($source); } } catch (\Exception $e) { if (isset($archive)) { $archiveManager->closeArchive($archive, true); \Library\FileObject::unlink($filename); } throw new RuntimeException($e->getMessage(), (int) $e->getCode(), $e); } return $filename; }