1 Star 1 Fork 1

xiaoyutab / xgotool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
find.go 1.90 KB
一键复制 编辑 原始数据 按行查看 历史
xiaoyutab 提交于 2024-04-30 10:12 . 调整xcron和xcache的目录结构
package xfile
import (
"errors"
"gitee.com/xiaoyutab/xgotool/individual/xcache"
)
// 获取已存在的文件标识
//
// md5 文件的MD5签名
// sha1 文件的SHA1签名
// size 文件大小,单位:B
func FindSearch(md5, sha1 string, size uint) (*File, error) {
if md5 == "" || sha1 == "" || size == 0 {
return nil, errors.New("查询参数错误")
}
f := File{}
cache_key := xcache.Key("xfile.findSearch", md5, sha1, size)
if xcache.Exists(cache_key) {
if err := xcache.GetStruct(cache_key, &f); err == nil {
return &f, nil
}
}
if _default.DB == nil {
return nil, errors.New("数据库未连接")
}
err := _default.DB.Table(_default.FileName).Where("md5", md5).Where("sha1", sha1).Where("size", size).Find(&f).Error
if err != nil {
return nil, err
}
if f.Id == 0 {
return nil, errors.New("文件不存在")
}
xcache.SetStruct(cache_key, f)
return &f, nil
}
// 查询文件详情信息
//
// 此方法会根据file表中的hash_id字段来获取上一级的文件信息,并进行覆盖
//
// id 文件ID
func Find(id uint) (*File, error) {
if id == 0 {
return nil, errors.New("文件ID传输错误")
}
f := File{}
cache_key := xcache.Key("xfile.find", id)
if xcache.Exists(cache_key) {
if err := xcache.GetStruct(cache_key, &f); err == nil {
return &f, nil
}
}
if _default.DB == nil {
return nil, errors.New("数据库未连接")
}
err := _default.DB.Table(_default.FileName).Where("id", id).Find(&f).Error
if err != nil {
return nil, err
}
if f.Id == 0 {
return nil, errors.New("文件不存在")
}
// 文件ID替换逻辑
for {
if f.HashId == 0 {
break
}
err := _default.DB.Table(_default.FileName).Where("id", f.HashId).Find(&f).Error
if err != nil {
return nil, err
}
if f.Id == 0 {
return nil, errors.New("上级文件未找到")
}
xcache.SetStruct(xcache.Key("xfile.find", f.Id), f)
}
xcache.SetStruct(cache_key, f)
return &f, nil
}
Go
1
https://gitee.com/xiaoyutab/xgotool.git
git@gitee.com:xiaoyutab/xgotool.git
xiaoyutab
xgotool
xgotool
v0.3.11

搜索帮助

53164aa7 5694891 3bd8fe86 5694891