63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
import { createClient, type BufferLike } from "webdav";
|
|
import fs from "fs/promises";
|
|
import path from "path";
|
|
|
|
export const REMOTE_DIR = "/";
|
|
export const LOCAL_DIR = path.resolve("./downloads");
|
|
|
|
export async function downloadMissing(remoteDir: string = REMOTE_DIR) {
|
|
await fs.mkdir(LOCAL_DIR, { recursive: true })
|
|
const client = createClient(
|
|
"https://nextcloud.papryk.com/public.php/dav/files/ckkL3Pg4X4Ye6pz",
|
|
{
|
|
username: "ckkL3Pg4X4Ye6pz",
|
|
password: "",
|
|
}
|
|
);
|
|
const contents = await client.getDirectoryContents(remoteDir);
|
|
|
|
const exisitngFiles = await fs.readdir(LOCAL_DIR);
|
|
|
|
const missing = contents.filter(f => !exisitngFiles.includes(f.basename))
|
|
|
|
console.log(missing)
|
|
|
|
let i = 0;
|
|
for (const item of missing) {
|
|
const result = await client.getFileContents(item.filename, {
|
|
format: "binary",
|
|
});
|
|
|
|
let buffer: Buffer;
|
|
|
|
if (typeof result === "string") {
|
|
buffer = Buffer.from(result);
|
|
} else if (Buffer.isBuffer(result)) {
|
|
buffer = result;
|
|
} else if (result instanceof ArrayBuffer) {
|
|
buffer = Buffer.from(new Uint8Array(result));
|
|
} else if ("data" in result) {
|
|
const data = result.data;
|
|
if (typeof data === "string") {
|
|
buffer = Buffer.from(data);
|
|
} else if (Buffer.isBuffer(data)) {
|
|
buffer = data;
|
|
} else if (data instanceof ArrayBuffer) {
|
|
buffer = Buffer.from(new Uint8Array(data));
|
|
} else {
|
|
buffer = Buffer.from(data as any);
|
|
}
|
|
} else {
|
|
buffer = Buffer.from(result as any);
|
|
}
|
|
|
|
const localPath = path.resolve(path.join(LOCAL_DIR, item.filename))
|
|
|
|
await fs.writeFile(localPath, buffer);
|
|
i++;
|
|
console.log(`downloaded ${i}/${missing.length} ${item.basename}`)
|
|
}
|
|
|
|
|
|
}
|