mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-10 12:49:52 +01:00
e99d3f7055
feat(F3): driver stub feat(F3): util.Logger feat(F3): driver compliance tests feat(F3): driver/users implementation feat(F3): driver/user implementation feat(F3): driver/{projects,project} implementation feat(F3): driver/{labels,label} implementation feat(F3): driver/{milestones,milestone} implementation feat(F3): driver/{repositories,repository} implementation feat(F3): driver/{organizations,organization} implementation feat(F3): driver/{releases,release} implementation feat(F3): driver/{issues,issue} implementation feat(F3): driver/{comments,comment} implementation feat(F3): driver/{assets,asset} implementation feat(F3): driver/{pullrequests,pullrequest} implementation feat(F3): driver/{reviews,review} implementation feat(F3): driver/{topics,topic} implementation feat(F3): driver/{reactions,reaction} implementation feat(F3): driver/{reviewComments,reviewComment} implementation feat(F3): CLI: f3 mirror chore(F3): move to code.forgejo.org feat(f3): upgrade to gof3 3.1.0 repositories in pull requests are represented with a reference instead of an owner/project pair of names
171 lines
4.2 KiB
Go
171 lines
4.2 KiB
Go
// Copyright Earl Warren <contact@earl-warren.org>
|
|
// Copyright Loïc Dachary <loic@dachary.org>
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package driver
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/storage"
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
"code.gitea.io/gitea/services/attachment"
|
|
|
|
"code.forgejo.org/f3/gof3/v3/f3"
|
|
f3_tree "code.forgejo.org/f3/gof3/v3/tree/f3"
|
|
"code.forgejo.org/f3/gof3/v3/tree/generic"
|
|
f3_util "code.forgejo.org/f3/gof3/v3/util"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
var _ f3_tree.ForgeDriverInterface = &issue{}
|
|
|
|
type asset struct {
|
|
common
|
|
|
|
forgejoAsset *repo_model.Attachment
|
|
sha string
|
|
contentType string
|
|
downloadFunc f3.DownloadFuncType
|
|
}
|
|
|
|
func (o *asset) SetNative(asset any) {
|
|
o.forgejoAsset = asset.(*repo_model.Attachment)
|
|
}
|
|
|
|
func (o *asset) GetNativeID() string {
|
|
return fmt.Sprintf("%d", o.forgejoAsset.ID)
|
|
}
|
|
|
|
func (o *asset) NewFormat() f3.Interface {
|
|
node := o.GetNode()
|
|
return node.GetTree().(f3_tree.TreeInterface).NewFormat(node.GetKind())
|
|
}
|
|
|
|
func (o *asset) ToFormat() f3.Interface {
|
|
if o.forgejoAsset == nil {
|
|
return o.NewFormat()
|
|
}
|
|
|
|
return &f3.ReleaseAsset{
|
|
Common: f3.NewCommon(o.GetNativeID()),
|
|
Name: o.forgejoAsset.Name,
|
|
ContentType: o.contentType,
|
|
Size: o.forgejoAsset.Size,
|
|
DownloadCount: o.forgejoAsset.DownloadCount,
|
|
Created: o.forgejoAsset.CreatedUnix.AsTime(),
|
|
SHA256: o.sha,
|
|
DownloadURL: o.forgejoAsset.DownloadURL(),
|
|
DownloadFunc: o.downloadFunc,
|
|
}
|
|
}
|
|
|
|
func (o *asset) FromFormat(content f3.Interface) {
|
|
asset := content.(*f3.ReleaseAsset)
|
|
o.forgejoAsset = &repo_model.Attachment{
|
|
ID: f3_util.ParseInt(asset.GetID()),
|
|
Name: asset.Name,
|
|
Size: asset.Size,
|
|
DownloadCount: asset.DownloadCount,
|
|
CreatedUnix: timeutil.TimeStamp(asset.Created.Unix()),
|
|
CustomDownloadURL: asset.DownloadURL,
|
|
}
|
|
o.contentType = asset.ContentType
|
|
o.sha = asset.SHA256
|
|
o.downloadFunc = asset.DownloadFunc
|
|
}
|
|
|
|
func (o *asset) Get(ctx context.Context) bool {
|
|
node := o.GetNode()
|
|
o.Trace("%s", node.GetID())
|
|
|
|
id := f3_util.ParseInt(string(node.GetID()))
|
|
|
|
asset, err := repo_model.GetAttachmentByID(ctx, id)
|
|
if repo_model.IsErrAttachmentNotExist(err) {
|
|
return false
|
|
}
|
|
if err != nil {
|
|
panic(fmt.Errorf("asset %v %w", id, err))
|
|
}
|
|
|
|
o.forgejoAsset = asset
|
|
|
|
path := o.forgejoAsset.RelativePath()
|
|
|
|
{
|
|
f, err := storage.Attachments.Open(path)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
hasher := sha256.New()
|
|
if _, err := io.Copy(hasher, f); err != nil {
|
|
panic(fmt.Errorf("io.Copy to hasher: %v", err))
|
|
}
|
|
o.sha = hex.EncodeToString(hasher.Sum(nil))
|
|
}
|
|
|
|
o.downloadFunc = func() io.ReadCloser {
|
|
o.Trace("download %s from copy stored in temporary file %s", o.forgejoAsset.DownloadURL, path)
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return f
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (o *asset) Patch(ctx context.Context) {
|
|
o.Trace("%d", o.forgejoAsset.ID)
|
|
if _, err := db.GetEngine(ctx).ID(o.forgejoAsset.ID).Cols("name").Update(o.forgejoAsset); err != nil {
|
|
panic(fmt.Errorf("UpdateAssetCols: %v %v", o.forgejoAsset, err))
|
|
}
|
|
}
|
|
|
|
func (o *asset) Put(ctx context.Context) generic.NodeID {
|
|
node := o.GetNode()
|
|
o.Trace("%s", node.GetID())
|
|
|
|
uploader, err := user_model.GetAdminUser(ctx)
|
|
if err != nil {
|
|
panic(fmt.Errorf("GetAdminUser %w", err))
|
|
}
|
|
|
|
o.forgejoAsset.UploaderID = uploader.ID
|
|
o.forgejoAsset.RepoID = f3_tree.GetProjectID(o.GetNode())
|
|
o.forgejoAsset.ReleaseID = f3_tree.GetReleaseID(o.GetNode())
|
|
o.forgejoAsset.UUID = uuid.New().String()
|
|
|
|
download := o.downloadFunc()
|
|
defer download.Close()
|
|
|
|
_, err = attachment.NewAttachment(ctx, o.forgejoAsset, download, o.forgejoAsset.Size)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
o.Trace("asset created %d", o.forgejoAsset.ID)
|
|
return generic.NodeID(fmt.Sprintf("%d", o.forgejoAsset.ID))
|
|
}
|
|
|
|
func (o *asset) Delete(ctx context.Context) {
|
|
node := o.GetNode()
|
|
o.Trace("%s", node.GetID())
|
|
|
|
if err := repo_model.DeleteAttachment(ctx, o.forgejoAsset, true); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func newAsset() generic.NodeDriverInterface {
|
|
return &asset{}
|
|
}
|