package app import ( "fmt" "io" "log/slog" "strings" filesengine "git.optclblast.xyz/draincloud/draincloud-core/internal/files_engine" "git.optclblast.xyz/draincloud/draincloud-core/internal/logger" "git.optclblast.xyz/draincloud/draincloud-core/internal/reqcontext" "github.com/davecgh/go-spew/spew" "github.com/gin-gonic/gin" "github.com/google/uuid" ) const ( maxFileSize = 10 << 30 ) func (d *DrainCloud) UploadFile(ctx *gin.Context) { if err := ctx.Request.ParseMultipartForm(maxFileSize); err != nil { logger.Error(ctx, "uploadFile handler error", logger.Err(err)) writeError(ctx, err) return } userID, err := reqcontext.GetUserID(ctx) if err != nil { writeError(ctx, ErrorAccessDenied) return } if err := d.uploadFile(ctx, userID); err != nil { logger.Error(ctx, "uploadFile handle", logger.Err(err)) writeError(ctx, err) return } } func (d *DrainCloud) uploadFile(ctx *gin.Context, userID uuid.UUID) error { title := ctx.PostForm("file") logger.Info(ctx, "uploadFile", slog.Any("postForm data", spew.Sdump(title))) file, header, err := ctx.Request.FormFile("file") if err != nil { return err } logger.Info(ctx, "uploadFile", slog.Any("header", spew.Sdump(header))) data, err := io.ReadAll(file) if err != nil { return err } ext := parseExtension(header.Filename) id, err := d.filesEngine.SaveFile(ctx, filesengine.File{ Name: header.Filename, UserID: userID, Data: data, Ext: ext, Size: int64(len(data)), Type: "", // че такое type? }) if err != nil { return fmt.Errorf("failed to save file: %w", err) } logger.Debug(ctx, "new file id", "id", id) return nil } func parseExtension(filename string) string { parts := strings.Split(filename, ".") if len(parts) == 0 { return "" } return parts[len(parts)-1] }