fastapi-storages icon indicating copy to clipboard operation
fastapi-storages copied to clipboard

Support `upload_to` argument for file/image types

Open aminalaee opened this issue 1 year ago • 7 comments

https://docs.djangoproject.com/en/4.2/ref/models/fields/#filefield

aminalaee avatar Jun 26 '23 12:06 aminalaee

U can override method get_name

def get_name(self, name: str) -> str:
        filename = secure_filename(Path(name).name)
        return str(Path(name).with_name(filename))

https://github.com/aminalaee/fastapi-storages/blob/615167aed103909271da82abff92d5c5cb47612f/fastapi_storages/s3.py#L63C12-L63C12

pyostr avatar Sep 27 '23 21:09 pyostr

@aminalaee should we consider using MEDIA_ROOT just like Django, and add upload_to argument to FileType or ImageType? Or would it be good, to have storage argument (like now), plus upload_to?

mmzeynalli avatar Oct 03 '23 08:10 mmzeynalli

@mmzeynalli What do you mean by storage arguments? Because I think we have storage class variables, if that's what you mean.

aminalaee avatar Oct 03 '23 09:10 aminalaee

My bad, explanation was not clear. I meant, when we create a FileType

class MyModel(Base):
    img  = mapped_column(FileType, upload_to="/path/to/file")

is what we want right? Which uses FileType.storage + upload_to path for file

mmzeynalli avatar Oct 03 '23 09:10 mmzeynalli

Yeah I think that makes sense, plus note that it can be a callable: https://docs.djangoproject.com/en/4.2/ref/models/fields/#django.db.models.FileField.upload_to

I think we can ignore upload_to="uploads/%Y/%m/%d/" for now, this can be achieved with the callable.

aminalaee avatar Oct 03 '23 09:10 aminalaee

How can I go about using a callable? I wanted to upload something like /tmp/{user.id}/

regisin avatar Oct 08 '23 21:10 regisin

As of now, this feature is not implemented. Instead, you can change file name when you receive the file (for ex. API), giving it the needed path

mmzeynalli avatar Oct 08 '23 21:10 mmzeynalli