add more documentation and order option

This commit is contained in:
gliggy
2021-11-26 11:53:58 -05:00
parent 240a95e656
commit bb38d4af66
2 changed files with 42 additions and 8 deletions

27
API.md Normal file
View File

@@ -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

View File

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