Skip to content

BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr

Add methods for downloading files

lotk825eec280937
diff --git a/core/api/files/files.go b/core/api/files/files.go
new file mode 100644
index 00000000..aca0df0c
--- /dev/null
+++ b/core/api/files/files.go
@@ -0,0 +1,15 @@
+// =================================================================================
+// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
+// =================================================================================
+
+package files
+
+import (
+ "context"
+
+ "billionmail-core/api/files/v1"
+)
+
+type IFilesV1 interface {
+ DownloadFile(ctx context.Context, req *v1.DownloadFileReq) (res *v1.DownloadFileRes, err error)
+}
diff --git a/core/api/files/v1/files.go b/core/api/files/v1/files.go
new file mode 100644
index 00000000..83204cf2
--- /dev/null
+++ b/core/api/files/v1/files.go
@@ -0,0 +1,42 @@
+package v1
+
+import (
+ "billionmail-core/utility/types/api_v1"
+ "github.com/gogf/gf/v2/frame/g"
+)
+
+//// UploadFileReq File upload request
+//type UploadFileReq struct {
+// g.Meta `path:"/file/upload" method:"post" tags:"Files" summary:"Upload file with support for chunked upload and base64 data"`
+// Authorization string `json:"authorization" dc:"Authorization" in:"header" v:"required"`
+// FName string `json:"f_name" v:"required" dc:"File name, no special characters allowed, max length 256 bytes"`
+// FPath string `json:"f_path" v:"required" dc:"File save path, root path '/' and './' not allowed"`
+// FSize int64 `json:"f_size" v:"required" dc:"Total file size in bytes"`
+// FStart int64 `json:"f_start" dc:"Start position for resumable upload in bytes, used for validation"`
+// B64Data string `json:"b64_data" dc:"Base64 encoded file content, mutually exclusive with blob parameter"`
+// Blob *ghttp.UploadFile `json:"blob" dc:"File content, mutually exclusive with b64_data parameter"`
+// DirMode string `json:"dir_mode" dc:"Directory permission mode, e.g.: '0755'"`
+// FileMode string `json:"file_mode" dc:"File permission mode, e.g.: '0644'"`
+//}
+//
+//// UploadFileRes File upload response
+//type UploadFileRes struct {
+// api_v1.StandardRes
+// Data struct {
+// FileId string `json:"file_id" dc:"Unique file identifier"`
+// UploadSize int64 `json:"upload_size" dc:"Number of bytes uploaded"`
+// FileUrl string `json:"file_url" dc:"File access URL"`
+// } `json:"data"`
+//}
+
+// DownloadFileReq File download request
+type DownloadFileReq struct {
+ g.Meta `path:"/file/download" method:"get" tags:"Files" summary:"Download file by path"`
+ Authorization string `json:"authorization" dc:"Authorization" in:"header" v:"required"`
+ FilePath string `json:"file_path" v:"required" dc:"File path for download"`
+}
+
+// DownloadFileRes File download response
+type DownloadFileRes struct {
+ api_v1.StandardRes
+}
diff --git a/core/internal/controller/files/files.go b/core/internal/controller/files/files.go
new file mode 100644
index 00000000..5acc69aa
--- /dev/null
+++ b/core/internal/controller/files/files.go
@@ -0,0 +1,121 @@
+// =================================================================================
+// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
+// =================================================================================
+
+package files
+
+import (
+ "github.com/gogf/gf/v2/errors/gerror"
+ "github.com/gogf/gf/v2/os/gfile"
+ "github.com/gogf/gf/v2/text/gregex"
+ "os"
+ "strconv"
+ "strings"
+)
+
+func (c *ControllerV1) validateFileName(fileName string) error {
+ if len(fileName) > 256 {
+ return gerror.New("File name exceeds 256 bytes limit")
+ }
+
+ if ok := gregex.IsMatchString(`[<>:"/\\|?*\x00-\x1F]`, fileName); ok {
+ return gerror.New("File name contains invalid characters")
+ }
+
+ return nil
+}
+
+func (c *ControllerV1) validateFilePath(filePath string) error {
+ if filePath == "/" {
+ return gerror.New("Cannot use system root directory")
+ }
+
+ if strings.Contains(filePath, "./") {
+ return gerror.New("Invalid path parameter")
+ }
+
+ return nil
+}
+
+func (c *ControllerV1) checkFileExists(filePath string) error {
+ if !gfile.Exists(filePath) {
+ return gerror.New("File not found")
+ }
+ return nil
+}
+
+func (c *ControllerV1) getFileInfo(filePath string) (*os.FileInfo, error) {
+ fileInfo, err := os.Stat(filePath)
+ if err != nil {
+ return nil, gerror.New("Failed to get file info: " + err.Error())
+ }
+ return &fileInfo, nil
+}
+
+func (c *ControllerV1) validateFileSize(actualSize int64, expectedSize int64) bool {
+ return actualSize == expectedSize
+}
+
+func (c *ControllerV1) openFileForWrite(filePath string) (*os.File, error) {
+ file, err := os.OpenFile(filePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
+ if err != nil {
+ return nil, gerror.New("Failed to open file: " + err.Error())
+ }
+ return file, nil
+}
+
+func (c *ControllerV1) readFileContent(filePath string) ([]byte, error) {
+ data, err := os.ReadFile(filePath)
+ if err != nil {
+ return nil, gerror.New("Failed to read file: " + err.Error())
+ }
+ return data, nil
+}
+
+func (c *ControllerV1) parseMode(mode string) (os.FileMode, error) {
+ var parsed uint64
+ var err error
+ if mode != "" {
+ parsed, err = strconv.ParseUint(mode, 8, 32)
+ if err != nil {
+ return 0, gerror.New("Invalid mode format: " + err.Error())
+ }
+ }
+ return os.FileMode(parsed), nil
+}
+
+func (c *ControllerV1) ensureDirectory(path string, mode string) error {
+ if path == "" {
+ return gerror.New("Directory path cannot be empty")
+ }
+
+ // set default directory permissions
+ dirMode := os.FileMode(0755)
+ if mode != "" {
+
+ var err error
+ dirMode, err = c.parseMode(mode)
+ if err != nil {
+ return err
+ }
+ }
+
+ if err := os.MkdirAll(path, dirMode); err != nil {
+ return gerror.New("Failed to create directory: " + err.Error())
+ }
+ return nil
+}
+
+func (c *ControllerV1) setFilePermission(path string, mode string) error {
+ if mode == "" {
+ // 默认文件权限0644
+ return os.Chmod(path, 0644)
+ }
+
+ fileMode, err := c.parseMode(mode)
+ if err != nil {
+ return err
+ }
+
+ return os.Chmod(path, fileMode)
+}
diff --git a/core/internal/controller/files/files_new.go b/core/internal/controller/files/files_new.go
new file mode 100644
index 00000000..a83df3fb
--- /dev/null
+++ b/core/internal/controller/files/files_new.go
@@ -0,0 +1,15 @@
+// =================================================================================
+// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
+// =================================================================================
+
+package files
+
+import (
+ "billionmail-core/api/files"
+)
+
+type ControllerV1 struct{}
+
+func NewV1() files.IFilesV1 {
+ return &ControllerV1{}
+}
diff --git a/core/internal/controller/files/files_v1_download_file.go b/core/internal/controller/files/files_v1_download_file.go
new file mode 100644
index 00000000..9ccc6a74
--- /dev/null
+++ b/core/internal/controller/files/files_v1_download_file.go
@@ -0,0 +1,33 @@
+package files
+
+import (
+ "billionmail-core/api/files/v1"
+ "billionmail-core/internal/service/public"
+ "context"
+ "github.com/gogf/gf/v2/errors/gerror"
+ "github.com/gogf/gf/v2/frame/g"
+ "github.com/gogf/gf/v2/os/gfile"
+ "path/filepath"
+)
+
+func (c *ControllerV1) DownloadFile(ctx context.Context, req *v1.DownloadFileReq) (res *v1.DownloadFileRes, err error) {
+ if err = c.validateFilePath(req.FilePath); err != nil {
+ return nil, err
+ }
+
+ if !gfile.Exists(req.FilePath) {
+ return nil, gerror.New(public.LangCtx(ctx, "File not found"))
+ }
+
+ fileName := filepath.Base(req.FilePath)
+
+ r := g.RequestFromCtx(ctx)
+
+ // set response headers
+ r.Response.Header().Set("Content-Type", "application/octet-stream")
+ r.Response.Header().Set("Content-Disposition", "attachment; filename="+fileName)
+
+ r.Response.ServeFileDownload(req.FilePath, fileName)
+
+ return nil, nil
+}