From 9472407096e11e6eabbef3cecad7fb4e76d3797d Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 2 Jul 2023 17:00:41 +0200 Subject: [PATCH] allow multiple glob patterns --- nextcloud-upload/README.md | 3 +- nextcloud-upload/run.js | 74 +++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/nextcloud-upload/README.md b/nextcloud-upload/README.md index 289967f..3518234 100644 --- a/nextcloud-upload/README.md +++ b/nextcloud-upload/README.md @@ -10,7 +10,8 @@ steps: server: https://cloud.ellpeck.de # the server to use user: EllBot # the user token: access-token # the access token, or password if 2FA is disabled - files: "**/*.md" # the file(s), uses glob pattern + files: # the file(s), uses glob patterns + - "**/*.md" dest: Uploads/CoolMarkdownFiles # the destination directory basedir: "." # optional, local base directory for files, defaults to . chunksize: # optional, chunk size in bytes, defaults to 52428800 diff --git a/nextcloud-upload/run.js b/nextcloud-upload/run.js index 300971e..07724c8 100644 --- a/nextcloud-upload/run.js +++ b/nextcloud-upload/run.js @@ -28,42 +28,44 @@ upload(); async function upload() { let basePath = `${serverEnv}/remote.php/dav`; const upload = new Upload(basePath, userEnv, userEnv, tokenEnv); - let files = await glob.glob(filesEnv, { cwd: baseDir }); - if (!files.length) - console.log("No files to upload"); - for (let file of files) { - let dest = `${destEnv}/${file}`; - if (!quiet) - console.log(`Uploading ${file} to ${dest}`); - - // we have to explicitly create any directories that don't exist yet - // (https://github.com/shiftpi/nextcloud-chunk-file-upload/issues/22) - let currDir = ""; - for (let dir of dest.split("/").slice(0, -1)) { - currDir += `${dir}/`; - try { - await axios.request({ - method: 'mkcol', - url: `${basePath}/files/${userEnv}/${currDir}`, - auth: { - username: userEnv, - password: tokenEnv - } - }); - if (!quiet) - console.log(`Created directory ${currDir}`); - } catch (error) { - // this is fine since the directory likely already exists - } - } - - // use lib to upload file - await upload.uploadFile(`${baseDir}/${file}`, dest, chunkSizeEnv).then(e => { + for (let pattern of filesEnv.split(",")) { + let files = await glob.glob(pattern, { cwd: baseDir }); + if (!files.length) + console.log("No files to upload"); + for (let file of files) { + let dest = `${destEnv}/${file}`; if (!quiet) - console.log(`Uploaded ${file} to ${dest} (${e})`); - }).catch(e => { - console.log(`Failed to upload file ${file} to ${dest} (${e}, error ${e.httpErrorCode}, ${e.httpErrorMessage})`); - process.exit(1); - }); + console.log(`Uploading ${file} to ${dest}`); + + // we have to explicitly create any directories that don't exist yet + // (https://github.com/shiftpi/nextcloud-chunk-file-upload/issues/22) + let currDir = ""; + for (let dir of dest.split("/").slice(0, -1)) { + currDir += `${dir}/`; + try { + await axios.request({ + method: 'mkcol', + url: `${basePath}/files/${userEnv}/${currDir}`, + auth: { + username: userEnv, + password: tokenEnv + } + }); + if (!quiet) + console.log(`Created directory ${currDir}`); + } catch (error) { + // this is fine since the directory likely already exists + } + } + + // use lib to upload file + await upload.uploadFile(`${baseDir}/${file}`, dest, chunkSizeEnv).then(e => { + if (!quiet) + console.log(`Uploaded ${file} to ${dest} (${e})`); + }).catch(e => { + console.log(`Failed to upload file ${file} to ${dest} (${e}, error ${e.httpErrorCode}, ${e.httpErrorMessage})`); + process.exit(1); + }); + } } }