ivanvermeyen / laravel-google-drive-demo

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

Keep Revision Forever #54

Open blackhun11 opened 5 years ago

blackhun11 commented 5 years ago

Hi, i want to ask how to keepForever our files? Thank You

ivanvermeyen commented 5 years ago

Hello,

I've been trying to come up with some example code, but at the moment I'm stuck. I can't verify if it works so I assume it doesn't.

These are the docs for the revisions: https://developers.google.com/drive/api/v3/reference/revisions

When I dd($service) I get:

Google_Service_Drive {#227 ▼
  +about: Google_Service_Drive_Resource_About {#239 ▶}
  +changes: Google_Service_Drive_Resource_Changes {#237 ▶}
  +channels: Google_Service_Drive_Resource_Channels {#228 ▶}
  +comments: Google_Service_Drive_Resource_Comments {#229 ▶}
  +files: Google_Service_Drive_Resource_Files {#241 ▶}
  +permissions: Google_Service_Drive_Resource_Permissions {#242 ▶}
  +replies: Google_Service_Drive_Resource_Replies {#235 ▶}
  +revisions: Google_Service_Drive_Resource_Revisions {#230 ▶}
  +teamdrives: Google_Service_Drive_Resource_Teamdrives {#238 ▶}
  +batchPath: null
  +rootUrl: "https://www.googleapis.com/"
  +version: "v3"
  +servicePath: "drive/v3/"
  +availableScopes: null
  +resource: null
  -client: Google_Client {#207 ▶}
  +"serviceName": "drive"
}

It seems you have to list the revisions of a file and then update the ones you wish to keep forever: https://stackoverflow.com/questions/35357494/does-google-drive-keep-revisions-forever

This is what I got so far:

Route::get('revisions', function() {
    $filename = 'test.txt';

    // Store a demo file
    Storage::cloud()->put($filename, 'Hello World');

    // Get the file to find the ID
    $dir = '/';
    $recursive = false; // Get subdirectories also?
    $contents = collect(Storage::cloud()->listContents($dir, $recursive));
    $file = $contents
        ->where('type', '=', 'file')
        ->where('filename', '=', pathinfo($filename, PATHINFO_FILENAME))
        ->where('extension', '=', pathinfo($filename, PATHINFO_EXTENSION))
        ->first(); // there can be duplicate file names!

    // Keep revisions forever
    // - https://developers.google.com/drive/v3/reference/revisions
    // Get the underlying service class
    $service = Storage::cloud()->getAdapter()->getService();
    // Get the revision you want to change
    $revisions = collect($service->revisions->listRevisions($file['basename']));
    $revisionId = $revisions->first()->getId(); // just as an example, grab the first revision

    $before = $revisions->first()->getKeepForever(); // null

    // Create a new revision and set the peepForever property...
    // Using the original revision didn't work because it sends the revision ID which isn't allowed...
    $newRevision = new \Google_Service_Drive_Revision();
    $newRevision->setKeepForever(true);

    // Update / save the revision properties
    $service->revisions->update($file['basename'], $revisionId, $newRevision);

    // Verify if it worked...
    // Well... it is still null... Why???
    $after = collect($service->revisions->listRevisions($file['basename']))->first()->getKeepForever();

    dd($before, $after);

    return ($before === null && $after === true)
        ? 'Success'
        : 'Well that did not work...';
});

Perhaps anyone else knows what's wrong here. Maybe it's because it's a new file...

I'll try to find some more clues on Google... :)

blackhun11 commented 5 years ago

Hi, thanks for your answer, actually i find how to keeprevisionforever but i must edit in GoogleDriveAdapter.php by add params keepRevisionForever = true

ivanvermeyen commented 5 years ago

Cool! Thanks for the tip!