Issues (96)

src/Tasks/RehashImagesTask.php (2 issues)

Labels
1
<?php
2
3
namespace LeKoala\DevToolkit\Tasks;
4
5
use LeKoala\DevToolkit\BuildTaskTools;
6
use SilverStripe\ORM\DB;
7
use SilverStripe\Assets\Image;
8
use SilverStripe\Dev\BuildTask;
9
use SilverStripe\Versioned\Versioned;
0 ignored issues
show
The type SilverStripe\Versioned\Versioned was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use SilverStripe\Assets\Storage\Sha1FileHashingService;
11
12
/**
13
 * In case images are resized by an external utility
14
 * file hash is not valid anymore
15
 *
16
 * We need a way to rehash them
17
 */
18
class RehashImagesTask extends BuildTask
19
{
20
    use BuildTaskTools;
21
22
    protected $title = "Rehash Images";
23
    private static $segment = 'RehashImagesTask';
0 ignored issues
show
The private property $segment is not used, and could be removed.
Loading history...
24
25
    public function run($request)
26
    {
27
        $this->request = $request;
28
        $this->addOption("go", "Set this to 1 to proceed", 0);
29
30
        $options = $this->askOptions();
31
32
        $go = $options['go'];
33
34
        $service = new Sha1FileHashingService();
35
36
        $images = Image::get();
37
        $tofix = $todelete = 0;
38
        foreach ($images as $image) {
39
            $hash = $image->getHash();
40
41
            $stream  = $image->getStream();
42
            if (!$stream) {
43
                $filename = $image->getFilename();
44
                $location = ASSETS_PATH . '/' . $filename;
45
                if (!is_file($location)) {
46
                    $todelete++;
47
                    if ($go) {
48
                        $this->message("Deleted file " . $image->ID . " since the file was not found");
49
                        $image->delete();
50
                    } else {
51
                        $this->message("Could not read file at $location. It would be deleted by this task.", "bad");
52
                    }
53
                    continue;
54
                }
55
                $stream = fopen($location, 'rb');
56
            }
57
58
            $fullhash = $service->computeFromStream($stream);
59
60
            if ($hash != $fullhash) {
61
                $tofix++;
62
                if ($go) {
63
                    DB::query("UPDATE File SET FileHash = '" . $fullhash . "' WHERE ID = " . $image->ID);
64
                    DB::query("UPDATE File_Live SET FileHash = '" . $fullhash . "' WHERE ID = " . $image->ID);
65
                    $this->message($image->ID . " has been fixed");
66
                } else {
67
                    $this->message($image->ID . " hash mismatch : $hash vs $fullhash. This task can fix this hash.");
68
                }
69
            }
70
        }
71
72
        if (!$tofix && !$todelete) {
73
            $this->message("All files are good", "good");
74
        }
75
    }
76
}
77