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

File Upload not working in RN / Expo #164

Open
klydra opened this issue Nov 3, 2024 · 2 comments
Open

File Upload not working in RN / Expo #164

klydra opened this issue Nov 3, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@klydra
Copy link

klydra commented Nov 3, 2024

What version of Elysia is running?

1.1.20

What platform is your computer?

Windows 11 x64

What steps can reproduce the bug?

API :

new Elysia().put(
  "upload",
  async ({ body }) => {
    await Bun.write("test.txt", body.file);
  },
  {
    body: t.Object({
      file: t.File(),
    }),
  },
);

What is the expected behavior?

File to upload

What do you see instead?

eden >= 1.0.13 throws Network request failed

eden <= 1.0.12 actually calls API and produces error (code: 422)

{
  "type": "validation",
  "on": "body",
  "summary": "Expected kind 'File'",
  "property": "/file",
  "message": "Expected kind 'File'",
  "expected": {
    "file": "File"
  },
  "found": {
    "file": "File"
  },
  "errors": [
    {
      "summary": "Expected kind 'File'",
      "type": 31,
      "schema": {
        "default": "File",
        "type": "string",
        "format": "binary"
      },
      "path": "/file",
      "value": "File",
      "message": "Expected kind 'File'",
      "errors": []
    }
  ]
}

Additional information

Eden Client is running on expo SDK 51 on Android.
Same function is working just fine in any version of Elysia on web.
All other API calls work fine.

Have you try removing the node_modules and bun.lockb and try again yet?

Yes

@klydra klydra added the bug Something isn't working label Nov 3, 2024
@klydra klydra changed the title File Upload not working File Upload not working in RN / Expo Nov 3, 2024
@klydra
Copy link
Author

klydra commented Nov 3, 2024

For anyone else running into this issue, I'm currently using this something like this as a workaround in the meantime.
It's less than optimal in so many ways, espeacially memory wise I'm sure, but works.

new Elysia().put(
  "upload",
  async ({ body }) => {
    if ("bytes" in body && body.bytes.length >= 10 * 1000 * 1000)
      return error(400, "file size limit exceeded");

    const path = "test.txt";

    if ("bytes" in body) await Bun.write(path, new Uint8Array(body.bytes));
    if ("file" in body) await Bun.write(path, body.file);
  },
  {
    body: t.Union([
      t.Object({
        bytes: t.Array(t.Number({ minimum: 0, maximum: 255 })),
      }),
      t.Object({
        file: t.File({
          maxSize: "10m",
        }),
      }),
    ]),
  },
);

@klydra
Copy link
Author

klydra commented Nov 3, 2024

There seem to be semi-related issues which discuss switching to formdata for the file content. But as far as I can tell, that should be a non-issue by now. I also tried to manually set the type in the route to "formdata" or "multipart/form-data", but could not observe any meaningful difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant