diff --git a/.gitignore b/.gitignore index 3c3629e..eb79dd5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.idea diff --git a/nextcloud-upload/README.md b/nextcloud-upload/README.md index ba156f0..a115fdb 100644 --- a/nextcloud-upload/README.md +++ b/nextcloud-upload/README.md @@ -32,4 +32,6 @@ steps: tags: # a set of tags to apply to uploaded files, tag is expected to already exist - mytag flatten: false # whether to flatten directories, causing all files to be placed directly in dest + retentionamount: 7 # amount of children that retentionbase is allowed to have before oldest ones are deleted on upload + retentionbase: Uploads # directory that the retentionamount applies to ``` diff --git a/nextcloud-upload/run.js b/nextcloud-upload/run.js index 1371549..66c9dc0 100644 --- a/nextcloud-upload/run.js +++ b/nextcloud-upload/run.js @@ -25,12 +25,53 @@ const chunkSizeEnv = process.env.PLUGIN_CHUNKSIZE || 10 * 1024 * 1024; const quiet = process.env.PLUGIN_QUIET || false; const tags = process.env.PLUGIN_TAGS || ""; const flatten = process.env.PLUGIN_FLATTEN || false; +const retentionBase = process.env.PLUGIN_RETENTIONBASE || ""; +const retentionAmount = process.env.PLUGIN_RETENTIONAMOUNT || 0; upload(); async function upload() { let basePath = `${serverEnv}/remote.php/dav`; + // delete files or directories that exceed the retention amount + if (retentionBase && retentionAmount) { + try { + let retentionPath = `${basePath}/files/${userEnv}/${retentionBase}`; + let response = await axios.request({ + method: "propfind", + url: retentionPath, + auth: { + username: userEnv, + password: tokenEnv + }, + // 404 means the directory doesn't exist, which is fine + validateStatus: s => s == 207 || s == 404 + }); + if (response.status != 404) { + let data = JSON.parse(xml.toJson(response.data)); + let dirs = data["d:multistatus"]["d:response"].slice(1); + // sort directories by last modified + dirs.sort((a, b) => new Date(a["d:propstat"]["d:prop"]["d:getlastmodified"]) - new Date(b["d:propstat"]["d:prop"]["d:getlastmodified"])); + while (dirs.length > parseInt(retentionAmount)) { + let dir = `${serverEnv}${dirs[0]["d:href"]}`; + await axios.request({ + method: "delete", + url: dir, + auth: { + username: userEnv, + password: tokenEnv + } + }); + console.log(`Deleted directory ${dir.substring(retentionPath.length + 1)} because retention amount of ${retentionAmount} was reached`); + dirs.splice(0, 1); + } + } + } catch (e) { + console.log(`Failed to delete old directories (${e})`); + process.exit(1); + } + } + // find ids for the tags we want to assign later let tagIds = new Map(); if (tags) { @@ -52,7 +93,7 @@ async function upload() { ` }); - var data = JSON.parse(xml.toJson(response.data))["d:multistatus"]["d:response"].map(e => e["d:propstat"]["d:prop"]); + let data = JSON.parse(xml.toJson(response.data))["d:multistatus"]["d:response"].map(e => e["d:propstat"]["d:prop"]); for (let tag of tags.split(",")) { let entry = data.find(e => e["oc:display-name"] == tag); if (!entry) { @@ -146,7 +187,7 @@ async function addTags(basePath, tagIds, fileName, location) { ` }); - var data = JSON.parse(xml.toJson(response.data)); + let data = JSON.parse(xml.toJson(response.data)); let fileId = data["d:multistatus"]["d:response"]["d:propstat"]["d:prop"]["oc:fileid"]; if (!quiet) console.log(`File id of ${fileName} is ${fileId}`); diff --git a/nextcloud-upload/test.sh b/nextcloud-upload/test.sh index 234628a..a0da691 100644 --- a/nextcloud-upload/test.sh +++ b/nextcloud-upload/test.sh @@ -1,8 +1,9 @@ export PLUGIN_SERVER=https://cloud.ellpeck.de export PLUGIN_USER=EllBot -export PLUGIN_FILES=Dockerfile -export PLUGIN_DEST=Test/Testing -export PLUGIN_TAGS="Autoremove 1D" +export PLUGIN_FILES=**/*.md +export PLUGIN_DEST=Uploads/$(date '+%M:%S') +export PLUGIN_RETENTION_BASE=Uploads +export PLUGIN_RETENTION_AMOUNT=3 npm install node run.js