From 219a1aa751dbb00a6e8e0ed35b513a8f0381c91e Mon Sep 17 00:00:00 2001 From: Hippo Date: Tue, 24 Dec 2019 20:30:01 +0530 Subject: [PATCH] Upload profile pic during user creation --- functions.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/functions.js b/functions.js index ca362de..37db245 100644 --- a/functions.js +++ b/functions.js @@ -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)) };