mirror of
https://shylinux.com/x/community
synced 2025-04-26 01:54:05 +08:00
196 lines
8.7 KiB
Go
196 lines
8.7 KiB
Go
package smh
|
|
|
|
import (
|
|
"net/http"
|
|
"path"
|
|
"time"
|
|
|
|
"shylinux.com/x/ice"
|
|
"shylinux.com/x/icebergs/base/mdb"
|
|
"shylinux.com/x/icebergs/base/nfs"
|
|
"shylinux.com/x/icebergs/base/web"
|
|
kit "shylinux.com/x/toolkits"
|
|
|
|
"shylinux.com/x/community/src/api"
|
|
"shylinux.com/x/community/src/gonganxitong"
|
|
"shylinux.com/x/community/src/renzhengshouquan"
|
|
"shylinux.com/x/community/src/renzhengshouquan/external/tencentcloud"
|
|
"shylinux.com/x/community/src/renzhengshouquan/external/tencentcloud/smh/model"
|
|
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
|
|
sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/smh/v20210712"
|
|
)
|
|
|
|
type smh struct {
|
|
renzhengshouquan.Table
|
|
tencentcloud.Tencentcloud
|
|
order string `data:"16"`
|
|
fields string `data:"name,info,library_id,library_secret,access_token,expire_time,user_uid"`
|
|
libraryCreate string `name:"libraryCreate" role:"leader"`
|
|
libraryRemove string `name:"libraryRemove" role:"leader"`
|
|
spaceCreate string `name:"spaceCreate" role:"leader"`
|
|
spaceRemove string `name:"spaceRemove" role:"leader"`
|
|
}
|
|
|
|
func (s smh) Init(m *ice.Message, arg ...string) {
|
|
s.Table.Init(m, arg...)
|
|
m.Cmd(web.SPIDE, mdb.CREATE, "https://api.tencentsmh.cn/api/v1/", "tencentsmh")
|
|
}
|
|
func (s smh) LibraryCreate(m *ice.Message, arg ...string) {
|
|
msg := m.Cmd(api.RENZHENGSHOUQUAN_AUTH, s.Select, model.UID, m.Option(model.AUTH_UID))
|
|
request := sdk.NewCreateLibraryRequest()
|
|
request.Name = common.StringPtr(msg.Append(model.NAME))
|
|
request.Remark = common.StringPtr(msg.Append(model.INFO))
|
|
request.LibraryExtension = &sdk.LibraryExtension{
|
|
IsFileLibrary: common.BoolPtr(true),
|
|
IsMultiSpace: common.BoolPtr(true),
|
|
IsMultiAlbum: common.BoolPtr(true),
|
|
}
|
|
response, err := s.newClient(m).CreateLibrary(request)
|
|
if m.Warn(err) {
|
|
return
|
|
}
|
|
req := sdk.NewDescribeLibrarySecretRequest()
|
|
req.LibraryId = response.Response.LibraryId
|
|
res, err := s.newClient(m).DescribeLibrarySecret(req)
|
|
if m.Warn(err) {
|
|
return
|
|
}
|
|
s.Table.Create(m, kit.Simple(msg.AppendSimple(model.NAME, model.INFO), model.LIBRARY_ID, *res.Response.LibraryId, model.LIBRARY_SECRET, *res.Response.LibrarySecret)...)
|
|
}
|
|
func (s smh) LibraryRemove(m *ice.Message, arg ...string) {
|
|
request := sdk.NewDeleteLibraryRequest()
|
|
request.LibraryId = common.StringPtr(m.Option(model.LIBRARY_ID))
|
|
if _, err := s.newClient(m).DeleteLibrary(request); err != nil {
|
|
if err, ok := err.(*errors.TencentCloudSDKError); ok {
|
|
if err.Code == "ResourceInUse.MultiSpace" {
|
|
m.Warn(true, "请先清理用户空间,清理")
|
|
} else {
|
|
m.Warn(true, err.Message)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
s.Table.Remove(m, arg...)
|
|
}
|
|
func (s smh) List(m *ice.Message, arg ...string) {
|
|
if len(arg) == 1 {
|
|
if s.ValueList(m, arg).Display("").Length() == 0 {
|
|
s.Button(m.SetResult(), "请创建媒体库", s.LibraryCreate)
|
|
} else {
|
|
msg := s.token(m)
|
|
m.Table(func(value ice.Maps) {
|
|
_msg := s.spide(m.Spawn(), kit.MergeURL(kit.Format("usage/%s", msg.Append(model.LIBRARY_ID)), msg.AppendSimple(model.ACCESS_TOKEN)))
|
|
m.PushRecord(kit.UnMarshal(_msg.Result()), "totalFileSize")
|
|
}).PushAction(s.LibraryRemove).Action()
|
|
}
|
|
} else if len(arg) == 2 {
|
|
m.Cmdy(api.RENZHENGSHOUQUAN_AUTH, arg[0]).Table(func(value ice.Maps) {
|
|
if renzhengshouquan.AuthStatus(kit.Int(value[model.AUTH_STATUS])) == renzhengshouquan.AuthIssued {
|
|
msg := s.cmdy(m.Spawn(), s.Select, model.PLACE_UID, value[model.PLACE_UID])
|
|
if space_id := msg.Append(model.SPACE_ID); space_id == "" {
|
|
m.Push(model.SPACE_ID, "").Push(model.PHOTO_UID, "").PushButton(s.SpaceCreate)
|
|
} else {
|
|
m.Push(model.SPACE_ID, msg.Append(model.SPACE_ID))
|
|
m.Push(model.PHOTO_UID, msg.Append(model.UID))
|
|
m.PushButton(s.SpaceRemove)
|
|
}
|
|
} else {
|
|
m.Push(model.SPACE_ID, "").Push(model.PHOTO_UID, "").PushButton()
|
|
}
|
|
})
|
|
}
|
|
}
|
|
func (s smh) SpaceCreate(m *ice.Message, arg ...string) {
|
|
msg := s.token(m)
|
|
if s.spide(m, http.MethodPost, kit.MergeURL(kit.Format("space/%s", msg.Append(model.LIBRARY_ID)), model.USER_ID, m.Option(model.PLACE_UID), msg.AppendSimple(model.ACCESS_TOKEN)),
|
|
"spaceTag", m.Option(model.NAME)).IsErr() {
|
|
return
|
|
}
|
|
m.Option(model.SPACE_ID, kit.Value(kit.UnMarshal(m.Result()), "spaceId"))
|
|
s.cmdy(m, s.Config, m.OptionSimple(model.PLACE_UID, model.USER_UID, model.SPACE_ID), model.VENDOR, m.PrefixKey())
|
|
}
|
|
func (s smh) SpaceRemove(m *ice.Message, arg ...string) {
|
|
msg := s.token(m)
|
|
if s.spide(m, http.MethodDelete, kit.MergeURL(kit.Format("space/%s/%s", msg.Append(model.LIBRARY_ID), m.Option(model.SPACE_ID)), msg.AppendSimple(model.ACCESS_TOKEN))).IsErr() {
|
|
return
|
|
}
|
|
s.cmdy(m.Options(model.UID, m.Option(model.PHOTO_UID)), s.Delete, m.OptionSimple(model.PLACE_UID, model.SPACE_ID), model.VENDOR, m.PrefixKey())
|
|
}
|
|
func (s smh) Folder(m *ice.Message, arg ...string) {
|
|
msg := s.token(m)
|
|
if len(arg) > 3 {
|
|
m.EchoImages(kit.MergeURL(kit.Format("https://api.tencentsmh.cn/api/v1/file/%s/%s/%s", msg.Append(model.LIBRARY_ID), arg[1], arg[3]),
|
|
"height_size", "320", "width_size", "320", model.ACCESS_TOKEN, arg[2])).Echo(arg[3])
|
|
return
|
|
}
|
|
s.spide(m, kit.MergeURL(kit.Format("directory/%s/%s/", msg.Append(model.LIBRARY_ID), arg[1]), msg.AppendSimple(model.ACCESS_TOKEN)))
|
|
kit.For(kit.Value(kit.UnMarshal(m.Result()), "contents"), func(value ice.Map) {
|
|
t, _ := time.ParseInLocation("2006-01-02T15:04:05.000Z", kit.Format(value["modificationTime"]), time.UTC)
|
|
value["modify_time"] = t.Add(time.Hour * 8).Format(ice.MOD_TIME)
|
|
m.PushRecord(value, "name", "size", "modify_time", "type", "fileType", "contentType").Push(model.UID, value["name"])
|
|
m.Push("icons", kit.MergeURL(kit.Format("https://api.tencentsmh.cn/api/v1/file/%s/%s/%s", msg.Append(model.LIBRARY_ID), arg[1], value["name"]),
|
|
"height_size", "128", "width_size", "128", model.ACCESS_TOKEN, arg[2]))
|
|
})
|
|
m.SetResult()
|
|
}
|
|
func (s smh) Upload(m *ice.Message, arg ...string) {
|
|
msg := s.token(m)
|
|
up := kit.Simple(m.Optionv(ice.MSG_UPLOAD))
|
|
_msg := m.Cmd(web.CACHE, up[0])
|
|
if s.spide(m, http.MethodPut, kit.MergeURL(kit.Format("file/%s/%s/%s", msg.Append(model.LIBRARY_ID), arg[1], path.Base(up[1])),
|
|
"conflict_resolution_strategy", "rename", "filesize", up[2], msg.AppendSimple(model.ACCESS_TOKEN))).IsErr() {
|
|
return
|
|
}
|
|
res := kit.UnMarshal(m.Result())
|
|
if s.spide(m.Options(web.SPIDE_HEADER, kit.Value(res, "headers")), http.MethodPut,
|
|
"https://"+kit.Format(kit.Value(res, web.DOMAIN))+kit.Format(kit.Value(res, nfs.PATH)), web.SPIDE_FILE, _msg.Append(nfs.FILE)).IsErr() {
|
|
return
|
|
}
|
|
s.spide(m, http.MethodPost, kit.MergeURL(kit.Format("file/%s/%s/%s", msg.Append(model.LIBRARY_ID), arg[1], kit.Value(res, "confirmKey")),
|
|
"confirm", "confirm", "conflict_resolution_strategy", "rename", msg.AppendSimple(model.ACCESS_TOKEN)))
|
|
}
|
|
func (s smh) Trash(m *ice.Message, arg ...string) {
|
|
msg := s.token(m)
|
|
s.spide(m, http.MethodDelete, kit.MergeURL(kit.Format("file/%s/%s/%s", msg.Append(model.LIBRARY_ID), arg[1], m.Option(model.NAME)), msg.AppendSimple(model.ACCESS_TOKEN)))
|
|
}
|
|
func (s smh) Token(m *ice.Message, arg ...string) {
|
|
if arg[1] == "" || arg[2] < m.Time() {
|
|
msg := s.token(m)
|
|
if _msg := s.spide(m.Spawn(), "token", msg.AppendSimple(model.LIBRARY_ID, model.LIBRARY_SECRET), model.SPACE_ID, arg[0]); !_msg.IsErr() {
|
|
res := kit.UnMarshal(_msg.Result())
|
|
s.cmdy(m, s.Config, model.ACCESS_TOKEN, kit.Value(res, "accessToken"), model.EXPIRE_TIME, m.Time(kit.Format(kit.Value(res, "expiresIn"))+"s"))
|
|
m.Push(model.ACCESS_TOKEN, kit.Value(res, "accessToken"))
|
|
}
|
|
} else {
|
|
m.Push(model.ACCESS_TOKEN, arg[1])
|
|
}
|
|
}
|
|
|
|
func init() { ice.TeamCtxCmd(smh{}) }
|
|
|
|
func init() { gonganxitong.PhotoVendor = smh{} }
|
|
|
|
func (s smh) cmdy(m *ice.Message, arg ...ice.Any) *ice.Message {
|
|
return m.Cmdy(append([]ice.Any{"web.team.gonganxitong.photo"}, arg...)...)
|
|
}
|
|
func (s smh) token(m *ice.Message, arg ...string) *ice.Message {
|
|
msg := s.Select(m.Spawn(), m.OptionSimple(model.AUTH_UID)...)
|
|
if msg.Append(model.ACCESS_TOKEN) == "" || msg.Append(model.EXPIRE_TIME) < m.Time() {
|
|
if _msg := s.spide(m.Spawn(), "token", msg.AppendSimple(model.LIBRARY_ID, model.LIBRARY_SECRET), "grant", "admin"); !_msg.IsErr() {
|
|
res := kit.UnMarshal(_msg.Result())
|
|
s.Update(m, kit.Dict(model.ACCESS_TOKEN, kit.Value(res, "accessToken"), model.EXPIRE_TIME, m.Time(kit.Format(kit.Value(res, "expiresIn"))+"s")), m.OptionSimple(model.AUTH_UID)...)
|
|
}
|
|
msg = s.Select(m.Spawn(), m.OptionSimple(model.AUTH_UID)...)
|
|
}
|
|
return msg
|
|
}
|
|
func (s smh) spide(m *ice.Message, arg ...ice.Any) *ice.Message {
|
|
return m.SetAppend().SetResult().Cmdy(append([]ice.Any{web.SPIDE, "tencentsmh", web.SPIDE_RAW}, arg...)...).Options(web.SPIDE_HEADER, kit.Dict())
|
|
}
|
|
func (s smh) newClient(m *ice.Message, arg ...string) *sdk.Client {
|
|
c, _ := sdk.NewClient(s.NewCredential(m), "", s.NewProfile(m))
|
|
return c
|
|
}
|