From db525e91954b4df48e5f63fd88435a632e8daf8a Mon Sep 17 00:00:00 2001 From: ekzyis Date: Thu, 19 Oct 2023 16:52:14 +0200 Subject: [PATCH] Mark S3 images as submitted in imgproxy job --- components/image.js | 6 +++--- worker/imgproxy.js | 11 ++++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/components/image.js b/components/image.js index 22796ef5..6c99de16 100644 --- a/components/image.js +++ b/components/image.js @@ -11,6 +11,8 @@ import { useMutation, useQuery } from '@apollo/client' const ImageContext = createContext({ unsubmitted: [] }) +const imageIdToUrl = id => `https://${process.env.NEXT_PUBLIC_AWS_UPLOAD_BUCKET}.s3.amazonaws.com/${id}` + export function ImageProvider ({ me, children }) { const { data, loading } = useQuery( gql` @@ -42,14 +44,12 @@ export function ImageProvider ({ me, children }) { }) const [unsubmittedImages, setUnsubmittedImages] = useState([]) - const imageIdToUrl = id => `https://${process.env.NEXT_PUBLIC_AWS_UPLOAD_BUCKET}.s3.amazonaws.com/${id}` - useEffect(() => { const images = data?.me?.images if (images) { setUnsubmittedImages(images.map(img => ({ ...img, url: imageIdToUrl(img.id) }))) } - }, [loading]) + }, [setUnsubmittedImages, loading]) const contextValue = { unsubmittedImages, diff --git a/worker/imgproxy.js b/worker/imgproxy.js index 67f25449..6f154fbd 100644 --- a/worker/imgproxy.js +++ b/worker/imgproxy.js @@ -12,6 +12,7 @@ if (!imgProxyEnabled) { const IMGPROXY_URL = process.env.NEXT_PUBLIC_IMGPROXY_URL const IMGPROXY_SALT = process.env.IMGPROXY_SALT const IMGPROXY_KEY = process.env.IMGPROXY_KEY +const AWS_S3_URL = `https://${process.env.NEXT_PUBLIC_AWS_UPLOAD_BUCKET}.s3.amazonaws.com/` const cache = new Map() @@ -64,10 +65,10 @@ export function imgproxy ({ models }) { let imgproxyUrls = {} try { if (item.text) { - imgproxyUrls = await createImgproxyUrls(id, item.text, { forceFetch }) + imgproxyUrls = await createImgproxyUrls(id, item.text, { models, forceFetch }) } if (item.url && !isJob(item)) { - imgproxyUrls = { ...imgproxyUrls, ...(await createImgproxyUrls(id, item.url, { forceFetch })) } + imgproxyUrls = { ...imgproxyUrls, ...(await createImgproxyUrls(id, item.url, { models, forceFetch })) } } } catch (err) { console.log('[imgproxy] error:', err) @@ -81,7 +82,7 @@ export function imgproxy ({ models }) { } } -export const createImgproxyUrls = async (id, text, { forceFetch }) => { +export const createImgproxyUrls = async (id, text, { models, forceFetch }) => { const urls = extractUrls(text) console.log('[imgproxy] id:', id, '-- extracted urls:', urls) // resolutions that we target: @@ -106,6 +107,10 @@ export const createImgproxyUrls = async (id, text, { forceFetch }) => { url = decodeOriginalUrl(url) console.log('[imgproxy] id:', id, '-- original url:', url) } + if (url.startsWith(AWS_S3_URL)) { + const s3Key = url.split('/').pop() + await models.upload.update({ data: { itemId: Number(id) }, where: { id: Number(s3Key) } }) + } if (!(await isImageURL(url, { forceFetch }))) { console.log('[imgproxy] id:', id, '-- not image url:', url) continue