Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to put custom filename #76

Open
shepilov-vladislav opened this issue Jun 21, 2023 · 4 comments
Open

No way to put custom filename #76

shepilov-vladislav opened this issue Jun 21, 2023 · 4 comments

Comments

@shepilov-vladislav
Copy link

I have a pretty specific library that is file extension oriented. In sqlalchemy-file it is not possible to specify a custom file name. However, the sqlalchemy_file.file.File.store_content function supports the name parameter, but the sqlalchemy_file.file.File.save_to_storage does not. So the file name will always be uuid4 only

@jowilf
Copy link
Owner

jowilf commented Jul 21, 2023

Do you mean custom file_id in the storage? because you can customize the filename with

file = File(content="Hello World", filename="hello.txt", content_type="text/plain")

Thesqlalchemy_file.file.File.store_content is used by processors to attach and save additional files into the storage (such as thumbnail) that is why it supports the name parameter.

You can also override the File class to provide a custom logic for save_to_storage including custom file_id.

To make it easier, maybe we can check if the File object contain the property file_id and use it instead of uuid.
Then the file_id can be customized like this:

file = File(content=..., filename=..., content_type=..., file_id="custom-file-id")

What do you think?

@Dexterp37
Copy link

Here's a sample implementation:

import uuid

from sqlalchemy_file.file import File
from sqlalchemy_file.stored_file import StoredFile
from typing import Any, Dict, Optional


class CustomFile(File):
    """Extends the SQLAlchemy `File` in order to provide a
    way to customize the storage path.

    Retains the attributes available for `File` and adds:

    Attributes:
        prefix (str):  The prefix to add to the stored filename
    """

    def __init__(self, prefix: str, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.filename_prefix = prefix

    def store_content(
        self,
        content: Any,
        upload_storage: Optional[str] = None,
        name: Optional[str] = None,
        metadata: Optional[Dict[str, Any]] = None,
        extra: Optional[Dict[str, Any]] = None,
        headers: Optional[Dict[str, str]] = None,
        content_path: Optional[str] = None,
    ) -> StoredFile:
        """Store content into provided `upload_storage`
        with additional `metadata`. Can be used by processors
        to store additional files.
        """
        name = name or f"{self.filename_prefix}{str(uuid.uuid4())}"
        return super().store_content(
            content,
            upload_storage,
            name,
            metadata,
            extra,
            headers,
            content_path,
        )

@shannon-rodricks
Copy link

shannon-rodricks commented May 1, 2024

So i have a similar requirement.
Im saving my images in S3 and i need to provide some sorta directory structure ex "media/post/<image_name>.jpg"
I need to override the store_content and provide a name.
The solution provided above would work for me, but I dont understand how to get the library to generate a CustomFile instead of File, when using the ImageField. I'm somewhat new to Python, hence the confusion.
Would appreciate any help.
Additionally, i need want to have the directory structure like so "media/post/<post_slug>/<image_name>.jpg". Is there a way i can achieve that?
Also, I'm currently manually replacing the s3 portion of the image with the cloudfront domain. Is there an alternative to that?

@NogueraMateo
Copy link

Hey, I am also saving my images in S3 but it stores the images in the root but I need them to be saved in a directory. Have you find a solution to this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants