Implement "image upload" part of push-ghost command

This commit is contained in:
Hippo 2019-12-24 14:50:48 +05:30
parent 772c9000f8
commit 95f48b24ba

View file

@ -3,6 +3,7 @@ const path = require('path')
const fs = require('fs')
const getPost = require('mediumexporter').getPost
const { createClient } = require('webdav')
const readline = require('readline')
/**
* function [fetchFromMedium]
@ -66,6 +67,80 @@ const fetchFromMedium = async (mediumUrl) => {
*/
const pushToGhost = (postSlug) => {
console.info('Pushing: ' + postSlug);
// Decide working path
postFolder = path.resolve('content/' + postSlug)
// Verify file exists
if (!fs.existsSync(postFolder)) {
console.error('Could not find post folder! Is it fetched?')
return false
}
// Decide file
const postContent = path.join(postFolder, 'index.md')
const postOutput = path.join(postFolder, 'ghost.md')
// Verify post exists
if (!fs.existsSync(postContent)) {
console.error("Could not find 'index.md' in " + postSlug + "! Is it fetched?")
return false
}
// Decide WebDAV upload path
current_date = new Date()
var uploadPath = path.join(
current_date.getUTCFullYear().toString(),
current_date.getUTCMonth().toString(),
postSlug
)
// Path where WebDAV files will be placed (eg. https://example.com:2078)
var davPath = path.join(process.env.WEBDAV_PATH_PREFIX, uploadPath)
// Public path to upload those files (eg. https://media.example.com/uploads)
// We'll do it directly since path.join mangles the protocol
var uploadedPath = process.env.WEBDAV_UPLOADED_PATH_PREFIX + '/' + uploadPath
// Process lines
const readInterface = readline.createInterface({
input: fs.createReadStream(postContent),
output: process.stdout,
terminal: false
})
const outStream = fs.createWriteStream(postOutput, { encoding: 'utf-8' })
readInterface.on('line', (line) => {
console.log('Line: ' + line + '\n')
// TODO: actually process line
var newLine = line
// check for images
const reImage = new RegExp('^!\\[(.*)\\]\\((\\S+?)\\)(.*)')
var m = reImage.exec(line)
if (m) {
// Get image name
var imageAlt = m[1]
var imageName = m[2].replace('*', '')
var imagePath = path.join(postFolder, 'images', imageName)
if (!fs.existsSync(imagePath)) {
console.warn('Skipping missing image: ' + imageName)
} else {
// TODO: upload pic
uploadDav(davPath, imagePath)
newLine = '![' + imageAlt + '](' + uploadedPath + '/' + imageName + ')'
}
}
outStream.write(newLine + '\n')
})
console.log('Processing over. Rest needs to be implemented :P')
};
/**