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