Skip to content

Commit

Permalink
Add exception handling, use native validate() function
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Carlino committed Apr 29, 2019
1 parent 71c72f0 commit ad8202b
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions src/FileMigrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataQuery;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Versioned\Versioned;
use SilverStripe\Assets\Dev\Tasks\FileMigrationHelper as BaseFileMigrationHelper;

/**
* Service to help migrate File dataobjects to the new APL.
Expand All @@ -23,7 +25,7 @@
*
* @deprecated 1.4.0 Use \SilverStripe\Assets\Dev\Tasks\FileMigrationHelper instead
*/
class FileMigrationHelper extends \SilverStripe\Assets\Dev\Tasks\FileMigrationHelper
class FileMigrationHelper extends BaseFileMigrationHelper
{
use Injectable;
use Configurable;
Expand All @@ -44,7 +46,7 @@ class FileMigrationHelper extends \SilverStripe\Assets\Dev\Tasks\FileMigrationHe
* @config
* @var int
*/
private static $log_interval = 100;
private static $log_interval = 10;

private static $dependencies = [
'logger' => '%$' . LoggerInterface::class,
Expand Down Expand Up @@ -98,7 +100,6 @@ public function run($base = null)
foreach ($query as $file) {
// Bypass the accessor and the filename from the column
$filename = $file->getField('Filename');

$success = $this->migrateFile($base, $file, $filename);

if ($processedCount % $this->config()->get('log_interval') === 0) {
Expand Down Expand Up @@ -140,20 +141,29 @@ protected function migrateFile($base, File $file, $legacyFilename)
return false;
}

// Remove this file if it violates allowed_extensions
$allowed = array_filter(File::getAllowedExtensions());
$extension = strtolower($file->getExtension());
if (!in_array($extension, $allowed)) {
$validationResult = $file->validate();
if (!$validationResult->isValid()) {
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");
$messages = implode("\n\n", array_map(function ($msg) {
return $msg['message'];
}, $validationResult->getMessages()));

$this->logger->warning(
sprintf(
"%s was not migrated because the file is not valid. More information: %s",
$legacyFilename,
$messages
)
);
}
return false;
}

// Fix file classname if it has a classname that's incompatible with it's extention
$extension = $file->getExtension();
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
Expand All @@ -169,6 +179,16 @@ protected function migrateFile($base, File $file, $legacyFilename)
}
$file = File::get_by_id($fileID);
}

if (!is_readable($path)) {
if ($this->logger) {
$this->logger->warning(sprintf(
'File at %s is not readable and could not be migrated.',
$path
));
return false;
}
}

// Copy local file into this filesystem
$filename = $file->generateFilename();
Expand All @@ -190,7 +210,19 @@ protected function migrateFile($base, File $file, $legacyFilename)
}

// Save and publish
$file->write();
try {
$file->write();
} catch (ValidationException $e) {
if ($this->logger) {
$this->logger->error(sprintf(
"File %s could not be migrated due to an error.
This problem likely existed before the migration began. Error: %s",
$legacyFilename,
$e->getMessage()
));
}
return false;
}
if (class_exists(Versioned::class)) {
$file->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
}
Expand All @@ -205,7 +237,6 @@ protected function migrateFile($base, File $file, $legacyFilename)
}
return $removed;
}
return true;
}

/**
Expand Down

0 comments on commit ad8202b

Please sign in to comment.