feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
50
backend/infra/contract/imagex/get_resource_opt.go
Normal file
50
backend/infra/contract/imagex/get_resource_opt.go
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package imagex
|
||||
|
||||
type GetResourceOpt func(option *GetResourceOption)
|
||||
|
||||
type GetResourceOption struct {
|
||||
Format string
|
||||
Template string
|
||||
Proto string
|
||||
Expire int
|
||||
}
|
||||
|
||||
func WithResourceFormat(format string) GetResourceOpt {
|
||||
return func(o *GetResourceOption) {
|
||||
o.Format = format
|
||||
}
|
||||
}
|
||||
|
||||
func WithResourceTemplate(template string) GetResourceOpt {
|
||||
return func(o *GetResourceOption) {
|
||||
o.Template = template
|
||||
}
|
||||
}
|
||||
|
||||
func WithResourceProto(proto string) GetResourceOpt {
|
||||
return func(o *GetResourceOption) {
|
||||
o.Proto = proto
|
||||
}
|
||||
}
|
||||
|
||||
func WithResourceExpire(expire int) GetResourceOpt {
|
||||
return func(o *GetResourceOption) {
|
||||
o.Expire = expire
|
||||
}
|
||||
}
|
||||
71
backend/infra/contract/imagex/imagex.go
Normal file
71
backend/infra/contract/imagex/imagex.go
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package imagex
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
//go:generate mockgen -destination ../../../internal/mock/infra/contract/imagex/imagex_mock.go --package imagex -source imagex.go
|
||||
type ImageX interface {
|
||||
GetUploadAuth(ctx context.Context, opt ...UploadAuthOpt) (*SecurityToken, error)
|
||||
GetUploadAuthWithExpire(ctx context.Context, expire time.Duration, opt ...UploadAuthOpt) (*SecurityToken, error)
|
||||
GetResourceURL(ctx context.Context, uri string, opts ...GetResourceOpt) (*ResourceURL, error)
|
||||
Upload(ctx context.Context, data []byte, opts ...UploadAuthOpt) (*UploadResult, error)
|
||||
GetServerID() string
|
||||
GetUploadHost(ctx context.Context) string
|
||||
}
|
||||
|
||||
type SecurityToken struct {
|
||||
AccessKeyID string `thrift:"access_key_id,1" frugal:"1,default,string" json:"access_key_id"`
|
||||
SecretAccessKey string `thrift:"secret_access_key,2" frugal:"2,default,string" json:"secret_access_key"`
|
||||
SessionToken string `thrift:"session_token,3" frugal:"3,default,string" json:"session_token"`
|
||||
ExpiredTime string `thrift:"expired_time,4" frugal:"4,default,string" json:"expired_time"`
|
||||
CurrentTime string `thrift:"current_time,5" frugal:"5,default,string" json:"current_time"`
|
||||
HostScheme string `thrift:"host_scheme,6" frugal:"6,default,string" json:"host_scheme"`
|
||||
}
|
||||
|
||||
type ResourceURL struct {
|
||||
// REQUIRED; 结果图访问精简地址,与默认地址相比缺少 Bucket 部分。
|
||||
CompactURL string `json:"CompactURL"`
|
||||
// REQUIRED; 结果图访问默认地址。
|
||||
URL string `json:"URL"`
|
||||
}
|
||||
|
||||
type UploadResult struct {
|
||||
Result *Result `json:"Results"`
|
||||
RequestId string `json:"RequestId"`
|
||||
FileInfo *FileInfo `json:"PluginResult"`
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
Uri string `json:"Uri"`
|
||||
UriStatus int `json:"UriStatus"` // 2000表示上传成功
|
||||
}
|
||||
|
||||
type FileInfo struct {
|
||||
Name string `json:"FileName"`
|
||||
Uri string `json:"ImageUri"`
|
||||
ImageWidth int `json:"ImageWidth"`
|
||||
ImageHeight int `json:"ImageHeight"`
|
||||
Md5 string `json:"ImageMd5"`
|
||||
ImageFormat string `json:"ImageFormat"`
|
||||
ImageSize int `json:"ImageSize"`
|
||||
FrameCnt int `json:"FrameCnt"`
|
||||
Duration int `json:"Duration"`
|
||||
}
|
||||
72
backend/infra/contract/imagex/upload_auth_opt.go
Normal file
72
backend/infra/contract/imagex/upload_auth_opt.go
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package imagex
|
||||
|
||||
type UploadAuthOpt func(option *UploadAuthOption)
|
||||
|
||||
type UploadAuthOption struct {
|
||||
ContentTypeBlackList []string
|
||||
ContentTypeWhiteList []string
|
||||
FileSizeUpLimit *string
|
||||
FileSizeBottomLimit *string
|
||||
KeyPtn *string
|
||||
UploadOverWrite *bool
|
||||
conditions map[string]string
|
||||
StoreKey *string
|
||||
}
|
||||
|
||||
func WithStoreKey(key string) UploadAuthOpt {
|
||||
return func(o *UploadAuthOption) {
|
||||
o.StoreKey = &key
|
||||
}
|
||||
}
|
||||
|
||||
func WithUploadKeyPtn(ptn string) UploadAuthOpt {
|
||||
return func(o *UploadAuthOption) {
|
||||
o.KeyPtn = &ptn
|
||||
}
|
||||
}
|
||||
|
||||
func WithUploadOverwrite(overwrite bool) UploadAuthOpt {
|
||||
return func(op *UploadAuthOption) {
|
||||
op.UploadOverWrite = &overwrite
|
||||
}
|
||||
}
|
||||
|
||||
func WithUploadContentTypeBlackList(blackList []string) UploadAuthOpt {
|
||||
return func(op *UploadAuthOption) {
|
||||
op.ContentTypeBlackList = blackList
|
||||
}
|
||||
}
|
||||
|
||||
func WithUploadContentTypeWhiteList(whiteList []string) UploadAuthOpt {
|
||||
return func(op *UploadAuthOption) {
|
||||
op.ContentTypeWhiteList = whiteList
|
||||
}
|
||||
}
|
||||
|
||||
func WithUploadFileSizeUpLimit(limit string) UploadAuthOpt {
|
||||
return func(op *UploadAuthOption) {
|
||||
op.FileSizeUpLimit = &limit
|
||||
}
|
||||
}
|
||||
|
||||
func WithUploadFileSizeBottomLimit(limit string) UploadAuthOpt {
|
||||
return func(op *UploadAuthOption) {
|
||||
op.FileSizeBottomLimit = &limit
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user