Made sure uploads respect storage limits

This commit is contained in:
ThaumRystra
2024-09-06 22:58:07 +02:00
parent 679a123a2a
commit e2ffaa203a
4 changed files with 88 additions and 37 deletions

View File

@@ -3,6 +3,7 @@ import SimpleSchema from 'simpl-schema';
import { incrementFileStorageUsed } from '/imports/api/users/methods/updateFileStorageUsed'; import { incrementFileStorageUsed } from '/imports/api/users/methods/updateFileStorageUsed';
import { CreaturePropertySchema } from '/imports/api/creature/creatureProperties/CreatureProperties'; import { CreaturePropertySchema } from '/imports/api/creature/creatureProperties/CreatureProperties';
import { CreatureSchema } from '/imports/api/creature/creatures/Creatures'; import { CreatureSchema } from '/imports/api/creature/creatures/Creatures';
import assertUserHasFileSpace from '/imports/api/files/assertUserHasFileSpace';
let createS3FilesCollection; let createS3FilesCollection;
if (Meteor.isServer) { if (Meteor.isServer) {
createS3FilesCollection = require('/imports/api/files/server/s3FileStorage').createS3FilesCollection createS3FilesCollection = require('/imports/api/files/server/s3FileStorage').createS3FilesCollection
@@ -18,6 +19,9 @@ const ArchiveCreatureFiles = createS3FilesCollection({
if (file.size > 10485760) { if (file.size > 10485760) {
return 'Please upload with size equal or less than 10MB'; 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)) { if (!/json/i.test(file.extension)) {
return 'Please upload only a JSON file'; return 'Please upload only a JSON file';
} }

View 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`
);
}
}

View File

@@ -1,5 +1,6 @@
import { incrementFileStorageUsed } from '/imports/api/users/methods/updateFileStorageUsed'; import { incrementFileStorageUsed } from '/imports/api/users/methods/updateFileStorageUsed';
import assertUserHasFileSpace from '/imports/api/files/assertUserHasFileSpace';
let createS3FilesCollection; let createS3FilesCollection;
if (Meteor.isServer) { if (Meteor.isServer) {
createS3FilesCollection = require('/imports/api/files/server/s3FileStorage').createS3FilesCollection createS3FilesCollection = require('/imports/api/files/server/s3FileStorage').createS3FilesCollection
@@ -11,10 +12,12 @@ const UserImages = createS3FilesCollection({
collectionName: 'userImages', collectionName: 'userImages',
storagePath: Meteor.isDevelopment ? '../../../../../fileStorage/userImages' : 'assets/app/userImages', storagePath: Meteor.isDevelopment ? '../../../../../fileStorage/userImages' : 'assets/app/userImages',
onBeforeUpload(file) { onBeforeUpload(file) {
// Allow upload files under 10MB // Allow upload files under 30MB
if (file.size > 10485760) { if (file.size > 30_000_000) {
return 'Please upload with size equal or less than 10MB'; return 'Images must be less than 30MB';
} }
// Make sure the user has enough space
assertUserHasFileSpace(Meteor.userId(), file.size);
// Allow common image extensions // Allow common image extensions
if (!/gif|png|jpe?g|webp/i.test(file.extension || '')) { if (!/gif|png|jpe?g|webp/i.test(file.extension || '')) {
return 'Please upload an image file only'; return 'Please upload an image file only';

View File

@@ -1,34 +1,46 @@
<template> <template>
<v-btn <div
outlined
class="image-upload-button"
v-bind="$attrs" v-bind="$attrs"
:color="fileUploadError ? 'error' : undefined" class="d-flex flex-column "
:disabled="uploadingInProgress"
@click="$refs.hiddenFileInput.click()"
> >
<v-icon left> <v-btn
mdi-file-upload-outline outlined
</v-icon> block
<template v-if="fileUploadError"> class="image-upload-button flex-grow-1"
{{ fileUploadError }} v-bind="$attrs"
</template> style="min-height: 64px;"
<template v-else> :loading="uploadingInProgress"
Upload Image @click="$refs.hiddenFileInput.click()"
</template>
<v-progress-linear
v-if="uploadingInProgress"
:value="progress"
:indeterminate="uploadIndeterminate"
/>
<input
ref="hiddenFileInput"
type="file"
accept="image/*"
style="display: none;"
@input="inputChange"
> >
</v-btn> <v-icon left>
mdi-file-upload-outline
</v-icon>
<div>
Upload Image
</div>
<template #loader>
<v-progress-circular
:value="progress"
:indeterminate="uploadIndeterminate"
/>
</template>
<input
ref="hiddenFileInput"
type="file"
accept="image/*"
style="display: none;"
@input="inputChange"
>
</v-btn>
<v-alert
v-if="fileUploadError"
outlined
type="error"
class="mb-0 mt-4"
>
{{ fileUploadError }}
</v-alert>
</div>
</template> </template>
<script lang="js"> <script lang="js">
@@ -62,7 +74,7 @@ export default {
} }
// Start the image insert process // Start the image insert process
let uploadInstance = UserImages.insert({ const uploadInstance = UserImages.insert({
file: file, file: file,
chunkSize: 'dynamic', chunkSize: 'dynamic',
allowWebWorkers: true, allowWebWorkers: true,
@@ -73,32 +85,42 @@ export default {
}, false); }, false);
uploadInstance.on('start', function () { uploadInstance.on('start', function () {
console.log('start')
self.progress = 0; self.progress = 0;
self.uploadIndeterminate = false; self.uploadIndeterminate = false;
// Remove errors
self.fileUploadError = undefined;
}); });
uploadInstance.on('end', function (error, fileObj) { uploadInstance.on('end', function (error, fileObj) {
console.log('end', error)
self.resetState(); self.resetState();
self.$emit('uploaded', UserImages.link(fileObj)); self.$emit('uploaded', UserImages.link(fileObj));
}); });
uploadInstance.on('uploaded', function (error, fileObj) { uploadInstance.on('uploaded', function (error, fileObj) {
self.resetState(); console.log('uploaded')
self.progress = 0; self.progress = 0;
}); });
uploadInstance.on('error', function (error, fileObj) { uploadInstance.on('error', function (error, fileObj) {
self.resetState(); console.log('error', error)
this.fileUploadError = error.reason || error.message || error.toString(); self.fileUploadError = error.reason || error.message || error.toString();
}); });
uploadInstance.on('progress', function (progress, fileObj) { uploadInstance.on('progress', function (progress, fileObj) {
// Update our progress bar with actual progress // Update our progress bar with actual progress
console.log('progress')
self.uploadIndeterminate = false; self.uploadIndeterminate = false;
self.progress = progress; self.progress = progress;
}); });
uploadInstance.start(); // Must manually start the upload try {
uploadInstance.start(); // Must manually start the upload
} catch (error) {
self.fileUploadError = error.reason || error.message || error.toString();
self.resetState();
}
}, },
}, },
methods: { methods: {
@@ -112,11 +134,10 @@ export default {
resetState() { resetState() {
// Remove file from input // Remove file from input
this.file = undefined; this.file = undefined;
this.$refs.hiddenFileInput.value = '';
// stop progress // stop progress
this.uploadingInProgress = false; this.uploadingInProgress = false;
this.progress = 0; this.progress = 0;
// Remove errors
this.fileUploadError = undefined;
}, },
}, },