Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
UploadFilesToGoogleDrive
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace App\Console\Commands;
4
5use Illuminate\Console\Command;
6use Illuminate\Support\Facades\Storage;
7
8class UploadFilesToGoogleDrive extends Command
9{
10    /**
11     * The name and signature of the console command.
12     *
13     * @var string
14     */
15    protected $signature = 'drive:uploads';
16
17    /**
18     * The console command description.
19     *
20     * @var string
21     */
22    protected $description = 'Upload files to google drive';
23
24    /**
25     * Execute the console command.
26     */
27    public function handle(): void
28    {
29        $client = new \Google_Client;
30        $client->setApplicationName('FST Backup files');
31        $client->setClientId(config('filesystems.disks.google.clientId'));
32        $client->setClientSecret(config('filesystems.disks.google.clientSecret'));
33        $client->setAccessType('offline');
34        $client->setApprovalPrompt('force');
35        $client->refreshToken(config('filesystems.disks.google.refreshToken'));
36        $folderID = config('filesystems.disks.google.folderId');
37
38        $service = new \Google_Service_Drive($client);
39
40        $files = \File::allFiles(storage_path('app/public/uploads'));
41
42        foreach ($files as $file) {
43            $filename = basename((string) $file->getPathname());
44
45            $optParams = [
46                'pageSize' => 10,
47                'includeItemsFromAllDrives' => true,
48                'supportsAllDrives' => true,
49                'q' => "name='{$filename}' and '{$folderID}' in parents",
50                'fields' => '*',
51            ];
52
53            $results = $service->files->listFiles($optParams);
54
55            if (count($results->getFiles()) == 0) {
56                Storage::disk('google')->put($filename, file_get_contents(storage_path().'/app/public/uploads/'.$filename));
57            }
58        }
59
60    }
61}