Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
imageapi_optimize
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
imageapi_optimize
Commits
2dbadc8a
Commit
2dbadc8a
authored
6 years ago
by
Steven Jones
Committed by
Steven Jones
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#2996896
by Steven Jones: Processor failure stops all optimization
parent
f3995c9a
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Entity/ImageAPIOptimizePipeline.php
+27
-28
27 additions, 28 deletions
src/Entity/ImageAPIOptimizePipeline.php
tests/src/Kernel/ImageOptimizePipelineTest.php
+1
-2
1 addition, 2 deletions
tests/src/Kernel/ImageOptimizePipelineTest.php
with
28 additions
and
30 deletions
src/Entity/ImageAPIOptimizePipeline.php
+
27
−
28
View file @
2dbadc8a
...
...
@@ -2,6 +2,7 @@
namespace
Drupal\imageapi_optimize\Entity
;
use
Drupal\Component\Utility\Crypt
;
use
Drupal\Core\Cache\Cache
;
use
Drupal\Core\Config\Entity\ConfigEntityBase
;
use
Drupal\Core\Entity\EntityStorageInterface
;
...
...
@@ -171,41 +172,39 @@ class ImageAPIOptimizePipeline extends ConfigEntityBase implements ImageAPIOptim
return
FALSE
;
}
if
(
count
(
$this
->
getProcessors
()))
{
/*
Copy image to optimize to a temp location so that:
1. It's always a local image.
2. The filename is only ascii characters.
3. Multiple pipelines can be applied to the same image at the same time.
*/
$file_extension
=
substr
(
strrchr
(
$image_uri
,
'.'
),
1
);
$temp_image_uri_base
=
\Drupal
::
service
(
'file_system'
)
->
tempnam
(
'temporary://'
,
'image_api_optimize_'
.
$this
->
id
());
// Ensure this file is cleaned up at the end of the request.
$this
->
temporaryFiles
[]
=
$temp_image_uri_base
;
// Copy the file we are trying to optimize to the temporary location.
file_unmanaged_copy
(
$image_uri
,
$temp_image_uri_base
,
FILE_EXISTS_REPLACE
);
// Some processors require the correct file extension, add that on.
$temp_image_uri
=
$temp_image_uri_base
.
'.'
.
$file_extension
;
file_unmanaged_move
(
$temp_image_uri_base
,
$temp_image_uri
);
// Ensure this file is cleaned up at the end of the request.
$this
->
temporaryFiles
[]
=
$temp_image_uri
;
$image_changed
=
FALSE
;
foreach
(
$this
->
getProcessors
()
as
$processor
)
{
$image_changed
=
$processor
->
applyToImage
(
$temp_image_uri
)
||
$image_changed
;
// The file may have changed on disk after each processor has been
// applied, and PHP has a cache of file size information etc. so clear
// it here so that later calls to filesize() etc. get the correct
// information.
clearstatcache
();
/*
Copy image to optimize to a temp location so that:
1. It's always a local image.
2. The filename is only ascii characters.
*/
$file_extension
=
strtolower
(
substr
(
strrchr
(
$image_uri
,
'.'
),
1
));
$temp_image_uri
=
'temporary://image_api_optimize_'
.
Crypt
::
randomBytesBase64
(
8
)
.
'.'
.
$file_extension
;
foreach
(
$this
->
getProcessors
()
as
$processor
)
{
// Create a copy of this image for the processor to work on.
$temp_image_uri
=
file_unmanaged_copy
(
$image_uri
,
$temp_image_uri
,
FILE_EXISTS_RENAME
);
if
(
$temp_image_uri
===
FALSE
)
{
return
FALSE
;
}
// Add the temporary file to be cleaned up later.
$this
->
temporaryFiles
[]
=
$temp_image_uri
;
// Apply the actual processor.
$image_changed
=
$processor
->
applyToImage
(
$temp_image_uri
);
// The file may have changed on disk after each processor has been
// applied, and PHP has a cache of file size information etc. so clear
// it here so that later calls to filesize() etc. get the correct
// information.
clearstatcache
();
if
(
$image_changed
)
{
// Copy the temporary file back over the original image.
file_unmanaged_move
(
$temp_image_uri
,
$image_uri
,
FILE_EXISTS_REPLACE
);
}
}
return
TRUE
;
}
/**
...
...
This diff is collapsed.
Click to expand it.
tests/src/Kernel/ImageOptimizePipelineTest.php
+
1
−
2
View file @
2dbadc8a
...
...
@@ -99,9 +99,8 @@ class ImageOptimizePipelineTest extends KernelTestBase {
// Apply the pipeline.
$pipeline
->
applyToImage
(
$image_uri
);
$this
->
markTestIncomplete
(
'Marking as incomplete to get the tests committed.'
);
// Check that the file was correctly 'optimized': adding 20 '1' characters.
//
$this->assertFileEqualsString($original_image_data . str_repeat('1', 20), $image_uri);
$this
->
assertFileEqualsString
(
$original_image_data
.
str_repeat
(
'1'
,
20
),
$image_uri
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment