github.com/infraboard/keyauth@v0.8.1/apps/storage/spec.go (about)

     1  package storage
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"net/http"
     7  
     8  	"github.com/infraboard/keyauth/apps/token/session"
     9  )
    10  
    11  // Service 存储服务
    12  type Service interface {
    13  	UploadFile(*UploadFileRequest) error
    14  	Download(*DownloadFileRequest) error
    15  }
    16  
    17  // NewUploadFileRequestFromHTTP todo
    18  func NewUploadFileRequestFromHTTP(r *http.Request) *UploadFileRequest {
    19  	return &UploadFileRequest{
    20  		reader:  r.Body,
    21  		meta:    make(map[string]string),
    22  		Session: session.NewSession(),
    23  	}
    24  }
    25  
    26  // NewUploadFileRequest todo
    27  func NewUploadFileRequest(bucketName, fileName string, file io.ReadCloser) *UploadFileRequest {
    28  	return &UploadFileRequest{
    29  		BucketName: bucketName,
    30  		FileName:   fileName,
    31  		reader:     file,
    32  		meta:       make(map[string]string),
    33  		Session:    session.NewSession(),
    34  	}
    35  }
    36  
    37  // UploadFileRequest 上传文件请求
    38  type UploadFileRequest struct {
    39  	BucketName string
    40  	FileName   string
    41  
    42  	*session.Session
    43  	reader io.ReadCloser
    44  	meta   map[string]string
    45  }
    46  
    47  // Validate 输入参数校验
    48  func (req *UploadFileRequest) Validate() error {
    49  	if req.BucketName == "" || req.FileName == "" {
    50  		return fmt.Errorf("bucket name or file name is \"\"")
    51  	}
    52  
    53  	if req.reader == nil {
    54  		return fmt.Errorf("object file reader is nil")
    55  	}
    56  
    57  	return nil
    58  }
    59  
    60  // Meta 文件meta
    61  func (req *UploadFileRequest) Meta() map[string]string {
    62  	return req.meta
    63  }
    64  
    65  // ReadCloser todo
    66  func (req *UploadFileRequest) ReadCloser() io.ReadCloser {
    67  	return req.reader
    68  }
    69  
    70  // NewDownloadFileRequest todo
    71  func NewDownloadFileRequest(bucketName, fileID string, writer io.Writer) *DownloadFileRequest {
    72  	return &DownloadFileRequest{
    73  		BucketName: bucketName,
    74  		FileID:     fileID,
    75  		writer:     writer,
    76  		Session:    session.NewSession(),
    77  	}
    78  }
    79  
    80  // DownloadFileRequest 上传文件请求
    81  type DownloadFileRequest struct {
    82  	BucketName string
    83  	FileID     string
    84  
    85  	*session.Session
    86  	writer io.Writer
    87  }
    88  
    89  // Validate 输入参数校验
    90  func (req *DownloadFileRequest) Validate() error {
    91  	if req.BucketName == "" || req.FileID == "" {
    92  		return fmt.Errorf("bucket name or file name is \"\"")
    93  	}
    94  
    95  	if req.writer == nil {
    96  		return fmt.Errorf("object file reader is nil")
    97  	}
    98  
    99  	return nil
   100  }
   101  
   102  // Writer todo
   103  func (req *DownloadFileRequest) Writer() io.Writer {
   104  	return req.writer
   105  }