protected function migrateFile($base, File $file, $legacyFilename)
    {
        $useLegacyFilenames = Config::inst()->get(FlysystemAssetStore::class, 'legacy_filenames');

        // Make sure this legacy file actually exists
        $path = $base . '/' . $legacyFilename;
        if (!file_exists($path)) {
            if ($this->logger) {
                $this->logger->warning("$legacyFilename not migrated because the file does not exist ($path)");
            }
            return false;
        }

        // Remove this file if it violates allowed_extensions
        $allowed = array_filter(File::getAllowedExtensions());
        $extension = strtolower($file->getExtension());
        if (!in_array($extension, $allowed)) {
            if ($this->config()->get('delete_invalid_files')) {
                $file->delete();
            }
            if ($this->logger) {
                $this->logger->warning("$legacyFilename not migrated because the extension $extension is not a valid extension");
            }
            return false;
        }

        // Fix file classname if it has a classname that's incompatible with it's extention
        if (!$this->validateFileClassname($file, $extension)) {
            // We disable validation (if it is enabled) so that we are able to write a corrected
            // classname, once that is changed we re-enable it for subsequent writes
            $validationEnabled = DataObject::Config()->get('validation_enabled');
            if ($validationEnabled) {
                DataObject::Config()->set('validation_enabled', false);
            }
            $destinationClass = $file->get_class_for_file_extension($extension);
            $file = $file->newClassInstance($destinationClass);
            $fileID = $file->write();
            if ($validationEnabled) {
                DataObject::Config()->set('validation_enabled', true);
            }
            $file = File::get_by_id($fileID);
        }

        // Copy local file into this filesystem
        $filename = $file->generateFilename();
        $result = $file->setFromLocalFile(
            $path,
            $filename,
            null,
            null,
            [
                'conflict' => $useLegacyFilenames ?
                    AssetStore::CONFLICT_USE_EXISTING :
                    AssetStore::CONFLICT_OVERWRITE
            ]
        );

        // Move file if the APL changes filename value
        if ($result['Filename'] !== $filename) {
            $file->setFilename($result['Filename']);
        }

        // Save and publish
        $file->write();
        if (class_exists(Versioned::class)) {
            $file->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
        }

        if (!$useLegacyFilenames) {
            // removing the legacy file since it has been migrated now and not using legacy filenames
            $removed = unlink($path);
            if (!$removed) {
                if ($this->logger) {
                    $this->logger->warning("$legacyFilename was migrated, but failed to remove the legacy file ($path)");
                }
            } elseif ($this->logger) {
                $this->logger->warning("Legacy file at $path was deleted");
            }
            return $removed;
        }
        return true;
    }
protected function migrateFile($base, File $file, $legacyFilename)
    {
        $useLegacyFilenames = Config::inst()->get(FlysystemAssetStore::class, 'legacy_filenames');

        // Make sure this legacy file actually exists
        $path = $base . '/' . $legacyFilename;
        if (!file_exists($path)) {
            if ($this->logger) {
                $this->logger->warning("$legacyFilename not migrated because the file does not exist ($path)");
            }
            return false;
        }

        // Remove this file if it violates allowed_extensions
        $allowed = array_filter(File::getAllowedExtensions());
        $extension = strtolower($file->getExtension());
        if (!in_array($extension, $allowed)) {
            if ($this->config()->get('delete_invalid_files')) {
                $file->delete();
            }
            if ($this->logger) {
                $this->logger->warning("$legacyFilename not migrated because the extension $extension is not a valid extension");
            }
            return false;
        }

        // Fix file classname if it has a classname that's incompatible with it's extention
        if (!$this->validateFileClassname($file, $extension)) {
            // We disable validation (if it is enabled) so that we are able to write a corrected
            // classname, once that is changed we re-enable it for subsequent writes
            $validationEnabled = DataObject::Config()->get('validation_enabled');
            if ($validationEnabled) {
                DataObject::Config()->set('validation_enabled', false);
            }
            $destinationClass = $file->get_class_for_file_extension($extension);
            $file = $file->newClassInstance($destinationClass);
            $fileID = $file->write();
            if ($validationEnabled) {
                DataObject::Config()->set('validation_enabled', true);
            }
            $file = File::get_by_id($fileID);
        }

        // Copy local file into this filesystem
        $filename = $file->generateFilename();
        $result = $file->setFromLocalFile(
            $path,
            $filename,
            null,
            null,
            [
                'conflict' => $useLegacyFilenames ?
                    AssetStore::CONFLICT_USE_EXISTING :
                    AssetStore::CONFLICT_OVERWRITE
            ]
        );

        // Move file if the APL changes filename value
        if ($result['Filename'] !== $filename) {
            $file->setFilename($result['Filename']);
        }

        // Save and publish
        $file->write();
        if (class_exists(Versioned::class)) {
            $file->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
        }

        if (!$useLegacyFilenames) {
            // removing the legacy file since it has been migrated now and not using legacy filenames
            $removed = unlink($path);
            if (!$removed) {
                if ($this->logger) {
                    $this->logger->warning("$legacyFilename was migrated, but failed to remove the legacy file ($path)");
                }
            } elseif ($this->logger) {
                $this->logger->warning("Legacy file at $path was deleted");
            }
            return $removed;
        }
        return true;
    }
