gitea

Зеркало из https://github.com/go-gitea/gitea
Форк
0
/
shared.go 
115 строк · 3.5 Кб
1
// Copyright 2020 The Gitea Authors. All rights reserved.
2
// SPDX-License-Identifier: MIT
3

4
package lfs
5

6
import (
7
	"errors"
8
	"fmt"
9
	"time"
10

11
	"code.gitea.io/gitea/modules/util"
12
)
13

14
const (
15
	// MediaType contains the media type for LFS server requests
16
	MediaType = "application/vnd.git-lfs+json"
17
	// Some LFS servers offer content with other types, so fallback to '*/*' if application/vnd.git-lfs+json cannot be served
18
	AcceptHeader = "application/vnd.git-lfs+json;q=0.9, */*;q=0.8"
19
)
20

21
// BatchRequest contains multiple requests processed in one batch operation.
22
// https://github.com/git-lfs/git-lfs/blob/main/docs/api/batch.md#requests
23
type BatchRequest struct {
24
	Operation string     `json:"operation"`
25
	Transfers []string   `json:"transfers,omitempty"`
26
	Ref       *Reference `json:"ref,omitempty"`
27
	Objects   []Pointer  `json:"objects"`
28
}
29

30
// Reference contains a git reference.
31
// https://github.com/git-lfs/git-lfs/blob/main/docs/api/batch.md#ref-property
32
type Reference struct {
33
	Name string `json:"name"`
34
}
35

36
// Pointer contains LFS pointer data
37
type Pointer struct {
38
	Oid  string `json:"oid" xorm:"UNIQUE(s) INDEX NOT NULL"`
39
	Size int64  `json:"size" xorm:"NOT NULL"`
40
}
41

42
// BatchResponse contains multiple object metadata Representation structures
43
// for use with the batch API.
44
// https://github.com/git-lfs/git-lfs/blob/main/docs/api/batch.md#successful-responses
45
type BatchResponse struct {
46
	Transfer string            `json:"transfer,omitempty"`
47
	Objects  []*ObjectResponse `json:"objects"`
48
}
49

50
// ObjectResponse is object metadata as seen by clients of the LFS server.
51
type ObjectResponse struct {
52
	Pointer
53
	Actions map[string]*Link `json:"actions,omitempty"`
54
	Links   map[string]*Link `json:"_links,omitempty"`
55
	Error   *ObjectError     `json:"error,omitempty"`
56
}
57

58
// Link provides a structure with information about how to access a object.
59
type Link struct {
60
	Href      string            `json:"href"`
61
	Header    map[string]string `json:"header,omitempty"`
62
	ExpiresAt *time.Time        `json:"expires_at,omitempty"`
63
}
64

65
// ObjectError defines the JSON structure returned to the client in case of an error.
66
type ObjectError struct {
67
	Code    int    `json:"code"`
68
	Message string `json:"message"`
69
}
70

71
var (
72
	// See https://github.com/git-lfs/git-lfs/blob/main/docs/api/batch.md#successful-responses
73
	// LFS object error codes should match HTTP status codes where possible:
74
	//   404 - The object does not exist on the server.
75
	//   409 - The specified hash algorithm disagrees with the server's acceptable options.
76
	//   410 - The object was removed by the owner.
77
	//   422 - Validation error.
78

79
	ErrObjectNotExist     = util.ErrNotExist // the object does not exist on the server
80
	ErrObjectHashMismatch = errors.New("the specified hash algorithm disagrees with the server's acceptable options")
81
	ErrObjectRemoved      = errors.New("the object was removed by the owner")
82
	ErrObjectValidation   = errors.New("validation error")
83
)
84

85
func (e *ObjectError) Error() string {
86
	return fmt.Sprintf("[%d] %s", e.Code, e.Message)
87
}
88

89
func (e *ObjectError) Unwrap() error {
90
	switch e.Code {
91
	case 404:
92
		return ErrObjectNotExist
93
	case 409:
94
		return ErrObjectHashMismatch
95
	case 410:
96
		return ErrObjectRemoved
97
	case 422:
98
		return ErrObjectValidation
99
	default:
100
		return errors.New(e.Message)
101
	}
102
}
103

104
// PointerBlob associates a Git blob with a Pointer.
105
type PointerBlob struct {
106
	Hash string
107
	Pointer
108
}
109

110
// ErrorResponse describes the error to the client.
111
type ErrorResponse struct {
112
	Message          string
113
	DocumentationURL string `json:"documentation_url,omitempty"`
114
	RequestID        string `json:"request_id,omitempty"`
115
}
116

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.