|
| 1 | +import { getProviderForUrlByPath } from "../detect.ts"; |
| 2 | +import type { |
| 3 | + ImageFormat, |
| 4 | + Operations, |
| 5 | + URLExtractor, |
| 6 | + URLGenerator, |
| 7 | + URLTransformer, |
| 8 | +} from "../types.ts"; |
| 9 | +import { |
| 10 | + createExtractAndGenerate, |
| 11 | + createOperationsHandlers, |
| 12 | + toCanonicalUrlString, |
| 13 | + toUrl, |
| 14 | +} from "../utils.ts"; |
| 15 | + |
| 16 | +type AppwriteOutputFormats = |
| 17 | + | ImageFormat |
| 18 | + | "gif"; |
| 19 | + |
| 20 | +const VIEW_URL_SUFFIX = "/view?"; |
| 21 | +const PREVIEW_URL_SUFFIX = "/preview?"; |
| 22 | + |
| 23 | +/** |
| 24 | + * @see https://appwrite.io/docs/products/storage/images |
| 25 | + */ |
| 26 | +export interface AppwriteOperations extends Operations<AppwriteOutputFormats> { |
| 27 | + /** |
| 28 | + * Set the width of the output image in pixels. |
| 29 | + * Output image will be resized keeping the aspect ratio intact. |
| 30 | + * @type {number} Range: 0-4000 |
| 31 | + */ |
| 32 | + width?: number; |
| 33 | + |
| 34 | + /** |
| 35 | + * Set the height of the output image in pixels. |
| 36 | + * Output image will be resized keeping the aspect ratio intact. |
| 37 | + * @type {number} Range: 0-4000 |
| 38 | + */ |
| 39 | + height?: number; |
| 40 | + |
| 41 | + /** |
| 42 | + * Set the gravity while cropping the output image, providing either width, height, or both. |
| 43 | + */ |
| 44 | + gravity?: |
| 45 | + | "center" |
| 46 | + | "top-left" |
| 47 | + | "top" |
| 48 | + | "top-right" |
| 49 | + | "left" |
| 50 | + | "right" |
| 51 | + | "bottom-left" |
| 52 | + | "bottom" |
| 53 | + | "bottom-right"; |
| 54 | + |
| 55 | + /** |
| 56 | + * Set the quality of the output image |
| 57 | + * @type {number} Range: 0-100 |
| 58 | + */ |
| 59 | + quality?: number; |
| 60 | + |
| 61 | + /** |
| 62 | + * Set a border with the given width in pixels for the output image. |
| 63 | + * @type {number} Range: 0-100 |
| 64 | + */ |
| 65 | + borderWidth?: number; |
| 66 | + |
| 67 | + /** |
| 68 | + * Set a border-color for the output image. |
| 69 | + * Accepts any valid hex color value without the leading '#'. |
| 70 | + */ |
| 71 | + borderColor?: string; |
| 72 | + |
| 73 | + /** |
| 74 | + * Set the border-radius in pixels. |
| 75 | + * @type {number} Range: 0-4000 |
| 76 | + */ |
| 77 | + borderRadius?: number; |
| 78 | + |
| 79 | + /** |
| 80 | + * Set opacity for the output image. |
| 81 | + * Works only with output formats supporting alpha channels, like 'png'. |
| 82 | + * @type {number} Range: 0-1 |
| 83 | + */ |
| 84 | + opacity?: number; |
| 85 | + |
| 86 | + /** |
| 87 | + * Rotates the output image by a degree. |
| 88 | + * @type {number} Range: -360-360 |
| 89 | + */ |
| 90 | + rotation?: number; |
| 91 | + |
| 92 | + /** |
| 93 | + * Set a background-color for the output image. |
| 94 | + * Accepts any valid hex color value without the leading '#'. |
| 95 | + * Works only with output formats supporting alpha channels, like 'png'. |
| 96 | + */ |
| 97 | + background?: string; |
| 98 | + |
| 99 | + /** |
| 100 | + * Set the output image format. |
| 101 | + * If not provided, will use the original image's format. |
| 102 | + */ |
| 103 | + output?: AppwriteOutputFormats; |
| 104 | +} |
| 105 | + |
| 106 | +const { operationsGenerator, operationsParser } = createOperationsHandlers< |
| 107 | + AppwriteOperations |
| 108 | +>({ |
| 109 | + keyMap: { |
| 110 | + format: "output", |
| 111 | + }, |
| 112 | + kvSeparator: "=", |
| 113 | + paramSeparator: "&", |
| 114 | +}); |
| 115 | + |
| 116 | +export const generate: URLGenerator<"appwrite"> = (src, modifiers) => { |
| 117 | + const url = toUrl( |
| 118 | + src.toString().replace(VIEW_URL_SUFFIX, PREVIEW_URL_SUFFIX), |
| 119 | + ); |
| 120 | + const projectParam = url.searchParams.get("project") ?? ""; |
| 121 | + |
| 122 | + const operations = operationsGenerator(modifiers); |
| 123 | + url.search = operations; |
| 124 | + url.searchParams.append("project", projectParam); |
| 125 | + |
| 126 | + return toCanonicalUrlString(url); |
| 127 | +}; |
| 128 | + |
| 129 | +export const extract: URLExtractor<"appwrite"> = (url) => { |
| 130 | + if (getProviderForUrlByPath(url) !== "appwrite") { |
| 131 | + return null; |
| 132 | + } |
| 133 | + const parsedUrl = toUrl(url); |
| 134 | + const operations = operationsParser(parsedUrl); |
| 135 | + // deno-lint-ignore no-explicit-any |
| 136 | + delete (operations as any).project; |
| 137 | + |
| 138 | + const projectParam = parsedUrl.searchParams.get("project") ?? ""; |
| 139 | + parsedUrl.search = ""; |
| 140 | + parsedUrl.searchParams.append("project", projectParam); |
| 141 | + |
| 142 | + const sourceUrl = parsedUrl.href; |
| 143 | + |
| 144 | + return { |
| 145 | + src: sourceUrl, |
| 146 | + operations, |
| 147 | + }; |
| 148 | +}; |
| 149 | + |
| 150 | +export const transform: URLTransformer< |
| 151 | + "appwrite" |
| 152 | +> = createExtractAndGenerate(extract, generate); |
0 commit comments