Skip to content

Commit

Permalink
Incorporate fast resume into project
Browse files Browse the repository at this point in the history
  • Loading branch information
mmgoodnow committed Jun 10, 2020
1 parent 144fed0 commit cc88a05
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/cross-seed.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bin/cmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"use strict";
55 changes: 55 additions & 0 deletions fastResume.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"use strict";

const fs = require("fs");
const b = require("bencode");
const path = require("path");

const add_fast_resume = (meta, basepath) => {
let files = [];
let fpath = path.join(basepath, meta.info.name.toString("utf8"));
if ("files" in meta.info) {
for (let file of meta.info.files) {
let ffpath = fpath;
for (let dir of file.path) {
ffpath = path.join(ffpath, dir.toString("utf8"));
}
let obj = { path: ffpath, size: file.length };
files.push(obj);
}
} else {
let obj = {};
obj.path = fpath;
obj.size = meta.info.length;
files.push(obj);
}

let resume = {};
resume.bitfield = Math.floor(meta.info.pieces.length / 20);
resume.files = [];
let piece_length = meta.info["piece length"];
let offset = 0;

for (let file of files) {
try {
let states = fs.statSync(file.path);
if (states.size != file.size) {
throw new Error("Files not matching!");
}
let obj = {};
obj.priority = 1;
obj.mtime = Math.floor(states.mtimeMs / 1000);
obj.completed =
Math.floor(
(offset + file.size + piece_length - 1) / piece_length
) - Math.floor(offset / piece_length);
resume.files.push(obj);
offset += file.size;
} catch (err) {
throw new Error("Files not matching!");
}
}
meta.libtorrent_resume = resume;
return meta;
};

module.exports = add_fast_resume;
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node

"use strict";
const parseTorrent = require("parse-torrent");
const fs = require("fs");
const path = require("path");
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"license": "ISC",
"dependencies": {
"axios": "^0.19.2",
"bencode": "^2.0.1",
"chalk": "^4.1.0",
"minimist": "^1.2.5",
"parse-torrent": "^7.1.3"
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ axios@^0.19.2:
dependencies:
follow-redirects "1.5.10"

bencode@^2.0.0:
bencode@^2.0.0, bencode@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/bencode/-/bencode-2.0.1.tgz#667a6a31c5e038d558608333da6b7c94e836c85b"
integrity sha512-2uhEl8FdjSBUyb69qDTgOEeeqDTa+n3yMQzLW0cOzNf1Ow5bwcg3idf+qsWisIKRH8Bk8oC7UXL8irRcPA8ZEQ==
Expand Down

0 comments on commit cc88a05

Please sign in to comment.