0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-08 02:52:39 -05:00

Added "uploads" limit type

refs https://linear.app/tryghost/issue/CORE-121/create-a-video-storage-adapter

 - The limit is here to accomodate file size checks
 - An example configuration is in the README
This commit is contained in:
Naz 2021-10-26 15:46:36 +04:00
parent 91a2e54484
commit 945e7ab520
2 changed files with 20 additions and 3 deletions

View file

@ -24,7 +24,7 @@ const LimitService = require('@tryghost/limit-service');
const limitService = new LimitService();
// setup limit configuration
// currently supported limit keys are: staff, members, customThemes, customIntegrations
// currently supported limit keys are: staff, members, customThemes, customIntegrations, uploads
// all limit configs support custom "error" configuration that is a template string
const limits = {
// staff and member are "max" type of limits accepting "max" configuration
@ -59,6 +59,12 @@ const limits = {
// maxPeriodic: 42,
// error: 'Your plan supports up to {{max}} emails. Please upgrade to reenable sending emails.'
// }
uploads: {
// max key is in bytes
max: 5000000,
// formatting of the {{ max }} vairable is in MB, e.g: 5MB
error: 'Your plan supports uploads of max size up to {{max}}. Please upgrade to reenable uploading.'
}
};
// This information is needed for the limit service to work with "max periodic" limits
@ -116,6 +122,11 @@ if (limitService.isLimited('members')) {
await limitService.errorIfIsOverLimit('members', {max: 10000});
}
if (limitService.isLimited('uploads')) {
// for the uploads limit we HAVE TO pass in the "currentCount" parameter and use bytes as a base unit
await limitService.errorIfIsOverLimit('uploads', {currentCount: frame.file.size});
}
// check if any of the limits are acceding
if (limitService.checkIfAnyOverLimit()) {
console.log('One of the limits has acceded!');
@ -130,7 +141,7 @@ At the moment there are four different types of limits that limit service allows
4. `allowList` - checks if provided value is defined in configured "allowlist". Example usecase: "disable theme activation if it is not an official theme". To configure this limit define ` allowlist: ['VALUE_1', 'VALUE_2', 'VALUE_N']` property in the "limits" parameter.
### Supported limits
There's a limited amount of limits that are supported by limit service. The are defined by "key" property name in the "config" module. List of currently supported limit names: `members`, `staff`, `customIntegrations`, `emails`, `customThemes`.
There's a limited amount of limits that are supported by limit service. The are defined by "key" property name in the "config" module. List of currently supported limit names: `members`, `staff`, `customIntegrations`, `emails`, `customThemes`, `uploads`.
All limits can act as `flag` or `allowList` types. Only certain (`members`, `staff`, and`customIntegrations`) can have a `max` limit. Only `emails` currently supports the `maxPeriodic` type of limit.

View file

@ -45,5 +45,11 @@ module.exports = {
return result.count;
}
},
customThemes: {}
customThemes: {},
uploads: {
// NOTE: this function should not ever be used as for uploads we compare the size
// of the uploaded file with the configured limit. Noop is here to keep the
// MaxLimit constructor happy
currentCountQuery: () => {}
}
};