Skip to content
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

feature/dot 771 image pipeline using cloudflare image transforms #920

Open
wants to merge 4 commits into
base: cheap-DAM
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/dashboard/src/modules/offline/use-offline-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createColumnHelper, getCoreRowModel, useReactTable } from "@tanstack/re
import Link from "next/link"
import { useMemo } from "react"
import { formatDate } from "../../utils/format"
import { buildAssetUrl } from "../../utils/s3"
import { buildAssetUrl, buildImgUrl } from "../../utils/s3"

interface Props {
data: Offline[]
Expand Down Expand Up @@ -46,7 +46,7 @@ export const useOfflineTable = ({ data }: Props) => {
return "Ingen bilde"
}
return (
<Anchor target="_blank" href={buildAssetUrl(image.assetKey)} rel="noopenere">
<Anchor target="_blank" href={buildImgUrl(image)} rel="noopenere">
Link
</Anchor>
)
Expand Down
42 changes: 42 additions & 0 deletions apps/dashboard/src/utils/s3.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Image } from "@dotkomonline/types"
import type { File } from "../../stubs/file/File"

// Expected response: 204 No Content. Returns resource URL if successful.
Expand Down Expand Up @@ -28,3 +29,44 @@ export async function s3UploadFile(file: File, fields: Record<string, string>, u
export function buildAssetUrl(key: string) {
return `https://s3.eu-north-1.amazonaws.com/cdn.staging.online.ntnu.no/testing/${key}`
}

function buildFinalCloudflareUrl(options: string, assetUrl: string) {

const cloudflareImagesBaseUrl = "https://onli.no/cdn-cgi/image"

return `${cloudflareImagesBaseUrl}/${options}/${assetUrl}`
}

type Size = { w?: number; h?: number }
export function buildImgUrl(image: Image, size?: Size) {
const assetUrl = buildAssetUrl(image.assetKey)

const options: string[] = []

const addOpt = (key: string, value: string) => options.push(`${key}=${value}`)

addOpt("scale", "fit-down") // https://developers.cloudflare.com/images/transform-images/transform-via-url/#recommended-image-sizes

if (image.crop) {
addOpt("trim.width", image.crop.width.toString())
addOpt("trim.height", image.crop.height.toString())
addOpt("trim.left", image.crop.left.toString())
addOpt("trim.top", image.crop.top.toString())
}

if (size?.w) {
addOpt("width", size.w.toString())
}

if (size?.h) {
addOpt("height", size.h.toString())
}

if (!size?.h && !size?.w) {
addOpt("width", "1920")
}

const optionsString = options.join(",")

return buildFinalCloudflareUrl(optionsString, assetUrl)
}
Loading