util

package module
v0.0.0-...-f01c5e8 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 16 Imported by: 12

Documentation

Index

Constants

View Source
const (
	GB              = 1 << 30
	MB              = 1024 * 1024
	Day             = 24 * 60 * 60
	RFC3339         = "2006-01-02 15:04:05Z07:00"
	MIMEOctetStream = "application/octet-stream"
	NormalFilePerm  = 0666
	NormalDirPerm   = 0750
	RepoName        = "Wuliu File Manager"
	RepoURL         = "https://github.com/ahui2016/wuliu"
	ProjectInfoPath = "project.json"
	FileCheckedPath = "file_checked.json"
	DatabasePath    = "project.db"
)
View Source
const (
	FILES      = "files"
	METADATA   = "metadata"
	INPUT      = "input"
	BUFFER     = "buffer"
	WEBPAGES   = "webpages"
	RECYCLEBIN = "recyclebin"
)

Variables

View Source
var (
	FilesBucket       = []byte("FilesBucket")
	ChecksumBucket    = []byte("ChecksumBucket")
	SizeBucket        = []byte("SizeBucket")
	TypeBucket        = []byte("TypeBucket")
	LikeBucket        = []byte("LikeBucket")
	LabelBucket       = []byte("LabelBucket")
	NotesBucket       = []byte("NotesBucket")
	KeywordsBucket    = []byte("KeywordsBucket")
	CollectionsBucket = []byte("CollectionsBucket")
	AlbumsBucket      = []byte("AlbumsBucket")
	CTimeBucket       = []byte("CTimeBucket")
	UTimeBucket       = []byte("UTimeBucket")
)
View Source
var (
	Epoch     = time.Unix(0, 0).Format(RFC3339)
	Separator = string(filepath.Separator)
)

Functions

func AddFilesToDB

func AddFilesToDB(files []FileAndMeta, db *bolt.DB) error

func AddToFileChecked

func AddToFileChecked(files []*File) error

func AscOrDesc

func AscOrDesc(descending bool) string

func CRC32Str36

func CRC32Str36(s string) string

CRC32Str36 把一个字符串转化为 crc32, 再转化为 36 进制。

func CheckNotAllowInBackup

func CheckNotAllowInBackup()

func CreateDatabase

func CreateDatabase()

func DamagedOfFileChecked

func DamagedOfFileChecked(fcMap map[string]*FileChecked) (ids []string)

func DatabaseFilesSize

func DatabaseFilesSize(db *bolt.DB) (fileN int, totalSize int64, err error)

func DeleteFilesByID

func DeleteFilesByID(ids []string, db *bolt.DB) error

DeleteFilesByID 尝试删除档案,包括档案本身, metadata 以及数据库条目。 注意,这里说的删除是将档案移动到专案根目录的 recyclebin 中, 如果 recyclebin 里有同名档案则直接覆盖。

func DeleteFromFileChecked

func DeleteFromFileChecked(ids []string) error

func DeleteInDB

func DeleteInDB(ids []string, db *bolt.DB) error

func DirIsEmpty

func DirIsEmpty(dirpath string) (ok bool, err error)

func DirIsNotEmpty

func DirIsNotEmpty(dirpath string) (ok bool, err error)

func ExecutableDir

func ExecutableDir() string

func FileSizeToString

func FileSizeToString(size float64, fixed int) string

FileSizeToString 把文件大小转换为方便人类阅读的格式。 fixed 指定小数点后几位, 设为负数表示不限制小数位。

func FindOrphans

func FindOrphans() (fileOrphans, metaOrphans []string, err error)

func FolderMustEmpty

func FolderMustEmpty(folder string)

func GetCwd

func GetCwd() string

func GetExePath

func GetExePath() string

GetExePath returns the path name for the executable that started the current process.

func GetFilenamesBase

func GetFilenamesBase(folder string) ([]string, error)

GetFilenamesBase 假设 folder 里全是普通档案,没有资料夹。

func GetKeysAndIdsLength

func GetKeysAndIdsLength(bucketName []byte, db *bolt.DB) (keyAndLength map[string]int, err error)

func IdsToNames

func IdsToNames(ids []string, db *bolt.DB) (names []string, err error)

