Skip to content

Commit 3e9dd88

Browse files
committed
switch to vuejs
the server api is stable updated the README file
1 parent 7ba0bf7 commit 3e9dd88

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+13885
-3372
lines changed

.browserslistrc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
> 1%
2+
last 2 versions

.eslintrc.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
node: true
5+
},
6+
extends: ["plugin:vue/essential", "@vue/prettier"],
7+
rules: {
8+
"no-console": process.env.NODE_ENV === "production" ? "error" : "off",
9+
"no-debugger": process.env.NODE_ENV === "production" ? "error" : "off"
10+
},
11+
parserOptions: {
12+
parser: "babel-eslint"
13+
}
14+
};

.gitignore

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
1+
.DS_Store
12
node_modules
2-
.env
3+
/dist
4+
5+
# local env files
6+
.env.local
7+
.env.*.local
8+
9+
# Log files
10+
npm-debug.log*
11+
yarn-debug.log*
12+
yarn-error.log*
13+
14+
# Editor directories and files
15+
.idea
16+
.vscode
17+
*.suo
18+
*.ntvs*
19+
*.njsproj
20+
*.sln
21+
*.sw?

README.md

+44-44
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,63 @@
11
# live-torrent
22

3-
Explore, stream and share torrents online
3+
Explore, stream and share torrents online.
44

5-
HTML5 video player: __Plyr.js__
5+
HTML5 video player: **Plyr.js**.
66

7-
project is __Web Torrent based__
7+
Project is **Web Torrent** based.
88

99
## How to use
1010

1111
### Environment Variables
1212

1313
> Note: you can use .env file to setup vars
1414
15-
Var | Default | Desc
16-
----|---------|-----
17-
PORT | 3000 | server listening port
15+
| Var | Default | Desc |
16+
| ---- | ------- | --------------------- |
17+
| PORT | 3000 | server listening port |
1818

1919
### For Users
2020

2121
```bash
22-
# clone the project
23-
git clone https://github.com/Davenchy/live-torrent.git
24-
2522
# install dependencies
26-
npm i
23+
npm install
24+
25+
# build frontend
26+
npm run build
2727

2828
# start the server
2929
npm start
30+
# or
31+
npm run torrent-server
3032
```
3133

3234
### For Developers
3335

3436
```bash
35-
# clone the project
36-
git clone https://github.com/Davenchy/live-torrent.git
37-
3837
# install dependencies
39-
npm i -D
38+
npm install -D
4039

41-
# run dev server
42-
npm run dev
40+
# run torrent api server
41+
npm run torrent-server-dev
42+
43+
# compile and hot-reloads vue.js frontend
44+
npm run serve
4345
```
4446

47+
#### To Build Frontend
48+
49+
```bash
50+
npm run build
51+
```
4552

4653
## Server API
4754

4855
### Get torrent info
4956

50-
Method | path | params
51-
----|----|----
52-
GET | /api/torrent/info | torrentId [required]
53-
GET | /api/torrent/info/:infoHash
57+
| Method | path | params |
58+
| ------ | ------------------- | -------------------- |
59+
| GET | /api/info | torrentId [required] |
60+
| GET | /api/info/:infoHash |
5461

5562
response:
5663

@@ -80,14 +87,14 @@ The File Object:
8087

