1 Star 0 Fork 0

linngc / central-mirror

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
embedfs_bucket.go 2.68 KB
一键复制 编辑 原始数据 按行查看 历史
linngc 提交于 2024-04-01 16:44 . update:提取speedsdk扩张到工具包
// Package embedfs
// @Link https://gitee.com/linngc/central-mirror
// @Copyright Copyright (c) 2024 central-mirror CLI
// @Author linngc
// @License
package embedfs
import (
"errors"
"fmt"
"gitee.com/linngc/central-mirror/contrib/container/core/memorycache/implement/model"
toolkitIo "gitee.com/linngc/central-mirror/contrib/container/utility/io"
"io"
"os"
"path"
)
// GetAbsoluteLocation 获取绝对路径
func (c *EmbedFs) getAbsoluteLocation(moduleName string) string {
return path.Join(c.cfgConfig.Storage, c.GetLocation(moduleName))
}
// GetLocation 组装并获取存储位置
// @param moduleName 参数是 文件模块名称(请求的文件全路径名称)
// @return 缓存位置(Storage) + 缓存类型(cosType) + 代理类型(proxy) + 代理文件相对路径(moduleName)
// storage/embedfs/alpine/v3.18/main/x86_64/acf-awall-0.4.1-r4.apk
// @Storage ./storage
// @cosType embedfs
// @proxy alpine
// @moduleName v3.18/main/x86_64/acf-awall-0.4.1-r4.apk
func (c *EmbedFs) GetLocation(moduleName string) string {
return path.Join(c.cosType.String(), c.proxy, moduleName)
}
// GetPath 获取存储文件夹
func (c *EmbedFs) getPath() string {
return path.Join(c.cfgConfig.Storage, c.cosType.String(), c.proxy)
}
// GetObjectsInfo 组装数据存储对象
func (c *EmbedFs) GetObjectsInfo(moduleName string, data []byte) (m *model.ObjectsInfo) {
m = &model.ObjectsInfo{Bucket: c.cosType.String(), Cache: c.cosType, Name: moduleName,
Location: c.GetLocation(moduleName), Size: int64(len(data)), Bytes: data}
return
}
// readerObject 获取对象
// @param moduleNames 参数是 文件模块名称(请求的文件全路径名称)
func (c *EmbedFs) readerObject(moduleName string) (byt []byte, err error) {
//读取文件时,要指定FileSystem下的相对路径
location := c.GetLocation(moduleName)
f, err := FileSystem.Open(location)
defer toolkitIo.CloseFile(f)
if err != nil || f == nil {
return byt, err
}
return io.ReadAll(f)
}
// fromObject 上传对象
// @param minioClient minio链接对象
// @param location 参数是 文件存储相对位置(请求的文件全路径名称)
// @param reader 文件byte信息
func (c *EmbedFs) fromObject(moduleName string, reader []byte) (err error) {
//创建文件夹时,要指定绝对路径(包含指定的缓存路径)
location := c.getAbsoluteLocation(moduleName)
dirPath := path.Dir(location)
if stat, err := os.Stat(dirPath); err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(dirPath, os.ModePerm); err != nil {
return err
}
} else if !stat.IsDir() {
return errors.New(fmt.Sprintf("%s is not a dir\n", dirPath))
}
if err := os.WriteFile(location, reader, os.ModePerm); err != nil {
return err
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/linngc/central-mirror.git
git@gitee.com:linngc/central-mirror.git
linngc
central-mirror
central-mirror
33d5664f4339

搜索帮助

344bd9b3 5694891 D2dac590 5694891