masbug / flysystem-google-drive-ext

Flysystem adapter for Google Drive with seamless virtual<=>display path translation
Other
222 stars 62 forks source link

Retrieve Objects Ids by default when useDisplayPaths is set to False #25

Closed unsa-cdn closed 3 years ago

unsa-cdn commented 3 years ago

Hi I was reading through your code and I noticed you're using 'useDisplayPaths' param to prevent that feature but I wonder this: In the function createDirectory I get nothing back so how would I know which Id was assigned by google to my directory? I saw from the tests you're calling getMetadata function to retrieve created object by name but it would cause me problems for duplicates. If I understood something wrong correct me, otherwise you could give me a small example how to create a folder and get back its id please.

masbug commented 3 years ago

Hi, this driver is focused on the case when useDisplayPaths==true so the support for a false case might be lacking. However, when you call the createDir function you get back an array. The path should contain the id. Does it not? See this snippet from the createDir:

...
        $folder = $this->createDirectory($name, $pdir !== '' ? basename($pdir) : $pdir);
        if ($folder !== null) {
            $itemId = $folder->getId();
            $this->cacheFileObjects[$itemId] = $folder;
            $this->cacheHasDirs[$itemId] = false;
            $this->cacheObjects([$itemId => $folder]);
            $path_parts = $this->splitFileExtension($name);
            $result = [
                'path' => Util::normalizeDirname($pdir).'/'.($this->useDisplayPaths ? $name : $itemId), // <-here the $itemId should be the folder ID
                'filename' => $path_parts['filename'],
                'extension' => $path_parts['extension']
            ];
            return $result;
        }

        return false;
...
erikn69 commented 3 years ago

Also acording Flysystems V1 Docs you have to use createDir, not 'createDirectory', Flysystems Docs image but if you are using createDirectory , maybe with $result->getId();

unsa-cdn commented 3 years ago

@masbug sorry I didn't mentioned it I was asking for branch 2.x which I think uses Flysystems V2 where createDir and CreateDirectory functions are the opposite from version 1.x

erikn69 commented 3 years ago

https://github.com/masbug/flysystem-google-drive-ext/blob/237f5f3dac94c18652687a3c732aeb252c812778/src/GoogleDriveAdapter.php#L765-L776 @unsa-cdn on V2 everything changes(CreateDirectory returns void) you has to use FileAttributes or DirectoryAttributes, Flysystems V2 docs both has function extraMetadata(): array

So, you can use: $adapter->getMetadata or $adapter->listContents and iterate a list of FileAttributes

$file=$adapter->getMetadata('path/to/file.txt'); //can be directory
if($file){
    $id=$file->getMetadata()['id']
}

because: $result = ['id' => $id]; and return new FileAttributes($result['display_path'],$object->getSize(),$visibility, $object->getModifiedTime(),$object->mimeType,$result); https://github.com/masbug/flysystem-google-drive-ext/blob/237f5f3dac94c18652687a3c732aeb252c812778/src/GoogleDriveAdapter.php#L1070-L1075

There is a PR for more direct access, but it's a breaking change github.com/thephpleague/flysystem/pull/1324

If something doesn't work or can be improved, i can make a PR

unsa-cdn commented 3 years ago

I see I need to provide a path to this getMetadata function but the way google drive woks is with Ids. I would need to migrate my current database to use display paths instead of storing Ids I don't think so. Then I guess I will have to stay in V1 of flysystem. Thank you for your help @erikn69

erikn69 commented 3 years ago

V2 its a headacke with the throws and this package is focused on the case when useDisplayPaths==true