|
1 | 1 | 'use strict'; |
2 | | -const fs = require('node:fs'); |
3 | | -//const oneDriveAPI = require('onedrive-api'); |
4 | 2 | const Onedrive = require('../oneDriveLib'); |
5 | | -//const got = require('@esm2cjs/got').default; |
6 | 3 |
|
7 | 4 | let od_accessToken; |
8 | 5 |
|
9 | | -/* |
10 | | -async function list(restoreSource, options, types, log, callback) { |
11 | | - const db_onedriveAccessJson = options.onedriveAccessJson !== undefined ? options.onedriveAccessJson : options.onedrive && options.onedrive.onedriveAccessJson !== undefined ? options.onedrive.onedriveAccessJson : ''; |
12 | | - const db_dir = options.dir !== undefined ? options.dir : options.onedrive && options.onedrive.dir !== undefined ? options.onedrive.dir : '/'; |
13 | | - const db_ownDir = options.ownDir !== undefined ? options.ownDir : options.onedrive && options.onedrive.ownDir !== undefined ? options.onedrive.ownDir : false; |
14 | | - const db_dirMinimal = options.dirMinimal !== undefined ? options.dirMinimal : options.onedrive && options.onedrive.dirMinimal !== undefined ? options.onedrive.dirMinimal : '/'; |
15 | | -
|
16 | | -
|
17 | | - // Token refresh |
18 | | - if (!restoreSource || restoreSource === 'onedrive') { |
19 | | - const onedrive = new Onedrive(); |
20 | | - od_accessToken = await onedrive.getToken(db_onedriveAccessJson, log).catch(err => log.warn(`Onedrive Token: ${err}`)); |
21 | | - } |
22 | | -
|
23 | | - if (od_accessToken && (!restoreSource || restoreSource === 'onedrive')) { |
24 | | -
|
25 | | - let dir = (db_dir || '').replace(/\\/g, '/'); |
26 | | -
|
27 | | - if (db_ownDir === true) { |
28 | | - dir = (db_dirMinimal || '').replace(/\\/g, '/'); |
29 | | - } |
30 | | -
|
31 | | - if (!dir || dir[0] !== '/') { |
32 | | - dir = `/${dir || ''}`; |
33 | | - } |
34 | | -
|
35 | | - if (!dir) { |
36 | | - dir = 'root'; |
37 | | - } |
38 | | - if (dir.startsWith('/')) { |
39 | | - dir = dir.substring(1); |
40 | | - } |
41 | | -
|
42 | | - oneDriveAPI.items |
43 | | - .getMetadata({ |
44 | | - accessToken: od_accessToken, |
45 | | - itemId: `${dir !== 'root' ? `root:/${dir}` : dir}`, |
46 | | - }) |
47 | | - .then((res) => { |
48 | | - if (res && res.id) { |
49 | | - oneDriveAPI.items |
50 | | - .listChildren({ |
51 | | - accessToken: od_accessToken, |
52 | | - itemId: res.id, |
53 | | - }) |
54 | | - .then((childrens) => { |
55 | | - if (childrens) { |
56 | | - let childs = JSON.parse(JSON.stringify(childrens.value)); |
57 | | - if (childs) { |
58 | | - childs = childs.map(file => { |
59 | | - return { path: file.name, name: file.name, size: file.size, id: file.id } |
60 | | - }).filter(file => (types.indexOf(file.name.split('_')[0]) !== -1 || types.indexOf(file.name.split('.')[0]) !== -1) && file.name.split('.').pop() == 'gz'); |
61 | | -
|
62 | | - const files = {}; |
63 | | - childs.forEach(file => { |
64 | | - const type = file.name.split('_')[0]; |
65 | | - files[type] = files[type] || []; |
66 | | - files[type].push(file); |
67 | | - }); |
68 | | -
|
69 | | - callback && callback(null, files, 'onedrive'); |
70 | | - } else { |
71 | | - callback && callback('Error on Onedrive list'); |
72 | | - } |
73 | | - } |
74 | | - }) |
75 | | - .catch((error) => { |
76 | | - log.error(`Onedrive list error: ${error}`); |
77 | | - setImmediate(callback, error); |
78 | | - }); |
79 | | - } |
80 | | - }) |
81 | | - .catch((error) => { |
82 | | - log.error(`Onedrive Metadata error: ${error}`); |
83 | | - setImmediate(callback, error); |
84 | | - }); |
85 | | - } else { |
86 | | - setImmediate(callback); |
87 | | - } |
88 | | -} |
89 | | -*/ |
90 | | - |
91 | 6 | async function list(restoreSource, options, types, log, callback) { |
92 | 7 | const db_onedriveAccessJson = options.onedriveAccessJson !== undefined ? options.onedriveAccessJson : options.onedrive && options.onedrive.onedriveAccessJson !== undefined ? options.onedrive.onedriveAccessJson : ''; |
93 | 8 | const db_dir = options.dir !== undefined ? options.dir : options.onedrive && options.onedrive.dir !== undefined ? options.onedrive.dir : '/'; |
@@ -143,107 +58,6 @@ async function list(restoreSource, options, types, log, callback) { |
143 | 58 | callback && callback(); |
144 | 59 | } |
145 | 60 | } |
146 | | -/* |
147 | | -async function getFile(options, fileName, toStoreName, log, callback) { |
148 | | - const db_onedriveAccessJson = options.onedriveAccessJson !== undefined ? options.onedriveAccessJson : options.onedrive?.onedriveAccessJson || ''; |
149 | | - const db_dir = options.dir !== undefined ? options.dir : options.onedrive?.dir || '/'; |
150 | | - const db_ownDir = options.ownDir !== undefined ? options.ownDir : options.onedrive?.ownDir || false; |
151 | | - const db_dirMinimal = options.dirMinimal !== undefined ? options.dirMinimal : options.onedrive?.dirMinimal || '/'; |
152 | | -
|
153 | | - const onedrive = new Onedrive(); |
154 | | - od_accessToken = await onedrive.getToken(db_onedriveAccessJson, log).catch(err => log.warn(`Onedrive Token: ${err}`)); |
155 | | -
|
156 | | - if (od_accessToken) { |
157 | | - // copy file to backupDir |
158 | | - const onlyFileName = fileName.split('/').pop(); |
159 | | -
|
160 | | - let dir = (db_dir || '').replace(/\\/g, '/'); |
161 | | -
|
162 | | - if (db_ownDir === true) { |
163 | | - dir = (db_dirMinimal || '').replace(/\\/g, '/'); |
164 | | - } |
165 | | -
|
166 | | - if (!dir || dir[0] !== '/') { |
167 | | - dir = `/${dir || ''}`; |
168 | | - } |
169 | | -
|
170 | | - if (!dir) { |
171 | | - dir = 'root'; |
172 | | - } |
173 | | -
|
174 | | - if (dir.startsWith('/')) { |
175 | | - dir = dir.substring(1); |
176 | | - } |
177 | | -
|
178 | | - oneDriveAPI.items |
179 | | - .getMetadata({ |
180 | | - accessToken: od_accessToken, |
181 | | - itemId: `${dir !== 'root' ? `root:/${dir}` : dir}`, |
182 | | - }) |
183 | | - .then((res) => { |
184 | | - if (res && res.id) { |
185 | | - oneDriveAPI.items |
186 | | - .listChildren({ |
187 | | - accessToken: od_accessToken, |
188 | | - itemId: res.id, |
189 | | - }) |
190 | | - .then((childrens) => { |
191 | | - if (childrens) { |
192 | | - const childs = JSON.parse(JSON.stringify(childrens.value)); |
193 | | - if (childs) { |
194 | | - const result = childs.filter((d) => d.name === onlyFileName); |
195 | | -
|
196 | | - if (result.length === 0) { |
197 | | - const err = new Error(`File "${onlyFileName}" not found in OneDrive`); |
198 | | - log.error(err); |
199 | | - callback && callback(err); |
200 | | - return; |
201 | | - } |
202 | | -
|
203 | | - const downloadUrl = result[0]['@microsoft.graph.downloadUrl']; |
204 | | - if (!downloadUrl) { |
205 | | - const err = new Error(`Download URL not found for file "${onlyFileName}"`); |
206 | | - log.error(err); |
207 | | - callback && callback(err); |
208 | | - return; |
209 | | - } |
210 | | -
|
211 | | - log.debug(`Onedrive: Download of "${fileName}" started`); |
212 | | -
|
213 | | - const writeStream = fs.createWriteStream(toStoreName); |
214 | | - writeStream.on('error', err => { |
215 | | - log.error(`OneDrive: Write error: ${err}`); |
216 | | - callback && callback(err); |
217 | | - callback = null; |
218 | | - }).on('close', result => { |
219 | | - log.debug(`OneDrive: Download of "${fileName}" finished`); |
220 | | - callback && callback(result); |
221 | | - }); |
222 | | -
|
223 | | - got.stream(downloadUrl) |
224 | | - .on('error', err => { |
225 | | - log.error(`OneDrive: Download stream error: ${err}`); |
226 | | - callback && callback(err); |
227 | | - }) |
228 | | - .pipe(writeStream); |
229 | | - } |
230 | | - } |
231 | | - }) |
232 | | - .catch((error) => { |
233 | | - log.error(`Onedrive list error: ${error}`); |
234 | | - callback && callback(error); |
235 | | - }); |
236 | | - } |
237 | | - }) |
238 | | - .catch((error) => { |
239 | | - log.error(`Onedrive Metadata error: ${error}`); |
240 | | - callback && callback(error); |
241 | | - }); |
242 | | - } else { |
243 | | - callback && callback('Not configured'); |
244 | | - } |
245 | | -} |
246 | | -*/ |
247 | 61 |
|
248 | 62 | async function getFile(options, fileName, toStoreName, log, callback) { |
249 | 63 | const db_onedriveAccessJson = options.onedriveAccessJson ?? options.onedrive?.onedriveAccessJson ?? ''; |
|
0 commit comments