feat: Add imagesDimensions field.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { getQuery, NextServerResponse } from "~/lib/server-helper"
|
||||
import { getFeed } from "~/models/home.model"
|
||||
import { decoratePageForImageDimensions } from "~/queries/page.server"
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const query = getQuery(req)
|
||||
@@ -16,6 +17,11 @@ export async function GET(req: Request) {
|
||||
useHTML: false,
|
||||
topic: query.topic,
|
||||
})
|
||||
|
||||
for (const item of result.list) {
|
||||
await decoratePageForImageDimensions(item)
|
||||
}
|
||||
|
||||
const res = new NextServerResponse()
|
||||
return res.status(200).json(result)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { getQuery, NextServerResponse } from "~/lib/server-helper"
|
||||
import { PagesSortTypes, PageVisibilityEnum } from "~/lib/types"
|
||||
import { getPagesBySite } from "~/models/page.model"
|
||||
import { decoratePageForImageDimensions } from "~/queries/page.server"
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const query = getQuery(req)
|
||||
@@ -20,6 +21,11 @@ export async function GET(req: Request) {
|
||||
: [query.tags as string],
|
||||
}),
|
||||
})
|
||||
|
||||
for (const item of result.list) {
|
||||
await decoratePageForImageDimensions(item)
|
||||
}
|
||||
|
||||
const res = new NextServerResponse()
|
||||
return res.status(200).json(result)
|
||||
}
|
||||
|
||||
@@ -89,6 +89,7 @@ export type ExpandedNote = NoteEntity & {
|
||||
summary?: string
|
||||
cover?: string
|
||||
images?: string[]
|
||||
imageDimensions?: Record<string, { width: number; height: number }>
|
||||
frontMatter?: Record<string, any>
|
||||
slug?: string
|
||||
audio?: string
|
||||
|
||||
+143
-1
@@ -1,4 +1,7 @@
|
||||
import https from "https"
|
||||
import url from "url"
|
||||
import AsyncLock from "async-lock"
|
||||
import sizeOf from "image-size"
|
||||
import { AnalyzeDocumentChain, loadSummarizationChain } from "langchain/chains"
|
||||
import { OpenAI } from "langchain/llms/openai"
|
||||
import { PromptTemplate } from "langchain/prompts"
|
||||
@@ -7,7 +10,7 @@ import removeMarkdown from "remove-markdown"
|
||||
import { Metadata } from "@prisma/client"
|
||||
import { QueryClient } from "@tanstack/react-query"
|
||||
|
||||
import { toGateway } from "~/lib/ipfs-parser"
|
||||
import { toCid, toGateway } from "~/lib/ipfs-parser"
|
||||
import prisma from "~/lib/prisma.server"
|
||||
import { cacheGet } from "~/lib/redis.server"
|
||||
import * as pageModel from "~/models/page.model"
|
||||
@@ -35,6 +38,145 @@ export const fetchGetPage = async (
|
||||
})
|
||||
}
|
||||
|
||||
export const getImageDimensionByUri = async (uri: string) => {
|
||||
return new Promise<{ width: number; height: number; uri: string }>(
|
||||
(resolve, reject) => {
|
||||
const options = url.parse(uri)
|
||||
|
||||
https.get(options, function (response) {
|
||||
const chunks: Buffer[] = []
|
||||
let totalLength = 0
|
||||
const MAX_BUFFER_LENGTH = 10240
|
||||
|
||||
response
|
||||
.on("data", function (chunk) {
|
||||
chunks.push(chunk)
|
||||
totalLength += chunk.length
|
||||
|
||||
if (totalLength > MAX_BUFFER_LENGTH) {
|
||||
response.destroy()
|
||||
const buffer = Buffer.concat(chunks)
|
||||
try {
|
||||
const dimension = sizeOf(buffer)
|
||||
if (dimension.width && dimension.height) {
|
||||
resolve({
|
||||
width: dimension.width,
|
||||
height: dimension.height,
|
||||
uri,
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
reject("Not enough data for image size detection.")
|
||||
}
|
||||
}
|
||||
})
|
||||
.on("end", function () {
|
||||
if (totalLength <= MAX_BUFFER_LENGTH) {
|
||||
const buffer = Buffer.concat(chunks)
|
||||
try {
|
||||
const dimension = sizeOf(buffer)
|
||||
if (dimension.width && dimension.height) {
|
||||
resolve({
|
||||
width: dimension.width,
|
||||
height: dimension.height,
|
||||
uri,
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
reject(e)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
export const cacheCoverDimensions = async ({
|
||||
cid,
|
||||
uris,
|
||||
}: {
|
||||
cid: string
|
||||
uris: string[]
|
||||
}) => {
|
||||
if (!uris.length) return
|
||||
|
||||
const dimensions = (await cacheGet({
|
||||
key: ["coverDimensions", cid],
|
||||
allowEmpty: true,
|
||||
noUpdate: true,
|
||||
noExpire: true,
|
||||
getValueFun: async () => {
|
||||
let result
|
||||
|
||||
await lock.acquire(`coverDimensions_${cid}`, async () => {
|
||||
const meta = await prisma.metadata.findFirst({
|
||||
where: {
|
||||
uri: `ipfs://${cid}`,
|
||||
},
|
||||
})
|
||||
|
||||
const key: keyof Metadata = "image_dimensions"
|
||||
const dimensions = meta?.image_dimensions
|
||||
|
||||
if (dimensions) {
|
||||
result = dimensions
|
||||
} else {
|
||||
const newDimensions: Record<
|
||||
string,
|
||||
{ width: number; height: number }
|
||||
> = {}
|
||||
|
||||
console.time(`get image dimensions ${cid}`)
|
||||
for (const uri of uris) {
|
||||
const dimension = await getImageDimensionByUri(toGateway(uri))
|
||||
if (dimension) {
|
||||
newDimensions[uri] = dimension
|
||||
}
|
||||
}
|
||||
console.timeEnd(`get image dimensions ${cid}`)
|
||||
|
||||
if (meta) {
|
||||
await prisma.metadata.update({
|
||||
where: { uri: `ipfs://${cid}` },
|
||||
data: {
|
||||
[key]: newDimensions,
|
||||
},
|
||||
})
|
||||
} else {
|
||||
await prisma.metadata.create({
|
||||
data: {
|
||||
uri: `ipfs://${cid}`,
|
||||
[key]: newDimensions,
|
||||
},
|
||||
})
|
||||
}
|
||||
result = newDimensions
|
||||
}
|
||||
})
|
||||
|
||||
return result
|
||||
},
|
||||
})) as Record<string, { width: number; height: number }> | undefined
|
||||
|
||||
return dimensions
|
||||
}
|
||||
|
||||
export const decoratePageForImageDimensions = async (
|
||||
page?: Awaited<ReturnType<typeof pageModel.getPage>> | null,
|
||||
) => {
|
||||
if (!page) return
|
||||
|
||||
const dimensions = await cacheCoverDimensions({
|
||||
cid: toCid(page?.metadata?.uri || ""),
|
||||
uris: page?.metadata?.content?.images || [],
|
||||
})
|
||||
|
||||
if (dimensions && Object.keys(dimensions).length && page?.metadata?.content) {
|
||||
page.metadata.content["imageDimensions"] = dimensions
|
||||
}
|
||||
}
|
||||
|
||||
export const prefetchGetPagesBySite = async (
|
||||
input: Parameters<typeof pageModel.getPagesBySite>[0],
|
||||
queryClient: QueryClient,
|
||||
|
||||
Reference in New Issue
Block a user