file

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const (
	Separator = string(filepath.Separator)
)

Variables

View Source
var FileCache = struct {
	sync.RWMutex
	m map[string]*FileInfoCache
}{/* contains filtered or unexported fields */}

FileCache 全局文件信息缓存

Functions

func Basename added in v0.1.6

func Basename(path string) string

func Chdir added in v0.1.6

func Chdir(dir string) (err error)

Chdir changes the current working directory to the named directory. If there is an error, it will be of type *PathError.

func Create added in v0.1.6

func Create(path string) (*os.File, error)

Create creates a file with given `path` recursively. The parameter `path` is suggested to be absolute path.

func Dir added in v0.1.6

func Dir(path string) string

func Exists added in v0.1.6

func Exists(path string) bool

Exists checks whether given `path` exist.

func Ext added in v0.1.6

func Ext(path string) string

Example: Ext("main.go") => .go Ext("api.json") => .json

func InitFileSystem

func InitFileSystem()

InitFileSystem 初始化文件系统

func IsAllowedImageExt

func IsAllowedImageExt(ext string) bool

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

func IsDir added in v0.1.6

func IsDir(path string) bool

IsDir checks whether given `path` a directory. Note that it returns false if the `path` does not exist.

func IsFile added in v0.1.6

func IsFile(path string) bool

IsFile checks whether given `path` a file, which means it's not a directory. Note that it returns false if the `path` does not exist.

func Join added in v0.0.7

func Join(paths ...string) string

func Mkdir added in v0.1.6

func Mkdir(path string) (err error)

Mkdir creates directories recursively with given `path`. The parameter `path` is suggested to be an absolute path instead of relative one.

func Move added in v0.1.6

func Move(src string, dst string) (err error)

Move renames (moves) `src` to `dst` path. If `dst` already exists and is not a directory, it'll be replaced.

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 Pwd added in v0.1.6

func Pwd() string

Pwd returns absolute path of current working directory. Note that it returns an empty string if retrieving current working directory failed.

func RealPath added in v0.1.6

func RealPath(path string) string

RealPath converts the given `path` to its absolute path and checks if the file path exists. If the file does not exist, return an empty string.

func RefreshAllFileCache

func RefreshAllFileCache()

RefreshAllFileCache 刷新所有文件缓存

func RefreshFileCache

func RefreshFileCache(fullPath string) error

RefreshFileCache 强制刷新文件缓存

func Rename added in v0.1.6

func Rename(src string, dst string) error

Rename is alias of Move. See Move.

func Search(name string, prioritySearchPaths ...string) (string, error)

Search searches for a file by name in the given priority search paths, current working directory, and executable directory. It returns the absolute file path if found, or an empty string and error if not found.

func SetCacheExpiration

func SetCacheExpiration(duration time.Duration)

SetCacheExpiration 设置缓存过期时间

func Stat added in v0.1.6

func Stat(path string) (os.FileInfo, error)

Stat returns a FileInfo describing the named file. If there is an error, it will be of type *PathError.

func Temp added in v0.0.7

func Temp(names ...string) string

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