-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit aa5d82f
Showing
11 changed files
with
373 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# For more information about the properties used in this file, | ||
# please see the EditorConfig documentation: | ||
# http://editorconfig.org | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_size = 4 | ||
indent_style = space | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[{*.yml,package.json}] | ||
indent_size = 2 | ||
|
||
# The indent size used in the package.json file cannot be changed: | ||
# https://github.com/npm/npm/pull/3180#issuecomment-16336516 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Contributing | ||
|
||
Contributions are welcome! Create an issue, explaining a bug or proposal. Submit pull requests if you feel brave. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Copyright (c) 2017, Dynamic | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
|
||
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# SilverStripe File Migration Task | ||
|
||
### Summary | ||
This Build Task allows for traversing a directory recursively and migrate those files into the SilverStripe filesystem. The task checks against the allowed extensions on [`File`](https://github.com/silverstripe/silverstripe-assets/blob/1/src/File.php#L165-L185). | ||
|
||
## Requirements | ||
|
||
* SilverStripe Assets ^1.0 | ||
|
||
## Installation | ||
|
||
`composer require dynamic/silverstripe-file-migration-task` | ||
|
||
## Usage | ||
|
||
### Configuration | ||
|
||
```yaml | ||
Dynamic\FileMigration\Tasks\FileMigrationTask: | ||
# Path to directory with files (required) | ||
existing_file_system_path: '/path/to/your/files' | ||
# Base folder to create in Assets (optional) | ||
base_upload_folder: '/uploads' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
Name: dynamic-file-migration-config | ||
--- | ||
SilverStripe\Assets\File: | ||
extensions: | ||
- Dynamic\FileMigration\Extensions\FileDataExtension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "dynamic/silverstripe-file-migration-task", | ||
"type": "silverstripe-vendormodule", | ||
"description": "Sync files with the File table", | ||
"keywords": [ | ||
"silverstripe", | ||
"File", | ||
"BuildTask", | ||
"Migration" | ||
], | ||
"license": "BSD-3-Clause", | ||
"authors": [ | ||
{ | ||
"name": "Dynamic", | ||
"email": "[email protected]", | ||
"homepage": "http://www.dynamicagency.com" | ||
} | ||
], | ||
"require": { | ||
"silverstripe/assets": "^1.0@dev" | ||
}, | ||
"require-dev": { | ||
"phpunit/PHPUnit": "^5.7", | ||
"squizlabs/php_codesniffer": "*" | ||
}, | ||
"config": { | ||
"process-timeout": 600 | ||
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "1.0.x-dev" | ||
} | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Dynamic\\FileMigration\\": "src/" | ||
} | ||
}, | ||
"minimum-stability": "dev", | ||
"prefer-stable": true, | ||
"scripts": { | ||
"lint": "vendor/bin/phpcs src/ tests/", | ||
"lint-clean": "vendor/bin/phpcbf src/ tests/" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0"?> | ||
<ruleset name="SS4"> | ||
<description>Coding standard for SilverStripe 4.x</description> | ||
|
||
<!-- Don't sniff third party libraries --> | ||
<exclude-pattern>*/vendor/*</exclude-pattern> | ||
<exclude-pattern>*/thirdparty/*</exclude-pattern> | ||
|
||
<!-- Show progress and output sniff names on violation, and add colours --> | ||
<arg value="sp"/> | ||
<arg name="colors"/> | ||
|
||
<!-- Use PSR-2 as a base standard --> | ||
<rule ref="PSR2"> | ||
<!-- Allow classes to not declare a namespace --> | ||
<exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace"/> | ||
|
||
<!-- Allow underscores in class names --> | ||
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps"/> | ||
|
||
<!-- Allow non camel cased method names --> | ||
<exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps"/> | ||
</rule> | ||
</ruleset> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<phpunit bootstrap="vendor/silverstripe/cms/tests/bootstrap.php" colors="true"> | ||
<testsuite name="silverstripe-file-migration-task"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
|
||
<filter> | ||
<whitelist addUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">src/</directory> | ||
<exclude> | ||
<directory suffix=".php">tests/</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Dynamic\FileMigration\Extensions; | ||
|
||
use SilverStripe\ORM\DataExtension; | ||
|
||
/** | ||
* Class FileDataExtension | ||
* @package Dynamic\FileSync\Extensions | ||
*/ | ||
class FileDataExtension extends DataExtension | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private static $db = [ | ||
'LegacyPath' => 'Text', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
<?php | ||
|
||
namespace Dynamic\FileMigration\Tasks; | ||
|
||
use SilverStripe\Assets\File; | ||
use SilverStripe\Assets\Folder; | ||
use SilverStripe\Control\Director; | ||
use SilverStripe\Dev\BuildTask; | ||
|
||
/** | ||
* Class FileSyncTask | ||
* @package Dynamic\FileSync\Tasks | ||
*/ | ||
class FileMigrationTask extends BuildTask | ||
{ | ||
/** | ||
* @var string $title Shown in the overview on the {@link TaskRunner} | ||
* HTML or CLI interface. Should be short and concise, no HTML allowed. | ||
*/ | ||
protected $title = 'SilverStripe File Migration Task'; | ||
|
||
/** | ||
* @var string $description Describe the implications the task has, | ||
* and the changes it makes. Accepts HTML formatting. | ||
*/ | ||
protected $description = 'A task for migration a local file set to the SilverStripe Assets Filesystem.'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private static $segment = 'FileMigrationTask'; | ||
|
||
/** | ||
* @var | ||
*/ | ||
private static $existing_file_system_path; | ||
|
||
/** | ||
* @var | ||
*/ | ||
private static $base_upload_folder = null; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private static $migrate_directory_structure = true; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private static $publish_on_migration = true; | ||
|
||
/** | ||
* @var | ||
*/ | ||
private $directory_map = []; | ||
|
||
/** | ||
* @param \SilverStripe\Control\HTTPRequest $request | ||
*/ | ||
public function run($request) | ||
{ | ||
sleep(1); | ||
$count = 0; | ||
$migrateDirectoryStructure = $this->config()->get('migrate_directory_structure'); | ||
$publish = $this->config()->get('publish_on_migration'); | ||
|
||
foreach ($this->traverseDirectory() as $file) { | ||
$folder = null; | ||
if ($migrateDirectoryStructure) { | ||
$folder = $this->processDirectory($file); | ||
} | ||
|
||
$destinationName = ($migrateDirectoryStructure) ? $this->getOriginalFilename($file) : null; | ||
if ($this->migrateFile($file, $publish, $folder, $destinationName)) { | ||
$count++; | ||
} | ||
} | ||
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]; | ||
static::write_it("{$count} files migration total time: {$time}"); | ||
} | ||
|
||
/** | ||
* @param null $directory | ||
* @return \Generator | ||
*/ | ||
protected function traverseDirectory($directory = null) | ||
{ | ||
$directory = ($directory !== null) ?: $this->config()->get('existing_file_system_path'); | ||
|
||
$objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory), \RecursiveIteratorIterator::SELF_FIRST); | ||
$allowedExtensions = File::singleton()->config()->get('allowed_extensions'); | ||
|
||
foreach ($objects as $name => $object) { | ||
if (is_file($name)) { | ||
$validFile = false; | ||
foreach ($this->yieldExtensions($allowedExtensions) as $extension) { | ||
if ($validFile == false && preg_match("/\.{$extension}/", $name)) { | ||
$validFile = true; | ||
yield $name; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param $allowedExtensions | ||
* @return \Generator | ||
*/ | ||
protected function yieldExtensions($allowedExtensions) | ||
{ | ||
foreach ($allowedExtensions as $extension) { | ||
yield $extension; | ||
} | ||
} | ||
|
||
/** | ||
* @param $path | ||
* @return bool|null|Folder | ||
*/ | ||
protected function processDirectory($path) | ||
{ | ||
$parts = explode('/', $path); | ||
$start = count($parts) - 4; | ||
|
||
while ($start < count($parts) - 1) { | ||
$reImplode[] = $parts[$start]; | ||
$start++; | ||
} | ||
|
||
$checkDirectory = $this->config()->get('base_upload_folder') . '/' . implode('/', $reImplode); | ||
|
||
if (!isset($this->directory_map[$checkDirectory])) { | ||
if ($folder = Folder::find_or_make($this->config()->get('base_upload_folder') . '/' . implode('/', $reImplode))) { | ||
static::write_it("New folder created and cached: {$folder->Filename}"); | ||
$this->directory_map[$checkDirectory] = $folder; | ||
return $folder; | ||
} | ||
} else { | ||
static::write_it("Using cached folder: {$this->directory_map[$checkDirectory]->Filename}"); | ||
return $this->directory_map[$checkDirectory]; | ||
} | ||
|
||
static::write_it("No folder could be found or created. File will be placed at the top level."); | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param $file | ||
* @return mixed | ||
*/ | ||
protected function getOriginalFilename($file) | ||
{ | ||
$parts = explode('/', $file); | ||
return $parts[count($parts) - 1]; | ||
} | ||
|
||
/** | ||
* @param $localPath | ||
* @param null $destination | ||
* @return bool | ||
* @throws \SilverStripe\ORM\ValidationException | ||
*/ | ||
protected function migrateFile($localPath, $publish, $destination = null, $originalFilename = null) | ||
{ | ||
if (!$existing = File::get()->filter('LegacyPath', $localPath)->first()) { | ||
$newFile = File::create(); | ||
$destinationName = ($destination !== null && $originalFilename !== null) ? $destination->Filename . $originalFilename : null; | ||
$newFile->setFromLocalFile($localPath, $destinationName); | ||
$newFile->LegacyPath = $localPath; | ||
$newFile->write(); | ||
|
||
if ($publish) { | ||
$newFile->publishFile(); | ||
} | ||
|
||
static::write_it("{$newFile->ID} - File {$newFile->Name} created.", false); | ||
unset($newFile); | ||
return true; | ||
} | ||
|
||
static::write_it("File {$existing->Name} already exists. Skipping"); | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* @param bool $indent | ||
*/ | ||
protected static function write_it($message = '', $indent = true) | ||
{ | ||
if (Director::is_cli()) { | ||
if ($indent) { | ||
echo "\t"; | ||
} | ||
echo "{$message}\n"; | ||
} else { | ||
if ($indent) { | ||
echo "<p style='margin-left: 25px;'>{$message}</p>"; | ||
} else { | ||
echo "<p>{$message}</p>"; | ||
} | ||
} | ||
} | ||
} |