fix: fix paths

This commit is contained in:
2025-04-05 10:40:48 +08:00
parent 3dfcf2a536
commit b0c12acbc6
4 changed files with 46 additions and 61 deletions

View File

@@ -6,7 +6,6 @@ import fsp from 'node:fs/promises';
import path from 'node:path';
// @ts-ignore
import TrackerServer from 'bittorrent-tracker/server';
import createTorrent from 'create-torrent';
import WebTorrent, { type Torrent } from 'webtorrent';
// Configuration
@@ -17,7 +16,7 @@ const STATIC_API_PATH = '/api/static';
const LOCAL_IP = '127.0.0.1';
const WORKSPACE_PATH = 'workspace';
const TRACKER_URL = `http://${LOCAL_IP}:${TRACKER_PORT}/announce`;
const API_BASE_URL = `http://${LOCAL_IP}:${API_PORT}/${STATIC_API_PATH}/`;
const API_BASE_URL = `http://${LOCAL_IP}:${API_PORT}${STATIC_API_PATH}/`;
// Initialize Fastify instance
const app = Fastify({ logger: true });
@@ -90,44 +89,22 @@ async function generateMockFile(filePath: string, size: number) {
await fsp.truncate(filePath, size);
}
// Generate bittorrent file
function generateTorrent(folderPath: string, torrentPath: string) {
return new Promise<void>((resolve, reject) => {
createTorrent(
folderPath,
// Add bittorrent and seed
async function seedTorrent(
torrentPath: string,
contentFolder: string
): Promise<Torrent> {
return new Promise((resolve) => {
const torrent = webTorrent.seed(
contentFolder,
{
announceList: [[TRACKER_URL]], // Specify tracker URL
private: false,
createdBy: 'WebTorrent',
comment: 'Generated by WebTorrent server',
createdBy: 'Konobangu Testing Torrents',
urlList: [API_BASE_URL],
},
async (err, torrent) => {
if (err) {
reject(new Error(`Failed to create torrent: ${err}`));
return;
}
await fsp.writeFile(torrentPath, torrent);
if (!fs.existsSync(torrentPath)) {
reject(new Error(`Torrent file ${torrentPath} was not created`));
return;
}
console.log(`Generated torrent with tracker: ${TRACKER_URL}`);
resolve();
}
);
});
}
// Add bittorrent and seed
async function seedTorrent(torrentPath: string): Promise<Torrent> {
return new Promise((resolve) => {
const torrent = webTorrent.seed(
torrentPath,
{
announce: [TRACKER_URL],
},
(t) => {
async (t) => {
await fsp.writeFile(torrentPath, t.torrentFile);
resolve(t);
}
);
@@ -156,9 +133,8 @@ app.post<{ Body: RequestSchema }>('/api/torrents/mock', async (req, _reply) => {
}
const torrentPath = path.join(WORKSPACE_PATH, `${id}.torrent`);
await generateTorrent(idFolder, torrentPath);
const torrent = await seedTorrent(torrentPath);
const torrent = await seedTorrent(torrentPath, idFolder);
const magnetUrl = `magnet:?xt=urn:btih:${torrent.infoHash}&tr=${TRACKER_URL}`;
return {