terabox-upload-tool 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,11 +8,16 @@ A simple and efficient Node.js library for uploading files and fetching file lis
8
8
  * Custom Directory Support: Specify the directory where the file should be uploaded.
9
9
  * Progress Tracking: Monitor the upload progress in real-time.
10
10
  * Fetch File List: Fetch file list from any directory.
11
+ * Get direct file download link
11
12
 
12
13
  ## Coming Soon (Open for Collaboration)
13
14
 
14
- * Fetch Files: Retrieve the resources download URL and add URL Path in JSON object returned by exiting [fetchFileList() ](./lib/index.js)method .
15
+ * Fetch Files: Retrieve the resources download URL and add URL Path in JSON object returned by existing [fetchFileList() ](./lib/index.js)method .
15
16
  * Delete Files: Remove files from your Terabox storage directly using this library.
17
+ * Video Streaming: Support for streaming videos.
18
+ * Fetch Upload History
19
+ * Fetch Download History
20
+ * Restructure code and files
16
21
 
17
22
  ## Installation
18
23
 
@@ -79,21 +84,17 @@ Future Enhancements (Open Collaboration)
79
84
 
80
85
  We are actively seeking contributors to add the following features:
81
86
 
82
- 1. Fetch Files:
83
-
84
- * Provide options to filter by file types, size, or date modified.
85
- * Need to Implement add resouces URL in file list objects.
86
- 2. Delete Files:
87
+ 1. Delete Files:
87
88
 
88
89
  * Enable users to delete specific files or directories from their Terabox storage.
89
90
  * Include safeguards like confirmation prompts before deletion.
90
- 3. Error Handling Enhancements:
91
+ 2. Error Handling Enhancements:
91
92
 
92
93
  * Improve error messages for easier debugging and user guidance.
93
- 4. Automated Tests:
94
+ 3. Automated Tests:
94
95
 
95
96
  * Add test cases to ensure reliability and robustness.
96
- 5. Documentation Updates:
97
+ 4. Documentation Updates:
97
98
 
98
99
  * Expand guides with screenshots and example workflows.
99
100
 
@@ -160,3 +161,5 @@ Get the 'ndus' from cookies in the header section
160
161
  ## Licence
161
162
 
162
163
  This project is licensed under the [MIT License.](./LICENSE)
164
+
165
+ [Github](https://github.com/Pahadi10/terabox-upload-tool)
@@ -0,0 +1,41 @@
1
+ const { generateSign, fetchHomeInfo, generateDownload } = require('./downloadHelper');
2
+
3
+ /**
4
+ * Generates a direct download link for a file from Terabox.
5
+ * @param {string} ndus - User authentication token.
6
+ * @param {string} fid - File ID of the file to be downloaded.
7
+ * @returns {Promise<{success: boolean, message: string, downloadLink?: string}>} - A promise that resolves to an object containing success status, message, and the direct download link if successful.
8
+ */
9
+ async function getDownloadLink(ndus, fid) {
10
+ try {
11
+ // Fetch home information to retrieve necessary parameters for signing
12
+ const homeInfo = await fetchHomeInfo(ndus);
13
+
14
+ if (!homeInfo || !homeInfo.data.sign3 || !homeInfo.data.sign1 || !homeInfo.data.timestamp) {
15
+ return { success: false, message: "Invalid home information received." };
16
+ }
17
+
18
+ const sign1 = homeInfo.data.sign3;
19
+ const sign2 = homeInfo.data.sign1;
20
+ const timestamp = homeInfo.data.timestamp;
21
+
22
+ // Generate the required sign using sign1 and sign2
23
+ const sign = generateSign(sign1, sign2);
24
+ if (!sign) {
25
+ return { success: false, message: "Failed to generate sign." };
26
+ }
27
+
28
+ // Fetch the download link
29
+ const responseDownload = await generateDownload(sign, fid, timestamp, ndus);
30
+ if (!responseDownload || !responseDownload.downloadLink[0]?.dlink) {
31
+ return { success: false, message: "Failed to retrieve download link." };
32
+ }
33
+
34
+ return { success: true, message: "Download link retrieved successfully.", downloadLink: responseDownload.downloadLink[0].dlink};
35
+ } catch (error) {
36
+ console.error("Error getting download link:", error);
37
+ return { success: false, message: error.message || "Unknown error occurred while fetching download link." };
38
+ }
39
+ }
40
+
41
+ module.exports = getDownloadLink;
@@ -0,0 +1,131 @@
1
+ const axios = require('axios');
2
+
3
+ /**
4
+ * Generates a cryptographic sign using RC4-like encryption.
5
+ * @param {string} s1 - First input string (key).
6
+ * @param {string} s2 - Second input string (data).
7
+ * @returns {string} - Base64 encoded signed string.
8
+ */
9
+ function generateSign(s1, s2) {
10
+ try {
11
+ if (!s1 || !s2) {
12
+ return { success: false, message: "Provide both the signs" };
13
+ }
14
+
15
+ const p = new Uint8Array(256);
16
+ const a = new Uint8Array(256);
17
+ const result = [];
18
+
19
+ for (let i = 0; i < 256; i++) {
20
+ a[i] = s1.charCodeAt(i % s1.length);
21
+ p[i] = i;
22
+ }
23
+
24
+ let j = 0;
25
+ for (let i = 0; i < 256; i++) {
26
+ j = (j + p[i] + a[i]) % 256;
27
+ [p[i], p[j]] = [p[j], p[i]];
28
+ }
29
+
30
+ let i = 0;
31
+ j = 0;
32
+ for (let q = 0; q < s2.length; q++) {
33
+ i = (i + 1) % 256;
34
+ j = (j + p[i]) % 256;
35
+ [p[i], p[j]] = [p[j], p[i]];
36
+ const k = p[(p[i] + p[j]) % 256];
37
+ result.push(s2.charCodeAt(q) ^ k);
38
+ }
39
+
40
+ return Buffer.from(result).toString('base64');
41
+ } catch (error) {
42
+ console.error("Error generating sign:", error.message);
43
+ return null;
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Fetches home information from Terabox API.
49
+ * @param {string} ndus - User authentication token.
50
+ * @returns {Promise<{success: boolean, message: string, data?: object}>} - The response data from the API.
51
+ */
52
+ async function fetchHomeInfo(ndus) {
53
+ const url = "https://www.1024terabox.com/api/home/info";
54
+
55
+ try {
56
+ if (!ndus) {
57
+ return { success: false, message: "User authentication token (ndus) is required" };
58
+ }
59
+
60
+ const response = await axios.get(url, {
61
+ params: {
62
+ app_id: "250528",
63
+ web: "1",
64
+ channel: "dubox",
65
+ clienttype: "0",
66
+ },
67
+ headers: {
68
+ "User-Agent": "Mozilla/5.0",
69
+ "Accept": "application/json",
70
+ "Cookie": `ndus=${ndus}`,
71
+ },
72
+ });
73
+ return { success: true, message: "Home info retrieved successfully.", data: response.data.data };
74
+ } catch (error) {
75
+ console.error("Error fetching home info:", error.response?.data || error.message);
76
+ return { success: false, message: error.message || "Failed to fetch home info." };
77
+ }
78
+ }
79
+
80
+ /**
81
+ * Generates a download link for a file from Terabox.
82
+ * @param {string} sign - Encrypted sign generated using `generateSign`.
83
+ * @param {string} fid - File ID of the file to be downloaded.
84
+ * @param {number} timestamp - Timestamp of the request.
85
+ * @param {string} ndus - User authentication token.
86
+ * @returns {Promise<{success: boolean, message: string, downloadLink?: string}>}
87
+ */
88
+ async function generateDownload(sign, fid, timestamp, ndus) {
89
+ const url = "https://www.1024terabox.com/api/download";
90
+
91
+ try {
92
+ if (!sign || !fid || !timestamp || !ndus) {
93
+ return { success: false, message: "Missing required parameters for generating download link." };
94
+ }
95
+
96
+ const response = await axios.get(url, {
97
+ params: {
98
+ app_id: "250528",
99
+ web: "1",
100
+ channel: "dubox",
101
+ clienttype: "0",
102
+ fidlist: `[${fid}]`,
103
+ type: "dlink",
104
+ vip: "2",
105
+ sign,
106
+ timestamp,
107
+ need_speed: "0",
108
+ },
109
+ headers: {
110
+ "User-Agent": "Mozilla/5.0",
111
+ "Accept": "application/json",
112
+ "Cookie": `ndus=${ndus}`,
113
+ },
114
+ });
115
+
116
+ if (!response.data.dlink) {
117
+ return { success: false, message: "No download link received." };
118
+ }
119
+
120
+ return { success: true, message: "Download link generated successfully.", downloadLink: response.data.dlink };
121
+ } catch (error) {
122
+ console.error("Error generating download link:", error.response?.data || error.message);
123
+ return { success: false, message: error.message || "Failed to generate download link." };
124
+ }
125
+ }
126
+
127
+ module.exports = {
128
+ generateSign,
129
+ fetchHomeInfo,
130
+ generateDownload,
131
+ };
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Fetches file details from Terabox using a short URL.
3
+ * @param {string} shortUrl - The short URL of the file.
4
+ * @returns {Promise<object|null>} - A promise that resolves to an object containing file details, or null if retrieval fails.
5
+ */
6
+ async function getFileDetails(shortUrl) {
7
+ // API Endpoint
8
+ const apiUrl = `https://www.terabox.com/api/shorturlinfo?app_id=250528&shorturl=${shortUrl}&root=1`;
9
+
10
+ // Request Headers
11
+ const headers = {
12
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
13
+ "Referer": "https://www.terabox.com/",
14
+ "Accept": "application/json, text/javascript, */*; q=0.01",
15
+ "X-Requested-With": "XMLHttpRequest"
16
+ };
17
+
18
+ try {
19
+ // Send GET request to fetch file details
20
+ const response = await fetch(apiUrl, { method: "GET", headers: headers });
21
+ const data = await response.json();
22
+
23
+ // Validate response
24
+ if (!data || !data.sign) {
25
+ console.error("Failed to retrieve file details. Response:", data);
26
+ return null;
27
+ }
28
+
29
+ // Extract relevant file details
30
+ const fileDetails = {
31
+ status: "success",
32
+ sign: data.sign,
33
+ timestamp: data.timestamp,
34
+ shareid: data.shareid,
35
+ uk: data.uk,
36
+ list: data.list || []
37
+ };
38
+
39
+ return fileDetails;
40
+
41
+ } catch (error) {
42
+ console.error("Error fetching file details:", error);
43
+ return null;
44
+ }
45
+ }
46
+
47
+ module.exports = getFileDetails;
@@ -0,0 +1,52 @@
1
+ const axios = require('axios');
2
+
3
+ /**
4
+ * Generates a short URL for a file in Terabox.
5
+ * @param {string} ndus - User authentication token.
6
+ * @param {string} path - The file path in Terabox.
7
+ * @param {string} fid - The file ID.
8
+ * @returns {Promise<object|null>} - A promise that resolves to the API response containing the short URL or null if an error occurs.
9
+ */
10
+ const getShortUrl = async (ndus, path, fid) => {
11
+ try {
12
+ // API Endpoint
13
+ const url = 'https://www.1024terabox.com/share/pset';
14
+ const cookies = `ndus=${ndus}`;
15
+
16
+ // Form Data Parameters
17
+ const formData = new URLSearchParams({
18
+ app_id: '250528',
19
+ web: '1',
20
+ channel: 'dubox',
21
+ clienttype: '0',
22
+ app: 'universe',
23
+ schannel: '0',
24
+ channel_list: '[0]',
25
+ period: '0',
26
+ path_list: `["${path}"]`,
27
+ fid_list: `[${fid}]`,
28
+ pwd: '',
29
+ public: '1',
30
+ scene: ''
31
+ });
32
+
33
+ // Request Headers
34
+ const headers = {
35
+ 'Content-Type': 'application/x-www-form-urlencoded',
36
+ 'Cookie': cookies,
37
+ 'Referer': 'https://www.1024terabox.com/',
38
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
39
+ };
40
+
41
+ // Send POST request to generate a short URL
42
+ const response = await axios.post(url, formData.toString(), { headers });
43
+
44
+ // Return the API response containing the short URL
45
+ return response.data;
46
+ } catch (error) {
47
+ console.error('Error generating short link:', error);
48
+ return null;
49
+ }
50
+ };
51
+
52
+ module.exports = getShortUrl;
@@ -27,4 +27,15 @@ function buildListUrl(appId, directory) {
27
27
  return `https://www.1024terabox.com/api/list?app_id=${appId}&web=1&channel=dubox&clienttype=0&order=time&desc=1&dir=${encodeURIComponent(directory)}&num=100&page=1&showempty=0`;
28
28
  }
29
29
 
30
- module.exports = { buildUploadUrl, buildCreateUrl, buildListUrl };
30
+
31
+ /**
32
+ * Builds the downloadable link for videos.
33
+ * @param {string} appId - The application ID
34
+ * @param {string} videoPath - The directory path to fetch the file list from
35
+ * @returns {string} - The file list URL
36
+ */
37
+ function buildVideoDownloadUrl(appId, videoPath) {
38
+ return `https://www.1024terabox.com/api/streaming?path=${encodeURIComponent(videoPath)}&app_id=${appId}&clienttype=0&type=M3U8_FLV_264_480&vip=1`;
39
+ }
40
+
41
+ module.exports = { buildUploadUrl, buildCreateUrl, buildListUrl, buildVideoDownloadUrl };
package/lib/index.js CHANGED
@@ -2,7 +2,8 @@ const axios = require('axios');
2
2
  const path = require('path');
3
3
  const fs = require('fs');
4
4
  const FormData = require('form-data');
5
- const { buildUploadUrl, buildCreateUrl, buildListUrl } = require('./utils');
5
+ const { buildUploadUrl, buildCreateUrl, buildListUrl} = require('./helpers/utils');
6
+ const getDownloadLink = require('./helpers/download/download')
6
7
 
7
8
  /**
8
9
  * Class to handle Terabox file operations
@@ -94,7 +95,7 @@ class TeraboxUploader {
94
95
  /**
95
96
  * Fetches the file list from Terabox
96
97
  * @param {string} directory - Directory to fetch the file list from (e.g., "/")
97
- * @returns {Promise<object>} - JSON response from the API
98
+ * @returns {Promise<object>} - JSON response with file details along with download link
98
99
  */
99
100
  async fetchFileList(directory = '/') {
100
101
  try {
@@ -105,11 +106,19 @@ class TeraboxUploader {
105
106
  Cookie: this.credentials.cookies,
106
107
  },
107
108
  });
108
-
109
- return response.data;
109
+ const finalDataArray = response.data.list;
110
+ for (const file of finalDataArray) {
111
+ const fid = file.fs_id;
112
+ file.downloadLink = await getDownloadLink(this.credentials.ndus, fid);
113
+ }
114
+
115
+ return { success: true, message: 'File list retrieved successfully.', data: finalDataArray };
110
116
  } catch (error) {
111
- console.error('Error fetching file list:', error.response?.data || error.message);
112
- throw error;
117
+ console.error('Error fetching file list:', error);
118
+ return {
119
+ success: false,
120
+ message: error.response?.data?.error || error.message || 'Failed to fetch file list.',
121
+ };
113
122
  }
114
123
  }
115
124
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "terabox-upload-tool",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "git add",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {