FileVault::getFilePath()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace SoareCostin\FileVault;
4
5
use Illuminate\Support\Facades\Storage;
6
use Illuminate\Support\Str;
7
8
class FileVault
9
{
10
    /**
11
     * The storage disk.
12
     *
13
     * @var string
14
     */
15
    protected $disk;
16
17
    /**
18
     * The encryption key.
19
     *
20
     * @var string
21
     */
22
    protected $key;
23
24
    /**
25
     * The algorithm used for encryption.
26
     *
27
     * @var string
28
     */
29
    protected $cipher;
30
31
    /**
32
     * The storage adapter.
33
     *
34
     * @var string
35
     */
36
    protected $adapter;
37
38
    public function __construct()
39
    {
40
        $this->disk = config('file-vault.disk');
41
        $this->key = config('file-vault.key');
42
        $this->cipher = config('file-vault.cipher');
43
    }
44
45
    /**
46
     * Set the disk where the files are located.
47
     *
48
     * @param  string  $disk
49
     * @return $this
50
     */
51
    public function disk($disk)
52
    {
53
        $this->disk = $disk;
54
55
        return $this;
56
    }
57
58
    /**
59
     * Set the encryption key.
60
     *
61
     * @param  string  $key
62
     * @return $this
63
     */
64
    public function key($key)
65
    {
66
        $this->key = $key;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Create a new encryption key for the given cipher.
73
     *
74
     * @return string
75
     */
76
    public static function generateKey()
77
    {
78
        return random_bytes(config('file-vault.cipher') === 'AES-128-CBC' ? 16 : 32);
79
    }
80
81
    /**
82
     * Encrypt the passed file and saves the result in a new file with ".enc" as suffix.
83
     *
84
     * @param string $sourceFile Path to file that should be encrypted, relative to the storage disk specified
85
     * @param string $destFile   File name where the encryped file should be written to, relative to the storage disk specified
86
     * @return $this
87
     */
88
    public function encrypt($sourceFile, $destFile = null, $deleteSource = true)
89
    {
90
        $this->registerServices();
91
92
        if (is_null($destFile)) {
93
            $destFile = "{$sourceFile}.enc";
94
        }
95
96
        $sourcePath = $this->getFilePath($sourceFile);
97
        $destPath = $this->getFilePath($destFile);
98
99
        // Create a new encrypter instance
100
        $encrypter = new FileEncrypter($this->key, $this->cipher);
101
102
        // If encryption is successful, delete the source file
103
        if ($encrypter->encrypt($sourcePath, $destPath) && $deleteSource) {
104
            Storage::disk($this->disk)->delete($sourceFile);
105
        }
106
107
        return $this;
108
    }
109
110
    public function encryptCopy($sourceFile, $destFile = null)
111
    {
112
        return self::encrypt($sourceFile, $destFile, false);
113
    }
114
115
    /**
116
     * Dencrypt the passed file and saves the result in a new file, removing the
117
     * last 4 characters from file name.
118
     *
119
     * @param string $sourceFile Path to file that should be decrypted
120
     * @param string $destFile   File name where the decryped file should be written to.
121
     * @return $this
122
     */
123
    public function decrypt($sourceFile, $destFile = null, $deleteSource = true)
124
    {
125
        $this->registerServices();
126
127
        if (is_null($destFile)) {
128
            $destFile = Str::endsWith($sourceFile, '.enc')
129
                        ? Str::replaceLast('.enc', '', $sourceFile)
130
                        : $sourceFile.'.dec';
131
        }
132
133
        $sourcePath = $this->getFilePath($sourceFile);
134
        $destPath = $this->getFilePath($destFile);
135
136
        // Create a new encrypter instance
137
        $encrypter = new FileEncrypter($this->key, $this->cipher);
138
139
        // If decryption is successful, delete the source file
140
        if ($encrypter->decrypt($sourcePath, $destPath) && $deleteSource) {
141
            Storage::disk($this->disk)->delete($sourceFile);
142
        }
143
144
        return $this;
145
    }
146
147
    public function decryptCopy($sourceFile, $destFile = null)
148
    {
149
        return self::decrypt($sourceFile, $destFile, false);
150
    }
151
152
    public function streamDecrypt($sourceFile)
153
    {
154
        $this->registerServices();
155
156
        $sourcePath = $this->getFilePath($sourceFile);
157
158
        // Create a new encrypter instance
159
        $encrypter = new FileEncrypter($this->key, $this->cipher);
160
161
        return $encrypter->decrypt($sourcePath, 'php://output');
162
    }
163
164
    protected function getFilePath($file)
165
    {
166
        if ($this->isS3File()) {
167
            return "s3://{$this->adapter->getBucket()}/{$file}";
0 ignored issues
show
Bug introduced by
The method getBucket cannot be called on $this->adapter (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
168
        }
169
170
        return Storage::disk($this->disk)->path($file);
171
    }
172
173
    protected function isS3File()
174
    {
175
        return $this->disk == 's3';
176
    }
177
178
    protected function setAdapter()
179
    {
180
        if ($this->adapter) {
181
            return;
182
        }
183
184
        $this->adapter = Storage::disk($this->disk)->getAdapter();
0 ignored issues
show
Bug introduced by
The method getAdapter() does not seem to exist on object<Illuminate\Contra...\Filesystem\Filesystem>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
185
    }
186
187
    protected function registerServices()
188
    {
189
        $this->setAdapter();
190
191
        if ($this->isS3File()) {
192
            $client = $this->adapter->getClient();
0 ignored issues
show
Bug introduced by
The method getClient cannot be called on $this->adapter (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
193
            $client->registerStreamWrapper();
194
        }
195
    }
196
}
197