-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDownloadBlobsCommand.php
129 lines (100 loc) · 3.3 KB
/
DownloadBlobsCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
declare(strict_types=1);
namespace Revolution\Bluesky\Console;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Revolution\Bluesky\Core\CID;
use Revolution\Bluesky\Facades\Bluesky;
use Revolution\Bluesky\Support\DidDocument;
use Revolution\Bluesky\Support\Identity;
use Symfony\Component\Mime\MimeTypes;
/**
* Sample command to download the actor's blob files.
*
* ```
* php artisan bluesky:download-blobs ***.bsky.social
* ```
*
* @link https://docs.bsky.app/blog/repo-export
*/
class DownloadBlobsCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bluesky:download-blobs {actor : DID or handle}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Download actor\'s blobs. Does not require auth.';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$actor = $this->argument('actor');
$this->warn('Actor: '.$actor);
if (Identity::isHandle($actor)) {
$did = Bluesky::resolveHandle($actor)->json('did');
} else {
$did = $actor;
}
if (! Identity::isDID($did)) {
$this->error('Invalid actor');
return 1;
}
/** @var string $did */
$this->warn('DID: '.$did);
$pds = DidDocument::make(Bluesky::identity()->resolveDID($did, cache: false)->json())->pdsUrl();
$this->warn('PDS: '.$pds);
$cursor = '';
do {
$response = Bluesky::client(auth: false)
->sync()
->baseUrl($pds.'/xrpc/')
->listBlobs(did: $did, cursor: $cursor)
->throw();
$response->collect('cids')->each(function ($cid) use ($actor, $did, $pds) {
$content = Bluesky::client(auth: false)
->sync()
->baseUrl($pds.'/xrpc/')
->getBlob(did: $did, cid: $cid)
->throw()
->body();
if (CID::verify(data: $content, cid: $cid, codec: CID::RAW)) {
$this->info('Verified');
} else {
$this->error('Verify failed');
}
$name = Str::slug($actor, dictionary: ['.' => '-', ':' => '-']);
$file = collect(['bluesky', 'download', $name, 'blob', $cid])
->implode(DIRECTORY_SEPARATOR);
Storage::put($file, $content);
$file_ext = $file.$this->ext((string) Storage::mimeType($file));
Storage::move($file, $file_ext);
$this->line('Download: '.Storage::path($file_ext));
});
/** @var string $cursor */
$cursor = $response->json('cursor');
$this->warn('cursor: '.$cursor);
} while (filled($cursor));
$this->info('Download successful');
return 0;
}
protected function ext(string $type): string
{
/** @var ?string $ext */
$ext = head(MimeTypes::getDefault()->getExtensions($type));
if (empty($ext)) {
return '';
}
return '.'.$ext;
}
}