Skip to content

Commit

Permalink
feat: allow metadata removal to be toggled by option
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKruckenberg committed Mar 30, 2021
1 parent c03f5a3 commit 5d0c781
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
13 changes: 8 additions & 5 deletions packages/core/src/lib/apply-transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { Sharp } from "sharp"
import { ImageTransformation, TransformResult } from "../types"
import { METADATA } from "./metadata"

export async function applyTransforms(transforms: ImageTransformation[], image: Sharp): Promise<TransformResult> {
export async function applyTransforms(transforms: ImageTransformation[], image: Sharp, removeMetadata: boolean = true): Promise<TransformResult> {
image[METADATA] = await image.metadata()

// delete the xmp buffer to not leak private metadata
delete image[METADATA].xmp
delete image[METADATA].exif
delete image[METADATA].iptc
if(removeMetadata) {
// delete the private metadata
delete image[METADATA].exif
delete image[METADATA].iptc
delete image[METADATA].xmp
delete image[METADATA].tifftagPhotoshop
}

image = transforms.reduce((img, transform) => transform(img), image)

Expand Down
9 changes: 9 additions & 0 deletions packages/vite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ See the section [Custom output formats](#custom-output-formats) for details.
Settings this option to true disables all warnings produced by this plugin.

**`default`** false

### removeMetadata

**removeMetadata**: *boolean*

Wether to remove potentially private metadata from the image, such as exif tags etc.

**`default`** true

## Contributing

Feel free to dive in! [Open an issue](https://github.com/JonasKruckenberg/vite-imagetools/issues/new) or submit PRs!
Expand Down
16 changes: 9 additions & 7 deletions packages/vite/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Plugin, ResolvedConfig } from "vite";
import { parseURL, loadImageFromDisk, builtins, resolveConfigs, applyTransforms, generateTransforms, getMetadata, generateImageID } from 'imagetools-core'
import { parseURL, loadImage, builtins, resolveConfigs, applyTransforms, generateTransforms, getMetadata, generateImageID } from 'imagetools-core'
import { basename, extname, join } from 'path'
import { createFilter, dataToEsm } from "@rollup/pluginutils";
import { builtinOutputFormats, urlFormat } from './output-formats'
import MagicString from 'magic-string'
import { OutputFormat, PluginOptions } from "./types";
import { createHash } from 'crypto'

const defaultOptions: PluginOptions = {
include: '**\/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}?*',
exclude: 'public\/**\/*',
silent: false
silent: false,
removeMetadata: true
}

export default function imagetools(userOptions: Partial<PluginOptions> = {}): Plugin {
Expand Down Expand Up @@ -43,15 +43,15 @@ export default function imagetools(userOptions: Partial<PluginOptions> = {}): Pl
const parameters = parseURL(src)
const imageConfigs = resolveConfigs(parameters)

const img = loadImageFromDisk(src.pathname)
const img = loadImage(src.pathname)

const outputMetadatas = []

for (const config of imageConfigs) {
const id = generateImageID(src, config)

const { transforms } = generateTransforms(config, transformFactories)
const { image, metadata } = await applyTransforms(transforms, img)
const { image, metadata } = await applyTransforms(transforms, img, pluginOptions.removeMetadata)

generatedImages.set(id, image)

Expand All @@ -72,8 +72,6 @@ export default function imagetools(userOptions: Partial<PluginOptions> = {}): Pl
outputMetadatas.push(metadata)
}

// console.log(outputMetadatas);

let outputFormat: OutputFormat = urlFormat

for (const [key, format] of Object.entries(outputFormats)) {
Expand All @@ -93,6 +91,10 @@ export default function imagetools(userOptions: Partial<PluginOptions> = {}): Pl

const image = generatedImages.get(id)

if(pluginOptions.removeMetadata === false) {
image.withMetadata()
}

res.setHeader('Content-Type', `image/${getMetadata(image, 'format')}`)
res.setHeader('Cache-Control', 'max-age=360000')
return image.clone().pipe(res)
Expand Down
6 changes: 6 additions & 0 deletions packages/vite/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ export interface PluginOptions {
* @default false
*/
silent: boolean

/**
* Wether to remove potentially private metadata from the image, such as exif tags etc.
* @default true
*/
removeMetadata: boolean
}

0 comments on commit 5d0c781

Please sign in to comment.