func InitFileChecked

func InitFileChecked()

func KeyExistsInBucket

func KeyExistsInBucket(key []byte, b *bolt.Bucket) bool

func MakeFolders

func MakeFolders(verbose bool)

func MkdirIfNotExists

func MkdirIfNotExists(name string) error

MkdirIfNotExists 创建資料夹, 忽略 ErrExist. 在 Windows 里, 文件夹的只读属性不起作用, 为了统一行为, 不把資料夹设为只读.

func MustInWuliu

func MustInWuliu()

func NameToID

func NameToID(name string) string

NameToID 目前采用 CRC32Str36

func NamesInBuffer

func NamesInBuffer() ([]string, error)

func NamesInFiles

func NamesInFiles() ([]string, error)

func NamesInInput

func NamesInInput() ([]string, error)

func NamesToIds

func NamesToIds(names []string) (ids []string)

func Now

func Now() string

func OpenDB

func OpenDB(root string) (*bolt.DB, error)

func PaddingRight

func PaddingRight(s, char string, length int) string

func PathExists

func PathExists(name string) bool

func PathNotExists

func PathNotExists(name string) (ok bool)

func PrintErrorExit

func PrintErrorExit(err error)

func PrintFilesMore

func PrintFilesMore(files []*File)

func PrintFilesSimple

func PrintFilesSimple(files []*File)

func PrintList

func PrintList[T any](list []T)

func PrintListWithSuffix

func PrintListWithSuffix[T any](list []T, suffix string)

func PrintVersionExit

func PrintVersionExit(ok bool)

func PrintWhereExit

func PrintWhereExit(ok bool)

func PutToBucket

func PutToBucket(key, value []byte, b *bolt.Bucket) error

func ReadFileChecked

func ReadFileChecked(root string) (fcMap map[string]*FileChecked, err error)

func RebuildCTimeBucket

func RebuildCTimeBucket(db *bolt.DB) error

func RebuildDatabase

func RebuildDatabase(root string)

RebuildDatabase 删除数据库,然后重建数据库并且重新填充数据。

func RebuildSomeBuckets

func RebuildSomeBuckets(db *bolt.DB) error

RebuildSomeBuckets 重建数据库的一部分索引,不需要读取硬盘。 由于不需要读取硬盘,因此不能反映最新的变化。

func RenameCTime

func RenameCTime(ctime, oldid, newid string, tx *bolt.Tx) error

RenameCTime 在更改檔案名稱時順便更新 CTimeBucket.

func StringSetToSlice

func StringSetToSlice(m map[string]bool) (keys []string)

func StringSliceToSet

func StringSliceToSet(s []string) map[string]bool

StringSliceToSet 用 map 當作 set

func TypeByFilename

func TypeByFilename(filename string) (filetype string)

https://github.com/gofiber/fiber/blob/master/utils/http.go (edited).

func WrapErrors

func WrapErrors(allErrors ...error) (wrapped error)

WrapErrors 把多个错误合并为一个错误.

func WriteFile

func WriteFile(name string, data []byte) error

WriteFile 写檔案, 使用权限 0666

func WriteJSON

func WriteJSON(data any, filename string) ([]byte, error)

WriteJSON 把 data 转换为漂亮格式的 JSON 并写入檔案 filename 中。

func WriteMSGP

func WriteMSGP(data any, filename string) error

WriteMSGP 把 data 转换为漂亮格式的 MessagePack 并写入檔案 filename 中。

func WriteProjectInfo

func WriteProjectInfo(info ProjectInfo) error

Types

type Base64String

type Base64String = string

type EditFiles

type EditFiles struct {
	IDs         []string `json:"ids"`         // 通过 ID 指定档案
	Filenames   []string `json:"filenames"`   // 通过档案名称指定档案
	Like        int      `json:"like"`        // 點贊
	Label       string   `json:"label"`       // 标签,便於搜尋
	Notes       string   `json:"notes"`       // 備註,便於搜尋
	Keywords    []string `json:"keywords"`    // 關鍵詞, 便於搜尋
	Collections []string `json:"collections"` // 集合(分组),一个档案可属于多个集合
	Albums      []string `json:"albums"`      // 相册(专辑),主要用于图片和音乐
}

