terabox-upload-tool 1.2.0 → 1.3.1
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 +114 -42
- package/examples/createfolder.js +27 -0
- package/examples/dl-test.js +70 -91
- package/examples/example.js +2 -0
- package/lib/helpers/fileDelete.js +45 -0
- package/lib/helpers/fileMove.js +46 -0
- package/lib/index.js +136 -26
- package/package.json +27 -22
package/README.md
CHANGED
|
@@ -1,23 +1,32 @@
|
|
|
1
1
|
# Terabox Upload Tool
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**🚀Boost Your Node.js Apps with TeraBox Upload Tool**
|
|
4
|
+
|
|
5
|
+
Supercharge your Node.js applications with the TeraBox Upload Tool — a powerful library for seamless integration with [TeraBox](https://www.terabox.com/wap), the leading cloud storage platform with 1TB of free space.
|
|
6
|
+
|
|
7
|
+
✅ Effortlessly:
|
|
8
|
+
|
|
9
|
+
- Upload, download, retrieve, and delete files
|
|
10
|
+
- Manage directories with ease
|
|
11
|
+
- Fetch file lists for better organization
|
|
12
|
+
|
|
13
|
+
Ideal for developers and automation enthusiasts looking for an efficient cloud storage solution in Node.js.
|
|
4
14
|
|
|
5
15
|
## Features
|
|
6
16
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
17
|
+
- File Upload: Easily upload files to Terabox storage.
|
|
18
|
+
- Custom Directory Support: Specify the directory where the file should be uploaded.
|
|
19
|
+
- Progress Tracking: Monitor the upload progress in real-time.
|
|
20
|
+
- Retrieve your Files: Retrieve your files from any directory.
|
|
21
|
+
- Download your Files: Get direct file download link.
|
|
22
|
+
- Delete or move your files from any directory.
|
|
12
23
|
|
|
13
24
|
## Coming Soon (Open for Collaboration)
|
|
14
25
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
* Fetch Download History
|
|
20
|
-
* Restructure code and files
|
|
26
|
+
- Video Streaming: Support for streaming videos.
|
|
27
|
+
- Fetch Upload History
|
|
28
|
+
- Fetch Download History
|
|
29
|
+
- Restructure code and files
|
|
21
30
|
|
|
22
31
|
## Installation
|
|
23
32
|
|
|
@@ -32,71 +41,134 @@ npm install terabox-upload-tool
|
|
|
32
41
|
### Setting up credentials
|
|
33
42
|
|
|
34
43
|
```javascript
|
|
35
|
-
const TeraboxUploader = require(
|
|
44
|
+
const TeraboxUploader = require("terabox-upload-tool");
|
|
36
45
|
|
|
37
46
|
const credentials = {
|
|
38
|
-
ndus:
|
|
39
|
-
appId:
|
|
40
|
-
uploadId:
|
|
47
|
+
ndus: "valid_ndus", //Required: Get this from your session (See guide below)
|
|
48
|
+
appId: "valid_appId", //Required: Get this from your session (See guide below)
|
|
49
|
+
uploadId: "valid_uploadId", //Required: Get this from your session (See guide below)
|
|
50
|
+
jsToken: "valid_jsToken", //Required: Get this from your session (See guide below)
|
|
51
|
+
browserId: "valid_browserId", //Required: Get this from your session (See guide below)
|
|
41
52
|
};
|
|
42
53
|
|
|
54
|
+
const uploader = new TeraboxUploader(credentials);
|
|
43
55
|
```
|
|
44
56
|
|
|
45
|
-
### Uploading
|
|
57
|
+
### Uploading
|
|
46
58
|
|
|
47
|
-
To upload a file,
|
|
59
|
+
To upload a file, use the instance of TeraboxUploader and specify the file path.
|
|
48
60
|
|
|
49
61
|
#### Example: Save File to a Specific Directory
|
|
50
62
|
|
|
51
63
|
```javascript
|
|
52
|
-
const uploader = new TeraboxUploader(credentials);
|
|
53
64
|
|
|
54
65
|
async function uploadFile() {
|
|
55
66
|
try {
|
|
56
|
-
const result = await uploader.uploadFile(
|
|
67
|
+
const result = await uploader.uploadFile(
|
|
68
|
+
filePath,
|
|
69
|
+
showProgress,
|
|
70
|
+
"/myUploads"
|
|
71
|
+
);
|
|
57
72
|
if (result.success) {
|
|
58
|
-
console.log(
|
|
59
|
-
console.log(
|
|
73
|
+
console.log("File uploaded successfully!");
|
|
74
|
+
console.log("File details:", result.fileDetails);
|
|
60
75
|
} else {
|
|
61
|
-
console.log(
|
|
76
|
+
console.log("Upload failed:", result.message);
|
|
62
77
|
}
|
|
63
78
|
} catch (error) {
|
|
64
|
-
console.log(
|
|
79
|
+
console.log("An error occurred during the upload:", error.message);
|
|
65
80
|
}
|
|
66
81
|
}
|
|
67
|
-
|
|
68
82
|
```
|
|
69
83
|
|
|
84
|
+
### Fetching files
|
|
85
|
+
|
|
86
|
+
Fetch the files in a directory
|
|
87
|
+
|
|
70
88
|
#### Example: Fetch a list of files from Terabox
|
|
71
89
|
|
|
72
90
|
```javascript
|
|
73
91
|
async function fetchFileList() {
|
|
74
92
|
try {
|
|
75
|
-
|
|
76
|
-
console.log(
|
|
93
|
+
const fileList = await uploader.fetchFileList("/myUploads"); //fetching files from 'myuplods' directory, default is '/' directory.
|
|
94
|
+
console.log("Files in your directory:", fileList);
|
|
95
|
+
} catch (error) {
|
|
96
|
+
console.log("Error fetching file list:", error.message);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Downloading a file
|
|
102
|
+
|
|
103
|
+
Download a file by it's fs_id aka fileId
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
async function download(fileId) { // fs_id from fetched file information
|
|
107
|
+
try {
|
|
108
|
+
const res = await uploader.downloadFile(fileId);
|
|
109
|
+
console.log(res);
|
|
77
110
|
} catch (error) {
|
|
78
|
-
console.log(
|
|
111
|
+
console.log(error);
|
|
79
112
|
}
|
|
80
113
|
}
|
|
81
114
|
```
|
|
82
115
|
|
|
83
|
-
|
|
116
|
+
### Delete a file
|
|
117
|
+
|
|
118
|
+
Delete a list of files, provide an array of path of files to delete
|
|
119
|
+
|
|
120
|
+
```javascript
|
|
121
|
+
async function deleteList() {
|
|
122
|
+
try {
|
|
123
|
+
const deleteD = await uploader.deleteFiles([
|
|
124
|
+
"/FliqloScr.zip",
|
|
125
|
+
"/Consent Letter.docx",
|
|
126
|
+
]);
|
|
127
|
+
console.log(deleteD);
|
|
128
|
+
} catch (error) {
|
|
129
|
+
console.error("Error fetching file list:", error.message);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Moving a file
|
|
135
|
+
|
|
136
|
+
Moves a file from one location to another with a new name.
|
|
137
|
+
|
|
138
|
+
```javascript
|
|
139
|
+
async function moveFile(){
|
|
140
|
+
try{
|
|
141
|
+
const moved = await uploader.moveFiles('/sample_960x540.mkv', '/uploads', 'sample.mkv') //old path, new path, new name
|
|
142
|
+
console.log(moved);
|
|
143
|
+
}catch(error){
|
|
144
|
+
console.log(error);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
## Future Enhancements (Open Collaboration)
|
|
84
151
|
|
|
85
152
|
We are actively seeking contributors to add the following features:
|
|
86
153
|
|
|
87
|
-
1.
|
|
154
|
+
1. **Error Handling Enhancements:**
|
|
155
|
+
|
|
156
|
+
- Improve error messages for easier debugging and user guidance.
|
|
157
|
+
|
|
158
|
+
2. **Automated Tests:**
|
|
159
|
+
|
|
160
|
+
- Add test cases to ensure reliability and robustness.
|
|
161
|
+
|
|
162
|
+
3. **Documentation Updates:**
|
|
88
163
|
|
|
89
|
-
|
|
90
|
-
* Include safeguards like confirmation prompts before deletion.
|
|
91
|
-
2. Error Handling Enhancements:
|
|
164
|
+
- Expand guides with screenshots and example workflows.
|
|
92
165
|
|
|
93
|
-
|
|
94
|
-
3. Automated Tests:
|
|
166
|
+
4. **Restructure Code and Files:**
|
|
95
167
|
|
|
96
|
-
|
|
97
|
-
4. Documentation Updates:
|
|
168
|
+
- Restructure the existing code and files
|
|
98
169
|
|
|
99
|
-
|
|
170
|
+
5. **New Features Addition:**
|
|
171
|
+
- Addition of new features and enhancements
|
|
100
172
|
|
|
101
173
|
## Contribution Guidelines
|
|
102
174
|
|
|
@@ -109,8 +181,8 @@ We welcome contributions from the community! Here’s how you can get started:
|
|
|
109
181
|
|
|
110
182
|
## Resources for Developers
|
|
111
183
|
|
|
112
|
-
|
|
113
|
-
|
|
184
|
+
- [Node.js File System Documentation](https://nodejs.org/api/fs.html)
|
|
185
|
+
- [Chrome Dev-Tools (Networks)](https://developer.chrome.com/docs/devtools/network)
|
|
114
186
|
|
|
115
187
|
<br>
|
|
116
188
|
|
|
@@ -124,7 +196,7 @@ Terabox File Management
|
|
|
124
196
|
|
|
125
197
|
Node.js Terabox SDK
|
|
126
198
|
|
|
127
|
-
##
|
|
199
|
+
## Guide
|
|
128
200
|
|
|
129
201
|
For getting your credentials, go to the terabox, create an account and follow the steps:
|
|
130
202
|
|
|
@@ -162,4 +234,4 @@ Get the 'ndus' from cookies in the header section
|
|
|
162
234
|
|
|
163
235
|
This project is licensed under the [MIT License.](./LICENSE)
|
|
164
236
|
|
|
165
|
-
[Github](https://github.com/Pahadi10/terabox-upload-tool)
|
|
237
|
+
[Github](https://github.com/Pahadi10/terabox-upload-tool)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
function createFolder(パス, bdstoken, app_id, jsToken) {
|
|
2
|
+
const url = `https://www.terabox.com/api/create?a=commit&bdstoken=${bdstoken}&app_id=${app_id}&web=1&channel=dubox&clienttype=0&jsToken=${jsToken}&dp-logid=46452600984514820032`;
|
|
3
|
+
|
|
4
|
+
const data = new URLSearchParams({
|
|
5
|
+
path: パス,
|
|
6
|
+
isdir: 1,
|
|
7
|
+
block_list: "[]"
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
fetch(url, {
|
|
11
|
+
method: 'POST',
|
|
12
|
+
headers: {
|
|
13
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
14
|
+
},
|
|
15
|
+
body: data.toString(),
|
|
16
|
+
})
|
|
17
|
+
.then(response => response.json())
|
|
18
|
+
.then(data => {
|
|
19
|
+
console.log('Success:', data);
|
|
20
|
+
})
|
|
21
|
+
.catch((error) => {
|
|
22
|
+
console.error('Error:', error);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 使用例
|
|
27
|
+
createFolder("/a", "43bfe2c66a09d4e4561d68d6f4f10340", "250528", "3F3649BB89A2A6A09BB50936F9C72BF1D8AD957C2C90DFD89F8213A3150FB8BCCDEB94CC4F3F3D863287FE9BCA60922AF06C81F36045A823C1DD16CF69961185");
|
package/examples/dl-test.js
CHANGED
|
@@ -1,117 +1,96 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
web: '1',
|
|
7
|
-
channel: 'dubox',
|
|
8
|
-
clienttype: '0',
|
|
9
|
-
jsToken: 'BDFC7E99221F980D72CA17B19C98F23E35D1C43EBD1B76F2CE493F27A1C2E7C71D7F637156999EF9A71106D82A4836DE040DA96397B95B7F95DDE4836EA0766B',
|
|
10
|
-
'dp-logid': '18397400250736220038',
|
|
11
|
-
cfg_category_keys: '[{"cfg_category_key":"web_download_to_pc_exp_flow_new","cfg_version":1},{"cfg_category_key":"web_download_to_pc","cfg_version":1}]',
|
|
12
|
-
version: '0',
|
|
13
|
-
language_type: 'ja'
|
|
1
|
+
async function getDownloadLink(fidlist, { jstoken, appid, sign, timestamp, bdstoken }) {
|
|
2
|
+
const apiUrls = {
|
|
3
|
+
sysCfg: 'https://www.terabox.com/api/getsyscfg',
|
|
4
|
+
homeInfo: 'https://www.terabox.com/api/home/info',
|
|
5
|
+
download: 'https://www.terabox.com/api/download'
|
|
14
6
|
};
|
|
15
7
|
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
const data = await response.json();
|
|
24
|
-
return data;
|
|
25
|
-
}
|
|
8
|
+
const dpLogIds = {
|
|
9
|
+
sysCfg: '18397400250736220038',
|
|
10
|
+
homeInfo: '18397400250736220039',
|
|
11
|
+
download: '18397400250736220040'
|
|
12
|
+
};
|
|
26
13
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const url = 'https://www.terabox.com/api/home/info';
|
|
30
|
-
const params = {
|
|
31
|
-
app_id: '250528',
|
|
14
|
+
const appParams = {
|
|
15
|
+
app_id: appid,
|
|
32
16
|
web: '1',
|
|
33
17
|
channel: 'dubox',
|
|
34
18
|
clienttype: '0',
|
|
35
|
-
jsToken:
|
|
36
|
-
|
|
19
|
+
jsToken: jstoken,
|
|
20
|
+
version: '0',
|
|
21
|
+
language_type: 'ja'
|
|
37
22
|
};
|
|
38
23
|
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
'Content-Type': 'application/json'
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
const data = await response.json();
|
|
47
|
-
return data;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// ダウンロードURL取得のための関数
|
|
51
|
-
async function getDownloadUrl(fidlist) {
|
|
52
|
-
const sysCfg = await getSysCfg();
|
|
53
|
-
const homeInfo = await getHomeInfo();
|
|
54
|
-
|
|
55
|
-
console.log('System Configuration:', sysCfg);
|
|
56
|
-
console.log('Home Info:', homeInfo);
|
|
57
|
-
|
|
58
|
-
const downloadUrl = 'https://www.terabox.com/api/download';
|
|
59
|
-
|
|
60
|
-
// ダウンロードパラメータの設定
|
|
61
|
-
const params = {
|
|
62
|
-
app_id: '250528',
|
|
63
|
-
web: '1',
|
|
64
|
-
channel: 'dubox',
|
|
65
|
-
clienttype: '0',
|
|
66
|
-
jsToken: 'BDFC7E99221F980D72CA17B19C98F23E35D1C43EBD1B76F2CE493F27A1C2E7C71D7F637156999EF9A71106D82A4836DE040DA96397B95B7F95DDE4836EA0766B',
|
|
67
|
-
'dp-logid': '18397400250736220040',
|
|
68
|
-
fidlist: JSON.stringify(fidlist), // fidlistを渡す
|
|
69
|
-
type: 'dlink',
|
|
70
|
-
vip: '2',
|
|
71
|
-
sign: 'VeJPxoh1ryXIWRvdcBXzMXJvl9iJMry0sUyKk7FrD5ohO4wghBmZ7w==',
|
|
72
|
-
timestamp: '1737845700',
|
|
24
|
+
const downloadParams = {
|
|
25
|
+
sign,
|
|
26
|
+
timestamp,
|
|
73
27
|
need_speed: '0',
|
|
74
|
-
|
|
28
|
+
vip: '2',
|
|
29
|
+
bdstoken
|
|
75
30
|
};
|
|
76
31
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
'
|
|
82
|
-
|
|
83
|
-
|
|
32
|
+
try {
|
|
33
|
+
// sysCfg API
|
|
34
|
+
const sysCfgResponse = await fetch(`${apiUrls.sysCfg}?${new URLSearchParams({
|
|
35
|
+
...appParams,
|
|
36
|
+
'dp-logid': dpLogIds.sysCfg,
|
|
37
|
+
cfg_category_keys: '[{"cfg_category_key":"web_download_to_pc_exp_flow_new","cfg_version":1},{"cfg_category_key":"web_download_to_pc","cfg_version":1}]'
|
|
38
|
+
})}`, {
|
|
39
|
+
method: 'GET',
|
|
40
|
+
headers: { 'Content-Type': 'application/json' }
|
|
41
|
+
});
|
|
42
|
+
const sysCfg = await sysCfgResponse.json();
|
|
43
|
+
console.log('System Configuration:', sysCfg);
|
|
84
44
|
|
|
85
|
-
|
|
45
|
+
// homeInfo API
|
|
46
|
+
const homeInfoResponse = await fetch(`${apiUrls.homeInfo}?${new URLSearchParams({
|
|
47
|
+
...appParams,
|
|
48
|
+
'dp-logid': dpLogIds.homeInfo
|
|
49
|
+
})}`, {
|
|
50
|
+
method: 'GET',
|
|
51
|
+
headers: { 'Content-Type': 'application/json' }
|
|
52
|
+
});
|
|
53
|
+
const homeInfo = await homeInfoResponse.json();
|
|
54
|
+
console.log('Home Info:', homeInfo);
|
|
86
55
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
56
|
+
// download API
|
|
57
|
+
const downloadResponse = await fetch(`${apiUrls.download}?${new URLSearchParams({
|
|
58
|
+
...appParams,
|
|
59
|
+
'dp-logid': dpLogIds.download,
|
|
60
|
+
fidlist: JSON.stringify(fidlist),
|
|
61
|
+
type: 'dlink',
|
|
62
|
+
...downloadParams
|
|
63
|
+
})}`, {
|
|
64
|
+
method: 'GET',
|
|
65
|
+
headers: { 'Content-Type': 'application/json' }
|
|
66
|
+
});
|
|
94
67
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
68
|
+
const downloadData = await downloadResponse.json();
|
|
69
|
+
if (downloadData.dlink && downloadData.dlink.length > 0) {
|
|
70
|
+
return downloadData.dlink[0].dlink;
|
|
71
|
+
} else {
|
|
72
|
+
throw new Error('Download link not found.');
|
|
73
|
+
}
|
|
100
74
|
} catch (error) {
|
|
101
75
|
console.error('Error:', error);
|
|
102
|
-
throw error;
|
|
76
|
+
throw error;
|
|
103
77
|
}
|
|
104
78
|
}
|
|
105
79
|
|
|
106
80
|
// 使用例
|
|
107
81
|
(async () => {
|
|
108
82
|
try {
|
|
109
|
-
const fidlist = [
|
|
110
|
-
const
|
|
111
|
-
|
|
83
|
+
const fidlist = [217911186381995]; // ダウンロード対象のファイルIDリスト
|
|
84
|
+
const params = {
|
|
85
|
+
jstoken: '3F3649BB89A2A6A09BB50936F9C72BF1D8AD957C2C90DFD89F8213A3150FB8BCCDEB94CC4F3F3D863287FE9BCA60922AF06C81F36045A823C1DD16CF69961185',
|
|
86
|
+
appid: '250528',
|
|
87
|
+
sign: '9BIWaJUNH2mrAjROBAjPpwF0/WLwaTnXJEbm3pJV43xiH8fwL33T1g==',
|
|
88
|
+
timestamp: '1738037365',
|
|
89
|
+
bdstoken: '43bfe2c66a09d4e4561d68d6f4f10340'
|
|
90
|
+
};
|
|
112
91
|
|
|
113
|
-
|
|
114
|
-
|
|
92
|
+
const downloadLink = await getDownloadLink(fidlist, params);
|
|
93
|
+
console.log('Download Link:', downloadLink);
|
|
115
94
|
} catch (error) {
|
|
116
95
|
console.error('Error:', error);
|
|
117
96
|
}
|
package/examples/example.js
CHANGED
|
@@ -6,6 +6,8 @@ const credentials = {
|
|
|
6
6
|
ndus: 'your-ndus-token', // Replace with your actual ndus token
|
|
7
7
|
appId: 'your-app-id', // Replace with your actual appId
|
|
8
8
|
uploadId: 'your-upload-id', // Replace with your actual uploadId
|
|
9
|
+
jsToken: 'your-js-token', // Replace with your jsToken
|
|
10
|
+
browserId: 'your-browser-id' // Replace with your browserId
|
|
9
11
|
};
|
|
10
12
|
|
|
11
13
|
// Step 2: Create a new instance of the TeraboxUploader with your credentials
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const axios = require("axios");
|
|
2
|
+
|
|
3
|
+
// ファイル削除APIリクエスト関数
|
|
4
|
+
const deleteFile = async (filelist, config) => {
|
|
5
|
+
// config が { credentials: { ... } } か、直接 { ndus, appId, jsToken, browserId } かを判定
|
|
6
|
+
const { appId, jsToken, browserId, ndus } = config.credentials || config;
|
|
7
|
+
const url = "https://www.1024terabox.com/api/filemanager"; // URLを変更
|
|
8
|
+
|
|
9
|
+
// クエリパラメータを設定
|
|
10
|
+
const params = {
|
|
11
|
+
opera: "delete",
|
|
12
|
+
app_id: appId,
|
|
13
|
+
jsToken: jsToken,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// URLSearchParamsは自動的にエンコードしますが、
|
|
17
|
+
// さらに明示的にエンコードしたい場合は encodeURIComponent を使います。
|
|
18
|
+
const data = new URLSearchParams();
|
|
19
|
+
// JSON形式の filelist をエンコードして追加
|
|
20
|
+
data.append("filelist", JSON.stringify(filelist));
|
|
21
|
+
|
|
22
|
+
// ヘッダー情報を設定
|
|
23
|
+
const headers = {
|
|
24
|
+
"Cookie": `browserid=${browserId}; ndus=${ndus};`,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
// APIリクエストを送信
|
|
29
|
+
const response = await axios.post(url, data.toString(), {
|
|
30
|
+
headers,
|
|
31
|
+
params,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// レスポンスを返す
|
|
35
|
+
return response.data;
|
|
36
|
+
} catch (error) {
|
|
37
|
+
// エラーを投げる
|
|
38
|
+
throw error.response ? error.response.data : error.message;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// モジュールとしてエクスポート
|
|
43
|
+
module.exports = {
|
|
44
|
+
deleteFile,
|
|
45
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const axios = require("axios");
|
|
2
|
+
|
|
3
|
+
// ファイル移動APIリクエスト関数
|
|
4
|
+
const moveFile = async (filelist, config) => {
|
|
5
|
+
// config が { credentials: { ... } } か、直接 { ndus, appId, jsToken, browserId } かを判定
|
|
6
|
+
const { appId, jsToken, browserId, ndus } = config.credentials || config;
|
|
7
|
+
const url = "https://www.1024terabox.com/api/filemanager"; // URLを変更
|
|
8
|
+
|
|
9
|
+
// クエリパラメータを設定(operaの値を "move" に変更)
|
|
10
|
+
const params = {
|
|
11
|
+
opera: "move",
|
|
12
|
+
app_id: appId,
|
|
13
|
+
jsToken: jsToken,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// URLSearchParamsを使用してForm dataを作成
|
|
17
|
+
const data = new URLSearchParams();
|
|
18
|
+
// JSON形式の filelist をエンコードして追加
|
|
19
|
+
// 例: [{"path":"/b","dest":"/a","newname":"c"}]
|
|
20
|
+
data.append("filelist", JSON.stringify(filelist));
|
|
21
|
+
|
|
22
|
+
// ヘッダー情報を設定
|
|
23
|
+
const headers = {
|
|
24
|
+
"Cookie": `browserid=${browserId}; ndus=${ndus};`,
|
|
25
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
// APIリクエストを送信
|
|
30
|
+
const response = await axios.post(url, data.toString(), {
|
|
31
|
+
headers,
|
|
32
|
+
params,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// レスポンスを返す
|
|
36
|
+
return response.data;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
// エラーを投げる
|
|
39
|
+
throw error.response ? error.response.data : error.message;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// モジュールとしてエクスポート
|
|
44
|
+
module.exports = {
|
|
45
|
+
moveFile,
|
|
46
|
+
};
|
package/lib/index.js
CHANGED
|
@@ -2,16 +2,27 @@ 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('./helpers/utils');
|
|
6
|
-
const getDownloadLink = require('./helpers/download/download')
|
|
5
|
+
const { buildUploadUrl, buildCreateUrl, buildListUrl } = require('./helpers/utils');
|
|
6
|
+
const getDownloadLink = require('./helpers/download/download');
|
|
7
|
+
const { deleteFile } = require('./helpers/fileDelete');
|
|
8
|
+
const { moveFile } = require('./helpers/fileMove');
|
|
9
|
+
const getShortUrl = require('./helpers/getShortUrl');
|
|
7
10
|
|
|
8
11
|
/**
|
|
9
12
|
* Class to handle Terabox file operations
|
|
10
13
|
*/
|
|
11
14
|
class TeraboxUploader {
|
|
12
15
|
constructor(credentials) {
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
// Now require jsToken and browserId for deletion functionality
|
|
17
|
+
if (
|
|
18
|
+
!credentials ||
|
|
19
|
+
!credentials.ndus ||
|
|
20
|
+
!credentials.appId ||
|
|
21
|
+
!credentials.uploadId ||
|
|
22
|
+
!credentials.jsToken ||
|
|
23
|
+
!credentials.browserId
|
|
24
|
+
) {
|
|
25
|
+
throw new Error("Credentials are required (ndus, appId, uploadId, jsToken, browserId).");
|
|
15
26
|
}
|
|
16
27
|
|
|
17
28
|
this.credentials = {
|
|
@@ -19,6 +30,8 @@ class TeraboxUploader {
|
|
|
19
30
|
cookies: `browserid=3BeB9xGWg2yuzOuPRnKtO0ZQx990OtItXpdwkRVAIKYiLxBkT8yVYM3TnVr=; lang=en; ndus=${credentials.ndus};`,
|
|
20
31
|
appId: credentials.appId,
|
|
21
32
|
uploadId: credentials.uploadId,
|
|
33
|
+
jsToken: credentials.jsToken,
|
|
34
|
+
browserId: credentials.browserId,
|
|
22
35
|
};
|
|
23
36
|
}
|
|
24
37
|
|
|
@@ -60,19 +73,23 @@ class TeraboxUploader {
|
|
|
60
73
|
|
|
61
74
|
// Finalize the upload (Create call)
|
|
62
75
|
const createUrl = buildCreateUrl();
|
|
63
|
-
const createResponse = await axios.post(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
+
const createResponse = await axios.post(
|
|
77
|
+
createUrl,
|
|
78
|
+
new URLSearchParams({
|
|
79
|
+
path: `${directory}/${fileName}`,
|
|
80
|
+
size: fileSize,
|
|
81
|
+
uploadid: this.credentials.uploadId,
|
|
82
|
+
target_path: directory,
|
|
83
|
+
block_list: JSON.stringify([postResponse.headers['content-md5']]),
|
|
84
|
+
local_mtime: Math.floor(Date.now() / 1000),
|
|
85
|
+
}).toString(),
|
|
86
|
+
{
|
|
87
|
+
headers: {
|
|
88
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
89
|
+
Cookie: this.credentials.cookies,
|
|
90
|
+
},
|
|
91
|
+
}
|
|
92
|
+
);
|
|
76
93
|
|
|
77
94
|
// Return success message
|
|
78
95
|
return {
|
|
@@ -81,7 +98,6 @@ class TeraboxUploader {
|
|
|
81
98
|
fileDetails: createResponse.data,
|
|
82
99
|
};
|
|
83
100
|
} catch (error) {
|
|
84
|
-
console.error('Error during upload process:', error.response?.data || error.message || error);
|
|
85
101
|
|
|
86
102
|
// Return failure message
|
|
87
103
|
return {
|
|
@@ -91,7 +107,6 @@ class TeraboxUploader {
|
|
|
91
107
|
}
|
|
92
108
|
}
|
|
93
109
|
|
|
94
|
-
|
|
95
110
|
/**
|
|
96
111
|
* Fetches the file list from Terabox
|
|
97
112
|
* @param {string} directory - Directory to fetch the file list from (e.g., "/")
|
|
@@ -106,13 +121,8 @@ class TeraboxUploader {
|
|
|
106
121
|
Cookie: this.credentials.cookies,
|
|
107
122
|
},
|
|
108
123
|
});
|
|
109
|
-
|
|
110
|
-
|
|
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 };
|
|
124
|
+
|
|
125
|
+
return { success: true, message: 'File list retrieved successfully.', data: response.data };
|
|
116
126
|
} catch (error) {
|
|
117
127
|
console.error('Error fetching file list:', error);
|
|
118
128
|
return {
|
|
@@ -121,6 +131,106 @@ class TeraboxUploader {
|
|
|
121
131
|
};
|
|
122
132
|
}
|
|
123
133
|
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Uploads a file to Terabox
|
|
137
|
+
* @param {int} fileId - fs_id in file details
|
|
138
|
+
* @returns {Promise<{success: boolean, message: string, fileDetails?: object}>} - A promise that resolves to an object with download link.
|
|
139
|
+
*/
|
|
140
|
+
async downloadFile(fileId) {
|
|
141
|
+
try {
|
|
142
|
+
const ndus = this.credentials.ndus;
|
|
143
|
+
const fid = fileId;
|
|
144
|
+
const response = await getDownloadLink(ndus, fid);
|
|
145
|
+
return response
|
|
146
|
+
} catch (error) {
|
|
147
|
+
return {
|
|
148
|
+
success: false,
|
|
149
|
+
message: error.response?.data?.error || error.message || 'Failed to fetch file list.',
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Deletes files from Terabox using the deletion API.
|
|
156
|
+
* @param {Array} fileList - List of file paths to be deleted on Terabox
|
|
157
|
+
* @returns {Promise<any>} - A promise that resolves to the deletion result.
|
|
158
|
+
*/
|
|
159
|
+
async deleteFiles(fileList) {
|
|
160
|
+
try {
|
|
161
|
+
const config = {
|
|
162
|
+
ndus: this.credentials.ndus,
|
|
163
|
+
appId: this.credentials.appId,
|
|
164
|
+
jsToken: this.credentials.jsToken,
|
|
165
|
+
browserId: this.credentials.browserId,
|
|
166
|
+
};
|
|
167
|
+
const result = await deleteFile(fileList, config);
|
|
168
|
+
return { success: true, message: 'Files deleted successfully.', result };
|
|
169
|
+
} catch (error) {
|
|
170
|
+
return {
|
|
171
|
+
success: false,
|
|
172
|
+
message: error.response?.data?.error || error.message || 'Failed to delete files.',
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Moves files on Terabox using the move API.
|
|
179
|
+
* Moves a file from one location to another with a new name.
|
|
180
|
+
* @param {string} sourcePath - The current path of the file.
|
|
181
|
+
* @param {string} destinationPath - The destination directory where the file will be moved.
|
|
182
|
+
* @param {string} newName - The new name for the file.
|
|
183
|
+
* @returns {Promise<any>} - A promise that resolves to the move result.
|
|
184
|
+
*/
|
|
185
|
+
async moveFiles(sourcePath, destinationPath, newName) {
|
|
186
|
+
try {
|
|
187
|
+
const config = {
|
|
188
|
+
ndus: this.credentials.ndus,
|
|
189
|
+
appId: this.credentials.appId,
|
|
190
|
+
jsToken: this.credentials.jsToken,
|
|
191
|
+
browserId: this.credentials.browserId,
|
|
192
|
+
};
|
|
193
|
+
// Create file list for move operation as per API: [{"path":"/b","dest":"/a","newname":"c"}]
|
|
194
|
+
const fileList = [{
|
|
195
|
+
path: sourcePath,
|
|
196
|
+
dest: destinationPath,
|
|
197
|
+
newname: newName
|
|
198
|
+
}];
|
|
199
|
+
const result = await moveFile(fileList, config);
|
|
200
|
+
return result;
|
|
201
|
+
} catch (error) {
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Generates a short URL for a file on Terabox
|
|
208
|
+
* @param {string} filePath - The file path on Terabox
|
|
209
|
+
* @param {string} fileId - The file ID on Terabox
|
|
210
|
+
* @returns {Promise<object>} - The API response containing the short URL
|
|
211
|
+
*/
|
|
212
|
+
async generateShortUrl(filePath, fileId) {
|
|
213
|
+
try {
|
|
214
|
+
const shortUrlResponse = await getShortUrl(this.credentials.ndus, filePath, fileId);
|
|
215
|
+
if (shortUrlResponse && shortUrlResponse.errno === 0) {
|
|
216
|
+
return {
|
|
217
|
+
success: true,
|
|
218
|
+
message: 'Short URL generated successfully.',
|
|
219
|
+
shortUrl: shortUrlResponse.shorturl,
|
|
220
|
+
};
|
|
221
|
+
} else {
|
|
222
|
+
return {
|
|
223
|
+
success: false,
|
|
224
|
+
message: shortUrlResponse?.errmsg || 'Failed to generate short URL.',
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
} catch (error) {
|
|
228
|
+
return {
|
|
229
|
+
success: false,
|
|
230
|
+
message: error.message || 'An error occurred while generating the short URL.',
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
}
|
|
124
234
|
}
|
|
125
235
|
|
|
126
236
|
module.exports = TeraboxUploader;
|
package/package.json
CHANGED
|
@@ -1,22 +1,27 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "terabox-upload-tool",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
"
|
|
8
|
-
},
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "terabox-upload-tool",
|
|
3
|
+
"version": "1.3.1",
|
|
4
|
+
"description": "A robust library designed for seamless integration with TeraBox, the leading cloud storage service offering 1 TB of free space. Effortlessly upload files, download files, delete files, manage directories, and retrieve file lists. Ideal for developers seeking efficient cloud storage solutions within their Node.js projects.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/Pahadi10/terabox-upload-tool"
|
|
8
|
+
},
|
|
9
|
+
"main": "lib/index.js",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"terabox",
|
|
15
|
+
"upload",
|
|
16
|
+
"file",
|
|
17
|
+
"storage",
|
|
18
|
+
"node"
|
|
19
|
+
],
|
|
20
|
+
"homepage": "https://github.com/Pahadi10/terabox-upload-tool#readme",
|
|
21
|
+
"author": "Nitesh Singh Bhandari",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"axios": "^1.7.9",
|
|
25
|
+
"form-data": "^4.0.1"
|
|
26
|
+
}
|
|
27
|
+
}
|