feat: add s3 blob handler(by readll all :( )
This commit is contained in:
BIN
internal/.DS_Store
vendored
BIN
internal/.DS_Store
vendored
Binary file not shown.
@ -5,6 +5,7 @@ import (
|
||||
"github.com/loveuer/nf"
|
||||
"nf-repo/internal/handler"
|
||||
"nf-repo/internal/interfaces"
|
||||
"nf-repo/internal/middleware/front"
|
||||
"nf-repo/internal/opt"
|
||||
)
|
||||
|
||||
@ -29,5 +30,7 @@ func NewApi(
|
||||
api.Post("/proxy", handler.ProxyDownloadImage(mh, bh))
|
||||
}
|
||||
|
||||
app.Use(front.NewFront(&front.DefaultFront, "dist/front/browser"))
|
||||
|
||||
return app
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import (
|
||||
"nf-repo/internal/opt"
|
||||
"nf-repo/internal/util/r"
|
||||
"nf-repo/internal/util/rerr"
|
||||
"nf-repo/internal/util/tools"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -186,7 +187,8 @@ func ProxyDownloadImage(mh interfaces.ManifestHandler, bh interfaces.BlobHandler
|
||||
})
|
||||
_ = c.Flush()
|
||||
|
||||
if des, err = puller.Get(c.Request.Context(), tn); err != nil {
|
||||
ctx := tools.TimeoutCtx(c.Request.Context())
|
||||
if des, err = puller.Get(ctx, tn); err != nil {
|
||||
return r.Resp500(c, err.Error())
|
||||
}
|
||||
|
||||
|
@ -2,8 +2,10 @@ package handler
|
||||
|
||||
import (
|
||||
"github.com/loveuer/nf"
|
||||
"github.com/sirupsen/logrus"
|
||||
"nf-repo/internal/interfaces"
|
||||
"nf-repo/internal/opt"
|
||||
"nf-repo/internal/util/rerr"
|
||||
)
|
||||
|
||||
type blob struct {
|
||||
@ -39,8 +41,14 @@ func Root(bh interfaces.BlobHandler, uh interfaces.UploadHandler, mh interfaces.
|
||||
return handleReferrers(c, mh)
|
||||
}
|
||||
|
||||
c.Set("Docker-Distribution-API-Version", "registry/2.0")
|
||||
c.SetHeader("Docker-Distribution-API-Version", "registry/2.0")
|
||||
|
||||
return c.SendStatus(200)
|
||||
logrus.
|
||||
WithField("path", c.Path()).
|
||||
WithField("method", c.Method()).
|
||||
WithField("headers", c.Request.Header).
|
||||
Warn()
|
||||
|
||||
return rerr.Error(c, rerr.ErrUnauthorized)
|
||||
}
|
||||
}
|
||||
|
156
internal/interfaces/blobs/s3.go
Normal file
156
internal/interfaces/blobs/s3.go
Normal file
@ -0,0 +1,156 @@
|
||||
package blobs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||
"github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"nf-repo/internal/interfaces"
|
||||
"nf-repo/internal/model"
|
||||
"nf-repo/internal/verify"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type s3Handler struct {
|
||||
client *s3.Client
|
||||
bucket string
|
||||
}
|
||||
|
||||
func (s *s3Handler) Get(ctx context.Context, repo string, hash model.Hash) (io.ReadCloser, error) {
|
||||
var (
|
||||
err error
|
||||
output *s3.GetObjectOutput
|
||||
)
|
||||
|
||||
if output, err = s.client.GetObject(ctx, &s3.GetObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(hash.Hex),
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return output.Body, nil
|
||||
}
|
||||
|
||||
func (s *s3Handler) Stat(ctx context.Context, repo string, hash model.Hash) (int64, error) {
|
||||
var (
|
||||
err error
|
||||
output *s3.GetObjectOutput
|
||||
)
|
||||
|
||||
if output, err = s.client.GetObject(ctx, &s3.GetObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(hash.Hex),
|
||||
}); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer output.Body.Close()
|
||||
|
||||
return *output.ContentLength, nil
|
||||
}
|
||||
|
||||
func (s *s3Handler) Put(ctx context.Context, repo string, hash model.Hash, rc io.ReadCloser) error {
|
||||
var (
|
||||
err error
|
||||
nrc io.ReadCloser
|
||||
)
|
||||
|
||||
if nrc, err = verify.ReadCloser(rc, verify.SizeUnknown, hash); err != nil {
|
||||
return err
|
||||
}
|
||||
defer nrc.Close()
|
||||
|
||||
var bs []byte
|
||||
if bs, err = io.ReadAll(nrc); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = s.client.PutObject(ctx, &s3.PutObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(hash.Hex),
|
||||
ACL: types.ObjectCannedACLPublicRead,
|
||||
Body: bytes.NewReader(bs),
|
||||
}, s3.WithAPIOptions(
|
||||
//v4.AddUnsignedPayloadMiddleware,
|
||||
//v4.RemoveComputePayloadSHA256Middleware,
|
||||
)); err != nil {
|
||||
logrus.
|
||||
WithField("path", "s3Handler.Put").
|
||||
WithField("err", err).
|
||||
Debug()
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *s3Handler) Delete(ctx context.Context, repo string, hash model.Hash) error {
|
||||
var (
|
||||
err error
|
||||
)
|
||||
|
||||
if _, err = s.client.DeleteObject(ctx, &s3.DeleteObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(hash.Hex),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewS3BlobHandler(
|
||||
ctx context.Context,
|
||||
endpoint string,
|
||||
accessKey string,
|
||||
secretKey string,
|
||||
bucket string,
|
||||
) interfaces.BlobHandler {
|
||||
var (
|
||||
err error
|
||||
cfg aws.Config
|
||||
client *s3.Client
|
||||
)
|
||||
|
||||
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
|
||||
return aws.Endpoint{
|
||||
URL: endpoint,
|
||||
}, nil
|
||||
})
|
||||
|
||||
if cfg, err = config.LoadDefaultConfig(ctx,
|
||||
config.WithEndpointResolverWithOptions(customResolver),
|
||||
config.WithCredentialsProvider(credentials.StaticCredentialsProvider{
|
||||
Value: aws.Credentials{AccessKeyID: accessKey, SecretAccessKey: secretKey},
|
||||
}),
|
||||
); err != nil {
|
||||
logrus.Panicf("init s3 client err: %v", err)
|
||||
}
|
||||
|
||||
client = s3.NewFromConfig(cfg, func(options *s3.Options) {
|
||||
options.UsePathStyle = true
|
||||
})
|
||||
|
||||
if _, err = client.HeadBucket(ctx, &s3.HeadBucketInput{
|
||||
Bucket: aws.String(bucket),
|
||||
}); err != nil {
|
||||
if !strings.Contains(err.Error(), "404") {
|
||||
logrus.Panicf("init s3 bucket err: %v", err)
|
||||
}
|
||||
|
||||
logrus.Info("s3 bucket not found, start create...")
|
||||
|
||||
if _, err = client.CreateBucket(ctx, &s3.CreateBucketInput{
|
||||
Bucket: aws.String(bucket),
|
||||
ACL: types.BucketCannedACLPublicRead,
|
||||
}); err != nil {
|
||||
logrus.Panicf("create s3 bucket err: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &s3Handler{client: client, bucket: bucket}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package tx
|
||||
package dbs
|
||||
|
||||
import (
|
||||
"context"
|
62
internal/interfaces/dbs/user.go
Normal file
62
internal/interfaces/dbs/user.go
Normal file
@ -0,0 +1,62 @@
|
||||
package dbs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"nf-repo/internal/interfaces/enums"
|
||||
"nf-repo/internal/sqlType"
|
||||
"time"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id uint64 `json:"id" gorm:"primaryKey;column:id"`
|
||||
CreatedAt int64 `json:"created_at" gorm:"column:created_at;autoCreateTime:milli"`
|
||||
UpdatedAt int64 `json:"updated_at" gorm:"column:updated_at;autoUpdateTime:milli"`
|
||||
DeletedAt int64 `json:"deleted_at" gorm:"index;column:deleted_at;default:0"`
|
||||
|
||||
Username string `json:"username" gorm:"column:username;type:varchar(64);unique"`
|
||||
Password string `json:"-" gorm:"column:password;type:varchar(256)"`
|
||||
|
||||
Status enums.Status `json:"status" gorm:"column:status;default:0"`
|
||||
|
||||
Nickname string `json:"nickname" gorm:"column:nickname;type:varchar(64)"`
|
||||
Comment string `json:"comment" gorm:"column:comment"`
|
||||
|
||||
Role enums.Role `json:"role" gorm:"column:role"`
|
||||
Privileges sqlType.NumSlice[enums.Privilege] `json:"privileges" gorm:"column:privileges;type:bigint[]"`
|
||||
|
||||
CreatedById uint64 `json:"created_by_id" gorm:"column:created_by_id"`
|
||||
CreatedByName string `json:"created_by_name" gorm:"column:created_by_name;type:varchar(64)"`
|
||||
|
||||
ActiveAt int64 `json:"active_at" gorm:"column:active_at"`
|
||||
Deadline int64 `json:"deadline" gorm:"column:deadline"`
|
||||
|
||||
LoginAt int64 `json:"login_at" gorm:"-"`
|
||||
}
|
||||
|
||||
func (u *User) IsValid(mustOk bool) error {
|
||||
now := time.Now()
|
||||
|
||||
if now.UnixMilli() >= u.Deadline {
|
||||
return errors.New("用户已过期")
|
||||
}
|
||||
|
||||
if now.UnixMilli() < u.ActiveAt {
|
||||
return errors.New("用户未启用")
|
||||
}
|
||||
|
||||
if u.DeletedAt > 0 {
|
||||
return errors.New("用户不存在")
|
||||
}
|
||||
|
||||
switch u.Status {
|
||||
case enums.StatusNormal:
|
||||
case enums.StatusFrozen:
|
||||
if mustOk {
|
||||
return errors.New("用户被冻结")
|
||||
}
|
||||
default:
|
||||
return errors.New("用户状态未知")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
55
internal/interfaces/enums/privilege.go
Normal file
55
internal/interfaces/enums/privilege.go
Normal file
@ -0,0 +1,55 @@
|
||||
package enums
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"nf-repo/internal/interfaces"
|
||||
)
|
||||
|
||||
type Privilege uint64
|
||||
|
||||
const (
|
||||
PrivilegeUserManage Privilege = iota + 1
|
||||
PrivilegeUpload
|
||||
)
|
||||
|
||||
func (p Privilege) Value() int64 {
|
||||
return int64(p)
|
||||
}
|
||||
|
||||
func (p Privilege) Code() string {
|
||||
switch p {
|
||||
case PrivilegeUserManage:
|
||||
return "user_manage"
|
||||
case PrivilegeUpload:
|
||||
return "image_upload"
|
||||
default:
|
||||
panic(fmt.Sprintf("unknown privilege: %d", p))
|
||||
}
|
||||
}
|
||||
|
||||
func (p Privilege) Label() string {
|
||||
switch p {
|
||||
case PrivilegeUserManage:
|
||||
return "用户管理"
|
||||
case PrivilegeUpload:
|
||||
return "上传镜像"
|
||||
default:
|
||||
panic(fmt.Sprintf("unknown privilege: %d", p))
|
||||
}
|
||||
}
|
||||
|
||||
func (p Privilege) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(map[string]any{
|
||||
"code": p.Code(),
|
||||
"value": p.Value(),
|
||||
"label": p.Label(),
|
||||
})
|
||||
}
|
||||
|
||||
func (p Privilege) All() []interfaces.Enum {
|
||||
return []interfaces.Enum{
|
||||
PrivilegeUserManage,
|
||||
PrivilegeUpload,
|
||||
}
|
||||
}
|
72
internal/interfaces/enums/role.go
Normal file
72
internal/interfaces/enums/role.go
Normal file
@ -0,0 +1,72 @@
|
||||
package enums
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"nf-repo/internal/interfaces"
|
||||
"nf-repo/internal/opt"
|
||||
)
|
||||
|
||||
type Role uint8
|
||||
|
||||
var _ interfaces.Enum = (*Role)(nil)
|
||||
|
||||
func (u Role) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(map[string]any{
|
||||
"code": u.Code(),
|
||||
"value": u.Value(),
|
||||
"label": u.Label(),
|
||||
})
|
||||
}
|
||||
|
||||
const (
|
||||
RoleRoot Role = 255
|
||||
RoleAdmin Role = 254
|
||||
RoleUser Role = 100
|
||||
)
|
||||
|
||||
func (u Role) Code() string {
|
||||
switch u {
|
||||
case RoleRoot:
|
||||
return "root"
|
||||
case RoleAdmin:
|
||||
return "admin"
|
||||
case RoleUser:
|
||||
return "user"
|
||||
default:
|
||||
panic(fmt.Sprintf("unknown role: %d", u))
|
||||
}
|
||||
}
|
||||
|
||||
func (u Role) Label() string {
|
||||
switch u {
|
||||
case RoleRoot:
|
||||
return "根用户"
|
||||
case RoleAdmin:
|
||||
return "管理员"
|
||||
case RoleUser:
|
||||
return "用户"
|
||||
default:
|
||||
panic(fmt.Sprintf("unknown role: %d", u))
|
||||
}
|
||||
}
|
||||
|
||||
func (u Role) Value() int64 {
|
||||
return int64(u)
|
||||
}
|
||||
|
||||
func (u Role) All() []interfaces.Enum {
|
||||
return []interfaces.Enum{
|
||||
RoleAdmin,
|
||||
RoleUser,
|
||||
}
|
||||
}
|
||||
|
||||
func (u Role) Where(db *gorm.DB) *gorm.DB {
|
||||
if opt.RoleMustLess {
|
||||
return db.Where("users.role < ?", u.Value())
|
||||
} else {
|
||||
return db.Where("users.role <= ?", u.Value())
|
||||
}
|
||||
}
|
54
internal/interfaces/enums/status.go
Normal file
54
internal/interfaces/enums/status.go
Normal file
@ -0,0 +1,54 @@
|
||||
package enums
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"nf-repo/internal/interfaces"
|
||||
)
|
||||
|
||||
type Status uint64
|
||||
|
||||
const (
|
||||
StatusNormal Status = iota
|
||||
StatusFrozen
|
||||
)
|
||||
|
||||
func (s Status) Value() int64 {
|
||||
return int64(s)
|
||||
}
|
||||
|
||||
func (s Status) Code() string {
|
||||
switch s {
|
||||
case StatusNormal:
|
||||
return "normal"
|
||||
case StatusFrozen:
|
||||
return "frozen"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
func (s Status) Label() string {
|
||||
switch s {
|
||||
case StatusNormal:
|
||||
return "正常"
|
||||
case StatusFrozen:
|
||||
return "冻结"
|
||||
default:
|
||||
return "异常"
|
||||
}
|
||||
}
|
||||
|
||||
func (s Status) All() []interfaces.Enum {
|
||||
return []interfaces.Enum{
|
||||
StatusNormal,
|
||||
StatusFrozen,
|
||||
}
|
||||
}
|
||||
|
||||
func (s Status) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(map[string]any{
|
||||
"value": s.Value(),
|
||||
"code": s.Code(),
|
||||
"label": s.Label(),
|
||||
})
|
||||
}
|
333
internal/middleware/front/dist/front/3rdpartylicenses.txt
vendored
Normal file
333
internal/middleware/front/dist/front/3rdpartylicenses.txt
vendored
Normal file
@ -0,0 +1,333 @@
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
Package: @angular/core
|
||||
License: "MIT"
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
Package: rxjs
|
||||
License: "Apache-2.0"
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright (c) 2015-2018 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors
|
||||
|
||||
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: tslib
|
||||
License: "0BSD"
|
||||
|
||||
Copyright (c) Microsoft Corporation.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
PERFORMANCE OF THIS SOFTWARE.
|
||||
--------------------------------------------------------------------------------
|
||||
Package: @angular/common
|
||||
License: "MIT"
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
Package: @angular/platform-browser
|
||||
License: "MIT"
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
Package: @angular/router
|
||||
License: "MIT"
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
Package: @angular/cdk
|
||||
License: "MIT"
|
||||
|
||||
The MIT License
|
||||
|
||||
Copyright (c) 2024 Google LLC.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
Package: @angular/material
|
||||
License: "MIT"
|
||||
|
||||
The MIT License
|
||||
|
||||
Copyright (c) 2024 Google LLC.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
Package: @angular/animations
|
||||
License: "MIT"
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
Package: @angular/forms
|
||||
License: "MIT"
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
Package: zone.js
|
||||
License: "MIT"
|
||||
|
||||
The MIT License
|
||||
|
||||
Copyright (c) 2010-2023 Google LLC. https://angular.io/license
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
--------------------------------------------------------------------------------
|
BIN
internal/middleware/front/dist/front/browser/favicon.ico
vendored
Normal file
BIN
internal/middleware/front/dist/front/browser/favicon.ico
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
16
internal/middleware/front/dist/front/browser/index.html
vendored
Normal file
16
internal/middleware/front/dist/front/browser/index.html
vendored
Normal file
File diff suppressed because one or more lines are too long
13
internal/middleware/front/dist/front/browser/main-V5ZNJQTZ.js
vendored
Normal file
13
internal/middleware/front/dist/front/browser/main-V5ZNJQTZ.js
vendored
Normal file
File diff suppressed because one or more lines are too long
2
internal/middleware/front/dist/front/browser/polyfills-7NI4OVGA.js
vendored
Normal file
2
internal/middleware/front/dist/front/browser/polyfills-7NI4OVGA.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
internal/middleware/front/dist/front/browser/styles-7HNLK6WW.css
vendored
Normal file
1
internal/middleware/front/dist/front/browser/styles-7HNLK6WW.css
vendored
Normal file
File diff suppressed because one or more lines are too long
61
internal/middleware/front/front.go
Normal file
61
internal/middleware/front/front.go
Normal file
@ -0,0 +1,61 @@
|
||||
package front
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"github.com/loveuer/nf"
|
||||
"github.com/loveuer/nf/nft/log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//go:embed dist/front/browser
|
||||
var DefaultFront embed.FS
|
||||
|
||||
func NewFront(ff *embed.FS, basePath string) nf.HandlerFunc {
|
||||
var (
|
||||
e error
|
||||
indexBytes []byte
|
||||
index string
|
||||
)
|
||||
|
||||
index = fmt.Sprintf("%s/index.html", basePath)
|
||||
|
||||
if indexBytes, e = ff.ReadFile(index); e != nil {
|
||||
log.Panic("read index file err: %v", e)
|
||||
}
|
||||
|
||||
return func(c *nf.Ctx) error {
|
||||
var (
|
||||
err error
|
||||
bs []byte
|
||||
path = c.Path()
|
||||
)
|
||||
|
||||
if bs, err = ff.ReadFile(basePath + path); err != nil {
|
||||
log.Debug("embed read file [%s]%s err: %v", basePath, path, err)
|
||||
c.Set("Content-Type", "text/html")
|
||||
_, err = c.Write(indexBytes)
|
||||
return err
|
||||
}
|
||||
|
||||
var dbs []byte
|
||||
if len(bs) > 512 {
|
||||
dbs = bs[:512]
|
||||
} else {
|
||||
dbs = bs
|
||||
}
|
||||
|
||||
switch {
|
||||
case strings.HasSuffix(path, ".js"):
|
||||
c.Set("Content-Type", "application/javascript")
|
||||
case strings.HasSuffix(path, ".css"):
|
||||
c.Set("Content-Type", "text/css")
|
||||
default:
|
||||
c.Set("Content-Type", http.DetectContentType(dbs))
|
||||
}
|
||||
|
||||
_, err = c.Write(bs)
|
||||
return err
|
||||
}
|
||||
}
|
@ -6,7 +6,8 @@ const (
|
||||
DefaultMaxSize = 32 * 1024 * 1024
|
||||
ReferrersEnabled = true
|
||||
|
||||
BaseAddress = "repo.me"
|
||||
BaseAddress = "repo.me"
|
||||
RoleMustLess = true
|
||||
)
|
||||
|
||||
var (
|
||||
|
9
internal/sqlType/err.go
Normal file
9
internal/sqlType/err.go
Normal file
@ -0,0 +1,9 @@
|
||||
package sqlType
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrConvertScanVal = errors.New("convert scan val to str err")
|
||||
ErrInvalidScanVal = errors.New("scan val invalid")
|
||||
ErrConvertVal = errors.New("convert err")
|
||||
)
|
76
internal/sqlType/jsonb.go
Normal file
76
internal/sqlType/jsonb.go
Normal file
@ -0,0 +1,76 @@
|
||||
package sqlType
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/jackc/pgtype"
|
||||
)
|
||||
|
||||
type JSONB struct {
|
||||
Val pgtype.JSONB
|
||||
Valid bool
|
||||
}
|
||||
|
||||
func NewJSONB(v interface{}) JSONB {
|
||||
j := new(JSONB)
|
||||
j.Val = pgtype.JSONB{}
|
||||
if err := j.Val.Set(v); err == nil {
|
||||
j.Valid = true
|
||||
return *j
|
||||
}
|
||||
|
||||
return *j
|
||||
}
|
||||
|
||||
func (j *JSONB) Set(value interface{}) error {
|
||||
if err := j.Val.Set(value); err != nil {
|
||||
j.Valid = false
|
||||
return err
|
||||
}
|
||||
|
||||
j.Valid = true
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (j *JSONB) Bind(model interface{}) error {
|
||||
return j.Val.AssignTo(model)
|
||||
}
|
||||
|
||||
func (j *JSONB) Scan(value interface{}) error {
|
||||
j.Val = pgtype.JSONB{}
|
||||
if value == nil {
|
||||
j.Valid = false
|
||||
return nil
|
||||
}
|
||||
|
||||
j.Valid = true
|
||||
|
||||
return j.Val.Scan(value)
|
||||
}
|
||||
|
||||
func (j JSONB) Value() (driver.Value, error) {
|
||||
if j.Valid {
|
||||
return j.Val.Value()
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (j JSONB) MarshalJSON() ([]byte, error) {
|
||||
if j.Valid {
|
||||
return j.Val.MarshalJSON()
|
||||
}
|
||||
|
||||
return json.Marshal(nil)
|
||||
}
|
||||
|
||||
func (j *JSONB) UnmarshalJSON(b []byte) error {
|
||||
if string(b) == "null" {
|
||||
j.Valid = false
|
||||
return j.Val.UnmarshalJSON(b)
|
||||
}
|
||||
|
||||
return j.Val.UnmarshalJSON(b)
|
||||
}
|
42
internal/sqlType/nullStr.go
Normal file
42
internal/sqlType/nullStr.go
Normal file
@ -0,0 +1,42 @@
|
||||
package sqlType
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// type NullString struct {
|
||||
// sql.NullString
|
||||
// }
|
||||
|
||||
type NullString struct{ sql.NullString }
|
||||
|
||||
func NewNullString(val string) NullString {
|
||||
if val == "" {
|
||||
return NullString{}
|
||||
}
|
||||
|
||||
return NullString{sql.NullString{Valid: true, String: val}}
|
||||
}
|
||||
|
||||
func (ns NullString) MarshalJSON() ([]byte, error) {
|
||||
if !ns.Valid {
|
||||
return json.Marshal(nil)
|
||||
}
|
||||
|
||||
return json.Marshal(ns.String)
|
||||
}
|
||||
|
||||
func (ns *NullString) UnmarshalJSON(data []byte) error {
|
||||
if string(data) == "null" {
|
||||
ns.Valid = false
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, &ns.String); err != nil {
|
||||
ns.Valid = true
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
53
internal/sqlType/set.go
Normal file
53
internal/sqlType/set.go
Normal file
@ -0,0 +1,53 @@
|
||||
package sqlType
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type Set map[string]struct{}
|
||||
|
||||
func (s Set) MarshalJSON() ([]byte, error) {
|
||||
array := make([]string, 0)
|
||||
for name := range s {
|
||||
array = append(array, name)
|
||||
}
|
||||
|
||||
return json.Marshal(array)
|
||||
}
|
||||
|
||||
func (s *Set) UnmarshalJSON(b []byte) error {
|
||||
array := make([]string, 0)
|
||||
if err := json.Unmarshal(b, &array); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
set := make(map[string]struct{})
|
||||
|
||||
for _, name := range array {
|
||||
set[name] = struct{}{}
|
||||
}
|
||||
|
||||
*s = set
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Set) ToStringSlice() []string {
|
||||
var (
|
||||
result = make([]string, 0, len(s))
|
||||
)
|
||||
|
||||
for key := range s {
|
||||
result = append(result, key)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *Set) FromStringSlice(ss *[]string) {
|
||||
if s == nil {
|
||||
m := make(Set)
|
||||
s = &m
|
||||
}
|
||||
|
||||
for idx := range *(ss) {
|
||||
(*s)[(*ss)[idx]] = struct{}{}
|
||||
}
|
||||
}
|
109
internal/sqlType/strSlice.go
Normal file
109
internal/sqlType/strSlice.go
Normal file
@ -0,0 +1,109 @@
|
||||
package sqlType
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type StrSlice []string
|
||||
|
||||
func (s *StrSlice) Scan(val interface{}) error {
|
||||
|
||||
str, ok := val.(string)
|
||||
if !ok {
|
||||
return ErrConvertScanVal
|
||||
}
|
||||
|
||||
if len(str) < 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
bs := make([]byte, 0, 128)
|
||||
bss := make([]byte, 0, 2*len(str))
|
||||
|
||||
quoteCount := 0
|
||||
|
||||
for idx := 1; idx < len(str)-1; idx++ {
|
||||
// 44: , 92: \ 34: "
|
||||
quote := str[idx]
|
||||
switch quote {
|
||||
case 44:
|
||||
if quote == 44 && str[idx-1] != 92 && quoteCount == 0 {
|
||||
if len(bs) > 0 {
|
||||
if !(bs[0] == 34 && bs[len(bs)-1] == 34) {
|
||||
bs = append([]byte{34}, bs...)
|
||||
bs = append(bs, 34)
|
||||
}
|
||||
|
||||
bss = append(bss, bs...)
|
||||
bss = append(bss, 44)
|
||||
}
|
||||
bs = bs[:0]
|
||||
} else {
|
||||
bs = append(bs, quote)
|
||||
}
|
||||
case 34:
|
||||
if str[idx-1] != 92 {
|
||||
quoteCount = (quoteCount + 1) % 2
|
||||
}
|
||||
bs = append(bs, quote)
|
||||
default:
|
||||
bs = append(bs, quote)
|
||||
}
|
||||
|
||||
//bs = append(bs, str[idx])
|
||||
}
|
||||
|
||||
if len(bs) > 0 {
|
||||
if !(bs[0] == 34 && bs[len(bs)-1] == 34) {
|
||||
bs = append([]byte{34}, bs...)
|
||||
bs = append(bs, 34)
|
||||
}
|
||||
|
||||
bss = append(bss, bs...)
|
||||
} else {
|
||||
if len(bss) > 2 {
|
||||
bss = bss[:len(bss)-2]
|
||||
}
|
||||
}
|
||||
|
||||
bss = append([]byte{'['}, append(bss, ']')...)
|
||||
|
||||
if err := json.Unmarshal(bss, s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s StrSlice) Value() (driver.Value, error) {
|
||||
if s == nil {
|
||||
return "{}", nil
|
||||
}
|
||||
|
||||
if len(s) == 0 {
|
||||
return "{}", nil
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
encoder := json.NewEncoder(buf)
|
||||
encoder.SetEscapeHTML(false)
|
||||
|
||||
if err := encoder.Encode(s); err != nil {
|
||||
return "{}", err
|
||||
}
|
||||
|
||||
bs := buf.Bytes()
|
||||
|
||||
bs[0] = '{'
|
||||
|
||||
if bs[len(bs)-1] == 10 {
|
||||
bs = bs[:len(bs)-1]
|
||||
}
|
||||
|
||||
bs[len(bs)-1] = '}'
|
||||
|
||||
return string(bs), nil
|
||||
}
|
71
internal/sqlType/uint64Slice.go
Normal file
71
internal/sqlType/uint64Slice.go
Normal file
@ -0,0 +1,71 @@
|
||||
package sqlType
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
type NumSlice[T ~int | ~int64 | ~uint | ~uint64] []T
|
||||
|
||||
func (n *NumSlice[T]) Scan(val interface{}) error {
|
||||
str, ok := val.(string)
|
||||
if !ok {
|
||||
return ErrConvertScanVal
|
||||
}
|
||||
|
||||
length := len(str)
|
||||
|
||||
if length <= 0 {
|
||||
*n = make(NumSlice[T], 0)
|
||||
return nil
|
||||
}
|
||||
|
||||
if str[0] != '{' || str[length-1] != '}' {
|
||||
return ErrInvalidScanVal
|
||||
}
|
||||
|
||||
str = str[1 : length-1]
|
||||
if len(str) == 0 {
|
||||
*n = make(NumSlice[T], 0)
|
||||
return nil
|
||||
}
|
||||
|
||||
numStrs := strings.Split(str, ",")
|
||||
nums := make([]T, len(numStrs))
|
||||
|
||||
for idx := range numStrs {
|
||||
num, err := cast.ToInt64E(strings.TrimSpace(numStrs[idx]))
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: can't convert to %T", ErrConvertVal, T(0))
|
||||
}
|
||||
|
||||
nums[idx] = T(num)
|
||||
}
|
||||
|
||||
*n = nums
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n NumSlice[T]) Value() (driver.Value, error) {
|
||||
if n == nil {
|
||||
return "{}", nil
|
||||
}
|
||||
|
||||
if len(n) == 0 {
|
||||
return "{}", nil
|
||||
}
|
||||
|
||||
ss := make([]string, 0, len(n))
|
||||
for idx := range n {
|
||||
ss = append(ss, strconv.Itoa(int(n[idx])))
|
||||
}
|
||||
|
||||
s := strings.Join(ss, ", ")
|
||||
|
||||
return fmt.Sprintf("{%s}", s), nil
|
||||
}
|
@ -53,3 +53,9 @@ var ErrDigestInvalid = &RepositoryError{
|
||||
Code: "NAME_INVALID",
|
||||
Message: "invalid digest",
|
||||
}
|
||||
|
||||
var ErrUnauthorized = &RepositoryError{
|
||||
Status: http.StatusUnauthorized,
|
||||
Code: "UNAUTHORIZED",
|
||||
Message: "access to the requested resource is not authorized",
|
||||
}
|
||||
|
@ -20,3 +20,19 @@ func Timeout(seconds ...int) (ctx context.Context) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func TimeoutCtx(ctx context.Context, seconds ...int) context.Context {
|
||||
var (
|
||||
duration time.Duration
|
||||
)
|
||||
|
||||
if len(seconds) > 0 && seconds[0] > 0 {
|
||||
duration = time.Duration(seconds[0]) * time.Second
|
||||
} else {
|
||||
duration = time.Duration(30) * time.Second
|
||||
}
|
||||
|
||||
nctx, _ := context.WithTimeout(ctx, duration)
|
||||
|
||||
return nctx
|
||||
}
|
||||
|
Reference in New Issue
Block a user