storage

package
v0.4.19 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2024 License: GPL-3.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TableNameFileInfo = "core_upload_files"
	TableNameTempFile = "core_upload_files_tmp"
)
View Source
const (
	// SettingPrefix 配置前缀
	SettingPrefix = "cos-"
)

Variables

This section is empty.

Functions

func GenerateFileStoragePath

func GenerateFileStoragePath(purpose string, owner uint, fileExt string) string

GenerateFileStoragePath 生成文件存储路径

func GetPartCount

func GetPartCount(size int64, partSize int64) int64

GetPartCount 获取分片数量

func InitDB

func InitDB(db *gorm.DB) error

InitDB .

func SaveCopyFile

func SaveCopyFile(db *gorm.DB, fi *FileInfo) error

SaveCopyFile 保存文件副本

func SaveTempFile

func SaveTempFile(tempFile *TempFile) error

SaveTempFile 保存临时文件

func UploadFile

func UploadFile(ctx context.Context, fi *FileInfo, r io.Reader) error

UploadFile 上传文件

Types

type CheckMultipartUploadResponse

type CheckMultipartUploadResponse struct {
	// UploadID 上传ID, 用于后续上传分片
	UploadID string `json:"upload_id"`
	// PartSize 分片大小
	PartSize int64 `json:"part_size"`
	// PartCount 分片数量
	PartCount int `json:"part_count"`
	// PartNumbersUploaded 已上传的分片
	PartNumbersUploaded []int `json:"uploaded"`
	// PartNumbersNeedUpload 需要上传的分片
	PartNumbersNeedUpload []int `json:"need_upload"`
	// UploadStatus 上传状态
	UploadStatus FileStatus `json:"upload_status"`
	// FileInfo 文件信息
	FileInfo *FileInfo `json:"file_info"`
}

CheckMultipartUploadResponse 检查分片上传响应

type FileInfo

type FileInfo struct {
	gorm.Model

	// CompanyID 公司ID
	CompanyID uint `gorm:"column:company_id" json:"company_id"`
	// CustomerID 内容所属客户ID
	CustomerID uint `gorm:"column:customer_id" json:"customer_id"`
	// EmployeeID 内容所属员工ID
	EmployeeID uint `gorm:"column:employee_id" json:"employee_id"`
	// AccountID 账户ID 为内容所属客户ID或者员工ID
	AccountID uint `gorm:"column:account_id" json:"account_id"`

	// Purpose 用途分类
	Purpose config.FilePurpose `gorm:"column:purpose;type:varchar(32)" json:"purpose"`
	// Filename 原始文件名
	Filename string `gorm:"column:filename;type:varchar(128)" json:"filename"`
	// FileExt 文件扩展名
	FileExt string `gorm:"column:file_ext;type:varchar(8)" json:"file_ext"`
	// MIMEType MIME类型
	MIMEType string `gorm:"column:mime_type" json:"mime_type"`
	// Size 文件大小
	Size int64 `gorm:"column:size" json:"size"`
	// Hash 文件hash hashMethod:hashValue
	Hash string `gorm:"column:hash;type:varchar(256);index" json:"hash"`
	// ChunkHash is the hash of the chunk of the file.
	ChunkHash string `gorm:"column:chunk_hash;type:varchar(64)" json:"chunk_hash"`
	// StoragePath 存储的相对路径
	StoragePath string `gorm:"column:path;type:varchar(128)" json:"-"`
	// PublicURL 公网访问地址,如果为空,则表示只能通过预签名URL访问
	PublicURL string `gorm:"column:public_url;type:varchar(256);index" json:"-"`

	// CopyNumber 文件副本数量
	CopyNumber int `gorm:"column:copy_number;type:int;default:1" json:"-"`
	// Status 文件状态
	Status FileStatus `gorm:"column:status;type:varchar(15);default:'normal'" json:"status"`
	// ErrMsg 错误信息
	ErrMsg string `gorm:"column:err_msg;type:varchar(256)" json:"err_msg"`
}

FileInfo .

func GetCompanyFileByID

func GetCompanyFileByID(db *gorm.DB, companyID, id uint) (*FileInfo, error)

GetCompanyFileByID 通过id获取文件信息

func GetFileByChunkHash

func GetFileByChunkHash(db *gorm.DB, hashstr string, size int64) (*FileInfo, error)

GetFileByChunkHash 通过hash获取文件信息

func GetFileByHash

func GetFileByHash(db *gorm.DB, hashstr string) (*FileInfo, error)

GetFileByHash 通过hash获取文件信息

func GetFileByID

func GetFileByID(db *gorm.DB, id uint) (*FileInfo, error)

GetFileByID 通过id获取文件信息

func GetFileByPublicURL

func GetFileByPublicURL(db *gorm.DB, publicURL string) (*FileInfo, error)

GetFileByPublicURL 通过publicURL获取文件信息

func (*FileInfo) TableName

