/** * Migrate a single file * * @param string $base Absolute base path (parent of assets folder) * @param File $file * @param type $legacyFilename * @return bool True if this file is imported successfully */ protected function migrateFile($base, File $file, $legacyFilename) { // Make sure this legacy file actually exists $path = $base . '/' . $legacyFilename; if (!file_exists($path)) { return false; } // Copy local file into this filesystem $filename = $file->getFilename(); $result = $file->setFromLocalFile($path, $filename, null, null, array('conflict' => AssetStore::CONFLICT_OVERWRITE)); // Move file if the APL changes filename value if ($result['Filename'] !== $filename) { $this->setFilename($result['Filename']); } // Save $file->write(); return true; }
public function testCreateWithFilenameWithSubfolder() { // Note: We can't use fixtures/setUp() for this, as we want to create the db record manually. // Creating the folder is necessary to avoid having "Filename" overwritten by setName()/setRelativePath(), // because the parent folders don't exist in the database $folder = Folder::find_or_make('/FileTest/'); $testfilePath = BASE_PATH . '/assets/FileTest/CreateWithFilenameHasCorrectPath.txt'; // Important: No leading slash $fh = fopen($testfilePath, "w"); fwrite($fh, str_repeat('x', 1000000)); fclose($fh); $file = new File(); $file->setFromLocalFile($testfilePath); $file->ParentID = $folder->ID; $file->write(); $this->assertEquals('CreateWithFilenameHasCorrectPath.txt', $file->Name, '"Name" property is automatically set from "Filename"'); $this->assertEquals('FileTest/CreateWithFilenameHasCorrectPath.txt', $file->Filename, '"Filename" property remains unchanged'); // TODO This should be auto-detected, see File->updateFilesystem() // $this->assertInstanceOf('Folder', $file->Parent(), 'Parent folder is created in database'); // $this->assertFileExists($file->Parent()->getURL(), 'Parent folder is created on filesystem'); // $this->assertEquals('FileTest', $file->Parent()->Name); // $this->assertInstanceOf('Folder', $file->Parent()->Parent(), 'Grandparent folder is created in database'); // $this->assertFileExists($file->Parent()->Parent()->getURL(), // 'Grandparent folder is created on filesystem'); // $this->assertEquals('assets', $file->Parent()->Parent()->Name); }