96 lines
2.4 KiB
Go
96 lines
2.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.optclblast.xyz/draincloud/draincloud-core/internal/common"
|
|
filesengine "git.optclblast.xyz/draincloud/draincloud-core/internal/files_engine"
|
|
"git.optclblast.xyz/draincloud/draincloud-core/internal/handler"
|
|
"git.optclblast.xyz/draincloud/draincloud-core/internal/resolvers/auth"
|
|
)
|
|
|
|
const (
|
|
maxFileSize = 10 << 30
|
|
)
|
|
|
|
type UploadFileHandler struct {
|
|
*handler.BaseHandler
|
|
filesEngine *filesengine.FilesEngine
|
|
}
|
|
|
|
func NewUploadFileHandler(
|
|
filesEngine *filesengine.FilesEngine,
|
|
) *UploadFileHandler {
|
|
h := &UploadFileHandler{
|
|
filesEngine: filesEngine,
|
|
BaseHandler: handler.New().
|
|
WithName("uploadfilev1").
|
|
WithRequiredResolveParams(
|
|
auth.AuthResolverV1Name,
|
|
// TODO with MultipartReaderResolverV1Name
|
|
// or
|
|
// MultipartDataResolverV1Name
|
|
),
|
|
}
|
|
h.WithProcessFunc(h.process)
|
|
return h
|
|
}
|
|
|
|
func (d *UploadFileHandler) process(ctx context.Context, req *common.Request, w handler.Writer) error {
|
|
// TODO fetch (interface{ ParseMultipartForm(size int) error }) from req.GetValue[ParseMultipartFormer](req.ResolveValues)
|
|
|
|
// if err := req.RawReq.ParseMultipartForm(maxFileSize); err != nil {
|
|
// logger.Error(ctx, "uploadFile handler error", logger.Err(err))
|
|
// return err
|
|
// }
|
|
|
|
// if err := d.uploadFile(ctx, userID); err != nil {
|
|
// logger.Error(ctx, "uploadFile handle", logger.Err(err))
|
|
// writeError(ctx, err)
|
|
// return
|
|
// }
|
|
return nil
|
|
}
|
|
|
|
// func (d *UploadFileHandler) uploadFile(ctx context.Context, req *common.Request) error {
|
|
// title := ctx.PostForm("file")
|
|
// logger.Info(ctx, "uploadFile", slog.Any("postForm data", spew.Sdump(title)))
|
|
|
|
// file, header, err := req.RawReq.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]
|
|
// }
|