|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\User; |
|
6
|
|
|
|
|
7
|
|
|
class GistBackupHandler |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var User $user */ |
|
10
|
|
|
private $user; |
|
11
|
|
|
|
|
12
|
|
|
/** @var GistFinder $gistFinder */ |
|
13
|
|
|
private $gistFinder; |
|
14
|
|
|
|
|
15
|
|
|
/** Max size in bytes of file that can be archived */ |
|
16
|
|
|
const MAX_FILE_SIZE = 100000; |
|
17
|
|
|
|
|
18
|
|
|
function __construct(GistFinder $gistFinder) |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
$this->user = auth()->user(); |
|
|
|
|
|
|
21
|
|
|
$this->gistFinder = $gistFinder; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Backup gists. |
|
26
|
|
|
* |
|
27
|
|
|
* @return string|integer A path to gist archive or 0 if there are no files to backup. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function backup() |
|
30
|
|
|
{ |
|
31
|
|
|
$files = $this->prepareFiles(); |
|
32
|
|
|
|
|
33
|
|
|
if (!count($files)) { |
|
34
|
|
|
return 0; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$zipPath = $this->zipFiles($files); |
|
38
|
|
|
|
|
39
|
|
|
$this->removeFiles(); |
|
40
|
|
|
|
|
41
|
|
|
return $zipPath; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get each gist file and save it in the user folder. |
|
46
|
|
|
* |
|
47
|
|
|
* Also checks the size of a file to determine if it should be ignored. |
|
48
|
|
|
* |
|
49
|
|
|
* @return array Paths to gist files. |
|
50
|
|
|
*/ |
|
51
|
|
|
private function prepareFiles() |
|
52
|
|
|
{ |
|
53
|
|
|
$gists = $this->gistFinder->fetchRawGists(); |
|
54
|
|
|
|
|
55
|
|
|
$files = []; |
|
56
|
|
|
foreach ($gists as $gist) { |
|
57
|
|
|
foreach ($gist['files'] as $file) { |
|
58
|
|
|
if ($file['size'] < self::MAX_FILE_SIZE) { |
|
59
|
|
|
$filename = $file['filename']; |
|
60
|
|
|
$contents = $this->gistFinder->getGistFileContents($file['raw_url']); |
|
61
|
|
|
$fileDate = date('YmdHis', strtotime($gist['updated_at'])); |
|
62
|
|
|
$filePath = "backups/{$this->user->id}/tmp/{$fileDate}-{$filename}"; |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
\Storage::put($filePath, $contents); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
$files[] = storage_path("app/$filePath"); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $files; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Create a zip archive and add files to it. |
|
76
|
|
|
* |
|
77
|
|
|
* @param array $files |
|
78
|
|
|
* |
|
79
|
|
|
* @return string Path to zip archive. |
|
80
|
|
|
* |
|
81
|
|
|
* @throws \Exception If the archive cannot be created. |
|
82
|
|
|
*/ |
|
83
|
|
|
private function zipFiles(array $files) |
|
84
|
|
|
{ |
|
85
|
|
|
$zipName = date("YmdHis") . '-gists-' . str_slug($this->user->nickname); |
|
|
|
|
|
|
86
|
|
|
$zipPath = storage_path("app/backups/{$this->user->id}/{$zipName}.zip"); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
$zip = new \ZipArchive; |
|
89
|
|
|
if ($zip->open($zipPath, \ZipArchive::CREATE) === true) { |
|
90
|
|
|
foreach ($files as $file) { |
|
91
|
|
|
$zip->addFile($file, basename($file)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$zip->close(); |
|
95
|
|
|
|
|
96
|
|
|
return $zipPath; |
|
97
|
|
|
} else { |
|
98
|
|
|
throw new \Exception("Cannot open $zipPath for writing."); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Delete the previously prepared files. |
|
104
|
|
|
*/ |
|
105
|
|
|
private function removeFiles() |
|
106
|
|
|
{ |
|
107
|
|
|
\Storage::deleteDirectory("backups/{$this->user->id}/tmp"); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.