Upload profile pic during user creation

This commit is contained in:
Hippo 2019-12-24 20:30:01 +05:30
parent 95f48b24ba
commit 219a1aa751

View file

@ -5,6 +5,8 @@ const getPost = require('mediumexporter').getPost
const { createClient } = require('webdav')
const readline = require('readline')
const MEDIUM_IMG_CDN = 'https://miro.medium.com/fit/c/'
/**
* function [fetchFromMedium]
* @returns [string] status
@ -177,6 +179,26 @@ const generateUserData = async (mediumUsername, email) => {
console.debug(`Name: ${json.payload.user.name}`)
console.debug(`Bio: ${json.payload.user.bio}`)
// Download and upload image
let imageId = json.payload.user.imageId
console.log(`Image: ${imageId}`)
let imagePath = MEDIUM_IMG_CDN + '256/256/' + imageId
let filetype = imageId.split('.')[imageId.split('.').length - 1]
let fileName = `${mediumUsername}.${filetype}`
let filePath = path.join(process.env.PWD, fileName)
console.log(`Fetching: ${imagePath}`)
const response = await (await r2.get(imagePath).response).buffer()
await fs.writeFileSync(filePath, response, 'base64')
console.log("Uploading to server")
await uploadDav(path.join(process.env.WEBDAV_PATH_PREFIX,'avatars'),
filePath)
// Generate Ghost JSON
const ghostData = {
@ -188,6 +210,7 @@ const generateUserData = async (mediumUsername, email) => {
bio: json.payload.user.bio,
email: email,
name: json.payload.user.name,
profile_image: process.env.WEBDAV_UPLOADED_PATH_PREFIX + '/avatars/' + fileName
}
]
},
@ -197,7 +220,6 @@ const generateUserData = async (mediumUsername, email) => {
}
}
return(JSON.stringify(ghostData))
};