From 43f9b660ce5ba0d2eba84975b80bece7937baa0a Mon Sep 17 00:00:00 2001 From: "Hafizh A. Y" Date: Wed, 28 Jan 2026 11:12:22 +0700 Subject: [PATCH] fix(BE): bug path s3 --- internal/common/service/common.document.service.go | 14 ++++++++++---- ...0128035908_alter_documents_path_length.down.sql | 7 +++++++ ...260128035908_alter_documents_path_length.up.sql | 7 +++++++ internal/entities/document.go | 2 +- 4 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 internal/database/migrations/20260128035908_alter_documents_path_length.down.sql create mode 100644 internal/database/migrations/20260128035908_alter_documents_path_length.up.sql diff --git a/internal/common/service/common.document.service.go b/internal/common/service/common.document.service.go index 44f2c116..a57aaa85 100644 --- a/internal/common/service/common.document.service.go +++ b/internal/common/service/common.document.service.go @@ -363,13 +363,19 @@ func (s *documentService) generateObjectKey(ext string) (string, error) { } u := uuid.New().String() - key := fmt.Sprintf("%s/%s%s", strings.Trim(s.keyPrefix, "/"), u, normalizedExt) - if s.keyPrefix == "" { - key = fmt.Sprintf("%s%s", u, normalizedExt) + keyPrefix := strings.Trim(s.keyPrefix, "/") + key := fmt.Sprintf("%s%s", u, normalizedExt) + if keyPrefix != "" { + key = fmt.Sprintf("%s/%s%s", keyPrefix, u, normalizedExt) } if len(key) > s.maxPathLength { - key = fmt.Sprintf("%s%s", u, normalizedExt) + compact := strings.ReplaceAll(u, "-", "") + if keyPrefix != "" { + key = fmt.Sprintf("%s/%s%s", keyPrefix, compact, normalizedExt) + } else { + key = fmt.Sprintf("%s%s", compact, normalizedExt) + } } if len(key) > s.maxPathLength { diff --git a/internal/database/migrations/20260128035908_alter_documents_path_length.down.sql b/internal/database/migrations/20260128035908_alter_documents_path_length.down.sql new file mode 100644 index 00000000..70a1e119 --- /dev/null +++ b/internal/database/migrations/20260128035908_alter_documents_path_length.down.sql @@ -0,0 +1,7 @@ +BEGIN; + +-- Migration: revert documents.path length +ALTER TABLE documents + ALTER COLUMN path TYPE VARCHAR(50); + +COMMIT; diff --git a/internal/database/migrations/20260128035908_alter_documents_path_length.up.sql b/internal/database/migrations/20260128035908_alter_documents_path_length.up.sql new file mode 100644 index 00000000..80ed2c06 --- /dev/null +++ b/internal/database/migrations/20260128035908_alter_documents_path_length.up.sql @@ -0,0 +1,7 @@ +BEGIN; + +-- Migration: extend documents.path length for environment prefixes +ALTER TABLE documents + ALTER COLUMN path TYPE VARCHAR(255); + +COMMIT; diff --git a/internal/entities/document.go b/internal/entities/document.go index 54974a02..a13dee0b 100644 --- a/internal/entities/document.go +++ b/internal/entities/document.go @@ -7,7 +7,7 @@ type Document struct { DocumentableType string `gorm:"size:50;not null;index:documents_documentable_polymorphic,priority:1"` DocumentableId uint64 `gorm:"not null;index:documents_documentable_polymorphic,priority:2"` Type string `gorm:"size:50;not null"` - Path string `gorm:"size:50;not null"` + Path string `gorm:"size:255;not null"` Name string `gorm:"size:50;not null"` Ext string `gorm:"size:50;not null"` Size float64 `gorm:"type:numeric(15,3);not null"`