Convert test WebDAV function into an actual reusable one

Now, if we want to upload something, we know whom to call ;-)
This commit is contained in:
Hippo 2019-12-24 14:50:13 +05:30
parent 2e9db3245f
commit 772c9000f8
2 changed files with 16 additions and 16 deletions

View file

@ -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
}

View file

@ -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 <username> <email>')
program.command('webdav-test <file>')
.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)