Tracking request/useMutation progress

I have a tRPC endpoint that takes in images in base64, and in the frontend i want the user to be able to watch the progress of the files being uploaded. Not sure on how to really do that. is there a function im missing? I tried looking at the documentation and cant find what im looking for.
105 Replies
Maj
Maj•2y ago
Uhh like a progress bar? if you upload images as base64 u wont be able to track that. U can progress track the client side upload. Usually user has to upload the file first then u upload it after its fully loaded into the client side
elpupper
elpupperOP•2y ago
yeah pretty much my goal even tho im deploying in the US there are still parts in the US where they have slow wifi i couldnt figure out how to do file(image) uploads through tRPC
Maj
Maj•2y ago
DEV Community
React Tips & Tricks: Uploading a File With A Progress Bar
Forms are often tricky to get right with React. While there are great libraries like formik or React...
elpupper
elpupperOP•2y ago
the whole point is to use this within tRPC
Maj
Maj•2y ago
Im not sure if tRPC supports that
elpupper
elpupperOP•2y ago
yeah thats what i feared
Keef
Keef•2y ago
I think its because its using json rpc so you can't use it on its own to do the file uploading cause the binary data might get morphed (just my guess?). I do puts to s3 presigned urls on the client side and it works well
Keef
Keef•2y ago
TomDoesTech
YouTube
Use Presigned PUT URLs to Easily Upload Files to AWS S3
Theo's Tweet: https://twitter.com/t3dotgg/status/1564019039877271552 Repository: https://github.com/TomDoesTech/pre-signed-put-url 0:00 Intro 0:43 Data flow 1:45 Bootstrap application 2:33 S3 bucket setup 5:07 Get PUT URL handler 10:36 Form 15:24 Does it work? 🌎 Follow me here: Discord: https://discord.gg/4ae2Esm6P7 Twitter: https://twitter.c...
elpupper
elpupperOP•2y ago
hmm that might be the move just upload the files client side
Keef
Keef•2y ago
it works very well How big are the files you want to upload?
elpupper
elpupperOP•2y ago
10mb 5-10 images
Keef
Keef•2y ago
should be able to do it without multi part uploads i think so that makes things easier
elpupper
elpupperOP•2y ago
10mb is the limit
Keef
Keef•2y ago
for progress just update some bit of state and render it if you go down this route "Uploaded 1/5 files" yadda yadda
elpupper
elpupperOP•2y ago
ok thanks ill have a look im pretty sure this should work with r2 thanks for that tho
Keef
Keef•2y ago
r2 is a object blob storage ?
elpupper
elpupperOP•2y ago
s3 alternative
Keef
Keef•2y ago
yeah should be fine
elpupper
elpupperOP•2y ago
u still use the aws-sdk
Keef
Keef•2y ago
the directions won't be 1:1 but basically you need an endpoint you can dump data into (s3 presigned url is basically that) and do thing Lemme see what I used I've been doing image uploads using the rust beta aws sdk
elpupper
elpupperOP•2y ago
haha how is that going
Keef
Keef•2y ago
ik i wrote the stuff for our frontend but its probably the aws sdk works 😄
elpupper
elpupperOP•2y ago
yeah it is the aws-sdk how doeds the presigned url work
Keef
Keef•2y ago
So I have a mutation that'll generate a link for me for 5 or so minutes Thats the "presigned url" User fills out the form which has file inputs and then you just do a casual put to the url and you are done
elpupper
elpupperOP•2y ago
so if nothing gets uploaded or its canceled it expires
Keef
Keef•2y ago
yep
elpupper
elpupperOP•2y ago
thats perfect
Keef
Keef•2y ago
the generation is a protected procedure too
elpupper
elpupperOP•2y ago
🔥 ok that is nice
Keef
Keef•2y ago
You shouldn't need to use the sdk on the client side at all. Just mentioning this so you don't leak your aws keys the video i linked is good
elpupper
elpupperOP•2y ago
yep i can have a server side function right im guessing thats the idea
Keef
Keef•2y ago
You just fetch/axios/http request to the url anything running in the trpc land is safe so the presigned generation can use the sdk cause it lives on the server the client side just gets the url back, PUT request to it with your data and done
elpupper
elpupperOP•2y ago
alright thanks a lot will have a look at the video and the aws-sdk docs thanks a lot for that i completely forgot about it
Keef
Keef•2y ago
Presigned URLs · Cloudflare R2 docs
Presigned URLs are an S3 concept for sharing direct access to your bucket without revealing your token secret. A presigned URL authorizes anyone with …
elpupper
elpupperOP•2y ago
over engineery moment
Keef
Keef•2y ago
have fun love
elpupper
elpupperOP•2y ago
thank u appreciate it would it make sense to zip files after you verify they are images then make a presigned url and upload that? that way instead of making 5-10 s3 calls u just make 1
Keef
Keef•2y ago
I personally wouldn’t lose sleep over the s3 calls but you can zip on the clientside using jszip from my googling You will have to unpack it at some point if you want to work with it
elpupper
elpupperOP•2y ago
yes ofcourse i already do that and also use jszip
Keef
Keef•2y ago
If it works it’s fine
elpupper
elpupperOP•2y ago
just making sure there is no weird shit that can possible happen surely can they? i mean they will get a presigned url and its gonna upload a zip file but is there a way to make sure the presigned url gets a zip file that only has 5-10 images thats a bit sketch actually
Keef
Keef•2y ago
you could do checksums to ensure the files are the same on both sides
elpupper
elpupperOP•2y ago
cause they would be able to see the presigned url and upload anything
Keef
Keef•2y ago
You can limit what content types they can upload and sizes
elpupper
elpupperOP•2y ago
through the presigned urls correct
Keef
Keef•2y ago
I think both? My form limits the choices a user can dump on to the web app but you also can provide meta data to the thing your uploading on the sdk
elpupper
elpupperOP•2y ago
thats the thing what if someone uploads a zip bomb the input is already just for images but i guess what my question is
Keef
Keef•2y ago
ask for 5 - 10 images then zip it yourself no worry about zip bombs
elpupper
elpupperOP•2y ago
would they be able to get the presigned url or not
Keef
Keef•2y ago
Yeah they can if they want to, trpc returns http which is easily snooped
elpupper
elpupperOP•2y ago
hmm this needs some more thought
elpupper
elpupperOP•2y ago
ill have a look at that
Keef
Keef•2y ago
Are these users signed up btw? as in you are limiting uploads behind a protected procedure
elpupper
elpupperOP•2y ago
yes authed by clerk shouldnt be worried then i guess right? cant i have custom auth header for the presigned url nah fuck it i will just do the 5-10 s3 calls i dont think u can really verify the .zip images within the presigned url
Keef
Keef•2y ago
s3 is pretty cheap just make sure you don't lose the uploads cause asking for reupload is awkward You would use a lambda or something to verify the upload You can listen for changes in a bucket/new uploads and spin off handlers based on that if you wanted to get fancy
elpupper
elpupperOP•2y ago
hmmm what if i had a multipart image upload for the presigned url and the output is zipped
Keef
Keef•2y ago
multipart is something specific where you have a extremely big file (something you can't dump into ram entirely) and you have to upload in parts your 50mb is nothing
elpupper
elpupperOP•2y ago
hmm well something to do with uploading all images into 1 endpoint and it being zipped i think i just gotta do a nextapi protected route and call it a day this is so stupid why is it so annoying in JS?
Maj
Maj•2y ago
typescript is becoming the standard 😉 😂
elpupper
elpupperOP•2y ago
i am using TS 😭 but handling files is the most BS activity ever
Maj
Maj•2y ago
Oh...
elpupper
elpupperOP•2y ago
its so easy and simple in python AND rust
Maj
Maj•2y ago
yeah i prefer rust over everything
elpupper
elpupperOP•2y ago
maybe after i deploy ill look into using rust in the backend ontop of nextjs just not sure how i would/can use clerk
Maj
Maj•2y ago
u using clerk for auth?
elpupper
elpupperOP•2y ago
yeah cant be asked to code everything from scratch just yet
Maj
Maj•2y ago
idk there are no providers for auth for rust and js. jwt and some other standard auth stuff can be implemented
elpupper
elpupperOP•2y ago
i know that you can use clerk api directly
Maj
Maj•2y ago
then thats possible but u would have to write everything if there is no library
elpupper
elpupperOP•2y ago
i mean
Maj
Maj•2y ago
elpupper
elpupperOP•2y ago
the only thing i'd really need is to verify the auth header interesting for protected api routes
Maj
Maj•2y ago
yah thats normal
elpupper
elpupperOP•2y ago
but thats about it i guess
Maj
Maj•2y ago
usually people just make middleware for auth stuff and check anything there
elpupper
elpupperOP•2y ago
yeah i know but still some protected routes
Maj
Maj•2y ago
ya
elpupper
elpupperOP•2y ago
would be very interesting
Maj
Maj•2y ago
i used expressjs alot of auth
elpupper
elpupperOP•2y ago
might whip something up with fastapi
Maj
Maj•2y ago
jwt
Maj
Maj•2y ago
elpupper
elpupperOP•2y ago
yeah i did the same but in fastapi but i just couldnt be asked to always have to reset everything up for every new project
Maj
Maj•2y ago
yea u can do it in any http server framework
elpupper
elpupperOP•2y ago
email verification, sms verification, login, oauth clerk just lets me plug n play very nice and IMO pretty cheap
Maj
Maj•2y ago
never used clerk i use nextauth for auth
elpupper
elpupperOP•2y ago
i hate js and ts i tried to get into nextauth and i just couldnt
Maj
Maj•2y ago
i love ts. after i got decent with rust. Ts was so much easier
elpupper
elpupperOP•2y ago
im also decent with rust just hate ts
Maj
Maj•2y ago
are u using nextjs?
elpupper
elpupperOP•2y ago
yes
Maj
Maj•2y ago
u can use middleware for protected pages
elpupper
elpupperOP•2y ago
yeah i know thats what im doing thats not my problem 😭 my problem is file uploading
Maj
Maj•2y ago
rip
elpupper
elpupperOP•2y ago
it works rn im converting to base64 and using tRPC but doing so doesnt let me track the progress
Maj
Maj•2y ago
yikes that will be hard probably idk how can you track a base64 string xd tracking how many bytes are loaded is easier
elpupper
elpupperOP•2y ago
as in im tryna figure out how much bytes are loaded while sending if that makes sense
Maj
Maj•2y ago
thats with native js browser file input field
elpupper
elpupperOP•2y ago
explain please ok
Maj
Maj•2y ago
no trpc.
elpupper
elpupperOP•2y ago
gonna see
Maj
Maj•2y ago
FileReader: progress event - Web APIs | MDN
The progress event is fired periodically as the FileReader reads data.
elpupper
elpupperOP•2y ago
thanks gonna have a look i might just not add a progress bar in the end
Want results from more Discord servers?
Add your server