1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\PersonalDataDownload; |
4
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Support\Facades\Storage; |
8
|
|
|
use Spatie\PersonalDataDownload\Exceptions\CouldNotAddToPersonalData; |
9
|
|
|
use Spatie\TemporaryDirectory\TemporaryDirectory; |
10
|
|
|
|
11
|
|
|
class PersonalData |
12
|
|
|
{ |
13
|
|
|
/** @var \Spatie\TemporaryDirectory\TemporaryDirectory */ |
14
|
|
|
protected $temporaryDirectory; |
15
|
|
|
|
16
|
|
|
/** @var array */ |
17
|
|
|
protected $files = []; |
18
|
|
|
|
19
|
|
|
/** @var \Illuminate\Database\Eloquent\Model */ |
20
|
|
|
public $user; |
21
|
|
|
|
22
|
|
|
public function __construct(TemporaryDirectory $temporaryDirectory) |
23
|
|
|
{ |
24
|
|
|
$this->temporaryDirectory = $temporaryDirectory; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function forUser(Model $user) |
28
|
|
|
{ |
29
|
|
|
$this->user = $user; |
30
|
|
|
|
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $nameInDownload |
36
|
|
|
* @param array|string $content |
37
|
|
|
*/ |
38
|
|
|
public function addContent(string $nameInDownload, $content) |
39
|
|
|
{ |
40
|
|
|
if (! is_string($content)) { |
41
|
|
|
$content = json_encode($content); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$path = $this->temporaryDirectory->path($nameInDownload); |
45
|
|
|
|
46
|
|
|
$this->ensureDoesNotOverwriteExistingFile($path); |
47
|
|
|
|
48
|
|
|
$this->files[] = $path; |
49
|
|
|
|
50
|
|
|
file_put_contents($path, $content); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function addFile(string $pathToFile, string $diskName = null) |
54
|
|
|
{ |
55
|
|
|
return is_null($diskName) |
56
|
|
|
? $this->copyLocalFile($pathToFile) |
57
|
|
|
: $this->copyFileFromDisk($pathToFile, $diskName); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function files(): array |
61
|
|
|
{ |
62
|
|
|
return $this->files; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function copyLocalFile(string $pathToFile) |
66
|
|
|
{ |
67
|
|
|
$fileName = pathinfo($pathToFile, PATHINFO_BASENAME); |
68
|
|
|
|
69
|
|
|
$destination = $this->temporaryDirectory->path($fileName); |
70
|
|
|
|
71
|
|
|
$this->ensureDoesNotOverwriteExistingFile($destination); |
72
|
|
|
|
73
|
|
|
(new Filesystem())->copy($pathToFile, $destination); |
74
|
|
|
|
75
|
|
|
$this->files[] = $destination; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function copyFileFromDisk(string $pathOnDisk, string $diskName) |
81
|
|
|
{ |
82
|
|
|
$stream = Storage::disk($diskName)->readStream($pathOnDisk); |
83
|
|
|
|
84
|
|
|
$pathInTemporaryDirectory = $this->temporaryDirectory->path($pathOnDisk); |
85
|
|
|
|
86
|
|
|
$this->ensureDoesNotOverwriteExistingFile($pathInTemporaryDirectory); |
87
|
|
|
|
88
|
|
|
file_put_contents($pathInTemporaryDirectory, stream_get_contents($stream), FILE_APPEND); |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function ensureDoesNotOverwriteExistingFile(string $path) |
94
|
|
|
{ |
95
|
|
|
if (file_exists($path)) { |
96
|
|
|
throw CouldNotAddToPersonalData::fileAlreadyAddedToPersonalData($path); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|