Add webdav upload script and docs

This commit is contained in:
MassiveBox 2025-03-26 23:13:08 +01:00
parent d6c414f887
commit bf3e635a1c
Signed by: massivebox
GPG key ID: 9B74D3A59181947D
7 changed files with 426 additions and 36 deletions

View file

@ -1,12 +1 @@
<script is:inline>
var _paq = _paq || [];
_paq.push(["trackPageView"]), _paq.push(["enableLinkTracking"]),
function() {
_paq.push(["setTrackerUrl", "//stats.massive.box/sjprfvo.php"]);
_paq.push(["setSiteId", "1"]);
_paq.push(['disableAlwaysUseSendBeacon', 'true']);
var a = document, r = a.createElement("script"), s = a.getElementsByTagName("script")[0];
r.async = !0, r.defer = !0, r.src = "//stats.massive.box/fauqtkg.php", s.parentNode.insertBefore(r, s)
}();
</script>
<noscript><img src="//stats.massive.box/sjprfvo.php?dzp=1&opl=1" /></noscript>
<script defer src="https://stats.massive.box/p.js" data-website-id="30344937-ffdf-4a6a-ba52-dc60a23aa4f2"></script>

View file

@ -1,6 +1,12 @@
const http = require('node:http');
const fs = require('fs');
const path = require('path');
import dotenv from 'dotenv';
dotenv.config();
import http from 'node:http';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
async function fetch(url, method = "GET") {
return new Promise((resolve) => {
@ -172,16 +178,18 @@ async function clearBlogFolder() {
}
async function main() {
console.log("Finding JoplinClipperServer port...");
let host = "http://localhost:" + await findPort();
let apiToken = await authenticate(host);
//let apiToken = "";
let apiToken = process.env.JOPLIN_KEY;
if (!apiToken) {
apiToken = await authenticate(host);
}
let notes = await getNotebookNotes(apiToken, host);
await clearBlogFolder();
for(let noteId of notes) {
let noteBody = await getNoteBody(apiToken, host, noteId);
let slug = await getNoteSlug(noteBody);

96
src/utils/upload.mjs Normal file
View file

@ -0,0 +1,96 @@
import dotenv from 'dotenv';
dotenv.config();
import { createClient } from 'webdav';
import fs from 'fs';
import path from 'path';
const client = createClient(
process.env.WEBDAV_ENDPOINT,
{
username: process.env.WEBDAV_USERNAME,
password: process.env.WEBDAV_PASSWORD,
maxBodyLength: Infinity,
maxContentLength: Infinity,
headers: {
Accept: "*/*",
Connection: "keep-alive"
}
}
);
async function createDirectoryIfNotExist(remotePath) {
try {
const exists = await client.exists(remotePath);
if (!exists) {
await client.createDirectory(remotePath);
}
} catch (error) {
if (error.status !== 409 && error.status !== 405) {
throw error;
}
}
}
async function uploadDirectory(localPath, remoteBasePath) {
try {
const items = fs.readdirSync(localPath, { withFileTypes: true });
for (const item of items) {
const localItemPath = path.join(localPath, item.name);
const remoteItemPath = path.posix.join(remoteBasePath, item.name);
if (item.isDirectory()) {
// Create directory on WebDAV server
await createDirectoryIfNotExist(remoteItemPath);
console.log(`Verified directory: ${remoteItemPath}`);
await uploadDirectory(localItemPath, remoteItemPath);
} else {
// Upload file
console.log(`Uploading file: ${remoteItemPath}`);
const content = fs.readFileSync(localItemPath);
await client.putFileContents(remoteItemPath, content);
}
}
} catch (error) {
console.error(`Error processing ${localPath}:`, error.message);
throw error;
}
}
async function clearFolder(remotePath) {
try {
const items = await client.getDirectoryContents(remotePath);
for (const item of items) {
if (item.basename !== '..') {
const itemPath = path.posix.join(remotePath, item.basename);
await client.deleteFile(itemPath);
}
}
} catch (error) {
console.error('Error clearing root folder:', error.message);
throw error;
}
}
// Usage example
async function main() {
const localDir = './dist';
let remoteDir = process.env.WEBDAV_REMOTE_DIR;
if(remoteDir === undefined) {
remoteDir = '/';
}
try {
// Clear root folder
await clearFolder(remoteDir);
// Upload new content
await uploadDirectory(localDir, remoteDir);
console.log('Directory upload complete');
} catch (error) {
console.error('Upload failed:', error.message);
}
}
main();