Implement "image upload" part of push-ghost command
This commit is contained in:
parent
772c9000f8
commit
95f48b24ba
1 changed files with 75 additions and 0 deletions
75
functions.js
75
functions.js
|
@ -3,6 +3,7 @@ const path = require('path')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const getPost = require('mediumexporter').getPost
|
const getPost = require('mediumexporter').getPost
|
||||||
const { createClient } = require('webdav')
|
const { createClient } = require('webdav')
|
||||||
|
const readline = require('readline')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* function [fetchFromMedium]
|
* function [fetchFromMedium]
|
||||||
|
@ -66,6 +67,80 @@ const fetchFromMedium = async (mediumUrl) => {
|
||||||
*/
|
*/
|
||||||
const pushToGhost = (postSlug) => {
|
const pushToGhost = (postSlug) => {
|
||||||
console.info('Pushing: ' + 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')
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue