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

rewrite image urls when running locally and strategy is configure #491

Merged
merged 1 commit into from
Jan 17, 2025
Merged
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
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
Loading