diff --git a/.gitignore b/.gitignore index 2fd83a6..66cf8ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea node_modules dist + diff --git a/.vscode/launch.json b/.vscode/launch.json index 4266a17..2b52070 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,14 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "type": "pwa-node", + "request": "launch", + "name": "GhostAPI", + "skipFiles": ["/**"], + "program": "${workspaceFolder}/dist/GhostAPI.js", + "outFiles": ["${workspaceFolder}/**/*.js"] + }, { "type": "pwa-node", "request": "launch", diff --git a/src/GhostAPI.ts b/src/GhostAPI.ts index e8238c6..dd8b351 100644 --- a/src/GhostAPI.ts +++ b/src/GhostAPI.ts @@ -19,6 +19,10 @@ switch (command) { .browse() .then((response: any) => console.log(JSON.stringify(response))) .catch((error: any) => console.error(error)); + api.images + .browse() + .then((response: any) => console.log(JSON.stringify(response))) + .catch((error: any) => console.error(error)); break; case 1: const post = fs.readFileSync("./src/data/draftpost.json", { diff --git a/src/convertor/images.ts b/src/convertor/images.ts new file mode 100644 index 0000000..c637320 --- /dev/null +++ b/src/convertor/images.ts @@ -0,0 +1,52 @@ +import fs from "fs"; +import http from "http"; +import https from "https"; +import path from "path"; + +import stream from "stream"; +import { Z_FIXED } from "zlib"; + +const Stream = stream.Transform; + +/** + * Download an image off an URL + * @param url the url containing the image + * @param filename the filename to be saved + */ +export async function downloadImagefromURL(url: string, filename: string) { + let client: any = http; + + if (url.toString().indexOf("https") === 0) { + client = https; + } + + client + .request(url, function (response: any) { + var data = new Stream(); + + response.on("data", function (chunk: any) { + data.push(chunk); + }); + response.on("end", function () { + fs.writeFileSync(filename, data.read()); + }); + }) + .end(); +} + +interface Image { + file: Blob | File | undefined; + purpose: "image" | "profile_image" | "icon"; + ref?: string; +} + +export async function uploadToGhost(api: any, filename: string) { + const data: Buffer = fs.readFileSync(filename); + let image: Image = { + ref: path.basename(filename), + file: new Blob([data]), + purpose: "image", + }; + let result = api.images.upload(image); + console.log(JSON.stringify(result)); +} diff --git a/temp/.gitkeep b/temp/.gitkeep new file mode 100644 index 0000000..e69de29