Compare commits

..

No commits in common. "cce6a44078f45c16b873462b5abecf5282b287af" and "ed93bf95fb648a7dc8a10e6994989ecc55a08ec0" have entirely different histories.

2 changed files with 55 additions and 82 deletions

View file

@ -1,13 +1,3 @@
---
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.
@ -17,19 +7,15 @@ 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
# 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
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
- mytag
flatten: false # whether to flatten directories, causing all files to be placed directly in dest
```

View file

@ -24,7 +24,6 @@ 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();
@ -75,9 +74,7 @@ async function upload() {
if (!files.length)
console.log(`No files found for pattern ${pattern}`);
for (let file of files) {
file = file.replaceAll("\\", "/");
let fileName = file.split("/").pop();
let dest = `${destEnv}/${flatten ? fileName : file}`;
let dest = `${destEnv}/${file}`;
// we have to explicitly create any directories that don't exist yet
// (https://github.com/shiftpi/nextcloud-chunk-file-upload/issues/22)
@ -95,46 +92,32 @@ async function upload() {
// 405 means the directory already exists
validateStatus: s => s == 201 || s == 405
});
if (response.status != 405) {
if (!quiet)
console.log(`Created directory ${dir}`);
// add tags to directory too
if (tagIds)
await addTags(basePath, tagIds, dir, currDir);
}
if (response.status != 405 && !quiet)
console.log(`Created directory ${currDir}`);
} catch (error) {
console.log(`Failed to create directory ${dir} (${error})`);
console.log(`Failed to create directory ${currDir} (${error})`);
process.exit(1);
}
}
// use lib to upload file
if (!quiet)
console.log(`Uploading ${fileName} to ${dest}`);
console.log(`Uploading ${file} to ${dest}`);
await upload.uploadFile(`${baseDir}/${file}`, dest, parseInt(chunkSizeEnv)).then(e => {
if (!quiet)
console.log(`Uploaded ${fileName} to ${dest}`);
console.log(`Uploaded ${file} to ${dest}`);
}).catch(e => {
console.log(`Failed to upload ${fileName} to ${dest} (${e})`);
console.log(`Failed to upload file ${file} to ${dest} (${e})`);
process.exit(1);
});
// add tags
if (tagIds.size)
await addTags(basePath, tagIds, fileName, dest);
}
}
}
async function addTags(basePath, tagIds, fileName, location) {
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}/${location}`,
url: `${basePath}/files/${userEnv}/${dest}`,
auth: {
username: userEnv,
password: tokenEnv
@ -149,7 +132,7 @@ async function addTags(basePath, tagIds, fileName, location) {
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}`);
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()) {
@ -164,11 +147,15 @@ async function addTags(basePath, tagIds, fileName, location) {
validateStatus: s => s == 201 || s == 409
});
if (!quiet)
console.log(`Added tag ${tag} to ${fileName}`);
console.log(`Added tag ${tag} to file ${file}`);
}
} catch (error) {
console.log(`Failed to assign tags ${tags} to ${fileName} (${error})`);
console.log(`Failed to assign tags ${tags} to file ${file} (${error})`);
process.exit(1);
}
}
}
}
}