func (*FileInfo) TableName() string

TableName table name

type FileStatus

type FileStatus string

FileStatus 文件状态

const (
	// FileStatusUnknown 未知
	FileStatusUnknown FileStatus = "unknown"
	// FileStatusNormal 正常
	FileStatusNormal FileStatus = "normal"
	// FileStatusDeleted 已删除
	FileStatusDeleted FileStatus = "deleted"
	// FileStatusFailed 上传失败
	FileStatusFailed FileStatus = "failed"

	// FileStatusUploading 上传中
	FileStatusUploading FileStatus = "uploading"
	// FileStatusUploadWaitComp 上传等待完成
	FileStatusUploadWaitComp FileStatus = "upload_wait_comp"
	// FileStatusUploadSuccess 上传成功
	FileStatusUploadSuccess FileStatus = "upload_success"
)

type InitMultipartUploadRequest

type InitMultipartUploadRequest struct {
	SHA1     string `json:"sha1"`
	Size     int64  `json:"size"`
	Filename string `json:"filename"`

	// 仅后台使用
	CompanyID  uint   `json:"-"`
	CustomerID uint   `json:"-"`
	EmployeeID uint   `json:"-"`
	UploadID   string `json:"-"`
}

InitMultipartUploadRequest 分片上传请求

type LocalStorage

type LocalStorage struct {
	Dir string
	// contains filtered or unexported fields
}

LocalStorage .

func NewLocalStorage

func NewLocalStorage(cfg config.LocalStorageConfig) (*LocalStorage, error)

NewLocalStorage .

func (*LocalStorage) CopyDir added in v0.4.13

func (ls *LocalStorage) CopyDir(storagePath, dest string) error

CopyDir 复制文件或文件夹

func (*LocalStorage) DeleteFile added in v0.4.10

func (ls *LocalStorage) DeleteFile(storagePath string) error

DeleteFile 删除文件

func (*LocalStorage) GetPresignedURL

func (ls *LocalStorage) GetPresignedURL(storagePath string) (string, error)

func (*LocalStorage) GetPublicURL

func (ls *LocalStorage) GetPublicURL(storagePath string, _ bool) string

func (*LocalStorage) ReadFile added in v0.4.10

func (ls *LocalStorage) ReadFile(storagePath string) (io.ReadCloser, error)

ReadFile 获取文件内容

func (*LocalStorage) Save

func (ls *LocalStorage) Save(ctx context.Context, fi *FileInfo, r io.Reader) error

type MinFs added in v0.4.10

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

MinFs .

func NewMinFs added in v0.4.10

func NewMinFs(cfg config.MinossConfig, opt config.StorageOption) (*MinFs, error)

NewMinFs 初始化MinFs

func (*MinFs) CopyDir added in v0.4.13

func (mfs *MinFs) CopyDir(storagePath, dest string) error

CopyDir 复制文件或文件夹

func (*MinFs) DeleteFile added in v0.4.10

func (mfs *MinFs) DeleteFile(storagePath string) error

DeleteFile 删除文件

func (*MinFs) GetPresignedURL added in v0.4.10

func (mfs *MinFs) GetPresignedURL(storagePath string) (string, error)

GetPresignedURL 获取预签名URL

func (*MinFs) GetPublicURL added in v0.4.10

func (mfs *MinFs) GetPublicURL(storagePath string, _ bool) string

GetPublicURL 获取公共URL

func (*MinFs) ReadFile added in v0.4.10

func (mfs *MinFs) ReadFile(storagePath string) (io.ReadCloser, error)

ReadFile 获取文件内容

func (*MinFs) Save added in v0.4.10

func (mfs *MinFs) Save(ctx context.Context, fi *FileInfo, r io.Reader) error

Save 保存文件

type Storager

type Storager interface {
	Save(ctx context.Context, fi *FileInfo, data io.Reader) error
	GetPublicURL(storagePath string, temp bool) string
	GetPresignedURL(storagePath string) (string, error)
	ReadFile(storagePath string) (io.ReadCloser, error)
	// Stat(name string) (*FileInfo, error)
	DeleteFile(storagePath string) error
	CopyDir(storagePath, dest string) error
}

Storager .

func LoadStorager

func LoadStorager(purpose string) (Storager, error)

LoadStorager 获取存储器

func NewStorage

func NewStorage(purpose string) (Storager, error)

NewStorage .

type TempFile

