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•12mo 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
elpupper•12mo 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•12mo 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
elpupper•12mo ago
the whole point is to use this within tRPC
Maj
Maj•12mo ago
Im not sure if tRPC supports that
elpupper
elpupper•12mo ago
yeah thats what i feared
Keef
Keef•12mo 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•12mo 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
elpupper•12mo ago
hmm that might be the move just upload the files client side
Keef
Keef•12mo ago
it works very well How big are the files you want to upload?
elpupper
elpupper•12mo ago
10mb 5-10 images
Keef
Keef•12mo ago
should be able to do it without multi part uploads i think so that makes things easier
elpupper
elpupper•12mo ago
10mb is the limit
Keef
Keef•12mo 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
elpupper•12mo ago
ok thanks ill have a look im pretty sure this should work with r2 thanks for that tho
Keef
Keef•12mo ago
r2 is a object blob storage ?
elpupper
elpupper•12mo ago
s3 alternative
Keef
Keef•12mo ago
yeah should be fine
elpupper
elpupper•12mo ago
u still use the aws-sdk
Keef
Keef•12mo 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
elpupper•12mo ago
haha how is that going
Keef
Keef•12mo ago
ik i wrote the stuff for our frontend but its probably the aws sdk works 😄
elpupper
elpupper•12mo ago
yeah it is the aws-sdk how doeds the presigned url work
Keef
Keef•12mo 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
elpupper•12mo ago
so if nothing gets uploaded or its canceled it expires
Keef
Keef•12mo ago
yep
elpupper
elpupper•12mo ago
thats perfect
Keef
Keef•12mo ago
the generation is a protected procedure too
elpupper
elpupper•12mo ago
🔥 ok that is nice
Keef
Keef•12mo 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
elpupper•12mo ago
yep i can have a server side function right im guessing thats the idea
Keef
Keef•12mo 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
elpupper•12mo 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•12mo 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
elpupper•12mo ago
over engineery moment
Keef
Keef•12mo ago
have fun love
elpupper
elpupper•12mo 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•12mo 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
elpupper•12mo ago
yes ofcourse i already do that and also use jszip
Keef
Keef•12mo ago
If it works it’s fine
elpupper
elpupper•12mo 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•12mo ago
you could do checksums to ensure the files are the same on both sides
elpupper
elpupper•12mo ago
cause they would be able to see the presigned url and upload anything
Keef
Keef•12mo ago
You can limit what content types they can upload and sizes
elpupper
elpupper•12mo ago
through the presigned urls correct
Keef
Keef•12mo 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
elpupper•12mo 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•12mo ago
ask for 5 - 10 images then zip it yourself no worry about zip bombs
elpupper
elpupper•12mo ago
would they be able to get the presigned url or not
Keef
Keef•12mo ago
Yeah they can if they want to, trpc returns http which is easily snooped
elpupper
elpupper•12mo ago
hmm this needs some more thought
elpupper
elpupper•12mo ago
ill have a look at that
Keef
Keef•12mo ago
Are these users signed up btw? as in you are limiting uploads behind a protected procedure
elpupper
elpupper•12mo 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•12mo 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
elpupper•12mo ago
hmmm what if i had a multipart image upload for the presigned url and the output is zipped
Keef
Keef•12mo 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
elpupper•12mo 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•12mo ago
typescript is becoming the standard 😉 😂
elpupper
elpupper•12mo ago
i am using TS 😭 but handling files is the most BS activity ever
Maj
Maj•12mo ago
Oh...
elpupper
elpupper•12mo ago
its so easy and simple in python AND rust
Maj
Maj•12mo ago
yeah i prefer rust over everything
elpupper
elpupper•12mo 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•12mo ago
u using clerk for auth?
elpupper
elpupper•12mo ago
yeah cant be asked to code everything from scratch just yet
Maj
Maj•12mo ago
idk there are no providers for auth for rust and js. jwt and some other standard auth stuff can be implemented
elpupper
elpupper•12mo ago
i know that you can use clerk api directly
Maj
Maj•12mo ago
then thats possible but u would have to write everything if there is no library
elpupper
elpupper•12mo ago
i mean
Maj
Maj•12mo ago
elpupper
elpupper•12mo ago
the only thing i'd really need is to verify the auth header interesting for protected api routes
Maj
Maj•12mo ago
yah thats normal
elpupper
elpupper•12mo ago
but thats about it i guess
Maj
Maj•12mo ago
usually people just make middleware for auth stuff and check anything there
elpupper
elpupper•12mo ago
yeah i know but still some protected routes
Maj
Maj•12mo ago
ya
elpupper
elpupper•12mo ago
would be very interesting
Maj
Maj•12mo ago
i used expressjs alot of auth
elpupper
elpupper•12mo ago
might whip something up with fastapi
Maj
Maj•12mo ago
jwt
Maj
Maj•12mo ago
elpupper
elpupper•12mo 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•12mo ago
yea u can do it in any http server framework
elpupper
elpupper•12mo ago
email verification, sms verification, login, oauth clerk just lets me plug n play very nice and IMO pretty cheap
Maj
Maj•12mo ago
never used clerk i use nextauth for auth
elpupper
elpupper•12mo ago
i hate js and ts i tried to get into nextauth and i just couldnt
Maj
Maj•12mo ago
i love ts. after i got decent with rust. Ts was so much easier
elpupper
elpupper•12mo ago
im also decent with rust just hate ts
Maj
Maj•12mo ago
are u using nextjs?
elpupper
elpupper•12mo ago
yes
Maj
Maj•12mo ago
u can use middleware for protected pages
elpupper
elpupper•12mo ago
yeah i know thats what im doing thats not my problem 😭 my problem is file uploading
Maj
Maj•12mo ago
rip
elpupper
elpupper•12mo ago
it works rn im converting to base64 and using tRPC but doing so doesnt let me track the progress
Maj
Maj•12mo 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
elpupper•12mo ago
as in im tryna figure out how much bytes are loaded while sending if that makes sense
Maj
Maj•12mo ago
thats with native js browser file input field
elpupper
elpupper•12mo ago
explain please ok
Maj
Maj•12mo ago
no trpc.
elpupper
elpupper•12mo ago
gonna see
Maj
Maj•12mo ago
FileReader: progress event - Web APIs | MDN
The progress event is fired periodically as the FileReader reads data.
elpupper
elpupper•12mo ago
thanks gonna have a look i might just not add a progress bar in the end