Created
December 3, 2022 18:43
-
-
Save redsolver/7dff317e244444d44113c0aa9015fd63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Blake3Hasher } from '@napi-rs/blake-hash' | |
import * as tus from "tus-js-client"; | |
import * as fs from "fs"; | |
const portalUrl = 'http://localhost:5522'; | |
const path = `/path/to/file.mp4` | |
const mhashBlake3Default = 0x1f; | |
const cidTypeRaw = 0x26; | |
const b3hash = await new Promise((resolve, reject) => { | |
const hasher = new Blake3Hasher() | |
const stream = fs.createReadStream(path); | |
stream.on('error', err => reject(err)); | |
stream.on('data', chunk => hasher.update(chunk)); | |
stream.on('end', () => resolve(hasher.digestBuffer())); | |
}); | |
console.log('BLAKE3 hash', b3hash.toString('hex')) | |
const hash = Buffer.concat([Buffer.alloc(1, mhashBlake3Default), b3hash]); | |
const cid = Buffer.concat([Buffer.alloc(1, cidTypeRaw), hash, numberToBuffer(fs.statSync(path).size)]); | |
const file = fs.createReadStream(path) | |
function numberToBuffer(value) { | |
const view = Buffer.alloc(16); | |
let lastIndex = 15; | |
for (var index = 0; index <= 15; ++index) { | |
if ((value % 256) !== 0) { | |
lastIndex = index; | |
} | |
view[index] = value % 256; | |
value = value >> 8; | |
} | |
return view.subarray(0, lastIndex + 1) | |
} | |
const options = { | |
endpoint: portalUrl + '/s5/upload/tus', | |
metadata: { | |
hash: hash.toString('base64url'), | |
}, | |
onError(error) { | |
console.error('An error occurred:') | |
console.error(error) | |
process.exitCode = 1 | |
}, | |
onProgress(bytesUploaded, bytesTotal) { | |
const percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(2) | |
console.log(bytesUploaded, bytesTotal, `${percentage}%`) | |
}, | |
onSuccess() { | |
console.log('Upload finished.') | |
console.log('CID:', 'u' + cid.toString('base64url')) | |
}, | |
} | |
const upload = new tus.Upload(file, options) | |
upload.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment