Skip to content

Commit 7d20a42

Browse files
committed
(simatec) Download for Onedrive with local API added
1 parent cb0ca9e commit 7d20a42

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

lib/list/onedrive.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22
const fs = require('node:fs');
3-
const oneDriveAPI = require('onedrive-api');
3+
//const oneDriveAPI = require('onedrive-api');
44
const Onedrive = require('../oneDriveLib');
5-
const got = require('@esm2cjs/got').default;
5+
//const got = require('@esm2cjs/got').default;
66

77
let od_accessToken;
88

@@ -143,7 +143,7 @@ async function list(restoreSource, options, types, log, callback) {
143143
callback && callback();
144144
}
145145
}
146-
146+
/*
147147
async function getFile(options, fileName, toStoreName, log, callback) {
148148
const db_onedriveAccessJson = options.onedriveAccessJson !== undefined ? options.onedriveAccessJson : options.onedrive?.onedriveAccessJson || '';
149149
const db_dir = options.dir !== undefined ? options.dir : options.onedrive?.dir || '/';
@@ -243,6 +243,40 @@ async function getFile(options, fileName, toStoreName, log, callback) {
243243
callback && callback('Not configured');
244244
}
245245
}
246+
*/
247+
248+
async function getFile(options, fileName, toStoreName, log, callback) {
249+
const db_onedriveAccessJson = options.onedriveAccessJson ?? options.onedrive?.onedriveAccessJson ?? '';
250+
const db_dir = options.dir ?? options.onedrive?.dir ?? '/';
251+
const db_ownDir = options.ownDir ?? options.onedrive?.ownDir ?? false;
252+
const db_dirMinimal = options.dirMinimal ?? options.onedrive?.dirMinimal ?? '/';
253+
254+
const onedrive = new Onedrive();
255+
const od_accessToken = await onedrive.getToken(db_onedriveAccessJson, log).catch(err => log.warn(`OneDrive Token: ${err}`));
256+
257+
if (!od_accessToken) {
258+
callback?.('Not configured');
259+
return;
260+
}
261+
262+
try {
263+
const dir = db_ownDir ? db_dirMinimal : db_dir;
264+
const onlyFileName = fileName.split('/').pop();
265+
266+
await onedrive.downloadFileByName({
267+
accessToken: od_accessToken,
268+
dir,
269+
fileName: onlyFileName,
270+
targetPath: toStoreName,
271+
log
272+
});
273+
274+
callback?.();
275+
} catch (err) {
276+
log.error(`OneDrive: ${err.message}`);
277+
callback?.(err);
278+
}
279+
}
246280

247281
module.exports = {
248282
list,

lib/oneDriveLib.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,67 @@ class onedrive {
355355
throw error;
356356
}
357357
}
358+
359+
async downloadFileByName({ accessToken, dir, fileName, targetPath, log }) {
360+
try {
361+
// Normalize directory path
362+
let normalizedDir = (dir || '').replace(/\\/g, '/');
363+
if (!normalizedDir || normalizedDir === '/') {
364+
normalizedDir = 'root';
365+
} else if (normalizedDir.startsWith('/')) {
366+
normalizedDir = normalizedDir.slice(1);
367+
}
368+
369+
// Get metadata of the directory to find its ID
370+
const metadata = await this.getMetadata({
371+
accessToken,
372+
itemPath: normalizedDir
373+
});
374+
375+
if (!metadata?.id) {
376+
throw new Error(`Could not retrieve metadata for path "${normalizedDir}"`);
377+
}
378+
379+
// List all files inside the directory
380+
const childrenRes = await this.listChildren({
381+
accessToken,
382+
itemId: metadata.id
383+
});
384+
385+
const children = childrenRes?.value || [];
386+
const file = children.find(f => f.name === fileName);
387+
388+
if (!file) {
389+
throw new Error(`File "${fileName}" not found in OneDrive`);
390+
}
391+
392+
const downloadUrl = file['@microsoft.graph.downloadUrl'];
393+
if (!downloadUrl) {
394+
throw new Error(`Download URL missing for "${fileName}"`);
395+
}
396+
397+
log.debug(`OneDrive: Download of "${fileName}" started`);
398+
399+
// Stream download to local file
400+
await new Promise((resolve, reject) => {
401+
const writeStream = fs.createWriteStream(targetPath);
402+
writeStream.on('finish', () => {
403+
log.debug(`OneDrive: Download of "${fileName}" finished`);
404+
resolve();
405+
});
406+
writeStream.on('error', reject);
407+
408+
got.stream(downloadUrl)
409+
.on('error', reject)
410+
.pipe(writeStream);
411+
});
412+
413+
} catch (err) {
414+
log.error(`downloadFileByName error: ${err.message}`);
415+
throw err;
416+
}
417+
}
418+
358419
}
359420

360421
module.exports = onedrive;

0 commit comments

Comments
 (0)