vaphes / pocketbase

PocketBase client SDK for python
https://pypi.org/project/pocketbase/
MIT License
359 stars 41 forks source link

[featured] Add file url method #21

Closed paulocoutinhox closed 1 year ago

paulocoutinhox commented 1 year ago

Hi,

Can you add file-url method?

https://pocketbase.io/docs/files-handling/#file-url

Thanks.

m29h commented 1 year ago

I think the function that you are looking for is already there.. record_service.py:59

regisin commented 1 year ago

I'm trying to use that existing method like this:

    client = PocketBase(settings.PB_URL)
    client.admins.auth_with_password(settings.USERNAME, settings.PASSWORD)
    record = client.collection("example").get_one(item_id)
    record_path = client.get_file_url(record, item.avatar, {})

But I get TypeError: quote_from_bytes() expected bytes. I'm not sure where the issue could be.

paulocoutinhox commented 1 year ago

I think that you have problems with strings and bytes.

Check if something is b"something" where you have strings.

regisin commented 1 year ago

Looks like the exception comes from urllib. And it's triggered by client.py:123.

Could the fact that get_one() returns a BaseModel and not a Record type be the issue? get_file_url() expects a Record type as the first argument.

m29h commented 1 year ago

The TypeError exception in urllib occurs if the quote function receives a list urllib.quote(["file.txt"]) and not a string urllib.quote("file.txt") Your item.avatar is probably a list because pocketbase file type record fields can (potentially) store multiple files and therefore the record field value is always a python list type.

So a to get what you want you need to index the first element of item.avatar to get the explicit file name string.

    record_path = client.get_file_url(record, item.avatar[0], {})

Pocketbase performs analogous to the official JavaScript client example which is also indexing the file record list to get the explicit file name.

const firstFilename = record.documents[0];