From 772c9000f8ad94a4e41ab4cfc064accf85c28e58 Mon Sep 17 00:00:00 2001 From: Hippo Date: Tue, 24 Dec 2019 14:50:13 +0530 Subject: [PATCH] Convert test WebDAV function into an actual reusable one Now, if we want to upload something, we know whom to call ;-) --- functions.js | 19 +++++-------------- index.js | 13 +++++++++++-- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/functions.js b/functions.js index e49e97b..ffa30ee 100644 --- a/functions.js +++ b/functions.js @@ -176,7 +176,7 @@ const createDirIfNotExist = async (client, folder) => { * function [createUser] * @returns [string] status */ -const webdavTest = async (filePath) => { +const uploadDav = async (dirPath, filePath) => { // connect to webdav client = createClient( @@ -187,24 +187,15 @@ const webdavTest = async (filePath) => { digest: process.env.WEBDAV_USE_DIGEST == 'true' }) - // decide path - current_date = new Date(); - var dir_path = path.join( - process.env.WEBDAV_PATH_PREFIX, - current_date.getUTCFullYear().toString(), - current_date.getUTCMonth().toString(), - 'test' // TODO: replace with article slug - ) - // create directory if not exists - console.debug(`Loading ${dir_path}`) - await createDirIfNotExist(client, dir_path) + console.debug(`Loading ${dirPath}`) + await createDirIfNotExist(client, dirPath) // upload a file console.debug('Uploading file') s = fs.createReadStream(filePath) .pipe(client.createWriteStream( - path.join(dir_path, path.basename(filePath)) + path.join(dirPath, path.basename(filePath)) )) .on('finish', () => console.debug('Uploaded successfully.')) @@ -216,5 +207,5 @@ module.exports = { pushToGhost, mediumToGhost, generateUserData, - webdavTest + uploadDav } diff --git a/index.js b/index.js index 6ce12e8..90c87da 100755 --- a/index.js +++ b/index.js @@ -5,13 +5,14 @@ const dotenv = require('dotenv') dotenv.config() const program = require('commander'); +const path = require('path') const { fetchFromMedium, pushToGhost, mediumToGhost, generateUserData, - webdavTest, + uploadDav, } = require ('./functions'); program @@ -56,7 +57,15 @@ program.command('create-user ') program.command('webdav-test ') .description('[test command] upload stuff to WebDAV') .action(async (file) => { - webdavTest(file); + // decide path + current_date = new Date(); + var dir_path = path.join( + process.env.WEBDAV_PATH_PREFIX, + current_date.getUTCFullYear().toString(), + current_date.getUTCMonth().toString(), + 'test' // TODO: replace with article slug + ) + uploadDav(dir_path, file); }); program.parse(process.argv)