protected function migrateFile ( $ base , File $ file , $ legacyFilename ) { $ useLegacyFilenames = Config :: inst ( ) -> get ( FlysystemAssetStore :: class , 'legacy_filenames' ) ; // Make sure this legacy file actually exists $ path = $ base . '/' . $ legacyFilename ; if ( ! file_exists ( $ path ) ) { if ( $ this -> logger ) { $ this -> logger -> warning ( "$legacyFilename not migrated because the file does not exist ($path)" ) ; } return false ; } // Remove this file if it violates allowed_extensions $ allowed = array_filter ( File :: getAllowedExtensions ( ) ) ; $ extension = strtolower ( $ file -> getExtension ( ) ) ; if ( ! in_array ( $ extension , $ allowed ) ) { if ( $ this -> config ( ) -> get ( 'delete_invalid_files' ) ) { $ file -> delete ( ) ; } if ( $ this -> logger ) { $ this -> logger -> warning ( "$legacyFilename not migrated because the extension $extension is not a valid extension" ) ; } return false ; } // Fix file classname if it has a classname that's incompatible with it's extention if ( ! $ this -> validateFileClassname ( $ file , $ extension ) ) { // We disable validation (if it is enabled) so that we are able to write a corrected // classname, once that is changed we re-enable it for subsequent writes $ validationEnabled = DataObject :: Config ( ) -> get ( 'validation_enabled' ) ; if ( $ validationEnabled ) { DataObject :: Config ( ) -> set ( 'validation_enabled' , false ) ; } $ destinationClass = $ file -> get_class_for_file_extension ( $ extension ) ; $ file = $ file -> newClassInstance ( $ destinationClass ) ; $ fileID = $ file -> write ( ) ; if ( $ validationEnabled ) { DataObject :: Config ( ) -> set ( 'validation_enabled' , true ) ; } $ file = File :: get_by_id ( $ fileID ) ; } // Copy local file into this filesystem $ filename = $ file -> generateFilename ( ) ; $ result = $ file -> setFromLocalFile ( $ path , $ filename , null , null , [ 'conflict' => $ useLegacyFilenames ? AssetStore :: CONFLICT_USE_EXISTING : AssetStore :: CONFLICT_OVERWRITE ] ) ; // Move file if the APL changes filename value if ( $ result [ 'Filename' ] !== $ filename ) { $ file -> setFilename ( $ result [ 'Filename' ] ) ; } // Save and publish $ file -> write ( ) ; if ( class_exists ( Versioned :: class ) ) { $ file -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE ) ; } if ( ! $ useLegacyFilenames ) { // removing the legacy file since it has been migrated now and not using legacy filenames $ removed = unlink ( $ path ) ; if ( ! $ removed ) { if ( $ this -> logger ) { $ this -> logger -> warning ( "$legacyFilename was migrated, but failed to remove the legacy file ($path)" ) ; } } elseif ( $ this -> logger ) { $ this -> logger -> warning ( "Legacy file at $path was deleted" ) ; } return $ removed ; } return true ; }
Migrate a single file

@param string $base Absolute base path (parent of assets folder)
@param File $file
@param string $legacyFilename
@return bool True if this file is imported successfully
@throws \SilverStripe\ORM\ValidationException
Migrate a single file