Skip to content

Commit 8976b19

Browse files
committed
Fix #72 #71 move to dropbox dep
1 parent 35c4361 commit 8976b19

File tree

5 files changed

+270
-773
lines changed

5 files changed

+270
-773
lines changed

lib/MTDropbox.js

+81-108
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,55 @@
11
import fs from 'fs';
22
import path from "path";
3-
import dropboxV2Api from 'dropbox-v2-api';
3+
import {Dropbox} from 'dropbox'; // https://www.npmjs.com/package/dropbox
44

5-
class MTDropbox {
6-
// https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder
5+
export default class MTDropbox {
6+
// https://github.com/dropbox/dropbox-sdk-js/blob/main/examples/javascript/node/basic.js
77
listFromDropbox(options) {
88
const dbx = getDropbox(options);
99
const path = options.getDropboxPath();
10-
return new Promise((resolve, reject)=> {
10+
return new Promise((resolve, reject) => {
1111
// DEBUG // console.log(`Dropbox filesListFolder ${path}:`);
12-
dbx({
13-
resource: 'files/list_folder',
14-
parameters: getDbxListFileFromPathParam(path)
15-
}, (err, result, response) => {
16-
if (err) {
17-
return reject(err);
18-
}
19-
// DEBUG // console.log(result); console.log(response.headers);
20-
const fileNames = result.entries
21-
.filter(e => e[".tag"] === "file")
22-
.map(e => e.path_lower);
23-
// DEBUG // console.log('response', fileNames)
24-
resolve(fileNames);
25-
});
12+
dbx.filesListFolder({path})
13+
.then(response => {
14+
const fileNames = response.result.entries
15+
.filter(e => e[".tag"] === "file")
16+
.map(e => e.path_lower);
17+
resolve(fileNames);
18+
})
19+
.catch(filesListError => {
20+
return reject(filesListError);
21+
});
2622
});
2723
}
2824

29-
// https://www.npmjs.com/package/dropbox-v2-api
30-
// https://www.dropbox.com/developers/documentation/http/documentation#files-upload
25+
// https://github.com/dropbox/dropbox-sdk-js/blob/main/examples/javascript/node/upload.js
3126
mongoDumpUploadOnDropbox(options, dumpResult) {
3227
const dbx = getDropbox(options);
3328
const path = options.getDropboxPath();
3429
const filename = dumpResult.fileName ? dumpResult.fileName : "mongodump.gz";
3530
const dbxFilename = path + "/" + filename;
36-
return new Promise((resolve, reject)=> {
37-
dbx({
38-
resource: 'files/upload',
39-
parameters: {
40-
"path": dbxFilename
41-
},
42-
readStream: fs.createReadStream(dumpResult.fullFileName)
43-
}, (err, result, response) => {
44-
// DEBUG // console.log("files/upload", JSON.stringify(err), JSON.stringify(result), JSON.stringify(response));
45-
if (err) {
46-
return reject(err);
31+
return new Promise((resolve, reject) => {
32+
fs.readFile(dumpResult.fullFileName, (readFileError, contents) => {
33+
if (readFileError) {
34+
return reject(readFileError);
4735
}
48-
dumpResult.dropboxFile = result.path_display;
49-
dumpResult.dropboxFileSize = result.size;
50-
dumpResult.message = dumpResult.message + ` - uploaded on dropbox as ${dumpResult.dropboxFile}`;
51-
resolve(dumpResult);
36+
dbx.filesUpload({path: dbxFilename, contents})
37+
.then(response => {
38+
const result = response.result;
39+
// DEBUG // console.log(result);
40+
dumpResult.dropboxFile = result.path_display;
41+
dumpResult.dropboxFileSize = result.size;
42+
dumpResult.message = dumpResult.message + ` - uploaded on dropbox as ${dumpResult.dropboxFile}`;
43+
resolve(dumpResult);
44+
})
45+
.catch(uploadErr => {
46+
return reject(uploadErr);
47+
});
5248
});
53-
});
49+
})
5450
}
5551

56-
// https://www.npmjs.com/package/dropbox-v2-api
57-
// https://www.dropbox.com/developers/documentation/http/documentation#files-download
52+
// https://github.com/dropbox/dropbox-sdk-js/blob/main/examples/javascript/node/download.js
5853
async mongorestoreDownloadFromDropbox(options) {
5954
const dbx = getDropbox(options);
6055
const dbxFullFilename = options.dumpFile;
@@ -67,28 +62,24 @@ class MTDropbox {
6762
// create path if not exist
6863
if (!fs.existsSync(localPath)) {
6964
await fs.promises.mkdir(localPath, {recursive: true})
70-
.catch( err => {
65+
.catch(err => {
7166
return Promise.reject(new Error(`path: cannot create ${localPath} : ${err} `));
7267
});
7368
}
7469
return new Promise((resolve, reject) => {
75-
// DEBUG // console.log("files/download", dbxFilename);
76-
dbx({
77-
resource: 'files/download',
78-
parameters: {
79-
"path": dbxFullFilename
80-
}
81-
}, (err, result, response) => {
82-
if (err) {
83-
return reject(err);
84-
}
85-
resolve({
86-
message: `dump downloaded into ${fullFileName}`,
87-
fileName,
88-
fullFileName
70+
dbx.filesDownload({"path": dbxFullFilename})
71+
.then(response => {
72+
// DEBUG // console.log(response.result);
73+
fs.writeFileSync(fullFileName, response.result.fileBinary);
74+
resolve({
75+
message: `dump downloaded into ${fullFileName}`,
76+
fileName,
77+
fullFileName
78+
});
79+
})
80+
.catch(uploadErr => {
81+
return reject(uploadErr);
8982
});
90-
})
91-
.pipe(fs.createWriteStream(fullFileName));
9283
});
9384
}
9485

@@ -97,35 +88,34 @@ class MTDropbox {
9788
const path = options.getDropboxPath();
9889
const mt = this;
9990
return new Promise((resolve, reject) => {
100-
dbx({
101-
resource: 'files/list_folder',
102-
parameters: getDbxListFileFromPathParam(path)
103-
}, async function (err, result, response) {
104-
if (err) {
105-
return reject(err);
106-
}
107-
// DEBUG // console.log(result); console.log(response.headers);
108-
if (result.has_more === true) {
109-
return reject(new Error(`dropbox backup directory ${path} has more than 2000 files. Rotation has been skipped`));
110-
}
111-
const initialBackupsCount = result.length;
112-
const deprecatedBackups = result.entries
113-
.filter(e => e[".tag"] === "file")
114-
.filter(e => new Date(e.client_modified) < new Date(ctimeMsMax))
115-
.map(e => {
116-
return {
117-
name: e.name,
118-
path_lower: e.path_lower,
119-
client_modified: e.client_modified
120-
};
121-
});
122-
const deprecatedBackupsCount = deprecatedBackups.length;
123-
const deletedBackups = await mt.backupsToClean(dbx, dryMode, deprecatedBackups, cleanCount, minCount);
124-
const cleanedCount = deletedBackups.length;
125-
const cleanedFiles = deletedBackups.map(db => db.path_lower);
126-
// DEBUG // console.log('fileNames', fileNames)
127-
return resolve({initialBackupsCount, deprecatedBackupsCount, cleanedCount, cleanedFiles});
128-
});
91+
dbx.filesListFolder({path})
92+
.then(async response => {
93+
const result = response.result;
94+
// DEBUG // console.log(result); console.log(response.headers);
95+
if (result.has_more === true) {
96+
return reject(new Error(`dropbox backup directory ${path} has more than 2000 files. Rotation has been skipped`));
97+
}
98+
const initialBackupsCount = result.length;
99+
const deprecatedBackups = result.entries
100+
.filter(e => e[".tag"] === "file")
101+
.filter(e => new Date(e.client_modified) < new Date(ctimeMsMax))
102+
.map(e => {
103+
return {
104+
name: e.name,
105+
path_lower: e.path_lower,
106+
client_modified: e.client_modified
107+
};
108+
});
109+
const deprecatedBackupsCount = deprecatedBackups.length;
110+
const deletedBackups = await mt.backupsToClean(dbx, dryMode, deprecatedBackups, cleanCount, minCount);
111+
const cleanedCount = deletedBackups.length;
112+
const cleanedFiles = deletedBackups.map(db => db.path_lower);
113+
// DEBUG // console.log('fileNames', fileNames)
114+
return resolve({initialBackupsCount, deprecatedBackupsCount, cleanedCount, cleanedFiles});
115+
})
116+
.catch(filesListError => {
117+
return reject(filesListError);
118+
});
129119
});
130120
}
131121

@@ -159,39 +149,22 @@ class MTDropbox {
159149

160150
backupDelete(dbx, backupPath) {
161151
return new Promise((resolve, reject) => {
162-
dbx({
163-
resource: 'files/delete',
164-
parameters: {"path": backupPath}
165-
}, function (err, result, response) {
166-
if (err) {
167-
console.log("DELETE ERROR", backupPath, err);
168-
reject(err);
169-
} else {
170-
console.log("DELETED", backupPath);
171-
resolve(backupPath);
172-
}
173-
});
174-
152+
dbx.filesDeleteV2({"path": backupPath})
153+
.then(() => resolve(backupPath))
154+
.catch(reject);
175155
});
176156
}
177157
}
178158

179159
//~ private
180160

181-
//https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder
182-
function getDbxListFileFromPathParam(path) {
183-
return {path, "recursive": false, limit: 2000, include_non_downloadable_files: false};
184-
}
185-
186161
function extractFilename(fullFileName) {
187162
return path.basename(fullFileName);
188163
}
189164

190165
function getDropbox(options) {
191-
return options.dropboxEnabled ? dropboxV2Api.authenticate({
192-
token: options.dropboxToken
193-
}) : null;
166+
if (!options.dropboxEnabled) {
167+
return null;
168+
}
169+
return new Dropbox({accessToken: options.dropboxToken});
194170
}
195-
196-
197-
export default MTDropbox;

lib/MTOptions.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class MTOptions {
1+
export default class MTOptions {
22
constructor(options) {
33
const LO = '127.0.0.1';
44
const opt = options ? options : {};
@@ -69,7 +69,7 @@ class MTOptions {
6969
this.port = parseInt(this.port, 10);
7070
}
7171
if (isNaN(this.port)) {
72-
throw `invalid port ${this.port}`;
72+
throw new Error(`invalid port ${this.port}`);
7373
}
7474
}
7575

@@ -83,5 +83,3 @@ class MTOptions {
8383
return ('dropboxLocalPath' in this) ? this.dropboxLocalPath : 'dropbox';
8484
}
8585
}
86-
87-
export default MTOptions;

0 commit comments

Comments
 (0)