github.com/kotovmak/go-admin@v1.1.1/modules/file/local.go (about)

     1  // Copyright 2019 GoAdmin Core Team. All rights reserved.
     2  // Use of this source code is governed by a Apache-2.0 style
     3  // license that can be found in the LICENSE file.
     4  
     5  package file
     6  
     7  import (
     8  	"mime/multipart"
     9  
    10  	"github.com/kotovmak/go-admin/modules/config"
    11  )
    12  
    13  // LocalFileUploader is an Uploader of local file engine.
    14  type LocalFileUploader struct {
    15  	BasePath string
    16  }
    17  
    18  // GetLocalFileUploader return the default Uploader.
    19  func GetLocalFileUploader() Uploader {
    20  	return &LocalFileUploader{
    21  		config.GetStore().Path,
    22  	}
    23  }
    24  
    25  // Upload implements the Uploader.Upload.
    26  func (local *LocalFileUploader) Upload(form *multipart.Form) error {
    27  	return Upload(func(fileObj *multipart.FileHeader, filename string) (string, error) {
    28  		if err := SaveMultipartFile(fileObj, (*local).BasePath+"/"+filename); err != nil {
    29  			return "", err
    30  		}
    31  		return filename, nil
    32  	}, form)
    33  }