Add estimated file life
This commit is contained in:
parent
dfc2320c1b
commit
76cbb2a77b
11
cli.js
11
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
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"url": "https://ttm.sh",
|
||||
"min_age": 30,
|
||||
"max_age": 365,
|
||||
"max_size": 256
|
||||
}
|
17
index.js
17
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();
|
||||
|
|
|
@ -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",
|
||||
|
|
12
yarn.lock
12
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"
|
||||
|
|
Reference in New Issue