Made sure uploads respect storage limits
This commit is contained in:
@@ -3,6 +3,7 @@ import SimpleSchema from 'simpl-schema';
|
||||
import { incrementFileStorageUsed } from '/imports/api/users/methods/updateFileStorageUsed';
|
||||
import { CreaturePropertySchema } from '/imports/api/creature/creatureProperties/CreatureProperties';
|
||||
import { CreatureSchema } from '/imports/api/creature/creatures/Creatures';
|
||||
import assertUserHasFileSpace from '/imports/api/files/assertUserHasFileSpace';
|
||||
let createS3FilesCollection;
|
||||
if (Meteor.isServer) {
|
||||
createS3FilesCollection = require('/imports/api/files/server/s3FileStorage').createS3FilesCollection
|
||||
@@ -18,6 +19,9 @@ const ArchiveCreatureFiles = createS3FilesCollection({
|
||||
if (file.size > 10485760) {
|
||||
return 'Please upload with size equal or less than 10MB';
|
||||
}
|
||||
// Make sure the user has enough space
|
||||
assertUserHasFileSpace(Meteor.userId(), file.size);
|
||||
// Only accept JSON
|
||||
if (!/json/i.test(file.extension)) {
|
||||
return 'Please upload only a JSON file';
|
||||
}
|
||||
|
||||
23
app/imports/api/files/assertUserHasFileSpace.ts
Normal file
23
app/imports/api/files/assertUserHasFileSpace.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { getUserTier } from '/imports/api/users/patreon/tiers';
|
||||
import prettyBytes from 'pretty-bytes';
|
||||
|
||||
export default function assertUserHasFileSpace(userId: string | null, spaceRequiredInBytes: number) {
|
||||
// Get the user
|
||||
if (!userId) throw new Meteor.Error('permission-denied', 'No user was provided');
|
||||
const user = Meteor.users.findOne(userId, { fields: { fileStorageUsed: 1 } });
|
||||
if (!user) throw new Meteor.Error('permission-denied', 'User not found');
|
||||
|
||||
// Work out how much space they have and need
|
||||
const fileStorageUsed = user.fileStorageUsed || 0;
|
||||
const fileStorageAllowed = getUserTier(Meteor.userId()).fileStorage * 1000000;
|
||||
let fileStorageLeft = fileStorageAllowed - fileStorageUsed;
|
||||
if (fileStorageLeft < 0) fileStorageLeft = 0;
|
||||
|
||||
// Throw an error if they don't have space
|
||||
if (fileStorageLeft < spaceRequiredInBytes) {
|
||||
throw new Meteor.Error('insufficient-space',
|
||||
`Not enough storage space left, you need ${prettyBytes(spaceRequiredInBytes)}, ` +
|
||||
`but only have ${prettyBytes(fileStorageLeft)} available`
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
|
||||
import { incrementFileStorageUsed } from '/imports/api/users/methods/updateFileStorageUsed';
|
||||
import assertUserHasFileSpace from '/imports/api/files/assertUserHasFileSpace';
|
||||
let createS3FilesCollection;
|
||||
if (Meteor.isServer) {
|
||||
createS3FilesCollection = require('/imports/api/files/server/s3FileStorage').createS3FilesCollection
|
||||
@@ -11,10 +12,12 @@ const UserImages = createS3FilesCollection({
|
||||
collectionName: 'userImages',
|
||||
storagePath: Meteor.isDevelopment ? '../../../../../fileStorage/userImages' : 'assets/app/userImages',
|
||||
onBeforeUpload(file) {
|
||||
// Allow upload files under 10MB
|
||||
if (file.size > 10485760) {
|
||||
return 'Please upload with size equal or less than 10MB';
|
||||
// Allow upload files under 30MB
|
||||
if (file.size > 30_000_000) {
|
||||
return 'Images must be less than 30MB';
|
||||
}
|
||||
// Make sure the user has enough space
|
||||
assertUserHasFileSpace(Meteor.userId(), file.size);
|
||||
// Allow common image extensions
|
||||
if (!/gif|png|jpe?g|webp/i.test(file.extension || '')) {
|
||||
return 'Please upload an image file only';
|
||||
|
||||
Reference in New Issue
Block a user