Files
bitvid/js/utils/torrentHash.js
T
google-labs-jules[bot] 7dc2977cf2 Update Cloudflare R2 upload to use minimal permissions and add progress bar
- Removed requirement for Cloudflare API Token, Zone ID, and Worker Token from Upload Modal and R2Service.
- Simplified R2 configuration to require only Account ID, S3 Access Keys, and a user-provided Public Bucket URL.
- Added a read-only field in Upload Modal displaying the derived target bucket name (npub-based) for user reference.
- Implemented client-side torrent info hash calculation using `WebTorrent` (via `js/utils/torrentHash.js`) to generate magnet links before upload.
- Added a visual upload progress bar to the Upload Modal.
- Updated `js/r2.js` to ensure the Public Bucket URL always includes the `https://` protocol.
2026-01-12 18:53:22 +00:00

63 lines
1.9 KiB
JavaScript

import WebTorrent from "../webtorrent.min.js";
import { userLogger } from "./logger.js";
/**
* Calculates the torrent infoHash for a given file client-side.
* This operation requires reading and hashing the entire file, so it may take time for large files.
*
* @param {File} file - The file object to hash.
* @returns {Promise<string>} - Resolves with the hex encoded infoHash.
*/
export async function calculateTorrentInfoHash(file) {
if (!file) {
throw new Error("No file provided for hashing.");
}
// Create a temporary client just for hashing.
// We disable trackers and dht/lsd to keep it quiet and fast (local only).
const client = new WebTorrent({
tracker: false,
dht: false,
lsd: false,
webSeeds: false,
});
return new Promise((resolve, reject) => {
// Seed the file. This calculates the infoHash.
// We use destroyStoreOnDestroy: true to clean up memory/storage afterwards.
try {
client.seed(
file,
{
announce: [], // No trackers
destroyStoreOnDestroy: true,
name: file.name,
},
(torrent) => {
const infoHash = torrent.infoHash;
// Destroy the client and the torrent data immediately.
client.destroy((err) => {
if (err) {
userLogger.warn("Error destroying temporary hashing client:", err);
}
if (infoHash) {
resolve(infoHash);
} else {
reject(new Error("Failed to calculate info hash."));
}
});
}
);
client.on("error", (err) => {
userLogger.error("WebTorrent client error during hashing:", err);
client.destroy();
reject(err);
});
} catch (err) {
client.destroy();
reject(err);
}
});
}