Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
66 / 66 |
| GoogleDriveService | |
100.00% |
1 / 1 |
|
100.00% |
7 / 7 |
29 | |
100.00% |
66 / 66 |
| listFiles | |
100.00% |
1 / 1 |
6 | |
100.00% |
17 / 17 |
|||
| listFolders | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| newFile | |
100.00% |
1 / 1 |
4 | |
100.00% |
10 / 10 |
|||
| createPermission | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| setPermission | |
100.00% |
1 / 1 |
6 | |
100.00% |
6 / 6 |
|||
| createFolder | |
100.00% |
1 / 1 |
6 | |
100.00% |
16 / 16 |
|||
| save | |
100.00% |
1 / 1 |
5 | |
100.00% |
15 / 15 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\GoogleApiWrapper\Services; |
| 4 | |
| 5 | use Exception; |
| 6 | use Google\Service\Drive; |
| 7 | use Google\Service\Drive\{DriveFile, Permission}; |
| 8 | use Illuminate\Support\Arr; |
| 9 | |
| 10 | class GoogleDriveService extends GoogleBaseService |
| 11 | { |
| 12 | /** |
| 13 | * Undocumented variable |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | protected $serviceClass = Drive::class; |
| 18 | |
| 19 | /** |
| 20 | * Undocumented variable |
| 21 | * |
| 22 | * @var DriveFile |
| 23 | */ |
| 24 | protected $file; |
| 25 | |
| 26 | /** |
| 27 | * Undocumented variable |
| 28 | * |
| 29 | * @var array |
| 30 | */ |
| 31 | const MIME_TYPES = [ |
| 32 | 'sheet' => 'application/vnd.google-apps.spreadsheet', |
| 33 | 'audio' => 'application/vnd.google-apps.audio', |
| 34 | 'doc' => 'application/vnd.google-apps.document', |
| 35 | '3rd_party_shortcut' => 'application/vnd.google-apps.drive-sdk', |
| 36 | 'drawing' => 'application/vnd.google-apps.drawing', |
| 37 | 'drive_file' => 'application/vnd.google-apps.file', |
| 38 | 'drive_folder' => 'application/vnd.google-apps.folder', |
| 39 | 'form' => 'application/vnd.google-apps.form', |
| 40 | 'fusion_tables' => 'application/vnd.google-apps.fusiontable', |
| 41 | 'my_maps' => 'application/vnd.google-apps.map', |
| 42 | 'photo' => 'application/vnd.google-apps.photo', |
| 43 | 'slide' => 'application/vnd.google-apps.presentation', |
| 44 | 'apps_scripts' => 'application/vnd.google-apps.script', |
| 45 | 'shortcut' => 'application/vnd.google-apps.shortcut', |
| 46 | 'sites' => 'application/vnd.google-apps.site', |
| 47 | 'unknown' => 'application/vnd.google-apps.unknown', |
| 48 | 'video' => 'application/vnd.google-apps.video', |
| 49 | ]; |
| 50 | |
| 51 | /** |
| 52 | * Undocumented function |
| 53 | * |
| 54 | * @return array |
| 55 | */ |
| 56 | public function listFiles($type = null, bool $owned = true): array |
| 57 | { |
| 58 | $query = $list = []; |
| 59 | |
| 60 | if ($owned) { |
| 61 | $query[] = sprintf("'%s' in owners", $this->client->getSubject()); |
| 62 | } |
| 63 | |
| 64 | if ($type) { |
| 65 | if (!$mime = self::MIME_TYPES[$type] ?? null) { |
| 66 | throw new Exception("Mime type '$type' not found."); |
| 67 | } |
| 68 | |
| 69 | $query[] = "mimeType = '$mime'"; |
| 70 | } |
| 71 | |
| 72 | $files = $this->service |
| 73 | ->files |
| 74 | ->listFiles(['q' => implode(' and ', $query)]) |
| 75 | ->getFiles(); |
| 76 | |
| 77 | foreach ($files as $file) { |
| 78 | $list[$file->id] = $type ? $file->name : [ |
| 79 | 'name' => $file->name, |
| 80 | 'mime' => $file->mimeType, |
| 81 | 'type' => array_flip(self::MIME_TYPES)[$file->mimeType] ?? explode('/', $file->mimeType)[0] |
| 82 | ]; |
| 83 | } |
| 84 | |
| 85 | return $list; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Undocumented function |
| 90 | * |
| 91 | * @return array |
| 92 | */ |
| 93 | public function listFolders(): array |
| 94 | { |
| 95 | return $this->listFiles('drive_folder'); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Undocumented function |
| 100 | * |
| 101 | * @param string $path |
| 102 | * @param mixed[type] $content |
| 103 | * @param array $options |
| 104 | * @return self |
| 105 | */ |
| 106 | public function newFile($content = null, array $options = []): self |
| 107 | { |
| 108 | $this->file = new DriveFile; |
| 109 | $this->setOption('fileContent', $content); |
| 110 | $this->setOption('fileOptions', $options); |
| 111 | |
| 112 | if (isset($options['description'])) { |
| 113 | $this->file->setDescription($options['description']); |
| 114 | } |
| 115 | if (isset($options['mime'])) { |
| 116 | $this->file->setMimeType(self::MIME_TYPES[$options['mime']] ?? $options['mime']); |
| 117 | } |
| 118 | |
| 119 | if (isset($options['permission'])) { |
| 120 | $this->withPermission($options['permission']); |
| 121 | } |
| 122 | |
| 123 | return $this; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Undocumented function |
| 128 | * |
| 129 | * @param [type] $opts |
| 130 | * @return void |
| 131 | */ |
| 132 | protected function createPermission($permission = null) |
| 133 | { |
| 134 | return new Permission($permission); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /** |
| 139 | * Undocumented function |
| 140 | * |
| 141 | * @param [type] $fileId |
| 142 | * @param [type] $permission |
| 143 | * @return void |
| 144 | */ |
| 145 | public function setPermission($fileId = null, $permission = null) |
| 146 | { |
| 147 | if ($permission || $this->getOption('permission') && ($this->getOption('fileId') || $fileId)) { |
| 148 | |
| 149 | $options = ['fields' => 'id']; |
| 150 | |
| 151 | if($message = $this->getOption('message')) { |
| 152 | $options['emailMessage'] = $message; |
| 153 | } |
| 154 | |
| 155 | return $this->service->permissions->create($this->getOption('fileId') ?? $fileId, $this->createPermission($permission ?? $this->getOption('permission')), $options); |
| 156 | } |
| 157 | throw new Exception('No permissions set !'); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | /** |
| 162 | * Undocumented function |
| 163 | * |
| 164 | * @param string $path |
| 165 | * @return mixed |
| 166 | */ |
| 167 | public function createFolder(string $path) |
| 168 | { |
| 169 | $exp = explode('/', $path); |
| 170 | $last = null; |
| 171 | |
| 172 | if (count($exp) && $path !== ".") { |
| 173 | $list = $this->listFolders(); |
| 174 | foreach ($exp as $folder) { |
| 175 | if ($folder !== '') { |
| 176 | $current = Arr::get(array_flip($list), $folder, ''); |
| 177 | if (!$current) { |
| 178 | $created = $this->newFile( |
| 179 | null, |
| 180 | [ |
| 181 | 'parentId' => $last, |
| 182 | 'mime' => 'drive_folder' |
| 183 | ] |
| 184 | )->save($folder); |
| 185 | $last = $created->id; |
| 186 | } else { |
| 187 | $last = $current; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | return $last; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Undocumented function |
| 197 | * |
| 198 | * @param mixed $file |
| 199 | * @param mixed $data |
| 200 | * @return |
| 201 | */ |
| 202 | public function save($path) |
| 203 | { |
| 204 | $pathinfo = pathinfo($path); |
| 205 | $options = $this->getOption('fileOptions'); |
| 206 | |
| 207 | if (isset($options['parentId'])) { |
| 208 | if ($options['parentId'] !== null) { |
| 209 | $this->file->setparents([$options['parentId']]); |
| 210 | } |
| 211 | } else { |
| 212 | $parent = $this->createFolder($pathinfo['dirname']); |
| 213 | if ($parent !== '') { |
| 214 | $this->file->setparents([$parent]); |
| 215 | } |
| 216 | } |
| 217 | $this->file->setName($pathinfo['filename']); |
| 218 | |
| 219 | $file = $this->service->files->create($this->file, [ |
| 220 | 'data' => $this->getOption('fileContent'), |
| 221 | 'uploadType' => 'multipart' |
| 222 | ]); |
| 223 | |
| 224 | if ($this->getOption('permission')) { |
| 225 | $this->setPermission($file->id); |
| 226 | } |
| 227 | |
| 228 | return $file; |
| 229 | } |
| 230 | } |