package util

import (
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"os"
)

// LimitUpload 限速上传
// limitBit 默认单位为bit/s。比如 41943040 为 5 MB/s。
func LimitUpload(bucket *oss.Bucket, limitBit int64, ossObjectKey string, localFilePath string) error {
	fd, err := os.Open(localFilePath)
	if err != nil {
		return err
	}
	defer fd.Close()
	if err = bucket.PutObject(ossObjectKey, fd, oss.TrafficLimitHeader(limitBit)); err != nil {
		return err
	}
	return nil
}

// LimitDownload 限速下载
// limitBit 默认单位为bit/s。比如 41943040 为 5 MB/s。
func LimitDownload(bucket *oss.Bucket, limitBit int64, ossObjectKey string, localFilePath string) error {
	if err := CreateParentDir(localFilePath); err != nil {
		return err
	}
	if err := bucket.GetObjectToFile(ossObjectKey, localFilePath, oss.TrafficLimitHeader(limitBit)); err != nil {
		return err
	}
	return nil
}
