Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
CA1R7 authored Jun 16, 2021
1 parent c889e4b commit 2ce6c79
Show file tree
Hide file tree
Showing 7 changed files with 1,804 additions and 203 deletions.
402 changes: 201 additions & 201 deletions LICENSE

Large diffs are not rendered by default.

115 changes: 113 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,113 @@
# node-annonfiles
Annonfiles unofficial API for uploading and downloading files
# node-annonfiles

Annonfiles unofficial API for uploading and downloading files.

# Installation

Install the package globally for use it anywhere easier

```
npm install -g node-annonfiles
```

# Usage

```js
const { upload, getInfo, download } = require("node-annonfiles");

(async () => {
try {
// If you have not keyAcess let it empty
const uploadFile = await upload("./package.json", { token: "PUT_KEYACCESS" });
console.log(uploadFile); // you will get same result on the top
// functions download,getInfo = (id: string) => result json
} catch (e) {
console.log(e);
}
})();
```

## CLI

### Upload files

```
node-annonfiles --upload file.txt
```

with keyaccess original of Annonfiles website

```
node-annonfiles --upload file.txt@{keyaccess}
```

Output

```json
Wait for uploading file.txt
{
"status": true,
"data": {
"file": {
"url": {
"full": "https://anonfiles.com/D9KbB411u2/file_txt",
"short": "https://anonfiles.com/D9KbB411u2"
},
"metadata": {
"id": "D9KbB411u2",
"name": "file.txt",
"size": {
"bytes": 5,
"readable": "5 B"
}
}
}
}
}
```

- Make sure store the ID of any output for use it to get the direct download link.

### Download files

You should use ID of the file you already uploaded it, I used `D9KbB411u2` just for an example.

```
node-annonfiles --download D9KbB411u2
```

Output

```
Fetching direct link...
https://cdn-140.anonfiles.com/D9KbB411u2/d0db8fb0-1623874914/file.txt
```

### Get Info Files

Use ID for get the info of files.

```
node-annonfiles --get D9KbB411u2
```

Output

```json
Fetching File Metadata...
{
"url": {
"short": "https://anonfiles.com/D9KbB411u2",
"full": "https://anonfiles.com/D9KbB411u2/file_txt"
},
"metadata": {
"size": { "bytes": 5, "readable": "5 B" },
"name": "file_txt",
"id": "D9KbB411u2"
}
}
```

# License

Copyright (c) 2021 CA1R71 Licensed under the Apcahe 2.0 license.
33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "node-annonfiles",
"version": "1.0.1",
"description": "Annonfiles api (unofficial) for uploading files",
"main": "src/index.js",
"scripts": {
"lint": "eslint src --ext .js,.json",
"lint:fix": "yarn lint --fix"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com/"
},
"repository": "git://github.com/CA1R7/node-annonfiles.git",
"keywords": [
"annonfiles",
"files",
"upload",
"download"
],
"author": "cair71 & redoan",
"license": "Apache-2.0",
"bin": "node-annonfiles",
"dependencies": {
"babel-eslint": "^10.1.0",
"cheerio": "^1.0.0-rc.9",
"eslint": "^7.27.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"htmlparser2": "^6.1.0",
"prettier": "^2.3.0",
"request": "^2.88.2"
}
}
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node
"use strict";
const { Init } = require("./middleware/initialize.js");
module.exports = new Init();
41 changes: 41 additions & 0 deletions src/middleware/cli/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Annonfiles CLI
*/

class CLI {
constructor() {
this.argvs = process.argv;
this.options = {};
this.initializeOptions();
}
initializeOptions() {
var i = 0;
for (let argv of this.argvs) {
if (argv.match(/^[--]{1,2}/)) {
this.startOption(argv, i);
}
i++;
}
}
startOption(argv, argvPlace) {
const optionAnswer = this.argvs[argvPlace + 1];
if (optionAnswer && !optionAnswer.match(/^[--]{1,2}/)) {
let argvName = argv.replace(/^(--|-)/, "");
this.options[argvName] = optionAnswer;
}
switch (argv) {
case "--version":
case "-v":
const { version } = require("../../../package.json");
console.log(`Version: ${version}`);
break;
}
}
}

module.exports = () => {
return new Promise((resolve) => {
const cli_p = new CLI();
resolve(cli_p.options);
});
};
92 changes: 92 additions & 0 deletions src/middleware/initialize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const path = require("path");
const cli = require("./cli");
const cheerio = require("cheerio");
const { post, get } = require("request");
const { parseDocument } = require("htmlparser2");
const { createReadStream, existsSync } = require("fs");

class Init {
constructor() {
this.endpoint = "https://api.anonfiles.com";
this.endpoint2 = "https://anonfiles.com";
this.initializeCommands();
}
async initializeCommands() {
const cli_options = await cli();
const CLICommandsFunctions = ["upload", "download", "get"];
CLICommandsFunctions.forEach(async (CLICommand) => {
if (Object.hasOwnProperty.call(cli_options, CLICommand)) {
switch (CLICommand) {
case "upload":
let upload_answer = cli_options.upload;
let [fileName, token] = upload_answer.match(/@/) ? upload_answer.split(/@/) : [upload_answer, undefined];
console.log(await this.upload(fileName, { token }));
break;
case "download":
case "get":
let id = cli_options[CLICommand];
if (!id) throw new Error("Please, provide A File ID");
console.log(CLICommand === "download" ? await this.download(id) : await this.getInfo(id));
break;
}
}
});
}
/**
* @author redoan
*/
download(fileId) {
return new Promise((resolve, reject) => {
console.log("Fetching direct link...");
get(`${this.endpoint2}/${fileId}`, (err, _res, body) => {
if (err) reject(String(err));
const DOM = parseDocument(body);
const $ = cheerio.load(DOM);
const tempLink = $("a[id=download-url]").attr("href");
resolve(tempLink);
});
});
}
/**
* @author redoan
*/
getInfo(fileId) {
return new Promise((resolve, reject) => {
console.log("Fetching File Metadata...");
get(`${this.endpoint}/v2/file/${fileId}/info`, (err, _res, body) => {
if (err) reject(`An Error Ocuured: ${err.message}`);
try {
const { file } = JSON.parse(body).data;
if (file) {
resolve(file);
} else throw new Error("Invalid data");
} catch (e) {
reject(e); // catching error of parsing JSON
}
});
});
}
upload(fileName, { token }) {
return new Promise((resolve, reject) => {
if (!fileName) reject("No filename on upload function");
let file = path.resolve(process.cwd(), fileName);
if (existsSync(file)) {
console.log(`Wait for uploading ${fileName} ${token ? `| Token: ${token}` : ""}`);
post(
{
url: `${this.endpoint}/upload${token ? `?token=${token}` : ""}`,
formData: {
file: createReadStream(file),
},
},
function (error, _response, body) {
if (error) reject(String(error));
resolve(body);
},
);
} else reject(`invalid file '${file}'`);
});
}
}

module.exports = { Init };
Loading

0 comments on commit 2ce6c79

Please sign in to comment.