-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid-factory-client.js
72 lines (62 loc) · 2.72 KB
/
grid-factory-client.js
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
import { Meteor } from 'meteor/meteor'
import { check, Match } from 'meteor/check'
import { FilesCollection } from 'meteor/ostrio:files'
import { getBeforeUpload } from './lib/both/getBeforeUpload'
import { getLog } from './lib/both/getLog'
import { getCheckSize } from './lib/both/getCheckSize'
import { getCheckExtension } from './lib/both/getCheckExtension'
import { getCheckUser } from './lib/both/getCheckUser'
/**
* High level abstract factory to create FilesCollection-factories with
* built-in GridFS storage. Options passed to this method apply to all
* instances, created with factories (but may be overridden by the concrete
* factories options).
*
* @param abstractOptions.i18nFactory {Function?} a function resolving an i18n string
* @param abstractOptions.onError {Function?} function to handle/pipe errors
* @param abstractOptions.debug {Boolean} flag to run internal debug logs
*
* @return {function(options): FilesCollection}
*/
export const createGridFilesFactory = (abstractOptions = {}) => {
check(abstractOptions, Match.ObjectIncluding(abstractOptionsDef))
const { i18nFactory, onError, debug } = abstractOptions
const abstractOnError = onError || (e => console.error(e))
const abstractLevelDebug = debug
/**
* A factory function to create new FilesCollection instances.
* @param options.maxSize
* @param options.extensions
* @param options.validateUser
* @param options.onError
* @param options.debug
* @param options.config
* @return {FilesCollection}
*/
const factoryFunction = (options = {}) => {
const { maxSize, extensions, validateUser, onError, debug, ...config } = options
const factoryLevelDebug = debug || abstractLevelDebug
const log = getLog(factoryLevelDebug)
log(`create files collection [${config.collectionName || config?.collection?._name}]`)
const onErrorHook = onError || abstractOnError
const checkSize = getCheckSize({ i18nFactory, maxSize, log })
const checkExtension = getCheckExtension({ i18nFactory, extensions, log })
const checkUser = getCheckUser({ log, i18nFactory, validateUser, onErrorHook })
const beforeUpload = getBeforeUpload({ log, checkUser, checkSize, checkExtension })
const factoryConfig = {
debug: factoryLevelDebug,
onbeforeunloadMessage: Meteor.isClient && (() => i18nFactory('filesCollection.onbeforeunloadMessage')),
onBeforeUpload: beforeUpload,
allowClientCode: false // Disallow remove files from Client
}
const productConfig = Object.assign(factoryConfig, config)
return new FilesCollection(productConfig)
}
return factoryFunction
}
// INTERNAL
const abstractOptionsDef = {
i18nFactory: Match.Maybe(Function),
onError: Match.Maybe(Function),
debug: Match.Maybe(Boolean)
}