Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UploadFilesToGoogleDrive
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 handle
0.00% covered (danger)
0.00%
0 / 23
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     * Create a new command instance.
26     *
27     * @return void
28     */
29    public function __construct()
30    {
31        parent::__construct();
32    }
33
34    /**
35     * Execute the console command.
36     *
37     * @return int
38     */
39    public function handle()
40    {
41        $client = new \Google_Client;
42        $client->setApplicationName('FST Backup files');
43        $client->setClientId(env('GOOGLE_DRIVE_CLIENT_ID'));
44        $client->setClientSecret(env('GOOGLE_DRIVE_CLIENT_SECRET'));
45        $client->setAccessType('offline');
46        $client->setApprovalPrompt('force');
47        $client->refreshToken(env('GOOGLE_DRIVE_REFRESH_TOKEN'));
48        $folderID = env('GOOGLE_DRIVE_FOLDER_ID');
49
50        $service = new \Google_Service_Drive($client);
51
52        $files = \File::allFiles(storage_path('app/public/uploads'));
53
54        foreach ($files as $file) {
55            $filename = basename($file->getPathname());
56
57            $optParams = [
58                'pageSize' => 10,
59                'includeItemsFromAllDrives' => true,
60                'supportsAllDrives' => true,
61                'q' => "name='{$filename}' and '{$folderID}' in parents",
62                'fields' => '*',
63            ];
64
65            $results = $service->files->listFiles($optParams);
66
67            if (count($results->getFiles()) == 0) {
68                Storage::disk('google')->put($filename, file_get_contents(storage_path().'/app/public/uploads/'.$filename));
69            }
70        }
71
72        return 0;
73    }
74}