Files
EdgeNode/internal/caches/writer_file.go

159 lines
3.1 KiB
Go
Raw Normal View History

2020-10-05 16:55:14 +08:00
package caches
import (
2021-01-13 12:02:50 +08:00
"encoding/binary"
"errors"
2021-01-13 12:02:50 +08:00
"github.com/iwind/TeaGo/types"
"io"
2020-10-05 16:55:14 +08:00
"os"
2021-01-13 12:02:50 +08:00
"strings"
"sync"
2020-10-05 16:55:14 +08:00
)
type FileWriter struct {
rawWriter *os.File
key string
2021-01-13 12:02:50 +08:00
headerSize int64
bodySize int64
2020-10-05 16:55:14 +08:00
expiredAt int64
endFunc func()
once sync.Once
2020-10-05 16:55:14 +08:00
}
func NewFileWriter(rawWriter *os.File, key string, expiredAt int64, endFunc func()) *FileWriter {
2020-10-05 16:55:14 +08:00
return &FileWriter{
key: key,
rawWriter: rawWriter,
expiredAt: expiredAt,
endFunc: endFunc,
2020-10-05 16:55:14 +08:00
}
}
// WriteHeader 写入数据
2021-01-13 12:02:50 +08:00
func (this *FileWriter) WriteHeader(data []byte) (n int, err error) {
n, err = this.rawWriter.Write(data)
this.headerSize += int64(n)
if err != nil {
_ = this.Discard()
}
return
}
// WriteHeaderLength 写入Header长度数据
2021-01-13 12:02:50 +08:00
func (this *FileWriter) WriteHeaderLength(headerLength int) error {
bytes4 := make([]byte, 4)
binary.BigEndian.PutUint32(bytes4, uint32(headerLength))
_, err := this.rawWriter.Seek(SizeExpiresAt+SizeStatus+SizeURLLength, io.SeekStart)
if err != nil {
_ = this.Discard()
return err
}
_, err = this.rawWriter.Write(bytes4)
if err != nil {
_ = this.Discard()
return err
}
return nil
}
// Write 写入数据
2020-10-05 16:55:14 +08:00
func (this *FileWriter) Write(data []byte) (n int, err error) {
n, err = this.rawWriter.Write(data)
2021-01-13 12:02:50 +08:00
this.bodySize += int64(n)
2020-10-05 16:55:14 +08:00
if err != nil {
2021-01-13 12:02:50 +08:00
_ = this.Discard()
2020-10-05 16:55:14 +08:00
}
return
}
// WriteAt 在指定位置写入数据
func (this *FileWriter) WriteAt(offset int64, data []byte) error {
_ = data
_ = offset
return errors.New("not supported")
}
// WriteBodyLength 写入Body长度数据
2021-01-13 12:02:50 +08:00
func (this *FileWriter) WriteBodyLength(bodyLength int64) error {
bytes8 := make([]byte, 8)
binary.BigEndian.PutUint64(bytes8, uint64(bodyLength))
_, err := this.rawWriter.Seek(SizeExpiresAt+SizeStatus+SizeURLLength+SizeHeaderLength, io.SeekStart)
if err != nil {
_ = this.Discard()
return err
}
_, err = this.rawWriter.Write(bytes8)
if err != nil {
_ = this.Discard()
return err
}
return nil
}
// Close 关闭
2020-10-05 16:55:14 +08:00
func (this *FileWriter) Close() error {
defer this.once.Do(func() {
this.endFunc()
})
path := this.rawWriter.Name()
2021-01-13 12:02:50 +08:00
err := this.WriteHeaderLength(types.Int(this.headerSize))
2020-10-05 16:55:14 +08:00
if err != nil {
_ = this.rawWriter.Close()
_ = os.Remove(path)
2021-01-13 12:02:50 +08:00
return err
}
err = this.WriteBodyLength(this.bodySize)
if err != nil {
_ = this.rawWriter.Close()
_ = os.Remove(path)
2021-01-13 12:02:50 +08:00
return err
2020-10-05 16:55:14 +08:00
}
2021-01-13 12:02:50 +08:00
err = this.rawWriter.Close()
if err != nil {
_ = os.Remove(path)
} else {
err = os.Rename(path, strings.Replace(path, ".tmp", "", 1))
if err != nil {
_ = os.Remove(path)
}
}
2020-10-05 16:55:14 +08:00
return err
}
// Discard 丢弃
2020-10-05 16:55:14 +08:00
func (this *FileWriter) Discard() error {
defer this.once.Do(func() {
this.endFunc()
})
2021-01-13 12:02:50 +08:00
_ = this.rawWriter.Close()
2020-10-05 16:55:14 +08:00
err := os.Remove(this.rawWriter.Name())
return err
}
2021-01-13 12:02:50 +08:00
func (this *FileWriter) HeaderSize() int64 {
return this.headerSize
}
func (this *FileWriter) BodySize() int64 {
return this.bodySize
2020-10-05 16:55:14 +08:00
}
func (this *FileWriter) ExpiredAt() int64 {
return this.expiredAt
}
func (this *FileWriter) Key() string {
return this.key
}
// ItemType 获取内容类型
func (this *FileWriter) ItemType() ItemType {
return ItemTypeFile
}