EditFiles 用于批量修改档案属性。

func NewEditFiles

func NewEditFiles(ids, filenames []string) *EditFiles

func (*EditFiles) Check

func (ef *EditFiles) Check() (err error)

type File

type File struct {
	ID          string   `json:"id"`          // 档案名称的 CRC32
	Filename    string   `json:"filename"`    // 档案名称
	Checksum    string   `json:"checksum"`    // BLAKE2b
	Size        int64    `json:"size"`        // length in bytes for regular files
	Type        string   `json:"type"`        // 檔案類型, 例: text/js, office/docx
	Like        int      `json:"like"`        // 點贊
	Label       string   `json:"label"`       // 标签,便於搜尋
	Notes       string   `json:"notes"`       // 備註,便於搜尋
	Keywords    []string `json:"keywords"`    // 關鍵詞, 便於搜尋
	Collections []string `json:"collections"` // 集合(分组),一个档案可属于多个集合
	Albums      []string `json:"albums"`      // 相册(专辑),主要用于图片和音乐
	CTime       string   `json:"ctime"`       // RFC3339 檔案入庫時間
	UTime       string   `json:"utime"`       // RFC3339 檔案更新時間
}

func FilesExistInDB

func FilesExistInDB(files []*File, db *bolt.DB) (existFiles []*File)

func GetAllFiles

func GetAllFiles(db *bolt.DB) (files []*File, err error)

func GetAllPics

func GetAllPics(db *bolt.DB) (pics []*File, err error)

func GetFileInBucket

func GetFileInBucket(id string, b *bolt.Bucket) (f File, err error)

func GetFileInDB

func GetFileInDB(id string, db *bolt.DB) (f File, err error)

func GetFilesByIDs

func GetFilesByIDs(ids []string, tx *bolt.Tx) (files []*File, err error)

func GetFilesInBucket

func GetFilesInBucket(pattern, mode string, bucketName []byte, db *bolt.DB) (files []*File, err error)

GetFilesInBucket, 其中 mode 是 exactly/contains/prefix/suffix 其中之一。

func NewFile

func NewFile(name string) *File

func NewFilesFrom

func NewFilesFrom(names []string, folder string) (files []*File, err error)

NewFilesFrom 把档案名 (names) 转换为 files, 此时假设档案在 folder 资料夹内。

func ReadFile

func ReadFile(filePath string) (f File)

type FileAndMeta

type FileAndMeta struct {
	*File
	Metadata []byte
}

type FileChecked

type FileChecked struct {
	ID      string // 档案名称的 CRC32
	Checked string // RFC3339 上次校驗檔案完整性的時間
	Damaged bool   // 上次校驗結果 (檔案是否損壞)
}

type HexString

type HexString = string

func FileSum512

func FileSum512(name string) (HexString, error)

BLAKE2b is faster than MD5, SHA-1, SHA-2, and SHA-3, on 64-bit x86-64 and ARM architectures. https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE2 https://blog.min.io/fast-hashing-in-golang-using-blake2/ https://pkg.go.dev/crypto/sha256#example-New-File

type ProjectInfo

type ProjectInfo struct {
	RepoName        string   // 用于判断资料夹是否 Wuliu 专案
	ProjectName     string   // 备份时要求专案名称相同
	IsBackup        bool     // 是否副本(副本禁止添加、删除等)
	Projects        []string // 第一个是主专案,然后是备份专案
	LastBackupAt    []string // 上次备份时间
	CheckInterval   int      // 检查完整性, 单位: day
	CheckSizeLimit  int      // 检查完整性, 单位: MB
	ExportSizeLimit int64    // 導出檔案體積上限,單位: MB
	ThumbSize       [2]int   // 縮略圖尺寸
}

func NewProjectInfo

func NewProjectInfo(name string) (info ProjectInfo)

func ReadProjectInfo

func ReadProjectInfo(root string) (info ProjectInfo)

type ProjectStatus

type ProjectStatus struct {
	*ProjectInfo
	Root         string // 专案根目录
	TotalSize    int64  // 全部檔案體積合計
	FilesCount   int    // 檔案數量合計
	DamagedCount int    // 受損檔案數量合計
}

Jump to

Keyboard shortcuts

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