-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.ts
98 lines (93 loc) · 4.4 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { InitPluginHook, PluginStateRepositoryToken } from '@ngageoint/mage.service/lib/plugins.api'
import { GetAppRequestContext, WebRoutesHooks } from '@ngageoint/mage.service/lib/plugins.api/plugins.api.web'
import { ObservationRepositoryToken, AttachmentStoreToken } from '@ngageoint/mage.service/lib/plugins.api/plugins.api.observations'
import { MageEventRepositoryToken } from '@ngageoint/mage.service/lib/plugins.api/plugins.api.events'
import { MongooseDbConnectionToken } from '@ngageoint/mage.service/lib/plugins.api/plugins.api.db'
import { SettingPermission } from '@ngageoint/mage.service/lib/entities/authorization/entities.permissions'
import { ImagePluginConfig, createImagePluginControl } from './processor'
import express from 'express'
import { FindUnprocessedAttachments } from './adapters.db.mongo'
import { SharpImageService } from './adapters.images.sharp'
const logPrefix = '[mage.image]'
const logMethods = [ 'log', 'debug', 'info', 'warn', 'error' ] as const
const consoleOverrides = logMethods.reduce((overrides, fn) => {
return {
...overrides,
[fn]: {
writable: false,
value: (...args: any[]): void => {
globalThis.console[fn](new Date().toISOString(), '-', logPrefix, ...args)
}
}
} as PropertyDescriptorMap
}, {} as PropertyDescriptorMap)
const console = Object.create(globalThis.console, consoleOverrides) as Console
const InjectedServices = {
stateRepo: PluginStateRepositoryToken,
eventRepo: MageEventRepositoryToken,
obsRepoForEvent: ObservationRepositoryToken,
attachmentStore: AttachmentStoreToken,
getDbConn: MongooseDbConnectionToken
}
/**
* The MAGE Image Plugin finds images attached to MAGE observations, generates
* thumbnail previews at configurable sizes, and optionally auto-orients the
* images by rotating them based on the EXIF orientation tag so all clients
* display the images correctly.
*/
const imagePluginHooks: InitPluginHook<typeof InjectedServices> = {
inject: {
stateRepo: PluginStateRepositoryToken,
eventRepo: MageEventRepositoryToken,
obsRepoForEvent: ObservationRepositoryToken,
attachmentStore: AttachmentStoreToken,
getDbConn: MongooseDbConnectionToken,
},
init: async (services): Promise<WebRoutesHooks> => {
console.info('intializing image plugin ...')
const { stateRepo, eventRepo, obsRepoForEvent, attachmentStore, getDbConn } = services
const queryAttachments = FindUnprocessedAttachments(getDbConn, console)
const imageService = SharpImageService()
const control = await createImagePluginControl(stateRepo, eventRepo, obsRepoForEvent, attachmentStore, queryAttachments, imageService, console)
control.start()
return {
webRoutes: {
protected(requestContext: GetAppRequestContext): express.Router {
// TODO: add api routes to save image processing settings
const routes = express.Router()
.use(express.json())
.use(async (req, res, next) => {
const context = requestContext(req)
const user = context.requestingPrincipal()
if (!user.role.permissions.find(x => x === SettingPermission.UPDATE_SETTINGS)) {
return res.status(403).json({ message: 'unauthorized' })
}
next()
})
routes.route('/config')
.get(async (req, res, next) => {
const config = await control.getConfig()
res.json(config)
})
.put(async (req, res, next) => {
const bodyConfig = req.body as any
const configPatch: Partial<ImagePluginConfig> = {
enabled: typeof bodyConfig.enabled === 'boolean' ? bodyConfig.enabled : undefined,
intervalBatchSize: typeof bodyConfig.intervalBatchSize === 'number' ? bodyConfig.intervalBatchSize : undefined,
intervalSeconds: typeof bodyConfig.intervalSeconds === 'number' ? bodyConfig.intervalSeconds : undefined,
thumbnailSizes: Array.isArray(bodyConfig.thumbnailSizes) ?
bodyConfig.thumbnailSizes.reduce((sizes: number[], size: any) => {
return typeof size === 'number' ? [...sizes, size] : sizes
}, [] as number[])
: []
}
const config = await control.applyConfig(configPatch)
res.json(config)
})
return routes
}
}
}
}
}
export = imagePluginHooks