From 0620392dc4fc5286888445cc535c2935041645cc Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 6 Jan 2017 19:48:50 +1300 Subject: [PATCH] FTFileMakerTask --- code/tasks/FTFileMakerTask.php | 201 +++++++++++++++++++++++++++++++++ composer.json | 3 +- 2 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 code/tasks/FTFileMakerTask.php diff --git a/code/tasks/FTFileMakerTask.php b/code/tasks/FTFileMakerTask.php new file mode 100644 index 0000000..1747d38 --- /dev/null +++ b/code/tasks/FTFileMakerTask.php @@ -0,0 +1,201 @@ + 'SilverStripe\Assets\File', + 'animated.gif' => 'SilverStripe\Assets\Image', + 'document.docx' => 'SilverStripe\Assets\File', + 'document.pdf' => 'SilverStripe\Assets\File', + 'image-huge-tall.jpg' => 'SilverStripe\Assets\Image', + 'image-huge-wide.jpg' => 'SilverStripe\Assets\Image', + 'image-large.jpg' => 'SilverStripe\Assets\Image', + 'image-large.png' => 'SilverStripe\Assets\Image', + 'image-large.gif' => 'SilverStripe\Assets\Image', + 'image-medium.jpg' => 'SilverStripe\Assets\Image', + 'image-small.jpg' => 'SilverStripe\Assets\Image', + 'image-tiny.jpg' => 'SilverStripe\Assets\Image', + 'image-medium.bmp' => 'SilverStripe\Assets\File', + 'spreadsheet.xlsx' => 'SilverStripe\Assets\File', + 'video.m4v' => 'SilverStripe\Assets\File', + ]; + + protected $folderCountByDepth = [ + 0 => 2, + 1 => 2, + 2 => 2, + 3 => 2, + 4 => 2, + ]; + + protected $fileCountByDepth = [ + 0 => 100, + 1 => 30, + 2 => 5, + 3 => 5, + 4 => 5, + ]; + + /** + * @var int Constrained by elements in $folderCountByDepth and $fileCountByDepth + */ + protected $depth = 2; + + public function run($request) + { + echo "Making files\n"; + + if ($request->getVar('reset')) { + $this->reset(); + } + + $fixtureFilePaths = $this->downloadFixtureFiles(); + $this->generateFiles($fixtureFilePaths); + } + + protected function reset() + { + echo "Resetting assets\n"; + + DB::query('TRUNCATE "File"'); + DB::query('TRUNCATE "File_Live"'); + DB::query('TRUNCATE "File_versions"'); + + if (file_exists(ASSETS_PATH) && ASSETS_PATH && ASSETS_PATH !== '/') { + exec("rm -rf " . ASSETS_PATH); + } + } + + protected function downloadFixtureFiles() + { + $client = new Client(['base_uri' => $this->fixtureFileBaseUrl]); + + // Initiate each request but do not block + $promises = []; + $paths = []; + foreach ($this->fixtureFileNames as $filename) { + $path = TEMP_FOLDER . '/' . $filename; + $paths[$filename] = $path; + $url = "{$this->fixtureFileBaseUrl}/{$filename}"; + if (!file_exists($path)) { + $promises[$filename] = $client->getAsync($filename, [ + 'sink' => $path + ]); + echo "Downloading $url\n"; + } + } + + // Wait on all of the requests to complete. Throws a ConnectException + // if any of the requests fail + Promise\unwrap($promises); + + return $paths; + } + + protected function generateFiles($fixtureFilePaths, $depth = 0, $prefix = "0", $parentID = 0) + { + $folderCount = $this->folderCountByDepth[$depth]; + $fileCount = $this->fileCountByDepth[$depth]; + + for ($i=1; $i<=$folderCount; $i++) { + $folder = new Folder([ + 'ParentID' => $parentID, + 'Title' => "testfolder-{$prefix}{$i}", + 'Name' => "testfolder-{$prefix}{$i}", + ]); + $folder->write(); + echo "\n"; + echo "Created Folder: '$folder->Title'\n"; + + for ($j=1; $j<=$fileCount; $j++) { + $randomFileName = array_keys($fixtureFilePaths)[rand(0, count($fixtureFilePaths)-1)]; + $randomFilePath = $fixtureFilePaths[$randomFileName]; + + $fileName = pathinfo($randomFilePath, PATHINFO_FILENAME) + . "-{$prefix}-{$j}" + . "." + . pathinfo($randomFilePath, PATHINFO_EXTENSION); + + // Add a random prefix to avoid all types of files showing up on a single screen page + $fileName = substr(md5($fileName), 0, 5) . '-' . $fileName; + + $class = $this->fixtureFileTypes[$randomFileName]; + + $file = new $class([ + 'ParentID' => $folder->ID, + 'Title' => $fileName, + 'Name' => $fileName, + ]); + $file->File->setFromLocalFile($randomFilePath, $folder->getFilename() . $fileName); + $file->write(); + + // Randomly publish + if (rand(0, 1) == 0) { + $file->publishFile(); + } + + // Randomly set old created date (for testing) + if (rand(0, 10) == 0) { + $file->Created = '2010-01-01 00:00:00'; + $file->Title = '[old] ' . $file->Title; + $file->write(); + } + + + echo " Created File: '$file->Title'\n"; + } + + if ($depth < $this->depth) { + $this->generateFiles($fixtureFilePaths, $depth+1, "{$prefix}-{$i}", $folder->ID); + } + } + } + +} diff --git a/composer.json b/composer.json index 659a01e..168f3e5 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "require": { "silverstripe/framework": "~4.0", - "silverstripe/cms": "~4.0" + "silverstripe/cms": "~4.0", + "guzzlehttp/guzzle": "5.x" } }