8188
```
8289

83-
______
90+
---
8491

8592
### Streaming
8693

87-
Method | path | params
88-
-------|-----|-------
89-
GET | /api/torrent/stream | torrentId [required], fileIndex [default = 0]
90-
GET | /api/torrent/stream/:infoHash/:fileIndex
94+
| Method | path | params |
95+
| ------ | -------------------------------- | --------------------------------------------- |
96+
| GET | /api/stream | torrentId [required], fileIndex [default = 0] |
97+
| GET | /api/stream/:infoHash/:fileIndex |
9198

9299
torrentId can be:
93100

@@ -99,28 +106,21 @@ torrentId can be:
99106
100107
### Download torrent as zip archive
101108

102-
Method | path | params
103-
-------|-----|-------
104-
GET | /api/torrent/download | torrentId [required]
105-
GET | /api/torrent/download/:infoHash
109+
| Method | path | params |
110+
| ------ | ----------------------- | -------------------- |
111+
| GET | /api/download | torrentId [required] |
112+
| GET | /api/download/:infoHash |
106113

107114
### Download torrent as playlist [.m3u]
108115

109-
Method | path | params
110-
-------|-----|-------
111-
GET | /api/torrent/playlist | torrentId [required]
112-
GET | /api/torrent/playlist/:infoHash
116+
| Method | path | params |
117+
| ------ | ----------------------- | -------------------- |
118+
| GET | /api/playlist | torrentId [required] |
119+
| GET | /api/playlist/:infoHash |
113120

114121
### Download torrent file [.torrent]
115122

116-
Method | path | params
117-
-------|-----|-------
118-
GET | /api/torrent/torrentfile | torrentId [required]
119-
GET | /api/torrent/torrentfile/:infoHash
120-
121-
## Front-End Routes
122-
123-
Route | Params
124-
------|-------
125-
/explorer | torrentId[required]
126-
/player | torrentId[required], fileIndex[required]
123+
| Method | path | params |
124+
| ------ | -------------------------- | -------------------- |
125+
| GET | /api/torrentfile | torrentId [required] |
126+
| GET | /api/torrentfile/:infoHash |

routes/torrent.js api/index.js

+29-29
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const router = require('express').Router();
2-
const playlistBuilder = require('../helpers/playlist-builder');
3-
const torrents = require('../helpers/torrents');
4-
const yazl = require('yazl');
5-
const pump = require('pump');
1+
const router = require("express").Router();
2+
const playlistBuilder = require("./playlist-builder");
3+
const torrents = require("./torrents");
4+
const yazl = require("yazl");
5+
const pump = require("pump");
66

77
const reqParser = (req, res, next) => {
88
req.torrentId = req.params.infoHash || req.query.torrentId;
@@ -23,9 +23,8 @@ const stream = (req, res) => {
2323
});
2424
};
2525

26-
router.get('/stream', reqParser, stream);
27-
router.get('/stream/:infoHash/:fileIndex', reqParser, stream);
28-
26+
router.get("/stream", reqParser, stream);
27+
router.get("/stream/:infoHash/:fileIndex", reqParser, stream);
2928

3029
// download zip file
3130
const downloadReqParser = (req, res, next) => {
@@ -42,46 +41,48 @@ const downloadReqParser = (req, res, next) => {
4241
const downloadZIP = (req, res) => {
4342
const { torrent } = req;
4443
const zipFile = new yazl.ZipFile();
45-
46-
res.attachment(torrent.name + '.zip');
47-
res.setHeader('Content-Length', torrent.length);
44+
45+
res.attachment(torrent.name + ".zip");
46+
res.setHeader("Content-Length", torrent.length);
4847
req.connection.setTimeout(3600000);
49-
48+
5049
pump(zipFile.outputStream, res);
51-
52-
torrent.files.forEach(f => zipFile.addReadStream(f.createReadStream(), f.path));
50+
51+
torrent.files.forEach(f =>
52+
zipFile.addReadStream(f.createReadStream(), f.path)
53+
);
5354
zipFile.end();
5455
};
5556

56-
router.get('/download', reqParser, downloadReqParser, downloadZIP);
57-
router.get('/download/:infoHash', reqParser, downloadReqParser, downloadZIP);
57+
router.get("/download", reqParser, downloadReqParser, downloadZIP);
58+
router.get("/download/:infoHash", reqParser, downloadReqParser, downloadZIP);
5859

5960
// add torrent file to the webtorrent client
6061
const torrentInfo = (req, res) => {
6162
const { torrentId } = req;
6263
torrents.add(torrentId, (err, torrent) => {
6364
if (err) res.sendStatus(500);
64-
else res.send(torrent.jsonify());
65+
else res.send(torrent.toJson());
6566
});
6667
};
6768

68-
router.get('/info', reqParser, torrentInfo);
69-
router.get('/info/:infoHash', reqParser, torrentInfo);
69+
router.get("/info", reqParser, torrentInfo);
70+
router.get("/info/:infoHash", reqParser, torrentInfo);
7071

7172
// build playlist
7273
const playlist = (req, res, next) => {
7374
const { torrentId } = req;
7475
torrents.add(torrentId, (err, torrent) => {
7576
if (err) res.sendStatus(500);
7677
else {
77-
req.torrent = torrent.jsonify();
78+
req.torrent = torrent.toJson();
7879
next();
7980
}
8081
});
8182
};
8283

83-
router.get('/playlist', reqParser, playlist, playlistBuilder);
84-
router.get('/playlist/:infoHash', reqParser, playlist, playlistBuilder);
84+
router.get("/playlist", reqParser, playlist, playlistBuilder);
85+
router.get("/playlist/:infoHash", reqParser, playlist, playlistBuilder);
8586

8687
// download torrent file
8788
const torrentFile = (req, res) => {
@@ -90,19 +91,18 @@ const torrentFile = (req, res) => {
9091
if (err) res.sendStatus(500);
9192
else {
9293
const tor = torrent.torrentFile;
93-
94-
res.attachment(torrent.name + '.torrent');
95-
res.setHeader('Content-Length', tor.length);
96-
res.setHeader('Content-Type', 'application/x-bittorrent');
94+
95+
res.attachment(torrent.name + ".torrent");
96+
res.setHeader("Content-Length", tor.length);
97+
res.setHeader("Content-Type", "application/x-bittorrent");
9798
req.connection.setTimeout(30000);
9899

99-
100100
res.send(tor);
101101
}
102102
});
103103
};
104104

105-
router.get('/torrentfile', reqParser, torrentFile);
106-
router.get('/torrentfile/:infoHash', reqParser, torrentFile);
105+
router.get("/torrentfile", reqParser, torrentFile);
106+
router.get("/torrentfile/:infoHash", reqParser, torrentFile);
107107

108108
module.exports = router;

api/playlist-builder.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module.exports = function(req, res) {
2+
const host = (req.secure ? "https://" : "http://") + req.headers.host;
3+
const { torrent } = req;
4+
5+
let m3u =
6+
"#EXTM3U\n" +
7+
torrent.files
8+
.filter(f => f.type.startsWith("video") || f.type.startsWith("audio"))
9+
.map(
10+
f =>
11+
`#EXTINF:0,${f.name}\n${host}/api/stream/${torrent.infoHash}/${
12+
f.index
13+
}`
14+
)
15+
.join("\n");
16+
17+
res.attachment(torrent.name + ".m3u");
18+
res.setHeader("Content-Length", m3u.length);
19+
res.setHeader("Content-Type", "application/mpegurl");
20+
req.connection.setTimeout(30000);
21+
22+
res.send(m3u);
23+
};

0 commit comments

Comments
 (0)