Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: file size limit for guest pages #359

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion client/components/open/forms/OpenFormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,11 @@ export default {
}
} else if (field.type === 'files' || (field.type === 'url' && field.file_upload)) {
inputProperties.multiple = (field.multiple !== undefined && field.multiple)
inputProperties.mbLimit = Math.min(Math.max(field.max_file_size, 1), this.form?.max_file_size ?? this.currentWorkspace?.max_file_size)
let maxFileSize = (this.form?.workspace && this.form?.workspace.max_file_size) ? this.form?.workspace?.max_file_size : 10
if (field?.max_file_size > 0) {
maxFileSize = Math.min(field.max_file_size, maxFileSize)
}
inputProperties.mbLimit = maxFileSize
Comment on lines +275 to +279
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for determining the file upload size limit (mbLimit) is correctly implemented. However, it could be refactored for improved readability and maintainability. Consider extracting this logic into a separate method. This not only makes the code cleaner but also easier to test and maintain.

methods: {
+  calculateMbLimit() {
+    let maxFileSize = 10; // Default value
+    if (this.form?.workspace?.max_file_size) {
+      maxFileSize = this.form.workspace.max_file_size;
+    }
+    if (this.field?.max_file_size > 0) {
+      maxFileSize = Math.min(this.field.max_file_size, maxFileSize);
+    }
+    return maxFileSize;
+  },
  inputProperties(field) {
    const inputProperties = {
      // Existing properties...
    };
    if (field.type === 'files' || (field.type === 'url' && field.file_upload)) {
      inputProperties.multiple = (field.multiple !== undefined && field.multiple);
-     let maxFileSize = (this.form?.workspace && this.form?.workspace.max_file_size) ? this.form?.workspace?.max_file_size : 10;
-     if (field?.max_file_size > 0) {
-       maxFileSize = Math.min(field.max_file_size, maxFileSize);
-     }
+     inputProperties.mbLimit = this.calculateMbLimit();
      inputProperties.accept = (this.form.is_pro && field.allowed_file_types) ? field.allowed_file_types : '';
    }
    // Rest of the method...
  }
}

This refactor introduces a calculateMbLimit method that encapsulates the logic for determining the maximum file size limit, making the inputProperties method cleaner and focusing it more on its primary responsibility.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
let maxFileSize = (this.form?.workspace && this.form?.workspace.max_file_size) ? this.form?.workspace?.max_file_size : 10
if (field?.max_file_size > 0) {
maxFileSize = Math.min(field.max_file_size, maxFileSize)
}
inputProperties.mbLimit = maxFileSize
methods: {
calculateMbLimit() {
let maxFileSize = 10; // Default value
if (this.form?.workspace?.max_file_size) {
maxFileSize = this.form.workspace.max_file_size;
}
if (this.field?.max_file_size > 0) {
maxFileSize = Math.min(this.field.max_file_size, maxFileSize);
}
return maxFileSize;
},
inputProperties(field) {
const inputProperties = {
// Existing properties...
};
if (field.type === 'files' || (field.type === 'url' && field.file_upload)) {
inputProperties.multiple = (field.multiple !== undefined && field.multiple);
inputProperties.mbLimit = this.calculateMbLimit();
inputProperties.accept = (this.form.is_pro && field.allowed_file_types) ? field.allowed_file_types : '';
}
// Rest of the method...
}
}

inputProperties.accept = (this.form.is_pro && field.allowed_file_types) ? field.allowed_file_types : ''
} else if (field.type === 'rating') {
inputProperties.numberOfStars = parseInt(field.rating_max_value) ?? 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export default {
return !this.typesWithoutPlaceholder.includes(this.field.type)
},
mbLimit() {
return this.form?.max_file_size ?? this.currentWorkspace?.max_file_size
return (this.form?.workspace && this.form?.workspace.max_file_size) ? this.form?.workspace?.max_file_size : 10
},
prefillSelectsOptions() {
if (!['select', 'multi_select'].includes(this.field.type)) return {}
Expand Down
Loading