Where's the best place to get the hash of a file?

When users upload files to my app, I want to generate the md5 hash - where's the best place to hook into the uploadthing flow to do this?
Solution:
```ts .onUploadComplete(async ({ metadata, file }) => { console.log("file url", file.url); const buffer = await fetch(file.url).then((f) => f.arrayBuffer());...
Jump to solution
9 Replies
greypixel
greypixel•2mo ago
(using Next)
Neto
Neto•2mo ago
const crypto = require("node:crypto");

const hash = crypto.createHash("md5").setEncoding("hex");

hash.end(Buffer.from("UThing"));

console.log(hash.read());
// ddd448340180a2d9ffafc031562e4900
const crypto = require("node:crypto");

const hash = crypto.createHash("md5").setEncoding("hex");

hash.end(Buffer.from("UThing"));

console.log(hash.read());
// ddd448340180a2d9ffafc031562e4900
replace Buffer.from("UThing") with the file buffer (fetch it)
greypixel
greypixel•2mo ago
Thanks, so it's best to do this in onUploadComplete? Does the file get streamed to my server before uploadThing? Or is the request somehow redirected?
Solution
Neto
Neto•2mo ago
.onUploadComplete(async ({ metadata, file }) => {
console.log("file url", file.url);

const buffer = await fetch(file.url).then((f) => f.arrayBuffer());
// calculate hash

// !!! Whatever is returned here is sent to the clientside `onClientUploadComplete` callback
return { };
}),
.onUploadComplete(async ({ metadata, file }) => {
console.log("file url", file.url);

const buffer = await fetch(file.url).then((f) => f.arrayBuffer());
// calculate hash

// !!! Whatever is returned here is sent to the clientside `onClientUploadComplete` callback
return { };
}),
something like this
greypixel
greypixel•2mo ago
Thanks, super useful 🙂 out of interest would there be a way to check the hash before it goes to uploadThing? just trying to get the right mental model
Neto
Neto•2mo ago
let me check
Neto
Neto•2mo ago
most likely not
No description
greypixel
greypixel•2mo ago
thanks ser 🙂 really appreciate yourtime
Neto
Neto•2mo ago
^^
Want results from more Discord servers?
Add your server