github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/resource/resource_util.go (about)

     1  package resource
     2  
     3  import (
     4  	"context"
     5  	"crypto/md5"
     6  	"fmt"
     7  	"io"
     8  	"mime/multipart"
     9  	"os"
    10  
    11  	"github.com/shirou/gopsutil/v3/disk"
    12  
    13  	"github.com/machinefi/w3bstream/pkg/depends/base/consts"
    14  	"github.com/machinefi/w3bstream/pkg/errors/status"
    15  	"github.com/machinefi/w3bstream/pkg/types"
    16  )
    17  
    18  var reserve = int64(100 * 1024 * 1024)
    19  
    20  func CheckFileMd5SumAndGetData(ctx context.Context, fh *multipart.FileHeader, md5Str string) (data []byte, sum string, err error) {
    21  	uploadConf := types.MustUploadConfigFromContext(ctx)
    22  
    23  	limit := uploadConf.FilesizeLimitBytes
    24  	diskReserve := uploadConf.DiskReserveBytes
    25  
    26  	if diskReserve != 0 {
    27  		info, _err := disk.Usage(os.TempDir())
    28  		if _err != nil {
    29  			err = status.UploadFileFailed.StatusErr().WithDesc(_err.Error())
    30  			return
    31  		}
    32  		if info.Free < uint64(diskReserve) {
    33  			err = status.UploadFileDiskLimit
    34  			return
    35  		}
    36  	}
    37  
    38  	f, _err := fh.Open()
    39  	if _err != nil {
    40  		err = status.UploadFileFailed.StatusErr().WithDesc(_err.Error())
    41  		return
    42  	}
    43  	defer f.Close()
    44  
    45  	data, err = io.ReadAll(f)
    46  	if err != nil {
    47  		return
    48  	}
    49  
    50  	if limit > 0 {
    51  		if int64(len(data)) > limit {
    52  			err = status.UploadFileSizeLimit
    53  			return
    54  		}
    55  	}
    56  
    57  	hash := md5.New()
    58  	_, err = hash.Write(data)
    59  	if err != nil {
    60  		return
    61  	}
    62  
    63  	sum = fmt.Sprintf("%x", hash.Sum(nil))
    64  	if md5Str != "" && sum != md5Str {
    65  		err = status.UploadFileMd5Unmatched
    66  		return
    67  	}
    68  	return
    69  }
    70  
    71  func UploadFile(ctx context.Context, data []byte, id types.SFID) (path string, err error) {
    72  	fs := types.MustFileSystemOpFromContext(ctx)
    73  
    74  	path = fmt.Sprintf("%s/%d", os.Getenv(consts.EnvResourceGroup), id)
    75  	err = fs.Upload(path, data)
    76  	if err != nil {
    77  		err = status.UploadFileFailed.StatusErr().WithDesc(err.Error())
    78  		return
    79  	}
    80  	return
    81  }
    82  
    83  func CheckExist(ctx context.Context, id types.SFID) bool {
    84  	fs := types.MustFileSystemOpFromContext(ctx)
    85  
    86  	path := fmt.Sprintf("%s/%d", os.Getenv(consts.EnvResourceGroup), id)
    87  	_, err := fs.StatObject(path)
    88  	return err == nil
    89  }