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