Compare commits

..

3 commits

Author SHA1 Message Date
Ell
cce6a44078 allow flattening directory structure 2023-07-03 23:12:02 +02:00
Ell
71ea8ddf31 also add tags to created directories 2023-07-03 22:47:36 +02:00
Ell
262ef59cc1 added plugin metadata 2023-07-03 19:29:36 +02:00
2 changed files with 82 additions and 55 deletions

View file

@ -1,3 +1,13 @@
---
name: Nextcloud Upload
authors: Ellpeck
description: Upload files to Nextcloud using chunking and optionally add tags to files
tags: [deploy, publish]
containerImage: ellpeck/woodpecker-nextcloud-upload
containerImageUrl: https://hub.docker.com/r/ellpeck/woodpecker-nextcloud-upload
url: https://github.com/Ellpeck/WoodpeckerPlugins/tree/main/nextcloud-upload
---
# Nextcloud Upload
Simple plugin to upload files to Nextcloud using chunking, based on a glob pattern and a destination location. Note that, since this uses Nextcloud's built-in chunking system, it likely doesn't work for other WebDAV applications.
@ -7,15 +17,19 @@ steps:
upload:
image: ellpeck/woodpecker-nextcloud-upload
settings:
# required settings
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: # 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 10485760, or 10 MiB
quiet: false # optional, whether to reduce output
tags: # optional, a set of tags to apply to uploaded files, tag is expected to already exist
# optional settings
basedir: "." # local base directory for files, defaults to .
chunksize: # chunk size in bytes, defaults to 10485760, or 10 MiB
quiet: false # whether to reduce output
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
```

View file

@ -24,6 +24,7 @@ const baseDir = process.env.PLUGIN_BASEDIR || ".";
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;
upload();
@ -74,7 +75,9 @@ async function upload() {
if (!files.length)
console.log(`No files found for pattern ${pattern}`);
for (let file of files) {
let dest = `${destEnv}/${file}`;
file = file.replaceAll("\\", "/");
let fileName = file.split("/").pop();
let dest = `${destEnv}/${flatten ? fileName : file}`;
// we have to explicitly create any directories that don't exist yet
// (https://github.com/shiftpi/nextcloud-chunk-file-upload/issues/22)
@ -92,70 +95,80 @@ async function upload() {
// 405 means the directory already exists
validateStatus: s => s == 201 || s == 405
});
if (response.status != 405 && !quiet)
console.log(`Created directory ${currDir}`);
if (response.status != 405) {
if (!quiet)
console.log(`Created directory ${dir}`);
// add tags to directory too
if (tagIds)
await addTags(basePath, tagIds, dir, currDir);
}
} catch (error) {
console.log(`Failed to create directory ${currDir} (${error})`);
console.log(`Failed to create directory ${dir} (${error})`);
process.exit(1);
}
}
// use lib to upload file
if (!quiet)
console.log(`Uploading ${file} to ${dest}`);
console.log(`Uploading ${fileName} to ${dest}`);
await upload.uploadFile(`${baseDir}/${file}`, dest, parseInt(chunkSizeEnv)).then(e => {
if (!quiet)
console.log(`Uploaded ${file} to ${dest}`);
console.log(`Uploaded ${fileName} to ${dest}`);
}).catch(e => {
console.log(`Failed to upload file ${file} to ${dest} (${e})`);
console.log(`Failed to upload ${fileName} to ${dest} (${e})`);
process.exit(1);
});
// add tags
if (tagIds.size) {
try {
// get file id: https://docs.nextcloud.com/server/latest/developer_manual/client_apis/WebDAV/basic.html#requesting-properties
let response = await axios.request({
method: "propfind",
url: `${basePath}/files/${userEnv}/${dest}`,
auth: {
username: userEnv,
password: tokenEnv
},
data: `<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">
<d:prop>
<oc:fileid/>
</d:prop>
</d:propfind>`
});
var 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 file ${file} is ${fileId}`);
// add tags: https://doc.owncloud.com/server/next/developer_manual/webdav_api/tags.html#assign-a-tag-to-a-file
for (let [tag, tagId] of tagIds.entries()) {
await axios.request({
method: "put",
url: `${basePath}/systemtags-relations/files/${fileId}/${tagId}`,
auth: {
username: userEnv,
password: tokenEnv
},
// 409 conflicted means the tag is already applied
validateStatus: s => s == 201 || s == 409
});
if (!quiet)
console.log(`Added tag ${tag} to file ${file}`);
}
} catch (error) {
console.log(`Failed to assign tags ${tags} to file ${file} (${error})`);
process.exit(1);
}
}
if (tagIds.size)
await addTags(basePath, tagIds, fileName, dest);
}
}
}
async function addTags(basePath, tagIds, fileName, location) {
try {
// get file id: https://docs.nextcloud.com/server/latest/developer_manual/client_apis/WebDAV/basic.html#requesting-properties
let response = await axios.request({
method: "propfind",
url: `${basePath}/files/${userEnv}/${location}`,
auth: {
username: userEnv,
password: tokenEnv
},
data: `<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">
<d:prop>
<oc:fileid/>
</d:prop>
</d:propfind>`
});
var 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}`);
// add tags: https://doc.owncloud.com/server/next/developer_manual/webdav_api/tags.html#assign-a-tag-to-a-file
for (let [tag, tagId] of tagIds.entries()) {
await axios.request({
method: "put",
url: `${basePath}/systemtags-relations/files/${fileId}/${tagId}`,
auth: {
username: userEnv,
password: tokenEnv
},
// 409 conflicted means the tag is already applied
validateStatus: s => s == 201 || s == 409
});
if (!quiet)
console.log(`Added tag ${tag} to ${fileName}`);
}
} catch (error) {
console.log(`Failed to assign tags ${tags} to ${fileName} (${error})`);
process.exit(1);
}
}