diff options
Diffstat (limited to 'models/lfs.go')
-rw-r--r-- | models/lfs.go | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/models/lfs.go b/models/lfs.go index 39b0b2dd69..94d3f57905 100644 --- a/models/lfs.go +++ b/models/lfs.go @@ -1,7 +1,11 @@ package models import ( + "crypto/sha256" + "encoding/hex" "errors" + "fmt" + "io" "code.gitea.io/gitea/modules/util" ) @@ -16,6 +20,11 @@ type LFSMetaObject struct { CreatedUnix util.TimeStamp `xorm:"created"` } +// Pointer returns the string representation of an LFS pointer file +func (m *LFSMetaObject) Pointer() string { + return fmt.Sprintf("%s\n%s%s\nsize %d\n", LFSMetaFileIdentifier, LFSMetaFileOidPrefix, m.Oid, m.Size) +} + // LFSTokenResponse defines the JSON structure in which the JWT token is stored. // This structure is fetched via SSH and passed by the Git LFS client to the server // endpoint for authorization. @@ -67,6 +76,16 @@ func NewLFSMetaObject(m *LFSMetaObject) (*LFSMetaObject, error) { return m, sess.Commit() } +// GenerateLFSOid generates a Sha256Sum to represent an oid for arbitrary content +func GenerateLFSOid(content io.Reader) (string, error) { + h := sha256.New() + if _, err := io.Copy(h, content); err != nil { + return "", err + } + sum := h.Sum(nil) + return hex.EncodeToString(sum), nil +} + // GetLFSMetaObjectByOid selects a LFSMetaObject entry from database by its OID. // It may return ErrLFSObjectNotExist or a database error. If the error is nil, // the returned pointer is a valid LFSMetaObject. |