Skip to content

Commit

Permalink
Add file sync task
Browse files Browse the repository at this point in the history
This task is intended to fix up the filesystem/db in version 4.

It will remove invalid db entries and create db entries for assets that are currently missing db entries.
  • Loading branch information
bergice committed Apr 24, 2019
1 parent bb0ae72 commit 9fde7c9
Show file tree
Hide file tree
Showing 8 changed files with 640 additions and 42 deletions.
53 changes: 53 additions & 0 deletions src/Dev/Tasks/FilesystemSyncTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace SilverStripe\Dev\Tasks;

use SilverStripe\Assets\FilesystemSyncTaskHelper;
use SilverStripe\Dev\BuildTask;
use SilverStripe\AssetAdmin\Helper\ImageThumbnailHelper;

/**
* This task will synchronise the filesystem with the database.
*
* The execution works like this:
*
* - if the skip_validate_database argument is not set (default):
* iterate files in db and check they have valid files in the filesystem - if not, remove the db entries
*
* - files in legacy folders create db entries - the file is then deleted
*
* - if the delete_broken argument is not set (default):
* files in hashed folders without db entries have db entries created
* - if the delete_broken argument is set:
* files in hashed folders without db entries are deleted
* db entry files without assets are deleted
*
* - delete folders that are now empty
*
* - generate thumbnails
*/
class FilesystemSyncTask extends BuildTask
{
private static $segment = 'FilesystemSyncTask';

protected $title = 'Filesystem Sync Task';

protected $description = '
Syncs files in the assets folder with the database.
Files in the assets folder that do not exist in the database will be added.
This is useful if you restore restore a file-only snapshot or want to sync a lot of files.
Parameters:
- path: Sets path to sync. Defaults to the root assets path.
- draft: Files will not be published if set.
- delete_broken: Hashed files without a matching database entry will be removed
and database file records will be removed if their asset is missing.
';

public function run($request)
{
$results = FilesystemSyncTaskHelper::singleton()->run($request->getVars());

// Generate Thumbnails
ImageThumbnailHelper::singleton()->run();
}
}
4 changes: 2 additions & 2 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ public function exists()
}

/**
* Find a File object by the given filename.
* Find a File object by the given name.
*
* @param string $filename Filename to search for, including any custom parent directories.
* @param string $filename Name to search for, including any custom parent directories.
* @return File
*/
public static function find($filename)
Expand Down
Loading

0 comments on commit 9fde7c9

Please sign in to comment.