make file upload size limit configurable (#386)

This commit is contained in:
Philip Okugbe
2024-10-10 21:28:28 +01:00
committed by GitHub
parent e333eee08b
commit 384f11f2b7
14 changed files with 428 additions and 400 deletions

View File

@ -1,6 +1,8 @@
import { handleAttachmentUpload } from "@docmost/editor-ext";
import { uploadFile } from "@/features/page/services/page-service.ts";
import { notifications } from "@mantine/notifications";
import {getFileUploadSizeLimit} from "@/lib/config.ts";
import {formatBytes} from "@/lib";
export const uploadAttachmentAction = handleAttachmentUpload({
onUpload: async (file: File, pageId: string): Promise<any> => {
@ -18,10 +20,10 @@ export const uploadAttachmentAction = handleAttachmentUpload({
if (file.type.includes("image/") || file.type.includes("video/")) {
return false;
}
if (file.size / 1024 / 1024 > 50) {
if (file.size > getFileUploadSizeLimit()) {
notifications.show({
color: "red",
message: `File exceeds the 50 MB attachment limit`,
message: `File exceeds the ${formatBytes(getFileUploadSizeLimit())} attachment limit`,
});
return false;
}

View File

@ -1,6 +1,8 @@
import { handleImageUpload } from "@docmost/editor-ext";
import { uploadFile } from "@/features/page/services/page-service.ts";
import { notifications } from "@mantine/notifications";
import {getFileUploadSizeLimit} from "@/lib/config.ts";
import { formatBytes } from "@/lib";
export const uploadImageAction = handleImageUpload({
onUpload: async (file: File, pageId: string): Promise<any> => {
@ -18,10 +20,10 @@ export const uploadImageAction = handleImageUpload({
if (!file.type.includes("image/")) {
return false;
}
if (file.size / 1024 / 1024 > 50) {
if (file.size > getFileUploadSizeLimit()) {
notifications.show({
color: "red",
message: `File exceeds the 50 MB attachment limit`,
message: `File exceeds the ${formatBytes(getFileUploadSizeLimit())} attachment limit`,
});
return false;
}

View File

@ -1,6 +1,8 @@
import { handleVideoUpload } from "@docmost/editor-ext";
import { uploadFile } from "@/features/page/services/page-service.ts";
import { notifications } from "@mantine/notifications";
import {getFileUploadSizeLimit} from "@/lib/config.ts";
import {formatBytes} from "@/lib";
export const uploadVideoAction = handleVideoUpload({
onUpload: async (file: File, pageId: string): Promise<any> => {
@ -19,13 +21,14 @@ export const uploadVideoAction = handleVideoUpload({
return false;
}
if (file.size / 1024 / 1024 > 50) {
if (file.size > getFileUploadSizeLimit()) {
notifications.show({
color: "red",
message: `File exceeds the 50 MB attachment limit`,
message: `File exceeds the ${formatBytes(getFileUploadSizeLimit())} attachment limit`,
});
return false;
}
return true;
},
});

View File

@ -1,51 +1,58 @@
import bytes from "bytes";
declare global {
interface Window {
CONFIG?: Record<string, string>;
}
interface Window {
CONFIG?: Record<string, string>;
}
}
export function getAppUrl(): string {
//let appUrl = window.CONFIG?.APP_URL || process.env.APP_URL;
//let appUrl = window.CONFIG?.APP_URL || process.env.APP_URL;
// if (import.meta.env.DEV) {
// return appUrl || "http://localhost:3000";
//}
// if (import.meta.env.DEV) {
// return appUrl || "http://localhost:3000";
//}
return `${window.location.protocol}//${window.location.host}`;
return `${window.location.protocol}//${window.location.host}`;
}
export function getBackendUrl(): string {
return getAppUrl() + '/api';
return getAppUrl() + '/api';
}
export function getCollaborationUrl(): string {
const COLLAB_PATH = '/collab';
const COLLAB_PATH = '/collab';
let url = getAppUrl();
if (import.meta.env.DEV) {
url = process.env.APP_URL;
}
let url = getAppUrl();
if (import.meta.env.DEV) {
url = process.env.APP_URL;
}
const wsProtocol = url.startsWith('https') ? 'wss' : 'ws';
return `${wsProtocol}://${url.split('://')[1]}${COLLAB_PATH}`;
const wsProtocol = url.startsWith('https') ? 'wss' : 'ws';
return `${wsProtocol}://${url.split('://')[1]}${COLLAB_PATH}`;
}
export function getAvatarUrl(avatarUrl: string) {
if (!avatarUrl) {
return null;
}
if (!avatarUrl) {
return null;
}
if (avatarUrl?.startsWith('http')) {
return avatarUrl;
}
if (avatarUrl?.startsWith('http')) {
return avatarUrl;
}
return getBackendUrl() + '/attachments/img/avatar/' + avatarUrl;
return getBackendUrl() + '/attachments/img/avatar/' + avatarUrl;
}
export function getSpaceUrl(spaceSlug: string) {
return '/s/' + spaceSlug;
return '/s/' + spaceSlug;
}
export function getFileUrl(src: string) {
return src?.startsWith('/files/') ? getBackendUrl() + src : src;
return src?.startsWith('/files/') ? getBackendUrl() + src : src;
}
export function getFileUploadSizeLimit() {
const limit = window.CONFIG?.FILE_UPLOAD_SIZE_LIMIT || process?.env.FILE_UPLOAD_SIZE_LIMIT || '50mb';
return bytes(limit);
}

View File

@ -5,12 +5,13 @@ import * as path from "path";
export const envPath = path.resolve(process.cwd(), "..", "..");
export default defineConfig(({ mode }) => {
const { APP_URL } = loadEnv(mode, envPath, "");
const { APP_URL, FILE_UPLOAD_SIZE_LIMIT } = loadEnv(mode, envPath, "");
return {
define: {
"process.env": {
APP_URL,
FILE_UPLOAD_SIZE_LIMIT
},
'APP_VERSION': JSON.stringify(process.env.npm_package_version),
},