Compare commits

...

2 commits

Author SHA1 Message Date
Ell
b190def36f readme cleanup 2023-07-16 22:13:50 +02:00
Ell
6446152ed3 added retention amount feature to nextcloud-upload 2023-07-16 22:12:04 +02:00
4 changed files with 53 additions and 6 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
node_modules node_modules
.idea

View file

@ -25,7 +25,11 @@ steps:
- "**/*.md" - "**/*.md"
dest: Uploads/CoolMarkdownFiles # the destination directory dest: Uploads/CoolMarkdownFiles # the destination directory
# optional settings # optional retention settings, useful if old builds should be deleted automatically
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
# misc optional settings
basedir: "." # local base directory for files, defaults to . basedir: "." # local base directory for files, defaults to .
chunksize: # chunk size in bytes, defaults to 10485760, or 10 MiB chunksize: # chunk size in bytes, defaults to 10485760, or 10 MiB
quiet: false # whether to reduce output quiet: false # whether to reduce output

View file

@ -25,12 +25,53 @@ const chunkSizeEnv = process.env.PLUGIN_CHUNKSIZE || 10 * 1024 * 1024;
const quiet = process.env.PLUGIN_QUIET || false; const quiet = process.env.PLUGIN_QUIET || false;
const tags = process.env.PLUGIN_TAGS || ""; const tags = process.env.PLUGIN_TAGS || "";
const flatten = process.env.PLUGIN_FLATTEN || false; const flatten = process.env.PLUGIN_FLATTEN || false;
const retentionBase = process.env.PLUGIN_RETENTIONBASE || "";
const retentionAmount = process.env.PLUGIN_RETENTIONAMOUNT || 0;
upload(); upload();
async function upload() { async function upload() {
let basePath = `${serverEnv}/remote.php/dav`; 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 // find ids for the tags we want to assign later
let tagIds = new Map(); let tagIds = new Map();
if (tags) { if (tags) {
@ -52,7 +93,7 @@ async function upload() {
</d:prop> </d:prop>
</d:propfind>` </d:propfind>`
}); });
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(",")) { for (let tag of tags.split(",")) {
let entry = data.find(e => e["oc:display-name"] == tag); let entry = data.find(e => e["oc:display-name"] == tag);
if (!entry) { if (!entry) {
@ -146,7 +187,7 @@ async function addTags(basePath, tagIds, fileName, location) {
</d:prop> </d:prop>
</d:propfind>` </d:propfind>`
}); });
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"]; let fileId = data["d:multistatus"]["d:response"]["d:propstat"]["d:prop"]["oc:fileid"];
if (!quiet) if (!quiet)
console.log(`File id of ${fileName} is ${fileId}`); console.log(`File id of ${fileName} is ${fileId}`);

View file

@ -1,8 +1,9 @@
export PLUGIN_SERVER=https://cloud.ellpeck.de export PLUGIN_SERVER=https://cloud.ellpeck.de
export PLUGIN_USER=EllBot export PLUGIN_USER=EllBot
export PLUGIN_FILES=Dockerfile export PLUGIN_FILES=**/*.md
export PLUGIN_DEST=Test/Testing export PLUGIN_DEST=Uploads/$(date '+%M:%S')
export PLUGIN_TAGS="Autoremove 1D" export PLUGIN_RETENTION_BASE=Uploads
export PLUGIN_RETENTION_AMOUNT=3
npm install npm install
node run.js node run.js