/** * Read the Bag, unpack it, drop files into the Dropbox files directory. * * @param string $filename The name of the uploaded bag. * * @return boolean $success True if the read succeeds. */ function bagithelpers_doReadBagIt($filename) { $success = false; $bag = new BagIt(BAGIT_TMP_DIRECTORY . '/' . $filename); $bag->fetch->download(); $bag->update(); $bag->validate(); if (count($bag->getBagErrors()) == 0) { // Copy each of the files. foreach ($bag->getBagContents() as $file) { copy($file, BASE_DIR . '/plugins/Dropbox/files/' . basename($file)); } $success = true; } return $success; }
public function testGetBagContents() { $bagContents = $this->bag->getBagContents(); $this->assertInternalType('array', $bagContents); $this->assertEquals(0, count($bagContents)); $tmp = tmpdir(); try { mkdir($tmp); mkdir("{$tmp}/data"); file_put_contents($tmp . "/data/something.txt", "Source-organization: University of Virginia Alderman Library\n" . "Contact-name: Eric Rochester\n" . "Bag-size: very, very small\n"); $bag = new BagIt($tmp); $bagContents = $bag->getBagContents(); $this->assertEquals(1, count($bagContents)); $this->assertEquals($tmp . '/data/something.txt', $bagContents[0]); } catch (Exception $e) { rrmdir($tmp); throw $e; } rrmdir($tmp); }
/** * This is the use case for consuming a bag from someone else. The user * does these actions: * * <ol> * <li>Open the bag;</li> * <li>Validate the downloaded contents;</li> * <li>Fetch on-line items in the bag;</li> * <li>Validate the bag's contents; and</li> * <li>Copy items from the bag onto the local disk.</li> * </ol> */ public function testBagConsumer() { $srcbag = __DIR__ . '/TestBag.tgz'; // 1. Open the bag; $bag = new BagIt($srcbag); $this->queueRm($bag->bagDirectory); $this->assertTrue($bag->isValid()); $this->assertTrue($bag->isExtended()); $bagInfo = $bag->getBagInfo(); $this->assertEquals('0.96', $bagInfo['version']); $this->assertEquals('UTF-8', $bagInfo['encoding']); $this->assertEquals('sha1', $bagInfo['hash']); $this->assertEquals('sha1', $bag->getHashEncoding()); $this->assertEquals(7, count($bag->getBagContents())); $this->assertEquals(0, count($bag->getBagErrors())); // 2. Validate the downloaded contents; $bag->validate(); $this->assertEquals(0, count($bag->getBagErrors())); // 3. Fetch on-line items in the bag; $bag->fetch->download(); $bag->update(); $bag->validate(); $this->assertEquals(8, count($bag->getBagContents())); // 4. Validate the bag's contents; and $this->assertEquals(0, count($bag->getBagErrors())); // 5. Copy items from the bag onto the local disk. $tmpdir = tmpdir(); mkdir($tmpdir); $this->queueRm($tmpdir); foreach ($bag->getBagContents() as $bagFile) { $basename = basename($bagFile); copy($bagFile, "{$tmpdir}/{$basename}"); } $this->assertEquals(count($bag->getBagContents()) + 2, count(scandir($tmpdir))); $this->assertEquals(count($bag->manifest->getData()) + 2, count(scandir($tmpdir))); }