Skip to content

Commit 1853932

Browse files
authored
Merge pull request #8 from MaximeLemolt/download-files
Find a file by ID + Download files
2 parents 625bdf5 + 0e5102c commit 1853932

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

src/Service/DriveApiService.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,97 @@ public function find(string $name, string $parentId = ''): ?Google_Service_Drive
328328
return $res;
329329
}
330330

331+
/**
332+
* Find a file by ID
333+
*
334+
* @param string $id Id to search for
335+
*/
336+
public function findById(string $fileId)
337+
{
338+
$response = $this->getDrive()->files->get($fileId, [
339+
'fields' => '*',
340+
'supportsAllDrives' => true
341+
]);
342+
343+
return $response;
344+
}
345+
346+
/**
347+
* Download a file
348+
*
349+
* @param string $fileId File to download Id
350+
* @param string $filepath The path where te file will be store
351+
*
352+
* Warning : the path start inside the public directory (need to create manually the 'downloads/' folder)
353+
*
354+
* @return string $filepath
355+
*/
356+
public function downloadFile(string $fileId, string $filepath = 'downloads/')
357+
{
358+
$drive = $this->getDrive();
359+
360+
$driveFile = $this->findById($fileId);
361+
$filename = $driveFile->getName();
362+
363+
// Check for extension
364+
$filename = (isset(pathinfo($filename)['extension'])) ? $filename : $filename . '.' . $driveFile->getFileExtension();
365+
366+
// Differents exports links already set up for Google documents
367+
if ($driveFile->getExportLinks() === null) {
368+
$response = $drive->files->get($driveFile->getId(), array(
369+
'alt' => 'media'));
370+
} else {
371+
// Drive uses special MIME Types, we need to convert them
372+
$fileInfos = $this->getGoogleDocsFileInfos($driveFile->getMimeType());
373+
374+
$response = $drive->files->export($driveFile->getId(), $fileInfos['mimeType'], array(
375+
'alt' => 'media'));
376+
377+
$filename = $driveFile->getName() . $fileInfos['extension'];
378+
}
379+
380+
$filepath .= $filename;
381+
382+
$fileContent = $response->getBody()->getContents();
383+
384+
file_put_contents($filepath, $fileContent);
385+
386+
return $filepath;
387+
}
388+
389+
/**
390+
* Convert Google Document MIME Type
391+
* All the Google Documents MIME Type on : https://developers.google.com/drive/api/v2/ref-export-formats
392+
*/
393+
public function getGoogleDocsFileInfos(string $mimeType)
394+
{
395+
$fileInfos = [];
396+
397+
switch($mimeType) {
398+
case 'application/vnd.google-apps.document':
399+
// MS Word document
400+
$fileInfos['mimeType'] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
401+
$fileInfos['extension'] = '.docx';
402+
break;
403+
case 'application/vnd.google-apps.spreadsheet':
404+
// MS Excel
405+
$fileInfos['mimeType'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
406+
$fileInfos['extension'] = '.xlsx';
407+
break;
408+
case 'application/vnd.google-apps.presentation':
409+
// MS PowerPoint
410+
$fileInfos['mimeType'] = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
411+
$fileInfos['extension'] = '.pptx';
412+
break;
413+
case 'application/vnd.google-apps.drawing':
414+
$fileInfos['mimeType'] = 'image/png';
415+
$fileInfos['extension'] = '.png';
416+
break;
417+
}
418+
419+
return $fileInfos;
420+
}
421+
331422
/**
332423
* @return ??
333424
**/

0 commit comments

Comments
 (0)