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

Allow to return a blob for getItemRaw (http driver) #435

Closed
1 task done
TheAlexLichter opened this issue May 1, 2024 · 4 comments
Closed
1 task done

Allow to return a blob for getItemRaw (http driver) #435

TheAlexLichter opened this issue May 1, 2024 · 4 comments

Comments

@TheAlexLichter
Copy link
Member

TheAlexLichter commented May 1, 2024

Describe the feature

To fetch binary files (e.g. images), using a blob response would be helpful for the http driver

A simple and optional implementation could be:

diff --git a/drivers/http.mjs b/drivers/http.mjs
index 99a72ce2289532f7bbc52ad6ff7ec5b8755aa62c..a07d5fe496341b4e1a8e68cdd0b4cd7154270c41 100644
@@ -32,7 +33,8 @@ export default defineDriver((opts) => {
           accept: "application/octet-stream",
           ...opts.headers,
           ...topts.headers
-        }
+        },
+        responseType: topts.responseType ?? opts.responseType
       }).catch(catchFetchError);
       return value;
     },

Additional information

  • Would you be willing to help implement this feature?

Referencing #142 for this too :)

@pi0
Copy link
Member

pi0 commented May 1, 2024

Please issue issues to explain why not how 🙏🏼 (eg: a user code example or reproduction)

@pi0 pi0 changed the title feat(http): allow to change responseType to use blob for getItemRaw Allow to return a blob for getItemRaw (http driver) May 1, 2024
@pi0 pi0 added the discussion label May 1, 2024
@TheAlexLichter
Copy link
Member Author

TheAlexLichter commented May 1, 2024

@pi0 Of course!

Why

Because if you want to fetch binary files, such as images from the http server unstorage provides, there is no way to do so with the HTTP driver and ensure the formatting is correct.

The goal would be doing something like this in the browser (or whereever) with the server running on the HTTP server of unstorage:

const storage = createStorage({
  driver: httpDriver({ base: "/api/storage" }),
});

const getItem = async () => {
  const blob = await storage.getItemRaw(name.value, {
    responseType: 'blob'
  })
  const url = URL.createObjectURL(blob)
  // do more with the URL
}

"server part"

export default defineEventHandler(event => {
  const storage = useStorage('assets:server')

  setHeader(event, 'Content-Type', 'image/png') // Image server only
  
  return createH3StorageHandler(storage)(event)
})

Other ideas

Right now, there is no way to change the response type being a blob, hence the binary files cannot be used easily.

  • Manual blob creation also failed so far.
  • Not using the http driver would be another option but feels like a hack around it.
  • Providing a way to hook into the full response could be helpful so it could be fully customizable but might offer too much flexibility.

If you have a better idea on solving this problem, please let me know 😋

@pi0
Copy link
Member

pi0 commented May 1, 2024

Thanks for explaining.

I think it makes sense but we should consider implementing a driver-agnostic type (ideally via undio supported types) rather than patching for only one driver. (HTTP driver then can respect this type and prefer native blob or response return when it can preserve blob/file information)

responseType is specific to the underlying library of HTTP driver (ofetch) and we cannot rely on it nor diverge the API only for one driver support (HTTP) because it can change and unstorage API should be consistent regardless of the driver.

If you have a better idea of solving this problem,

In the meantime, I would suggest you directly use fetch for the HTTP driver if need blob access. raw support is still marked as experimental for unstorage after all for the very same reason of limitations and inconsistent behavior.

Let's track this via #142 👍🏼 (I have updated undio note in there)

@pi0 pi0 closed this as completed May 1, 2024
@TheAlexLichter
Copy link
Member Author

Makes sense! Thanks for the explanation ☺️

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