Files
google-labs-jules[bot] 4e840c6159 feat: improve magnet link reliability with webseeds
- Update `UploadModal.js` to pre-calculate R2 keys and public URLs before upload.
- Update `torrentHash.js` to embed the calculated video URL as a webseed (`url-list`) in the `.torrent` file.
- Update `r2Service.js` to accept forced keys/URLs and append `&xs=` (pointing to the .torrent file) to the magnet link.
- Ensures magnet links work for peerless downloads via WebSeed (BEP19) and HTTP Seeding (BEP17).
2026-01-15 13:26:27 +00:00

73 lines
1.9 KiB
JavaScript

import WebTorrent from "../webtorrent.min.js";
import { userLogger } from "./logger.js";
const TORRENT_SEED_OPTIONS = {
announce: [],
destroyStoreOnDestroy: true,
};
function seedTorrentForFile(file, urlList = []) {
if (!file) {
return Promise.reject(new Error("No file provided for hashing."));
}
const client = new WebTorrent({
tracker: false,
dht: false,
lsd: false,
webSeeds: false,
});
return new Promise((resolve, reject) => {
try {
client.seed(
file,
{
...TORRENT_SEED_OPTIONS,
name: file.name,
urlList: urlList,
},
(torrent) => {
const infoHash = torrent.infoHash;
const torrentFile = torrent.torrentFile || torrent.torrentFileBuffer || null;
client.destroy((err) => {
if (err) {
userLogger.warn("Error destroying temporary hashing client:", err);
}
if (infoHash) {
resolve({ infoHash, torrentFile });
} 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);
}
});
}
/**
* 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) {
const { infoHash } = await seedTorrentForFile(file);
return infoHash;
}
export async function createTorrentMetadata(file, urlList = []) {
return seedTorrentForFile(file, urlList);
}