From 76cbb2a77bd2c4aba2694939c91d9eaffcd07539 Mon Sep 17 00:00:00 2001 From: Jake Walker Date: Tue, 9 Apr 2019 19:09:28 +0100 Subject: [PATCH] Add estimated file life --- cli.js | 11 +++++++++-- config.json | 6 ++++++ index.js | 17 ++++++++++++++++- package.json | 1 + yarn.lock | 12 ++++++++++++ 5 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 config.json diff --git a/cli.js b/cli.js index ada5248..8ca73c9 100755 --- a/cli.js +++ b/cli.js @@ -8,6 +8,7 @@ const validUrl = require("valid-url"); const ora = require("ora"); const Funnies = require("funnies").Funnies; const ttmsh = require("./index.js"); +const fileBytes = require("file-bytes"); let funnies = new Funnies(); @@ -50,8 +51,14 @@ async function upload(path) { path = response.path; } - // run the action, i.e upload the file - doAction("file", path); + fileBytes(path).then(size => { + var sizeMib = size / 1048576; + var ret = ttmsh.retention(sizeMib); + console.log(`This file is expected to last approximately ${ret} days.`); + + // run the action, i.e upload the file + doAction("file", path); + }); } // this function is ran when the shorten command is used diff --git a/config.json b/config.json new file mode 100644 index 0000000..3fc2e59 --- /dev/null +++ b/config.json @@ -0,0 +1,6 @@ +{ + "url": "https://ttm.sh", + "min_age": 30, + "max_age": 365, + "max_size": 256 +} \ No newline at end of file diff --git a/index.js b/index.js index 30ca9b6..77c754c 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,20 @@ const rp = require("request-promise"); const fs = require("fs"); +const config = require("./config.json") + +/** + * A function to calculate how long a file will last + * + * @param {integer} file_size How large the file is (MB) + * @returns An integer of how many days the file will last, or false if the file is too large. + */ +module.exports.retention = function(file_size) { + if (file_size > config["max_size"]) { + return false; + } + + return Math.floor(config["min_age"] + (-config["max_age"] + config["min_age"]) * Math.pow((file_size / config["max_size"] - 1), 3)); +} /** * A wrapper for communication with ttm.sh @@ -36,7 +51,7 @@ module.exports.do = function(action, data) { return rp({ method: "POST", - url: "https://ttm.sh", + url: config.url, formData: formData }).then(function(body) { return body.trim(); diff --git a/package.json b/package.json index 503711e..5b1b477 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "dependencies": { "commander": "^2.19.0", "enquirer": "^2.3.0", + "file-bytes": "^2.0.3", "funnies": "^2.0.0", "ora": "^3.2.0", "request": "^2.88.0", diff --git a/yarn.lock b/yarn.lock index 7119fb7..3cedb79 100644 --- a/yarn.lock +++ b/yarn.lock @@ -211,6 +211,13 @@ fast-json-stable-stringify@^2.0.0: resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#d5142c0caee6b1189f87d3a76111064f86c8bbf2" integrity sha1-1RQsDK7msRifh9OnYREGT4bIu/I= +file-bytes@^2.0.3: + version "2.0.3" + resolved "https://registry.yarnpkg.com/file-bytes/-/file-bytes-2.0.3.tgz#bffdf2bf04331fe9f6d3c7d44192e983759ea027" + integrity sha1-v/3yvwQzH+n208fUQZLpg3WeoCc= + dependencies: + pify "^2.3.0" + forever-agent@~0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91" @@ -382,6 +389,11 @@ performance-now@^2.1.0: resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b" integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns= +pify@^2.3.0: + version "2.3.0" + resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c" + integrity sha1-7RQaasBDqEnqWISY59yosVMw6Qw= + prop-types@^15.5.6: version "15.7.2" resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.7.2.tgz#52c41e75b8c87e72b9d9360e0206b99dcbffa6c5"