code.gitea.io/gitea@v1.22.3/modules/lfs/shared.go (about)

     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  	Error   *ObjectError     `json:"error,omitempty"`
    55  }
    56  
    57  // Link provides a structure with information about how to access a object.
    58  type Link struct {
    59  	Href      string            `json:"href"`
    60  	Header    map[string]string `json:"header,omitempty"`
    61  	ExpiresAt *time.Time        `json:"expires_at,omitempty"`
    62  }
    63  
    64  // ObjectError defines the JSON structure returned to the client in case of an error.
    65  type ObjectError struct {
    66  	Code    int    `json:"code"`
    67  	Message string `json:"message"`
    68  }
    69  
    70  var (
    71  	// See https://github.com/git-lfs/git-lfs/blob/main/docs/api/batch.md#successful-responses
    72  	// LFS object error codes should match HTTP status codes where possible:
    73  	//   404 - The object does not exist on the server.
    74  	//   409 - The specified hash algorithm disagrees with the server's acceptable options.
    75  	//   410 - The object was removed by the owner.
    76  	//   422 - Validation error.
    77  
    78  	ErrObjectNotExist     = util.ErrNotExist // the object does not exist on the server
    79  	ErrObjectHashMismatch = errors.New("the specified hash algorithm disagrees with the server's acceptable options")
    80  	ErrObjectRemoved      = errors.New("the object was removed by the owner")
    81  	ErrObjectValidation   = errors.New("validation error")
    82  )
    83  
    84  func (e *ObjectError) Error() string {
    85  	return fmt.Sprintf("[%d] %s", e.Code, e.Message)
    86  }
    87  
    88  func (e *ObjectError) Unwrap() error {
    89  	switch e.Code {
    90  	case 404:
    91  		return ErrObjectNotExist
    92  	case 409:
    93  		return ErrObjectHashMismatch
    94  	case 410:
    95  		return ErrObjectRemoved
    96  	case 422:
    97  		return ErrObjectValidation
    98  	default:
    99  		return errors.New(e.Message)
   100  	}
   101  }
   102  
   103  // PointerBlob associates a Git blob with a Pointer.
   104  type PointerBlob struct {
   105  	Hash string
   106  	Pointer
   107  }
   108  
   109  // ErrorResponse describes the error to the client.
   110  type ErrorResponse struct {
   111  	Message          string
   112  	DocumentationURL string `json:"documentation_url,omitempty"`
   113  	RequestID        string `json:"request_id,omitempty"`
   114  }