github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/pkg/filesystem/fsctx/stream.go (about)

     1  package fsctx
     2  
     3  import (
     4  	"errors"
     5  	"github.com/HFO4/aliyun-oss-go-sdk/oss"
     6  	"io"
     7  	"time"
     8  )
     9  
    10  type WriteMode int
    11  
    12  const (
    13  	Overwrite WriteMode = 0x00001
    14  	// Append 只适用于本地策略
    15  	Append WriteMode = 0x00002
    16  	Nop    WriteMode = 0x00004
    17  )
    18  
    19  type UploadTaskInfo struct {
    20  	Size            uint64
    21  	MimeType        string
    22  	FileName        string
    23  	VirtualPath     string
    24  	Mode            WriteMode
    25  	Metadata        map[string]string
    26  	LastModified    *time.Time
    27  	SavePath        string
    28  	UploadSessionID *string
    29  	AppendStart     uint64
    30  	Model           interface{}
    31  	Src             string
    32  }
    33  
    34  // Get mimetype of uploaded file, if it's not defined, detect it from file name
    35  func (u *UploadTaskInfo) DetectMimeType() string {
    36  	if u.MimeType != "" {
    37  		return u.MimeType
    38  	}
    39  
    40  	return oss.TypeByExtension(u.FileName)
    41  }
    42  
    43  // FileHeader 上传来的文件数据处理器
    44  type FileHeader interface {
    45  	io.Reader
    46  	io.Closer
    47  	io.Seeker
    48  	Info() *UploadTaskInfo
    49  	SetSize(uint64)
    50  	SetModel(fileModel interface{})
    51  	Seekable() bool
    52  }
    53  
    54  // FileStream 用户传来的文件
    55  type FileStream struct {
    56  	Mode            WriteMode
    57  	LastModified    *time.Time
    58  	Metadata        map[string]string
    59  	File            io.ReadCloser
    60  	Seeker          io.Seeker
    61  	Size            uint64
    62  	VirtualPath     string
    63  	Name            string
    64  	MimeType        string
    65  	SavePath        string
    66  	UploadSessionID *string
    67  	AppendStart     uint64
    68  	Model           interface{}
    69  	Src             string
    70  }
    71  
    72  func (file *FileStream) Read(p []byte) (n int, err error) {
    73  	if file.File != nil {
    74  		return file.File.Read(p)
    75  	}
    76  
    77  	return 0, io.EOF
    78  }
    79  
    80  func (file *FileStream) Close() error {
    81  	if file.File != nil {
    82  		return file.File.Close()
    83  	}
    84  
    85  	return nil
    86  }
    87  
    88  func (file *FileStream) Seek(offset int64, whence int) (int64, error) {
    89  	if file.Seekable() {
    90  		return file.Seeker.Seek(offset, whence)
    91  	}
    92  
    93  	return 0, errors.New("no seeker")
    94  }
    95  
    96  func (file *FileStream) Seekable() bool {
    97  	return file.Seeker != nil
    98  }
    99  
   100  func (file *FileStream) Info() *UploadTaskInfo {
   101  	return &UploadTaskInfo{
   102  		Size:            file.Size,
   103  		MimeType:        file.MimeType,
   104  		FileName:        file.Name,
   105  		VirtualPath:     file.VirtualPath,
   106  		Mode:            file.Mode,
   107  		Metadata:        file.Metadata,
   108  		LastModified:    file.LastModified,
   109  		SavePath:        file.SavePath,
   110  		UploadSessionID: file.UploadSessionID,
   111  		AppendStart:     file.AppendStart,
   112  		Model:           file.Model,
   113  		Src:             file.Src,
   114  	}
   115  }
   116  
   117  func (file *FileStream) SetSize(size uint64) {
   118  	file.Size = size
   119  }
   120  
   121  func (file *FileStream) SetModel(fileModel interface{}) {
   122  	file.Model = fileModel
   123  }