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

About file bytes field #241

Open
greyli opened this issue Apr 30, 2022 · 2 comments
Open

About file bytes field #241

greyli opened this issue Apr 30, 2022 · 2 comments

Comments

@greyli
Copy link
Member

greyli commented Apr 30, 2022

To keep it clear, I restrict the File field to be a binary file and validate its type when deserializing the request data.

For base64-encoded files, I'm thinking of creating a FileByte field:

class FileByte(String):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.metadata['type'] = 'string'
        self.metadata['format'] = 'byte'

    default_error_messages = {
        'invalid': 'Not a valid byte file.',
    }

    def _deserialize(self, value, attr, data, **kwargs) -> t.Any:
        if not isinstance(value, str):
            raise self.make_error('invalid_byte')
        else:
            return bytes(value, encoding='ascii')

but I'm not sure if it is the correct way to do this.

And to handle this field, the location should be json or form instead of files, the field name may not intuitive enough.


Improve the File field serialization:

  • Allow setting custom response content type
  • Convert bytes to string?
@greyli greyli created this issue from a note in 1.0 (Later) Apr 30, 2022
@Weitspringer
Copy link

Sorry that I don't have an answer but another question. Since you seem to be working with the File field I wanted to ask something as I am unable to figure it out:
Is it possible to define a schema for multiple files? I am starting to think it is only supposed to work as a single file input field.
I use the following:

class UploadIn(Schema):
    file = File()

@app.post('/upload')
@app.input(UploadIn(), location='files')
def upload(data):
    print(data)
    files = data['file']

If I use @app.input(UploadIn(many=True), location='files'), this does not work properly.
Do you have any experience regarding that by any chance?

@Weitspringer
Copy link

Sorry that I don't have an answer but another question. Since you seem to be working with the File field I wanted to ask something as I am unable to figure it out: Is it possible to define a schema for multiple files? I am starting to think it is only supposed to work as a single file input field. I use the following:

class UploadIn(Schema):
    file = File()

@app.post('/upload')
@app.input(UploadIn(), location='files')
def upload(data):
    print(data)
    files = data['file']

If I use @app.input(UploadIn(many=True), location='files'), this does not work properly. Do you have any experience regarding that by any chance?

Never mind, I had to do

from apiflask import Schema
from apiflask.fields import File, List

class UploadIn(Schema):
    file = List(File())

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

No branches or pull requests

2 participants