package service import ( "context" "fmt" "mime/multipart" "github.com/google/uuid" "github.com/spf13/afero" "gitrepo.ru/neonxp/nquest/pkg/models" "gorm.io/gorm" ) type File struct { DB *gorm.DB store afero.Fs } func NewFile(db *gorm.DB, store afero.Fs) *File { return &File{ DB: db, store: store, } } func (u *File) Upload( ctx context.Context, filename string, contentType string, size int, questID uuid.UUID, r multipart.File, ) (uuid.UUID, error) { defer r.Close() file := &models.File{ ID: uuid.New(), Filename: filename, ContentType: contentType, QuestID: questID, Size: size, } if err := u.store.MkdirAll(questID.String(), 0755); err != nil { return file.ID, err } filePath := fmt.Sprintf("%s/%s", questID.String(), filename) if err := afero.WriteReader(u.store, filePath, r); err != nil { return file.ID, err } return file.ID, u.DB.WithContext(ctx).Create(file).Error } func (u *File) GetFilesByQuest(ctx context.Context, quest uuid.UUID) ([]*models.File, error) { list := make([]*models.File, 0) return list, u.DB.WithContext(ctx).Find(&list, `quest_id = ?`, quest.String()).Error } func (u *File) GetFilesByID(ctx context.Context, fileID uuid.UUID) (*models.File, error) { file := &models.File{} return file, u.DB.WithContext(ctx).First(file, fileID).Error }