ivanvermeyen / laravel-google-drive-demo

Laravel & Google Drive Storage - Demo project with Laravel 5.4
405 stars 155 forks source link

Export Google Doc #24

Closed jigsawsoul closed 6 years ago

jigsawsoul commented 6 years ago

Hi,

With the current setup is it possiable to be able to export a Google Doc, I believe you can do this through the API using the following https://developers.google.com/drive/v3/reference/files/export - I am just unsure how to include this with this package.

Also can the package return the a public sharing link for a Google Doc?

Many thanks.

ivanvermeyen commented 6 years ago

Hi,

It seems to be possible to get the URL to a cloud file simply by calling this method:

Storage::cloud()->url($someFile['path']);

I've added this to the share example just now (see: https://github.com/ivanvermeyen/laravel-google-drive-demo/commit/7fbd798834a25735de14c0baa05d155b17c86d08)

Haven't tested it with a Google doc, but I suppose that will work too.

Let me know if this helps ;)

jigsawsoul commented 6 years ago

Hi @ivanvermeyen

The above example works perfect for a file uploaded to to Google Drive. Although is it possiable to be able to download a file which has been created within Google Docs.

array:9 [▼
  "type" => "file"
  "path" => "1pE1Xy1zYgsHmUzrIAEC6T8u3VClZoh6Q/17nBRYacjqretMZL7f7T9z2s2azjw1_MR3k8WUF53xv4"
  "filename" => "Google Doc Test"
  "extension" => ""
  "timestamp" => 1524341769
  "mimetype" => "application/vnd.google-apps.document"
  "size" => 0
  "dirname" => "1pE1Xy1zYgsHmUzrIAEC6T8u3VClZoh6Q"
  "basename" => "17nBRYacjqretMZL7f7T9z2s2azjw1_MR3k8WUF53xv4"
]

Getting the rawData from within the /get example I recieve this error message.

"""
{\n
 "error": {\n
  "errors": [\n
   {\n
    "domain": "global",\n
    "reason": "fileNotDownloadable",\n
    "message": "Only files with binary content can be downloaded. Use Export with Google Docs files.",\n
    "locationType": "parameter",\n
    "location": "alt"\n
   }\n
  ],\n
  "code": 403,\n
  "message": "Only files with binary content can be downloaded. Use Export with Google Docs files."\n
 }\n
}\n
"""

I believe you can do this through the API using the following https://developers.google.com/drive/v3/reference/files/export - is there away to add this to the package...?

ivanvermeyen commented 6 years ago

Hi, I finally got some time to research this problem.

In our GoogleDriveAdapter we extend the adapter from nao-pon/flysystem-google-drive and we add the getService() method to access the original Google services. This allows you to use more advanced functionality of the Google Drive API.

So now we can use the export functionality, like in this demo code I added:

https://github.com/ivanvermeyen/laravel-google-drive-demo/blob/1e5034fbdf3f0d787bf377cd79269120662fb290/routes/web.php#L296-L302

Of course, you can change the mime type dynamically to whatever format Google supports. In the example I just used PDF.

ivanvermeyen commented 6 years ago

I'm assuming this issue is resolved. Feel free to reopen if it's not. :)

richardhedges commented 4 years ago

@ivanvermeyen I've created a Google Document, with the word "test" in it, and tried your example above (with text/plain as the mimetype instead), but I can't seem to retrieve the contents of the file, only an empty string.

$export = $service->files->export($file['path'], 'text/plain');
$contents = $export->getBody();

The above returns a Guzzle stream, so if I change the $contents variable to the following to retrieve the contents, I instead see an empty string:

$contents = $export->getBody()->getContents();
Full code if it's needed ``` $service = Storage::cloud()->getAdapter()->getService(); $files = Storage::cloud()->listContents('/', false); foreach ($files as $file) { if ($file['name'] == 'test') { $export = $service->files->export($file['path'], 'text/plain'); $contents = $export->getBody()->getContents(); dd($contents); } } ```