Skip to content

Commit cb0ca9e

Browse files
committed
(simatec) Onedrive list with new API Request
1 parent 0f03729 commit cb0ca9e

File tree

3 files changed

+133
-25
lines changed

3 files changed

+133
-25
lines changed

admin/custom/customComponents.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/list/onedrive.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const got = require('@esm2cjs/got').default;
66

77
let od_accessToken;
88

9+
/*
910
async function list(restoreSource, options, types, log, callback) {
1011
const db_onedriveAccessJson = options.onedriveAccessJson !== undefined ? options.onedriveAccessJson : options.onedrive && options.onedrive.onedriveAccessJson !== undefined ? options.onedrive.onedriveAccessJson : '';
1112
const db_dir = options.dir !== undefined ? options.dir : options.onedrive && options.onedrive.dir !== undefined ? options.onedrive.dir : '/';
@@ -85,6 +86,63 @@ async function list(restoreSource, options, types, log, callback) {
8586
setImmediate(callback);
8687
}
8788
}
89+
*/
90+
91+
async function list(restoreSource, options, types, log, callback) {
92+
const db_onedriveAccessJson = options.onedriveAccessJson !== undefined ? options.onedriveAccessJson : options.onedrive && options.onedrive.onedriveAccessJson !== undefined ? options.onedrive.onedriveAccessJson : '';
93+
const db_dir = options.dir !== undefined ? options.dir : options.onedrive && options.onedrive.dir !== undefined ? options.onedrive.dir : '/';
94+
const db_ownDir = options.ownDir !== undefined ? options.ownDir : options.onedrive && options.onedrive.ownDir !== undefined ? options.onedrive.ownDir : false;
95+
const db_dirMinimal = options.dirMinimal !== undefined ? options.dirMinimal : options.onedrive && options.onedrive.dirMinimal !== undefined ? options.onedrive.dirMinimal : '/';
96+
97+
let od_accessToken;
98+
99+
// Refresh token if necessary
100+
if (!restoreSource || restoreSource === 'onedrive') {
101+
const onedrive = new Onedrive();
102+
try {
103+
od_accessToken = await onedrive.getToken(db_onedriveAccessJson, log);
104+
} catch (err) {
105+
log.warn(`Onedrive Token: ${err}`);
106+
}
107+
108+
if (od_accessToken) {
109+
let dir = (db_dir || '').replace(/\\/g, '/');
110+
111+
// Use minimal path if ownDir is true
112+
if (db_ownDir === true) {
113+
dir = (db_dirMinimal || '').replace(/\\/g, '/');
114+
}
115+
116+
// Normalize directory format
117+
if (!dir || dir[0] !== '/') {
118+
dir = `/${dir || ''}`;
119+
}
120+
121+
if (!dir) {
122+
dir = 'root';
123+
}
124+
125+
if (dir.startsWith('/')) {
126+
dir = dir.substring(1);
127+
}
128+
129+
try {
130+
// Call internal listBackups method from class
131+
const files = await onedrive.listBackups({ accessToken: od_accessToken, dir, types, log });
132+
133+
callback && callback(null, files, 'onedrive');
134+
} catch (error) {
135+
log.error(`Onedrive listBackups error: ${error}`);
136+
callback && callback(error);
137+
}
138+
139+
} else {
140+
callback && callback('No access token available');
141+
}
142+
} else {
143+
callback && callback();
144+
}
145+
}
88146

89147
async function getFile(options, fileName, toStoreName, log, callback) {
90148
const db_onedriveAccessJson = options.onedriveAccessJson !== undefined ? options.onedriveAccessJson : options.onedrive?.onedriveAccessJson || '';

lib/oneDriveLib.js

Lines changed: 73 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -203,29 +203,6 @@ class onedrive {
203203
throw new Error('Upload did not complete properly');
204204
}
205205

206-
async getMetadata({ accessToken, itemPath }) {
207-
const targetPath = itemPath === 'root' ? 'root' : `root:/${itemPath}`;
208-
const url = `https://graph.microsoft.com/v1.0/me/drive/${targetPath}`;
209-
210-
const response = await got.get(url, {
211-
headers: { Authorization: `Bearer ${accessToken}` },
212-
responseType: 'json'
213-
});
214-
215-
return response.body;
216-
}
217-
218-
async listChildren({ accessToken, itemId }) {
219-
const url = `https://graph.microsoft.com/v1.0/me/drive/items/${itemId}/children`;
220-
221-
const response = await got.get(url, {
222-
headers: { Authorization: `Bearer ${accessToken}` },
223-
responseType: 'json'
224-
});
225-
226-
return response.body;
227-
}
228-
229206
async downloadFile({ accessToken, dir, onlyFileName, downloadPath, log }) {
230207
try {
231208
const meta = await this.getMetadata({ accessToken, itemPath: dir });
@@ -305,6 +282,79 @@ class onedrive {
305282

306283
return response;
307284
}
285+
286+
async getMetadata({ accessToken, itemPath }) {
287+
const targetPath = itemPath === 'root' ? 'root' : `root:/${itemPath}`;
288+
const url = `https://graph.microsoft.com/v1.0/me/drive/${targetPath}`;
289+
290+
const response = await got.get(url, {
291+
headers: { Authorization: `Bearer ${accessToken}` },
292+
responseType: 'json'
293+
});
294+
295+
return response.body;
296+
}
297+
298+
async listChildren({ accessToken, itemId }) {
299+
const url = `https://graph.microsoft.com/v1.0/me/drive/items/${itemId}/children`;
300+
301+
const response = await got.get(url, {
302+
headers: { Authorization: `Bearer ${accessToken}` },
303+
responseType: 'json'
304+
});
305+
306+
return response.body;
307+
}
308+
309+
async listBackups({ accessToken, dir, types, log }) {
310+
try {
311+
// Normalize path
312+
let normalizedDir = (dir || '').replace(/\\/g, '/');
313+
if (!normalizedDir || normalizedDir === '/') {
314+
normalizedDir = 'root';
315+
} else {
316+
if (normalizedDir.startsWith('/')) {
317+
normalizedDir = normalizedDir.slice(1);
318+
}
319+
}
320+
321+
const metadata = await this.getMetadata({ accessToken, itemPath: normalizedDir });
322+
if (!metadata?.id) {
323+
throw new Error('Could not retrieve metadata');
324+
}
325+
326+
const childrenRes = await this.listChildren({ accessToken, itemId: metadata.id });
327+
let children = childrenRes?.value;
328+
if (!children) return null;
329+
330+
// Filter and transform children
331+
children = children
332+
.map(file => ({
333+
path: file.name,
334+
name: file.name,
335+
size: file.size,
336+
id: file.id
337+
}))
338+
.filter(file =>
339+
(types.includes(file.name.split('_')[0]) || types.includes(file.name.split('.')[0])) &&
340+
file.name.endsWith('.gz')
341+
);
342+
343+
// Group by type
344+
const files = {};
345+
for (const file of children) {
346+
const type = file.name.split('_')[0];
347+
if (!files[type]) files[type] = [];
348+
files[type].push(file);
349+
}
350+
351+
return files;
352+
353+
} catch (error) {
354+
log.error(`listBackups error: ${error.message}`);
355+
throw error;
356+
}
357+
}
308358
}
309359

310360
module.exports = onedrive;

0 commit comments

Comments
 (0)