From 837859838c4ca1f480451121c0307694f558a2dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Tue, 29 Mar 2022 13:09:39 +0200 Subject: [PATCH] Adding max file size limit --- mattermost-plugin/server/plugin.go | 1 + server/api/api.go | 6 +++++- server/app/app.go | 4 ++++ server/services/config/config.go | 1 + webapp/src/components/cardDetail/imagePaste.tsx | 7 +++++++ webapp/src/components/content/imageElement.tsx | 11 ++++++++--- 6 files changed, 26 insertions(+), 4 deletions(-) diff --git a/mattermost-plugin/server/plugin.go b/mattermost-plugin/server/plugin.go index e91c87ba1..7fde51702 100644 --- a/mattermost-plugin/server/plugin.go +++ b/mattermost-plugin/server/plugin.go @@ -219,6 +219,7 @@ func (p *Plugin) createBoardsConfig(mmconfig mmModel.Config, baseURL string, ser FilesDriver: *mmconfig.FileSettings.DriverName, FilesPath: *mmconfig.FileSettings.Directory, FilesS3Config: filesS3Config, + MaxFileSize: *mmconfig.FileSettings.MaxFileSize, Telemetry: enableTelemetry, TelemetryID: serverID, WebhookUpdate: []string{}, diff --git a/server/api/api.go b/server/api/api.go index aacf53274..50064bbde 100644 --- a/server/api/api.go +++ b/server/api/api.go @@ -1981,9 +1981,13 @@ func (a *API) handleUploadFile(w http.ResponseWriter, r *http.Request) { return } + if a.app.GetConfig().MaxFileSize > 0 { + r.Body = http.MaxBytesReader(w, r.Body, a.app.GetConfig().MaxFileSize) + } + file, handle, err := r.FormFile(UploadFormFileKey) if err != nil { - fmt.Fprintf(w, "%v", err) + a.errorResponse(w, r.URL.Path, http.StatusRequestEntityTooLarge, "", err) return } defer file.Close() diff --git a/server/app/app.go b/server/app/app.go index 641f060cd..6b02c7a9b 100644 --- a/server/app/app.go +++ b/server/app/app.go @@ -53,6 +53,10 @@ func (a *App) SetConfig(config *config.Configuration) { a.config = config } +func (a *App) GetConfig() *config.Configuration { + return a.config +} + func New(config *config.Configuration, wsAdapter ws.Adapter, services Services) *App { app := &App{ config: config, diff --git a/server/services/config/config.go b/server/services/config/config.go index 19838d424..40e1070f7 100644 --- a/server/services/config/config.go +++ b/server/services/config/config.go @@ -37,6 +37,7 @@ type Configuration struct { FilesDriver string `json:"filesdriver" mapstructure:"filesdriver"` FilesS3Config AmazonS3Config `json:"filess3config" mapstructure:"filess3config"` FilesPath string `json:"filespath" mapstructure:"filespath"` + MaxFileSize int64 `json:"maxfilesize" mapstructure:"mafilesize"` Telemetry bool `json:"telemetry" mapstructure:"telemetry"` TelemetryID string `json:"telemetryid" mapstructure:"telemetryid"` PrometheusAddress string `json:"prometheus_address" mapstructure:"prometheus_address"` diff --git a/webapp/src/components/cardDetail/imagePaste.tsx b/webapp/src/components/cardDetail/imagePaste.tsx index f69cc614c..2174402e1 100644 --- a/webapp/src/components/cardDetail/imagePaste.tsx +++ b/webapp/src/components/cardDetail/imagePaste.tsx @@ -4,6 +4,7 @@ import {useEffect, useCallback} from 'react' import {ImageBlock, createImageBlock} from '../../blocks/imageBlock' +import {sendFlashMessage} from '../flashMessages' import octoClient from '../../octoClient' import mutator from '../../mutator' @@ -25,8 +26,10 @@ export default function useImagePaste(boardId: string, cardId: string, contentOr const uploaded = await Promise.all(uploads) const blocksToInsert: ImageBlock[] = [] + let someFilesNotUploaded = false for (const fileId of uploaded) { if (!fileId) { + someFilesNotUploaded = true continue } const block = createImageBlock() @@ -36,6 +39,10 @@ export default function useImagePaste(boardId: string, cardId: string, contentOr blocksToInsert.push(block) } + if (someFilesNotUploaded) { + sendFlashMessage({content: "Some files not uploaded. File size limit reached", severity: 'normal'}) + } + mutator.performAsUndoGroup(async () => { const newContentBlocks = await mutator.insertBlocks(boardId, blocksToInsert, 'pasted images') const newContentOrder = JSON.parse(JSON.stringify(contentOrder)) diff --git a/webapp/src/components/content/imageElement.tsx b/webapp/src/components/content/imageElement.tsx index 93275b5e9..36d166ac8 100644 --- a/webapp/src/components/content/imageElement.tsx +++ b/webapp/src/components/content/imageElement.tsx @@ -7,6 +7,7 @@ import {ImageBlock, createImageBlock} from '../../blocks/imageBlock' import octoClient from '../../octoClient' import {Utils} from '../../utils' import ImageIcon from '../../widgets/icons/image' +import {sendFlashMessage} from '../../components/flashMessages' import {contentRegistry} from './contentRegistry' @@ -52,9 +53,13 @@ contentRegistry.registerContentType({ Utils.selectLocalFile(async (file) => { const fileId = await octoClient.uploadFile(boardId, file) - const block = createImageBlock() - block.fields.fileId = fileId || '' - resolve(block) + if (fileId) { + const block = createImageBlock() + block.fields.fileId = fileId || '' + resolve(block) + } else { + sendFlashMessage({content: 'Unable to upload the file. File size limit reached.', severity: 'normal'}) + } }, '.jpg,.jpeg,.png,.gif') },