Skip to content

Commit

Permalink
rewrite image urls when running locally and strategy is configure (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
theorm authored Jan 17, 2025
1 parent c54b41c commit 5f2e3b3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/models/images.model.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { OpenPermissions } from '../util/bigint'
import Article from './articles.model'

const Page = require('./pages.model')
const Issue = require('./issues.model')
const Newspaper = require('./newspapers.model')
const Article = require('./articles.model')
const { getExternalFragment } = require('../hooks/iiif.js')

class Image {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { loadYamlFile } from '../../util/yaml'
import { redactResponse, redactResponseDataItem, webAppExploreRedactionCondition } from '../../hooks/redaction'
import {
RedactionPolicy,
redactResponse,
redactResponseDataItem,
webAppExploreRedactionCondition,
} from '../../hooks/redaction'
import { HookContext } from '@feathersjs/feathers'
import { ImpressoApplication } from '../../types'

// const { authenticate } = require('@feathersjs/authentication').hooks;
const {
Expand All @@ -16,7 +23,47 @@ const { resolveFacets, resolveQueryComponents } = require('../../hooks/search-in

const { eachFilterValidator } = require('../search/search.validators')

export const imageRedactionPolicyWebApp = loadYamlFile(`${__dirname}/resources/imageRedactionPolicyWebApp.yml`)
export const imageRedactionPolicyWebApp: RedactionPolicy = loadYamlFile(
`${__dirname}/resources/imageRedactionPolicyWebApp.yml`
)

const getPrefix = (prefixes: string[], url?: string): string | undefined => {
return url == null ? undefined : prefixes.find(prefix => url.startsWith(prefix))
}

/**
* A hook used in development environment to update the IIIF URLs in images to
* point to the local server.
* The prefixes to match are defined in the `localPrefixes` configuration key.
*/
const updateIiifUrls = (context: HookContext<ImpressoApplication>) => {
if (context.type !== 'after') {
throw new Error('The updateIiifUrls hook should be used as an after hook only')
}

const proxyConfig = context.app.get('proxy')
const prefixes = proxyConfig?.localPrefixes
const host = proxyConfig?.host

const replaceableItems = ['pages', 'regions']
const replaceableFields = ['iiif', 'iiifThumbnail', 'iiifFragment']

if (prefixes != null && prefixes.length > 0 && host != null) {
context.result?.data?.forEach((image: Record<string, any>) => {
replaceableItems.forEach((key: string) => {
image?.[key]?.forEach((item: Record<string, any>) => {
replaceableFields.forEach((field: string) => {
const iiifPrefix = getPrefix(prefixes, item[field])
if (iiifPrefix != null) {
item[field] = item[field].replace(iiifPrefix, host)
}
})
})
})
})
console.log(context.result.data)
}
}

export default {
before: {
Expand Down Expand Up @@ -97,6 +144,7 @@ export default {
resolveFacets(),
displayQueryParams(['queryComponents', 'filters']),
resolveQueryComponents(),
updateIiifUrls,
redactResponseDataItem(imageRedactionPolicyWebApp, webAppExploreRedactionCondition),
],
get: [redactResponse(imageRedactionPolicyWebApp, webAppExploreRedactionCondition)],
Expand Down

0 comments on commit 5f2e3b3

Please sign in to comment.