Skip to content

Commit

Permalink
feat(imago): make output path optional, record img buffer
Browse files Browse the repository at this point in the history
- update OutputSpec.path handling
- if no path given, record encoded img buffer itself in outputs
- update outputProc() & __outputRaw()
- update docs
  • Loading branch information
postspectacular committed Jul 6, 2024
1 parent 5ac2831 commit 90258b2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
9 changes: 7 additions & 2 deletions packages/imago/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,12 @@ export interface OutputSpec extends ProcSpec {
id: string;
/**
* Possibly templated output path. See {@link formatPath} for details.
* Ignored if {@link OutputSpec.blurhash} is being used, otherwise **required**.
* Ignored if {@link OutputSpec.blurhash} is being used.
*
* Otherwise, if given, the image will be written to the result path and the
* path stored in the `outputs` object returned by {@link processImage}. If
* no path is given, no file will be written and the encoded image buffer
* itself will be recorded in `outputs`.
*/
path?: string;
/**
Expand Down Expand Up @@ -531,7 +536,7 @@ export interface ImgProcCtx {
* Paths of all exported images, keyed by IDs given via {@link OutputSpec} /
* {@link output}.
*/
outputs: Record<string, string>;
outputs: Record<string, string | Buffer>;
/**
* See {@link ImgProcOpts.env} for details/comments.
*/
Expand Down
42 changes: 25 additions & 17 deletions packages/imago/src/ops/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ export const outputProc: Processor = async (spec, input, ctx) => {
if (opts.tile) output = output.tile(opts.tile);
if (format) output = output.toFormat(<any>format);
const result = await output.toBuffer();
const path = join(
outDir,
formatPath(opts.path, ctx, <OutputSpec>spec, result)
);
writeFile(path, result, null, ctx.logger);
ctx.outputs[opts.id] = path;
if (opts.path !== undefined) {
const path = join(
outDir,
formatPath(opts.path, ctx, <OutputSpec>spec, result)
);
writeFile(path, result, null, ctx.logger);
ctx.outputs[opts.id] = path;
} else {
ctx.outputs[opts.id] = result;
}
return [input, false];
};

Expand All @@ -87,17 +91,21 @@ const __outputRaw = async (
const { data, info } = await output
.raw()
.toBuffer({ resolveWithObject: true });
const path = join(outDir, formatPath(opts.path!, ctx, opts, data));
writeFile(path, data, null, ctx.logger);
ctx.outputs[opts.id] = path;
if (meta) {
writeJSON(
path + ".meta.json",
{ ...info, exif: ctx.exif },
undefined,
undefined,
ctx.logger
);
if (opts.path !== undefined) {
const path = join(outDir, formatPath(opts.path!, ctx, opts, data));
writeFile(path, data, null, ctx.logger);
ctx.outputs[opts.id] = path;
if (meta) {
writeJSON(
path + ".meta.json",
{ ...info, exif: ctx.exif },
undefined,
undefined,
ctx.logger
);
}
} else {
ctx.outputs[opts.id] = data;
}
};

Expand Down

0 comments on commit 90258b2

Please sign in to comment.