Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
optimize static resource thumbnails (#10828)
Browse files Browse the repository at this point in the history
* optimize static resource thumbnails

* console log

* Update packages/server-core/src/media/static-resource/static-resource.hooks.ts

Co-authored-by: Hanzla Mateen <[email protected]>

---------

Co-authored-by: Hanzla Mateen <[email protected]>
  • Loading branch information
HexaField and hanzlamateen authored Aug 3, 2024
1 parent c013946 commit e25b8d6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,52 @@ const isKeyPublic = (context: HookContext<StaticResourceService>) => {
return context
}

const resolveThumbnailURL = async (context: HookContext<StaticResourceService>) => {
if (!context.result) return context
const data = context.result
const dataArr = data ? (Array.isArray(data) ? data : 'data' in data ? data.data : [data]) : []

context.hashedThumbnailResults = {}

const thumbkeyToIndex = new Map<string, string>()
const storageProvider = getStorageProvider()

for (const resource of dataArr) {
/** Thumbnail resources should resolve themselves for their thumbnail fields */
if (resource.type === 'thumbnail') {
resource.thumbnailKey = resource.key
const thumbnailURL = storageProvider.getCachedURL(resource.key, context.params.isInternal)
const thumbnailURLWithHash = thumbnailURL + '?hash=' + resource.hash.slice(0, 6)
context.hashedThumbnailResults[resource.id] = thumbnailURLWithHash
} else {
if (resource.thumbnailKey) thumbkeyToIndex.set(resource.thumbnailKey, resource.id)
}
}

if (!thumbkeyToIndex.size) return context

const thumbnailResources = await context.app.service(staticResourcePath).find({
query: {
type: 'thumbnail',
key: {
$in: [...thumbkeyToIndex.keys()]
}
},
paginate: false
})

for (const thumbnailResource of thumbnailResources) {
const thumbnailURL = storageProvider.getCachedURL(thumbnailResource.key, context.params.isInternal)
const thumbnailURLWithHash = thumbnailURL + '?hash=' + thumbnailResource.hash.slice(0, 6)
const id = thumbkeyToIndex.get(thumbnailResource.key)

if (!id) continue
context.hashedThumbnailResults[id] = thumbnailURLWithHash
}

return context
}

export default {
around: {
all: [schemaHooks.resolveResult(staticResourceResolver)]
Expand Down Expand Up @@ -267,8 +313,9 @@ export default {

after: {
all: [],
find: [],
find: [resolveThumbnailURL],
get: [
resolveThumbnailURL,
iff(
isProvider('external'),
iffElse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ export const staticResourceDbToSchema = (rawData: StaticResourceDatabaseType): S
}
}

const getThumbnailURL = (staticResource: StaticResourceType, context: HookContext) => {
if (context.method !== 'find' && context.method !== 'get') {
return ''
}

const values = context.hashedThumbnailResults

return values[staticResource.id]
}

/**
* the first few characters of resources hashes are appended as a version identifier to allow for cache busting
*/
Expand All @@ -80,21 +90,7 @@ export const staticResourceResolver = resolve<StaticResourceType, HookContext>(
)
}),
thumbnailURL: virtual(async (staticResource, context) => {
if (!staticResource.thumbnailKey) return
const storageProvider = getStorageProvider()
/** @todo optimize this */
const thumbnailStaticResource = await context.app.service('static-resource').find({
query: {
type: 'thumbnail',
key: staticResource.thumbnailKey
}
})
if (!thumbnailStaticResource.data.length) return
return (
storageProvider.getCachedURL(staticResource.thumbnailKey, context.params.isInternal) +
'?hash=' +
thumbnailStaticResource.data[0].hash.slice(0, 6)
)
return getThumbnailURL(staticResource, context)
})
},
{
Expand Down

0 comments on commit e25b8d6

Please sign in to comment.