gitea
Зеркало из https://github.com/go-gitea/gitea
1// Copyright 2020 The Gitea Authors. All rights reserved.
2// SPDX-License-Identifier: MIT
3
4package lfs
5
6import (
7"errors"
8"fmt"
9"time"
10
11"code.gitea.io/gitea/modules/util"
12)
13
14const (
15// MediaType contains the media type for LFS server requests
16MediaType = "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
18AcceptHeader = "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
23type BatchRequest struct {
24Operation string `json:"operation"`
25Transfers []string `json:"transfers,omitempty"`
26Ref *Reference `json:"ref,omitempty"`
27Objects []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
32type Reference struct {
33Name string `json:"name"`
34}
35
36// Pointer contains LFS pointer data
37type Pointer struct {
38Oid string `json:"oid" xorm:"UNIQUE(s) INDEX NOT NULL"`
39Size 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
45type BatchResponse struct {
46Transfer string `json:"transfer,omitempty"`
47Objects []*ObjectResponse `json:"objects"`
48}
49
50// ObjectResponse is object metadata as seen by clients of the LFS server.
51type ObjectResponse struct {
52Pointer
53Actions map[string]*Link `json:"actions,omitempty"`
54Links map[string]*Link `json:"_links,omitempty"`
55Error *ObjectError `json:"error,omitempty"`
56}
57
58// Link provides a structure with information about how to access a object.
59type Link struct {
60Href string `json:"href"`
61Header map[string]string `json:"header,omitempty"`
62ExpiresAt *time.Time `json:"expires_at,omitempty"`
63}
64
65// ObjectError defines the JSON structure returned to the client in case of an error.
66type ObjectError struct {
67Code int `json:"code"`
68Message string `json:"message"`
69}
70
71var (
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
79ErrObjectNotExist = util.ErrNotExist // the object does not exist on the server
80ErrObjectHashMismatch = errors.New("the specified hash algorithm disagrees with the server's acceptable options")
81ErrObjectRemoved = errors.New("the object was removed by the owner")
82ErrObjectValidation = errors.New("validation error")
83)
84
85func (e *ObjectError) Error() string {
86return fmt.Sprintf("[%d] %s", e.Code, e.Message)
87}
88
89func (e *ObjectError) Unwrap() error {
90switch e.Code {
91case 404:
92return ErrObjectNotExist
93case 409:
94return ErrObjectHashMismatch
95case 410:
96return ErrObjectRemoved
97case 422:
98return ErrObjectValidation
99default:
100return errors.New(e.Message)
101}
102}
103
104// PointerBlob associates a Git blob with a Pointer.
105type PointerBlob struct {
106Hash string
107Pointer
108}
109
110// ErrorResponse describes the error to the client.
111type ErrorResponse struct {
112Message string
113DocumentationURL string `json:"documentation_url,omitempty"`
114RequestID string `json:"request_id,omitempty"`
115}
116