1
1
import fs from 'fs' ;
2
2
import path from "path" ;
3
- import dropboxV2Api from 'dropbox-v2-api' ;
3
+ import { Dropbox } from 'dropbox' ; // https://www.npmjs.com/package/dropbox
4
4
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
7
7
listFromDropbox ( options ) {
8
8
const dbx = getDropbox ( options ) ;
9
9
const path = options . getDropboxPath ( ) ;
10
- return new Promise ( ( resolve , reject ) => {
10
+ return new Promise ( ( resolve , reject ) => {
11
11
// 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
+ } ) ;
26
22
} ) ;
27
23
}
28
24
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
31
26
mongoDumpUploadOnDropbox ( options , dumpResult ) {
32
27
const dbx = getDropbox ( options ) ;
33
28
const path = options . getDropboxPath ( ) ;
34
29
const filename = dumpResult . fileName ? dumpResult . fileName : "mongodump.gz" ;
35
30
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 ) ;
47
35
}
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
+ } ) ;
52
48
} ) ;
53
- } ) ;
49
+ } )
54
50
}
55
51
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
58
53
async mongorestoreDownloadFromDropbox ( options ) {
59
54
const dbx = getDropbox ( options ) ;
60
55
const dbxFullFilename = options . dumpFile ;
@@ -67,28 +62,24 @@ class MTDropbox {
67
62
// create path if not exist
68
63
if ( ! fs . existsSync ( localPath ) ) {
69
64
await fs . promises . mkdir ( localPath , { recursive : true } )
70
- . catch ( err => {
65
+ . catch ( err => {
71
66
return Promise . reject ( new Error ( `path: cannot create ${ localPath } : ${ err } ` ) ) ;
72
67
} ) ;
73
68
}
74
69
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 ) ;
89
82
} ) ;
90
- } )
91
- . pipe ( fs . createWriteStream ( fullFileName ) ) ;
92
83
} ) ;
93
84
}
94
85
@@ -97,35 +88,34 @@ class MTDropbox {
97
88
const path = options . getDropboxPath ( ) ;
98
89
const mt = this ;
99
90
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
+ } ) ;
129
119
} ) ;
130
120
}
131
121
@@ -159,39 +149,22 @@ class MTDropbox {
159
149
160
150
backupDelete ( dbx , backupPath ) {
161
151
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 ) ;
175
155
} ) ;
176
156
}
177
157
}
178
158
179
159
//~ private
180
160
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
-
186
161
function extractFilename ( fullFileName ) {
187
162
return path . basename ( fullFileName ) ;
188
163
}
189
164
190
165
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 } ) ;
194
170
}
195
-
196
-
197
- export default MTDropbox ;
0 commit comments