type TempFile struct {
	gorm.Model

	CompanyID  uint `gorm:"column:company_id" json:"company_id"`
	CustomerID uint `gorm:"column:customer_id" json:"customer_id"`
	EmployeeID uint `gorm:"column:employee_id" json:"employee_id"`
	// Purpose is the purpose of the file.
	Purpose config.FilePurpose `gorm:"column:purpose;type:varchar(32)" json:"purpose"`

	Filename string `gorm:"column:filename;type:varchar(128)" json:"filename"`
	// FileExt is the extension of the file.
	FileExt  string `gorm:"column:file_ext;type:varchar(8)" json:"file_ext"`
	MIMEType string `gorm:"column:mime_type" json:"mime_type"`
	// Size is the size of the file.
	Size int64 `gorm:"column:size" json:"size"`
	// ChunkHash
	ChunkHash string `gorm:"column:chunk_hash;type:varchar(64)" json:"chunk_hash"`
	// StoragePath is the path of the file in the storage.
	StoragePath string `gorm:"column:path;type:varchar(128)" json:"-"`
	// PublicURL is the public URL of the file.
	PublicURL string `gorm:"column:public_url;type:varchar(256)" json:"-"`

	// ErrMsg is the error message of the file.
	ErrMsg string `gorm:"column:err_msg;type:varchar(256)" json:"err_msg"`

	// Annotations is the annotations of the file.
	Annotations    map[string]string `gorm:"-" json:"annotations"`
	AnnotationsStr string            `gorm:"column:annotations;type:varchar(255)" json:"-"`

	// ThirdUploadID 对象存储的分片上传ID
	ThirdUploadID string `gorm:"column:third_upload_id;type:varchar(128)" json:"-"`
	// ExpiredAt 对象存储的分片上传ID过期时间
	ExpiredAt time.Time `gorm:"column:expired_at" json:"-"`

	// PartSize 对象存储的分片大小
	PartSize int64 `gorm:"column:part_size" json:"-"`
	// PartCount 对象存储的分片数量
	PartCount int64 `gorm:"column:part_count" json:"-"`
}

TempFile 临时文件信息

func GetTempFileByHash

func GetTempFileByHash(hashstr string, size int64) (*TempFile, error)

GetTempFileByHash 根据hash获取临时文件

func GetTempFileByThirdUploadID

func GetTempFileByThirdUploadID(companyID uint, thirdUploadID string) (*TempFile, error)

GetTempFileByThirdUploadID 根据thirdUploadID获取临时文件

func (*TempFile) TableName

func (*TempFile) TableName() string

TableName 表名

type TencentCos

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

TencentCos ..

func NewTencentCos

func NewTencentCos(cfg config.TencentCOSConfig, opt config.StorageOption) (*TencentCos, error)

NewTencentCos .

func (*TencentCos) CompleteUploadTask

func (tc *TencentCos) CompleteUploadTask(ctx context.Context, tempFile *TempFile) error

CompleteUploadTask .

func (*TencentCos) CopyDir added in v0.4.13

func (tc *TencentCos) CopyDir(storagePath, dest string) error

CopyDir 复制文件或文件夹

func (*TencentCos) DeleteFile added in v0.4.10

func (tc *TencentCos) DeleteFile(storagePath string) error

DeleteFile 删除文件

func (*TencentCos) GetPresignedURL

func (tc *TencentCos) GetPresignedURL(storagePath string) (string, error)

GetPresignedURL 获取预签名URL

func (*TencentCos) GetPublicURL

func (tc *TencentCos) GetPublicURL(storagePath string, temp bool) string

GetPublicURL .

func (*TencentCos) InitUploadTask

func (tc *TencentCos) InitUploadTask(ctx context.Context, tempFile *TempFile) error

InitUploadTask .

func (*TencentCos) ListUploadExistsTrunk

func (tc *TencentCos) ListUploadExistsTrunk(ctx context.Context, tempFile *TempFile) ([]int, error)

ListUploadExistsTrunk .

func (*TencentCos) ReadFile added in v0.4.10

func (tc *TencentCos) ReadFile(storagePath string) (io.ReadCloser, error)

ReadFile 获取文件内容

func (*TencentCos) Save

func (tc *TencentCos) Save(ctx context.Context, fi *FileInfo, r io.Reader) error

Save .

func (*TencentCos) UploadTrunk

func (tc *TencentCos) UploadTrunk(ctx context.Context, tempFile *TempFile, partNumber int, r io.Reader, size int64) error

UploadTrunk .

type Uploader

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

Uploader 大文件分片上传

func GetUploader

func GetUploader(ctx context.Context, logger *zap.SugaredLogger, group, key string, req InitMultipartUploadRequest) (*Uploader, error)

GetUploader 获取大文件上传器

func (*Uploader) Cancel

func (u *Uploader) Cancel() error

Cancel 取消上传

func (*Uploader) CompleteUpload

func (u *Uploader) CompleteUpload(db *gorm.DB) (*FileInfo, error)

CompleteUpload 合并分片

func (*Uploader) ListTrunks

func (u *Uploader) ListTrunks() (*CheckMultipartUploadResponse, error)

ListTrunks 列出分片

func (*Uploader) UploadPart

func (u *Uploader) UploadPart(partNumber int, data io.Reader, size int64) error

UploadPart 上传分片

Jump to

Keyboard shortcuts

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