summaryrefslogtreecommitdiffstats
path: root/services/f3/driver/asset.go
blob: 6759cc645cb31fa2050c5c13405de39da1da0fec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// 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 := node.GetID().Int64()

	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.NewNodeID(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{}
}