This repository has been archived on 2019-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
ttmsh-cli/index.js

61 lines
1.6 KiB
JavaScript

const rp = require("request-promise");
const fs = require("fs");
const config = require("./config.json")
module.exports.config = config;
/**
* 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 -1 if the file is too large.
*/
module.exports.retention = function(file_size) {
if (file_size > config["max_size"]) {
return -1;
}
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
*
* @param {string} action An action to perform (e.g file, shorten)
* @param {string} data The data to upload (e.g path to file, url to shorten)
* @returns A promise with the shortened link in a string
*/
module.exports.do = function(action, data) {
if (!action) {
throw new Error("Invalid action!");
}
if (!data) {
throw new Error("Invalid data!");
}
const formData = {}
switch (action) {
case "upload":
// in the form data, set file to be a file stream
// data is our file path if the action is 'file'
formData["file"] = fs.createReadStream(data);
break;
case "shorten":
// in the form data, set shorten to be the url to shorten
// data is our url if the action is 'shorten'
formData["shorten"] = data;
break;
default:
throw new Error("Invalid action!");
}
return rp({
method: "POST",
url: config.url,
formData: formData
}).then(function(body) {
return body.trim();
});
}