aboutsummaryrefslogtreecommitdiffstats
path: root/services/repository/files/upload.go
blob: b783cbd01de2f666f652a40477bbbff1e25acc7c (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
// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package files

import (
	"context"
	"fmt"
	"os"
	"path"
	"sync"

	repo_model "code.gitea.io/gitea/models/repo"
	user_model "code.gitea.io/gitea/models/user"
	"code.gitea.io/gitea/modules/log"
)

// UploadRepoFileOptions contains the uploaded repository file options
type UploadRepoFileOptions struct {
	LastCommitID string
	OldBranch    string
	NewBranch    string
	TreePath     string
	Message      string
	Files        []string // In UUID format.
	Signoff      bool
	Author       *IdentityOptions
	Committer    *IdentityOptions
}

type lazyLocalFileReader struct {
	*os.File
	localFilename string
	counter       int
	mu            sync.Mutex
}

var _ LazyReadSeeker = (*lazyLocalFileReader)(nil)

func (l *lazyLocalFileReader) Close() error {
	l.mu.Lock()
	defer l.mu.Unlock()

	if l.counter > 0 {
		l.counter--
		if l.counter == 0 {
			if err := l.File.Close(); err != nil {
				return fmt.Errorf("close file %s: %w", l.localFilename, err)
			}
			l.File = nil
		}
		return nil
	}
	return fmt.Errorf("file %s already closed", l.localFilename)
}

func (l *lazyLocalFileReader) OpenLazyReader() error {
	l.mu.Lock()
	defer l.mu.Unlock()

	if l.File != nil {
		l.counter++
		return nil
	}

	file, err := os.Open(l.localFilename)
	if err != nil {
		return err
	}
	l.File = file
	l.counter = 1
	return nil
}

// UploadRepoFiles uploads files to the given repository
func UploadRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, opts *UploadRepoFileOptions) error {
	if len(opts.Files) == 0 {
		return nil
	}

	uploads, err := repo_model.GetUploadsByUUIDs(ctx, opts.Files)
	if err != nil {
		return fmt.Errorf("GetUploadsByUUIDs [uuids: %v]: %w", opts.Files, err)
	}

	changeOpts := &ChangeRepoFilesOptions{
		LastCommitID: opts.LastCommitID,
		OldBranch:    opts.OldBranch,
		NewBranch:    opts.NewBranch,
		Message:      opts.Message,
		Signoff:      opts.Signoff,
		Author:       opts.Author,
		Committer:    opts.Committer,
	}
	for _, upload := range uploads {
		changeOpts.Files = append(changeOpts.Files, &ChangeRepoFile{
			Operation:     "upload",
			TreePath:      path.Join(opts.TreePath, upload.Name),
			ContentReader: &lazyLocalFileReader{localFilename: upload.LocalPath()},
		})
	}

	_, err = ChangeRepoFiles(ctx, repo, doer, changeOpts)
	if err != nil {
		return err
	}
	if err := repo_model.DeleteUploads(ctx, uploads...); err != nil {
		log.Error("DeleteUploads: %v", err)
	}
	return nil
}