From bb38d4af66e623d15b689aa64b3410ec8294f3fb Mon Sep 17 00:00:00 2001 From: gliggy Date: Fri, 26 Nov 2021 11:53:58 -0500 Subject: [PATCH] add more documentation and order option --- API.md | 27 +++++++++++++++++++++++++++ index.js | 23 +++++++++++++++-------- 2 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 API.md diff --git a/API.md b/API.md new file mode 100644 index 0000000..1ddf5e7 --- /dev/null +++ b/API.md @@ -0,0 +1,27 @@ +Hello, good evening, and welcome, to makesweet's API! +Here's how to use it: +1. get a computer +2. get curl +3. send a POST request with things in it +4. receive a response +5. do whatever you want with it. + + +examples (assuming MAKESWEET_API is set as env var): + +``` +curl -X POST $MAKESWEET_API/make/heart-locket?text=hello -o hello.gif +``` +send only text (use &text for more text) + +``` +curl -X POST $MAKESWEET_API/make/heart-locket -F "images[]=@image.jpg" -o animation.gif +``` +send only images (for multiple images, repeat with another -F) + +``` +curl -X POST $MAKESWEET_API/make/heart-locket?text=squirrel -F "images[]=@image.jpg" -o animation.gif +``` +send both text and images + +the default is images go before text, use &textfirst=1 to make text go first diff --git a/index.js b/index.js index ae64ee9..8e16215 100644 --- a/index.js +++ b/index.js @@ -19,9 +19,9 @@ app.get('/', (req, res) => { }) app.post('/make/:template', upload.any('images'), handleErrors, (req, res) => { - if (!req.files) { - throw new ApiError(400, 'i hunger for images...'); - } +// if (!req.files) { +// throw new ApiError(400, 'i hunger for images...'); +// } const textHtml = `${process.cwd()}/text.html`; const output = `${workDir}/${uuid.v4()}.gif`; const template = getTemplatePathFromName(req.params.template); @@ -30,6 +30,9 @@ app.post('/make/:template', upload.any('images'), handleErrors, (req, res) => { try { generator.addTexts(req.query.text, textHtml); generator.addImages(req.files); + if (req.query.textfirst) { + generator.setTextFirst(); + } generator.setOutput(output); res.send(generator.apply()); } finally { @@ -102,6 +105,7 @@ class Generator { this.template = null; this.output = null; this.tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'makesweet-api')); + this.textFirst = false; } useFile(fname) { this.fnames.push(fname); @@ -110,6 +114,9 @@ class Generator { this.useFile(fname); this.template = fname; } + setTextFirst() { + this.textFirst = true; + } addImages(files) { if (!files) { return; } for (const file of files) { @@ -149,11 +156,11 @@ class Generator { command.push('paulfitz/makesweet'); command.push('--zip', path.resolve(this.template)); command.push('--in'); - for (const image of this.images) { - command.push(image); - } - for (const text of this.texts) { - command.push(text); + const parts = this.textFirst ? [this.texts, this.images] : [this.images, this.texts]; + for (const part of parts) { + for (const file of part) { + command.push(file); + } } command.push('--gif', this.output); return command;