-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: proxy and optimize media for Premium users
- Loading branch information
1 parent
fde230c
commit 86d7cd8
Showing
4 changed files
with
174 additions
and
68 deletions.
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,84 @@ | ||
import { joinURL } from "ufo"; | ||
import { createOperationsGenerator } from "@nuxt/image/dist/runtime/utils/index"; | ||
import type { ProviderGetImage } from "@nuxt/image"; | ||
import { Buffer } from "buffer"; | ||
|
||
// https://docs.imgproxy.net/ | ||
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(string: string) { | ||
return Buffer.from(string, "utf8") | ||
.toString("base64") | ||
.replace(/=/g, "") | ||
.replace(/\+/g, "-") | ||
.replace(/\//g, "_"); | ||
} | ||
|
||
const defaultModifiers = { | ||
// fit: "fill", | ||
// width: 0, | ||
// height: 0, | ||
// gravity: "no", | ||
// enlarge: 1, | ||
// format: "webp", | ||
}; | ||
|
||
/** | ||
* | ||
* @see https://github.com/nuxt/image/issues/378 | ||
*/ | ||
export const getImage: ProviderGetImage = (src, options) => { | ||
|
||
// Skip if src is a relative URL | ||
if (src.startsWith("/")) { | ||
return { url: src }; | ||
} | ||
|
||
// Skip GIFs, since imgproxy doesn't support them | ||
if (src.endsWith(".gif")) { | ||
return { url: src }; | ||
} | ||
|
||
const { modifiers, baseURL } = options; | ||
|
||
const mergeModifiers = { ...defaultModifiers, ...modifiers }; | ||
|
||
const encodedUrl = urlSafeBase64(src); | ||
|
||
const path = joinURL("/", operationsGenerator(mergeModifiers), encodedUrl); | ||
|
||
return { | ||
url: joinURL(baseURL, path), | ||
}; | ||
}; |
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.