Test Failed
Push — master ( 8c814b...380005 )
by Raffael
08:49
created

PreviewCreator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 85
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createPreview() 0 33 3
A storePreview() 0 26 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\App\Preview;
13
14
use Balloon\Converter;
15
use Balloon\Filesystem\Node\File;
16
use MongoDB\BSON\ObjectId;
17
use MongoDB\Database;
18
use Psr\Log\LoggerInterface;
19
20
class PreviewCreator extends Preview
21
{
22
    /**
23
     * Converter.
24
     *
25
     * @var Converter
26
     */
27
    protected $converter;
28
29
    /**
30
     * Constructor.
31
     */
32
    public function __construct(Database $db, LoggerInterface $logger, Converter $converter)
33
    {
34
        parent::__construct($db, $logger);
35
        $this->converter = $converter;
36
    }
37
38
    /**
39
     * Create preview.
40
     */
41
    public function createPreview(File $file): ObjectId
42
    {
43
        $this->logger->debug('create preview for file ['.$file->getId().']', [
44
            'category' => get_class($this),
45
        ]);
46
47
        try {
48
            $stream = $this->converter->createPreview($file);
49
            $result = stream_get_contents($stream);
50
            $hash = md5($result);
51
            rewind($stream);
52
53
            $found = $this->db->{'thumbnail.files'}->findOne([
54
                'md5' => $hash,
55
            ], ['_id', 'thumbnail']);
56
57
            if ($found) {
58
                $this->logger->debug('found existing preview ['.$found['_id'].'] with same hash, use stored preview', [
59
                    'category' => get_class($this),
60
                ]);
61
62
                $file->setAppAttribute(__NAMESPACE__, 'preview', $found['_id']);
63
64
                return $found['_id'];
65
            }
66
67
            return $this->storePreview($file, $stream);
68
        } catch (\Exception $e) {
69
            $file->unsetAppAttribute(__NAMESPACE__, 'preview');
70
71
            throw $e;
72
        }
73
    }
74
75
    /**
76
     * Store new preview.
77
     */
78
    protected function storePreview(File $file, $content): ObjectId
79
    {
80
        try {
81
            $id = new ObjectId();
82
            $bucket = $this->db->selectGridFSBucket(['bucketName' => 'thumbnail']);
83
            $stream = $bucket->openUploadStream(null, ['_id' => $id]);
84
            /*fwrite($stream, $content);
85
            fclose($stream);*/
86
            stream_copy_to_stream($content, $stream);
87
88
            $file->setAppAttribute(__NAMESPACE__, 'preview', $id);
89
90
            $this->logger->info('stored new preview ['.$id.'] for file ['.$file->getId().']', [
91
                'category' => get_class($this),
92
            ]);
93
94
            return $id;
95
        } catch (\Exception $e) {
96
            $this->logger->error('failed store preview for file ['.$file->getId().']', [
97
                'category' => get_class($this),
98
                'exception' => $e,
99
            ]);
100
101
            throw $e;
102
        }
103
    }
104
}
105