How to get external storage from File ID?

Hello there,

I’m working on a custom application for Nextcloud that use external storages (main app from Nextcloud), with a custom backend inherited from WebDAV

My application will also add a tab in File sidebar menu

On this tab, I want to get all informations related to External storage which this file belongs to (at least the URL). But during all my tests, I can’t check to which external storage my files belongs to (it seems there is no ID or param that can be checked)

My idea was:

  • get file details using $files = $userFolder->getById($fileId);
  • get all external storages using $this->globalStoragesService->getStorages();
  • loop on all external storage to check the file storage ID = external storage ID

But it seems the ID is totally different (I got 1 for external storage, and something totally different for the file)

Because multiple external storage can be attached with same path, I can’t really check only the file path / external storage path

Do you have another approach to do this ?

Thanks a lot,
Florian

Hey Florian,

check out IUserMountCache. You can call IUserMountCache->getMountsForFileId(…) which will give you mounts that contain the file in question, the mount then references the underlying storage id.

1 Like

@marcelklehr Thanks for the tip, the solution works perfectly!

Here is the code, is this can help someone else:

    public function getMountForSpecificFile(IUser $user, int $fileId): array {
        try {
            $mountsForFile = $this->test->getMountsForFileId($fileId, $user->getUID());
            
            if (empty($mountsForFile)) {
                return ['message' => 'no found for file ' . $fileId, 'error' => 'mount_not_found'];
            }

            // get configuration for external storage from ID
            $storage = $this->globalStoragesService->getStorage($mountsForFile[0]->getMountId());
            return $storage;

        } catch (Exception $e) {
            return ['message' => 'error getting mount config', 'error' => $e->getMessage()];
        } catch (NotFoundException $e) {
            return ['message' => 'mount not found for file ' . $fileId, 'error' => $e->getMessage()];
        } catch (StorageNotAvailableException $e) {
            return ['message' => 'mount not available for file ' . $fileId, 'error' => $e->getMessage()];
        }
	}
1 Like

I don’t know the getStorage method off the top of my head but I think mountIDs and storageIDs are distinct, might need to get the storageID from the mount instead of the mountID