From f5532acf9707a3a64d37741b16a4038871928f4a Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Thu, 1 Jul 2021 20:39:57 +0530 Subject: [PATCH] Fixes file upload for empty txt file and ico file --- compose/service/attachment.go | 6 ++++- compose/service/attachment_actions.gen.go | 32 +++++++++++++++++++++++ compose/service/attachment_actions.yaml | 4 +++ system/service/attachment.go | 6 ++++- system/service/attachment_actions.gen.go | 32 +++++++++++++++++++++++ system/service/attachment_actions.yaml | 4 +++ 6 files changed, 82 insertions(+), 2 deletions(-) diff --git a/compose/service/attachment.go b/compose/service/attachment.go index 0df6b1b34..5dd8aa474 100644 --- a/compose/service/attachment.go +++ b/compose/service/attachment.go @@ -384,6 +384,10 @@ func (svc attachment) create(name string, size int64, fh io.ReadSeeker, att *typ return errors.New("cannot create attachment: store handler not set") } + if size == 0 { + return AttachmentErrNotAllowedToCreateEmptyAttachment() + } + aProps.setName(name) aProps.setSize(size) @@ -433,7 +437,7 @@ func (svc attachment) extractMimetype(file io.ReadSeeker) (mimetype string, err } func (svc attachment) processImage(original io.ReadSeeker, att *types.Attachment) (err error) { - if !strings.HasPrefix(att.Meta.Original.Mimetype, "image/") { + if !strings.HasPrefix(att.Meta.Original.Mimetype, "image/") || att.Meta.Original.Mimetype == "image/x-icon" { // Only supporting previews from images (for now) return } diff --git a/compose/service/attachment_actions.gen.go b/compose/service/attachment_actions.gen.go index 066fbe3ea..6aef3e690 100644 --- a/compose/service/attachment_actions.gen.go +++ b/compose/service/attachment_actions.gen.go @@ -868,6 +868,38 @@ func AttachmentErrNotAllowedToCreate(mm ...*attachmentActionProps) *errors.Error return e } +// AttachmentErrNotAllowedToCreateEmptyAttachment returns "compose:attachment.notAllowedToCreateEmptyAttachment" as *errors.Error +// +// +// This function is auto-generated. +// +func AttachmentErrNotAllowedToCreateEmptyAttachment(mm ...*attachmentActionProps) *errors.Error { + var p = &attachmentActionProps{} + if len(mm) > 0 { + p = mm[0] + } + + var e = errors.New( + errors.KindInternal, + + p.Format("not allowed to create empty attachments", nil), + + errors.Meta("type", "notAllowedToCreateEmptyAttachment"), + errors.Meta("resource", "compose:attachment"), + + // action log entry; no formatting, it will be applied inside recordAction fn. + errors.Meta(attachmentLogMetaKey{}, "failed to create attachment; empty file"), + errors.Meta(attachmentPropsMetaKey{}, p), + + errors.StackSkip(1), + ) + + if len(mm) > 0 { + } + + return e +} + // AttachmentErrFailedToExtractMimeType returns "compose:attachment.failedToExtractMimeType" as *errors.Error // // diff --git a/compose/service/attachment_actions.yaml b/compose/service/attachment_actions.yaml index 0195023be..d933fdb21 100644 --- a/compose/service/attachment_actions.yaml +++ b/compose/service/attachment_actions.yaml @@ -101,6 +101,10 @@ errors: message: "not allowed to create attachments" log: "could not create attachments; insufficient permissions" + - error: notAllowedToCreateEmptyAttachment + message: "not allowed to create empty attachments" + log: "failed to create attachment; empty file" + - error: failedToExtractMimeType message: "could not extract mime type" diff --git a/system/service/attachment.go b/system/service/attachment.go index bebf7552a..dcb7ff34c 100644 --- a/system/service/attachment.go +++ b/system/service/attachment.go @@ -210,6 +210,10 @@ func (svc attachment) create(ctx context.Context, name string, size int64, fh io return errors.New("cannot create attachment: store handler not set") } + if size == 0 { + return AttachmentErrNotAllowedToCreateEmptyAttachment(aaProps) + } + aaProps.setName(name) aaProps.setSize(size) @@ -259,7 +263,7 @@ func (svc attachment) extractMimetype(file io.ReadSeeker) (mimetype string, err } func (svc attachment) processImage(original io.ReadSeeker, att *types.Attachment) (err error) { - if !strings.HasPrefix(att.Meta.Original.Mimetype, "image/") { + if !strings.HasPrefix(att.Meta.Original.Mimetype, "image/") || att.Meta.Original.Mimetype == "image/x-icon" { // Only supporting previews from images (for now) return } diff --git a/system/service/attachment_actions.gen.go b/system/service/attachment_actions.gen.go index 0c233b42d..31a7dc32d 100644 --- a/system/service/attachment_actions.gen.go +++ b/system/service/attachment_actions.gen.go @@ -493,6 +493,38 @@ func AttachmentErrNotAllowedToCreate(mm ...*attachmentActionProps) *errors.Error return e } +// AttachmentErrNotAllowedToCreateEmptyAttachment returns "system:attachment.notAllowedToCreateEmptyAttachment" as *errors.Error +// +// +// This function is auto-generated. +// +func AttachmentErrNotAllowedToCreateEmptyAttachment(mm ...*attachmentActionProps) *errors.Error { + var p = &attachmentActionProps{} + if len(mm) > 0 { + p = mm[0] + } + + var e = errors.New( + errors.KindInternal, + + p.Format("not allowed to create empty attachments", nil), + + errors.Meta("type", "notAllowedToCreateEmptyAttachment"), + errors.Meta("resource", "system:attachment"), + + // action log entry; no formatting, it will be applied inside recordAction fn. + errors.Meta(attachmentLogMetaKey{}, "failed to create attachment; empty file"), + errors.Meta(attachmentPropsMetaKey{}, p), + + errors.StackSkip(1), + ) + + if len(mm) > 0 { + } + + return e +} + // AttachmentErrFailedToExtractMimeType returns "system:attachment.failedToExtractMimeType" as *errors.Error // // diff --git a/system/service/attachment_actions.yaml b/system/service/attachment_actions.yaml index b3330fb6f..f689b10ef 100644 --- a/system/service/attachment_actions.yaml +++ b/system/service/attachment_actions.yaml @@ -57,6 +57,10 @@ errors: message: "not allowed to create attachments" log: "failed to create attachment; insufficient permissions" + - error: notAllowedToCreateEmptyAttachment + message: "not allowed to create empty attachments" + log: "failed to create attachment; empty file" + - error: failedToExtractMimeType message: "could not extract mime type"