-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate legacy thumbnails (fixes #235) #242
Migrate legacy thumbnails (fixes #235) #242
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks generally good. I'm thinking of adding some sort of "normalise path" method to asset store to help with the file migration task. That might have save you some trouble.
There's a couple edge cases that I've highlighted and some possible improvements.
I'm thinking maybe add a test case for two image files in the same folder with variants sharing the same _resampled
sub folder.
Maybe add an assertion that the variants don't get regenerated when the developer asks for them later on. That's more of a nice to have.
Travis failure is caused by the main file migration task which hasn't been converted to use the strategies yet. |
That's a lot of code! Is there something in particular you want me to review? I can't review all of this but I can look at specifics if you need input. |
@tractorcow Awesome, thanks for the offer! Maybe just think about gnarly edge cases that we might've missed? Stuff like "chained format thumbnail file names", "multiple files in same _resampled folder".
Hah, funny I actually had that case in there ( |
@tractorcow Oh I just realised you might not have picked up on the fact that most commits in this PR are pulled in from a separate PR: #223. That other PR is actually more important to get your input on (it refactors a lot of the asset abstraction behaviour), the thumbnail task here is just another cleanup thing that's less important. |
Yeah, I don't see how that would happen - the locations of generated thumbnails should be asserted as part of the existing image manipulation tests, has nothing to do with the migration task, right? Anyway, I've added Ready for re-review :) |
912bdb7
to
d554c33
Compare
I've merge 1.4 back into this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've confirmed that this thing works correctly.
I've used a script to generate dummy SS3 variants and confirmed the files get moved around after running the thumbnail helper.
The tasks needs to be moved to the SilverStripe\Assets\Dev\Tasks
namespace to be inline with the other ones.
I'll get that done tomorrow morning.
@@ -0,0 +1,198 @@ | |||
<?php | |||
|
|||
namespace SilverStripe\Assets\Migration; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs to be updated to SilverStripe\Assets\Dev\Tasks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix namespace.
Legacy thumbnails weren't migrated, but rather re-created on demand. This increases disk space requirements, CPU resources, and wait times for users (since most of these are generated inline with PHP responses).
For the vast majority of sites, you really don't want to run your file migration as part of dev build. The step is involved enough to warrant it's own task. I don't think this is an API change, since the setting won't have affect for anyone who has already enabled it - they would've already done the one-off migration.
934d883
to
9b74fa1
Compare
|
||
// findVariants() returns *both* the original file and the variant | ||
// See FileIDHelperResolutionStrategyTest->testFindVariant() | ||
$this->assertCount(2, $protectedVariants); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
findVariants
returns a generator. Technically, those are not countable. PHPUnit seems to get around that by iterating over the generator until it reaches the end or goes over the limit. That does meant that when the test fails, you always get a "Failed asserting that actual size 0 matches expected size 2." even if the actual size is something other than zero.
I'm not going any where with this ... just pointing it out. I was half way going down the rabbit hole when I realised that it wasn't a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it is broken.
The moved variant is on the public store. You are looking on the protected store, so you are not getting the variant at all.
You are getting the main file twice:
- Once because it matches the NaturalFileIDHelper
- Once because it matches the LegacyFileIDHelper
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aaaah great catch! This is a super gnarly test. I've removed it with fa4b14c, and added it as a known limitation. See comment on main thread for more rationale: #242 (comment)
I've decided to remove a test around thumbnails on protected files, since it's hitting diminishing returns: Probably spent a day on getting all the stars aligned in the various abstractions of abstractions in there. The test currently fails, which correctly points out that this feature isn't implemented. I've documented it as a known limitation instead. It's an edge case.
/**
* Tests cases where files have already been migrated to 4.3 or older,
* and some images have since been unpublished or otherwise marked protected.
* The previously orphaned thumbnails wouldn't have been moved to the protected store in this case.
* The migration task should find those orphans, and reunite them with the original image.
* And they lived happily ever after.
*/
public function testMigratesThumbnailsForProtectedFiles()
{
/** @var TestAssetStore $store */
$store = singleton(AssetStore::class); // will use TestAssetStore
// Remove the legacy helper, otherwise it'll move the _resampled files as well,
// which "pollutes" our test case.
/** @var FileIDHelperResolutionStrategy $strategy */
$publicStrategy = $store->getPublicResolutionStrategy();
$helpers = $publicStrategy->getResolutionFileIDHelpers();
$origHelpers = [];
foreach ($helpers as $i => $helper) {
$origHelpers[] = clone $helper;
if ($helper instanceof LegacyFileIDHelper) {
unset($helpers[$i]);
}
}
$publicStrategy->setResolutionFileIDHelpers($helpers);
$store->setPublicResolutionStrategy($publicStrategy);
/** @var Image $image */
$image = $this->objFromFixture(Image::class, 'nested');
$formats = ['ResizedImage' => [60,60]];
$expectedLegacyPath = $this->createLegacyResampledImageFixture($store, $image, $formats);
// Protect file *after* creating legacy fixture,
// but without moving the _resampled "orphan"
$image->protectFile();
$image->write();
// We need to retain the legacy helper for later assertions.
$publicStrategy->setResolutionFileIDHelpers($origHelpers);
$store->setPublicResolutionStrategy($publicStrategy);
$helper = new LegacyThumbnailMigrationHelper();
$moved = $helper->run($store);
$this->assertCount(1, $moved);
$publicVariants = $store->getPublicResolutionStrategy()
->findVariants(
new ParsedFileID($image->Filename, $image->Hash),
$store->getPublicFilesystem()
);
$this->assertCount(0, $publicVariants);
$protectedVariants = $store->getProtectedResolutionStrategy()
->findVariants(
new ParsedFileID($image->Filename, $image->Hash),
$store->getProtectedFilesystem()
);
// findVariants() returns *both* the original file and the variant
// See FileIDHelperResolutionStrategyTest->testFindVariant()
// TODO This is counting the original file twice, doesn't include the actual variant
// See https://github.com/silverstripe/silverstripe-assets/pull/242/files#r279615136
$this->assertCount(1, $protectedVariants);
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've fixed some linting error to please the PHPCS Gods.
Merge on green.
Merge after silverstripe/silverstripe-framework#8924
Relies on @max #223 to be merged first. The newest commits by me are the actual PR here