mirror of
https://github.com/Ellpeck/WoodpeckerPlugins.git
synced 2024-11-27 04:28:34 +01:00
Compare commits
3 commits
ed93bf95fb
...
cce6a44078
Author | SHA1 | Date | |
---|---|---|---|
cce6a44078 | |||
71ea8ddf31 | |||
262ef59cc1 |
2 changed files with 82 additions and 55 deletions
|
@ -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
|
||||
```
|
||||
|
|
|
@ -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,32 +95,46 @@ 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) {
|
||||
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}/${dest}`,
|
||||
url: `${basePath}/files/${userEnv}/${location}`,
|
||||
auth: {
|
||||
username: userEnv,
|
||||
password: tokenEnv
|
||||
|
@ -132,7 +149,7 @@ async function upload() {
|
|||
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}`);
|
||||
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()) {
|
||||
|
@ -147,15 +164,11 @@ async function upload() {
|
|||
validateStatus: s => s == 201 || s == 409
|
||||
});
|
||||
if (!quiet)
|
||||
console.log(`Added tag ${tag} to file ${file}`);
|
||||
console.log(`Added tag ${tag} to ${fileName}`);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.log(`Failed to assign tags ${tags} to file ${file} (${error})`);
|
||||
console.log(`Failed to assign tags ${tags} to ${fileName} (${error})`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue