1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| import * as md5 from "md5"; import * as fs from "fs-extra"; import * as simplegit from "simple-git/promise"; import * as path from "path"; import * as moment from "moment"; import * as Hexo from "hexo"; import { Stream } from "stream"; const config = require("./package.json"); const blog = config.meta.blog
class ExpressError extends Error { status: number;
constructor(msg, code) { super(msg); this.status = code; } }
const hexo = new Hexo(blog, { silent: true, safe: true });
interface PostContent { categories: string[]; dateCreated: Date; description: string; title: string; mt_keywords: string; wp_slug: string; }
interface MediaObject { overwrite: boolean; bits: Buffer; name: string; type: string; }
export async function newPost(blogid: string, username: string, password: string, post: PostContent, publish: boolean, callback: Function) { if (blogid) { if (username.toLowerCase() === "hpdell" && md5(password) === "2b759a6996d878c41bb7d56ce530d031") { const git = simplegit(blog); if (await git.checkIsRepo()) { try { let postInfo = (await fs.readFile(path.resolve(path.join(blog, "scaffolds", "post.md")))).toString(); postInfo = postInfo.replace("{{ title }}", post.title); postInfo = postInfo.replace("{{ date }}", moment(post.dateCreated).format("YYYY-MM-DD HH:mm:ss")); if (post.categories && post.categories.length) { postInfo = postInfo.replace("categories:", `categories: ${post.categories[0]}`); } if (post.mt_keywords) { let tags = post.mt_keywords.split(","); let tagInfo = tags.map(item => ` - ${item}`).join("\n"); postInfo = postInfo.replace("tags:", `tags:\n${tagInfo}`); } let content = postInfo + post.description; let postPath = path.resolve(path.join(blog, "source", "_posts", `${post.wp_slug}.md`)); try { await fs.writeFile(postPath, content); try { await publishToGitHub(git, path.join("source", "_posts", `${post.wp_slug}.md`)); callback(null, post.wp_slug); } catch (error) { callback(new ExpressError((error as Error).message, 500)); } } catch (error) { callback(new ExpressError("Internal Server Error: Cannot write post file.", 500)); } } catch (error) { callback(new ExpressError("Internal Server Error: Cannot read scaffolds.", 500)); } } else { callback(new ExpressError("Inernal Server Error: Not a hexo repo.", 500)); } } else { callback(new ExpressError("Username or Password is wrong.", 500)); } } else { callback(new ExpressError("Blog id is required.", 500)); } }
export async function getPost(postid: string, username: string, password: string, callback: Function) { try { await hexo.load() let posts = hexo.locals.get("posts").filter((v, i) => v.title === postid).toArray(); if (posts && posts.length) { let post = posts[0]; let postStructure: PostContent = { categories: post.categories, dateCreated: post.date.toDate(), description: post.content, title: post.title, mt_keywords: post.tags.join(","), wp_slug: path.basename(post.path) }; callback(null, postStructure) } } catch (error) { callback(new ExpressError("Post not found", 500)); } }
export function getCategories(blogid: string, username: string, password: string, callback: Function) { callback(null, config.meta.categories.map((item: string) => { return { categoryid: item, description: item, title: item } })); }
export async function newMediaObject(blogid: string, username: string, password: string, mediaObject: MediaObject, callback: Function) { if (blogid) { if (username.toLowerCase() === "hpdell" && md5(password) === "2b759a6996d878c41bb7d56ce530d031") { let imgPath = path.join(blog, "source", "assets", "img", mediaObject.name); try { await fs.writeFile(imgPath, mediaObject.bits); callback(null, { url: "/" + ["assets", "img", mediaObject.name].join("/") }) } catch (error) { callback(new ExpressError("Writefile Wrong.", 500)); } } else { callback(new ExpressError("Username or Password is wrong.", 500)); } } else { callback(new ExpressError("Blog id is required.", 500)); } }
async function publishToGitHub(git: simplegit.SimpleGit, postPath) { try { let imgDirPath = path.join("source", "assets", "img", "*"); await git.add([postPath, imgDirPath]); await git.commit(`Add Post: ${path.basename(postPath)}`); await git.pull("origin", "master", { "--rebase": "true" }); await git.push(); } catch (error) { throw new Error("git error"); } }
|