file

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2024 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package file 提供文件处理、上传和水印功能

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidPath  = errors.New("无效的文件路径")
	ErrFileNotExist = errors.New("文件不存在")
	ErrReadFailed   = errors.New("读取文件失败")
	ErrWriteFailed  = errors.New("写入文件失败")
)
View Source
var FileCache = struct {
	sync.RWMutex
	m map[string]*FileInfoCache
}{/* contains filtered or unexported fields */}

FileCache 全局文件信息缓存

Functions

func InitFileSystem

func InitFileSystem()

InitFileSystem 初始化文件系统

func IsAllowedImageExt

func IsAllowedImageExt(ext string) bool

IsAllowedImageExt 检查是否是允许的图片扩展名

func ProcessFilesInParallel

func ProcessFilesInParallel(files []string, processor func(string) error, maxConcurrency int64) error

ProcessFilesInParallel 并行处理多个文件,带并发限制

func ProcessLargeFile

func ProcessLargeFile(filePath string, processor ChunkProcessor) error

ProcessLargeFile 处理大文件

func RefreshAllFileCache

func RefreshAllFileCache()

RefreshAllFileCache 刷新所有文件缓存

func RefreshFileCache

func RefreshFileCache(fullPath string) error

RefreshFileCache 强制刷新文件缓存

func SetCacheExpiration

func SetCacheExpiration(duration time.Duration)

SetCacheExpiration 设置缓存过期时间

Types

type ChunkProcessor

type ChunkProcessor struct {
	ChunkSize int64
	Handler   func([]byte) error
}

ChunkProcessor 用于处理大文件的分块处理器

type File

type File struct {
	// contains filtered or unexported fields
}

File 文件类

func (*File) ChunkRead

func (f *File) ChunkRead(chunkSize int64, handler func([]byte) error) error

ChunkRead 分块读取大文件

func (*File) Ext

func (f *File) Ext() string

Ext 文件后缀名

func (*File) FullPath

func (f *File) FullPath() string

FullPath 文件完整路径

func (*File) ModifyTime

func (f *File) ModifyTime() (time.Time, error)

ModifyTime 文件修改时间

func (*File) Name

func (f *File) Name() string

Name 文件名

func (*File) Path

func (f *File) Path() string

Path 文件路径

func (*File) ReadAll

func (f *File) ReadAll() ([]byte, error)

ReadAll 读取全部

func (*File) ReadBlock

func (f *File) ReadBlock(size int64) ([]byte, int64, error)

ReadBlock 读取块

func (*File) ReadLine

func (f *File) ReadLine() (string, error)

ReadLine 读取一行

func (*File) Size

func (f *File) Size() (int64, error)

Size 获得文件大小

func (*File) StreamRead

func (f *File) StreamRead(handler func([]byte) error) error

StreamRead 流式读取文件

func (*File) Truncate

func (f *File) Truncate() error

Truncate 清空文件

func (*File) Write

func (f *File) Write(b []byte) error

Write 写入

func (*File) WriteAppend

func (f *File) WriteAppend(b []byte) error

WriteAppend 追加文件

func (*File) WriteAt

func (f *File) WriteAt(b []byte, offset int64) (int, error)

WriteAt 在固定点写入

type FileError

type FileError struct {
	Op  string
	Err error
}

FileError 自定义错误类型

func (*FileError) Error

func (e *FileError) Error() string

type FileHandler

type FileHandler struct {
	// contains filtered or unexported fields
}

FileHandler 处理文件上传和水印

func NewFileHandler

func NewFileHandler(cfg UploadConfig) (*FileHandler, error)

NewFileHandler 创建一个新的 FileHandler 实例

func (*FileHandler) Upload

func (fh *FileHandler) Upload(field string, r *http.Request) ([]UploadResult, error)

Upload 执行上传操作

type FileInfoCache

type FileInfoCache struct {
	// contains filtered or unexported fields
}

FileInfoCache 用于缓存文件信息

type Filer

type Filer interface {
	Size() (int64, error)
	ReadAll() ([]byte, error)
	ReadLine() (string, error)
	ReadBlock(size int64) ([]byte, int64, error)
	Write(b []byte) error
	WriteAt(b []byte, offset int64) (int, error)
	WriteAppend(b []byte) error
	Truncate() error
	Ext() string
	Name() string
	Path() string
	FullPath() string
	ModifyTime() (time.Time, error)
	StreamRead(handler func([]byte) error) error
	ChunkRead(chunkSize int64, handler func([]byte) error) error
}

Filer 文件接口

func NewFile

func NewFile(fullPath string) Filer

NewFile 新建文件

type UploadConfig

type UploadConfig struct {
	Dir        string
	Format     string
	MaxSize    int64
	AllowedExt []string
	Watermark  *WatermarkConfig
}

UploadConfig 上传配置

type UploadResult

type UploadResult struct {
	Filename string
	Error    error
}

UploadResult 表示单个文件的上传结果

type Watermark

type Watermark struct {
	// contains filtered or unexported fields
}

Watermark 用于给图片添加水印功能

func NewWatermark

func NewWatermark(cfg *WatermarkConfig) (*Watermark, error)

NewWatermark 创建一个新的 Watermark 实例

func (*Watermark) Mark

func (w *Watermark) Mark(file *os.File, ext string) error

Mark 将水印写入文件 Mark 将水印写入文件

type WatermarkConfig

type WatermarkConfig struct {
	Path         string
	Padding      int
	Pos          WatermarkPos
	Transparency uint8
	Rotation     float64
}

WatermarkConfig 水印配置

type WatermarkPos

type WatermarkPos int

WatermarkPos 表示水印的位置

const (
	TopLeft WatermarkPos = iota
	TopRight
	BottomLeft
	BottomRight
	Center
)

水印的位置常量

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL