/** * Create the bag, generate tar. * * @param array $file_ids Array of ids, posted from the form. * @param string $name The name of the bag. * * @return boolean $success True if the new bag validates. */ function bagithelpers_doBagIt($collection_id, $collection_name) { $db = get_db(); $key = sha1(microtime(true) . mt_rand(10000, 90000)); $collection_name = $collection_name . '-' . $key; // Instantiate the bag, get the collection. $bag = new BagIt(BAGIT_BAG_DIRECTORY . '/' . $collection_name); $collection = $db->getTable('BagitFileCollection')->find($collection_id); // Get the files associated with the collection. $files = $collection->getFiles(); // Retrieve the files and add them to the new bag. foreach ($files as $file) { $bagurl = file_display_url($file); $bag->fetch->add($bagurl, 'data/' . $file->original_filename); } // Update the hashes. $bag->update(); // Tar it up. $bag->package(BAGIT_BAG_DIRECTORY . '/' . $collection_name); return $bag->isValid() ? $collection_name : false; }
public function testValidateChecksum() { $tmp = tmpdir(); try { mkdir($tmp); file_put_contents($tmp . "/manifest-sha1.txt", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa data/missing.txt\n"); mkdir($tmp . '/data'); touch($tmp . '/data/missing.txt'); $bag = new BagIt($tmp); $bag->validate(); $bagErrors = $bag->getBagErrors(); $this->assertFalse($bag->isValid()); $this->assertTrue(seenAtKey($bagErrors, 0, 'data/missing.txt')); $this->assertTrue(seenAtKey($bagErrors, 1, 'Checksum mismatch.')); } 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))); }