This commit is contained in:
Laur Ivan 2022-06-29 23:12:32 +02:00
parent c7a0e57c42
commit 8cf8c12d54
5 changed files with 65 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.idea
node_modules
dist

8
.vscode/launch.json vendored
View File

@ -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": ["<node_internals>/**"],
"program": "${workspaceFolder}/dist/GhostAPI.js",
"outFiles": ["${workspaceFolder}/**/*.js"]
},
{
"type": "pwa-node",
"request": "launch",

View File

@ -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", {

52
src/convertor/images.ts Normal file
View File

@ -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));
}

0
temp/.gitkeep Normal file
View File