-
Notifications
You must be signed in to change notification settings - Fork 253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(provider): imgproxy #385
Open
shadow81627
wants to merge
1
commit into
nuxt:v0
Choose a base branch
from
shadow81627:feat/provider/imgproxy
base: v0
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
title: Imgproxy Provider | ||
description: 'Nuxt Image has first class integration with Imgproxy' | ||
navigation: | ||
title: Imgproxy | ||
--- | ||
|
||
Integration between [Imgproxy](https://imgproxy.net/) and the image module. | ||
|
||
To use this provider you just need to specify the base url of your service in Imgproxy. | ||
|
||
```js{}[nuxt.config.js] | ||
export default { | ||
image: { | ||
imgix: { | ||
baseURL: 'http://imgproxy.example.com', | ||
key: 'xxxxxxxxxxxxxx', | ||
salt: 'xxxxxxxxxxxxxx' | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ const BuiltInProviders = [ | |
'glide', | ||
'imagekit', | ||
'imgix', | ||
'imgproxy', | ||
'ipx', | ||
'netlify', | ||
'prismic', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { joinURL, withBase } from 'ufo' | ||
import createHmac from 'create-hmac' | ||
import type { ProviderGetImage } from 'src' | ||
import defu from 'defu' | ||
import { createOperationsGenerator } from '~image' | ||
|
||
const operationsGenerator = createOperationsGenerator({ | ||
keyMap: { | ||
resize: 'rs', | ||
size: 's', | ||
fit: 'rt', | ||
width: 'w', | ||
height: 'h', | ||
dpr: 'dpr', | ||
enlarge: 'el', | ||
extend: 'ex', | ||
gravity: 'g', | ||
crop: 'c', | ||
padding: 'pd', | ||
trim: 't', | ||
rotate: 'rot', | ||
quality: 'q', | ||
maxBytes: 'mb', | ||
background: 'bg', | ||
backgroundAlpha: 'bga', | ||
blur: 'bl', | ||
sharpen: 'sh', | ||
watermark: 'wm', | ||
preset: 'pr', | ||
cacheBuster: 'cb', | ||
stripMetadata: 'sm', | ||
stripColorProfile: 'scp', | ||
autoRotate: 'ar', | ||
filename: 'fn', | ||
format: 'f' | ||
}, | ||
formatter: (key, value) => `${key}:${value}` | ||
}) | ||
|
||
function urlSafeBase64 (value: Buffer | string) { | ||
return Buffer.from(value) | ||
.toString('base64') | ||
.replace(/=/g, '') | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_') | ||
} | ||
|
||
const hexDecode = (hex: string) => Buffer.from(hex, 'hex') | ||
|
||
function sign (salt: string, target: string, secret: string) { | ||
const hmac = createHmac('sha256', hexDecode(secret)) | ||
|
||
hmac.update(hexDecode(salt)) | ||
hmac.update(target) | ||
|
||
return urlSafeBase64(hmac.digest()) | ||
} | ||
|
||
const defaultModifiers = { | ||
fit: 'fill', | ||
width: 0, | ||
height: 0, | ||
gravity: 'no', | ||
enlarge: 1, | ||
format: 'webp' | ||
} | ||
|
||
export const getImage: ProviderGetImage = (src, { modifiers = {}, baseURL = '/', key, salt } = {}) => { | ||
const mergeModifiers = defu(modifiers, defaultModifiers) | ||
const encodedUrl = urlSafeBase64(src) | ||
const path = joinURL('/', operationsGenerator(mergeModifiers), encodedUrl) | ||
const signature = sign(salt, path, key) | ||
|
||
return { | ||
url: withBase(joinURL(signature, path), baseURL) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Provider options are also included in the pre-built JS that the web browser downloads to generate the image URL on the client side. Therefore, it is possible for a third party to steal the key and salt, but is it safe to leak these values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@misaon This PR is implementing #378 (comment)
Do you have any insight for the above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related to #276