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 # 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. 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: upload:
image: ellpeck/woodpecker-nextcloud-upload image: ellpeck/woodpecker-nextcloud-upload
settings: settings:
# required settings
server: https://cloud.ellpeck.de # the server to use server: https://cloud.ellpeck.de # the server to use
user: EllBot # the user user: EllBot # the user
token: access-token # the access token, or password if 2FA is disabled token: access-token # the access token, or password if 2FA is disabled
files: # the file(s), uses glob patterns files: # the file(s), uses glob patterns
- "**/*.md" - "**/*.md"
dest: Uploads/CoolMarkdownFiles # the destination directory dest: Uploads/CoolMarkdownFiles # the destination directory
basedir: "." # optional, local base directory for files, defaults to .
# optional settings chunksize: # optional, chunk size in bytes, defaults to 10485760, or 10 MiB
basedir: "." # local base directory for files, defaults to . quiet: false # optional, whether to reduce output
chunksize: # chunk size in bytes, defaults to 10485760, or 10 MiB tags: # optional, a set of tags to apply to uploaded files, tag is expected to already exist
quiet: false # whether to reduce output
tags: # a set of tags to apply to uploaded files, tag is expected to already exist
- mytag - 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 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;
upload(); upload();
@ -75,9 +74,7 @@ async function upload() {
if (!files.length) if (!files.length)
console.log(`No files found for pattern ${pattern}`); console.log(`No files found for pattern ${pattern}`);
for (let file of files) { for (let file of files) {
file = file.replaceAll("\\", "/"); let dest = `${destEnv}/${file}`;
let fileName = file.split("/").pop();
let dest = `${destEnv}/${flatten ? fileName : file}`;
// we have to explicitly create any directories that don't exist yet // we have to explicitly create any directories that don't exist yet
// (https://github.com/shiftpi/nextcloud-chunk-file-upload/issues/22) // (https://github.com/shiftpi/nextcloud-chunk-file-upload/issues/22)
@ -95,46 +92,32 @@ async function upload() {
// 405 means the directory already exists // 405 means the directory already exists
validateStatus: s => s == 201 || s == 405 validateStatus: s => s == 201 || s == 405
}); });
if (response.status != 405) { if (response.status != 405 && !quiet)
if (!quiet) console.log(`Created directory ${currDir}`);
console.log(`Created directory ${dir}`);
// add tags to directory too
if (tagIds)
await addTags(basePath, tagIds, dir, currDir);
}
} catch (error) { } catch (error) {
console.log(`Failed to create directory ${dir} (${error})`); console.log(`Failed to create directory ${currDir} (${error})`);
process.exit(1); process.exit(1);
} }
} }
// use lib to upload file // use lib to upload file
if (!quiet) if (!quiet)
console.log(`Uploading ${fileName} to ${dest}`); console.log(`Uploading ${file} to ${dest}`);
await upload.uploadFile(`${baseDir}/${file}`, dest, parseInt(chunkSizeEnv)).then(e => { await upload.uploadFile(`${baseDir}/${file}`, dest, parseInt(chunkSizeEnv)).then(e => {
if (!quiet) if (!quiet)
console.log(`Uploaded ${fileName} to ${dest}`); console.log(`Uploaded ${file} to ${dest}`);
}).catch(e => { }).catch(e => {
console.log(`Failed to upload ${fileName} to ${dest} (${e})`); console.log(`Failed to upload file ${file} to ${dest} (${e})`);
process.exit(1); process.exit(1);
}); });
// add tags // add tags
if (tagIds.size) if (tagIds.size) {
await addTags(basePath, tagIds, fileName, dest);
}
}
}
async function addTags(basePath, tagIds, fileName, location) {
try { try {
// get file id: https://docs.nextcloud.com/server/latest/developer_manual/client_apis/WebDAV/basic.html#requesting-properties // get file id: https://docs.nextcloud.com/server/latest/developer_manual/client_apis/WebDAV/basic.html#requesting-properties
let response = await axios.request({ let response = await axios.request({
method: "propfind", method: "propfind",
url: `${basePath}/files/${userEnv}/${location}`, url: `${basePath}/files/${userEnv}/${dest}`,
auth: { auth: {
username: userEnv, username: userEnv,
password: tokenEnv password: tokenEnv
@ -149,7 +132,7 @@ async function addTags(basePath, tagIds, fileName, location) {
var data = JSON.parse(xml.toJson(response.data)); var 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 file ${file} is ${fileId}`);
// add tags: https://doc.owncloud.com/server/next/developer_manual/webdav_api/tags.html#assign-a-tag-to-a-file // 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()) { for (let [tag, tagId] of tagIds.entries()) {
@ -164,11 +147,15 @@ async function addTags(basePath, tagIds, fileName, location) {
validateStatus: s => s == 201 || s == 409 validateStatus: s => s == 201 || s == 409
}); });
if (!quiet) if (!quiet)
console.log(`Added tag ${tag} to ${fileName}`); console.log(`Added tag ${tag} to file ${file}`);
} }
} catch (error) { } 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); process.exit(1);
} }
}
}
}
} }