You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.0 KiB

Git LFS support v2 (#122) * Import github.com/git-lfs/lfs-test-server as lfs module base Imported commit is 3968aac269a77b73924649b9412ae03f7ccd3198 Removed: Dockerfile CONTRIBUTING.md mgmt* script/ vendor/ kvlogger.go .dockerignore .gitignore README.md * Remove config, add JWT support from github.com/mgit-at/lfs-test-server Imported commit f0cdcc5a01599c5a955dc1bbf683bb4acecdba83 * Add LFS settings * Add LFS meta object model * Add LFS routes and initialization * Import github.com/dgrijalva/jwt-go into vendor/ * Adapt LFS module: handlers, routing, meta store * Move LFS routes to /user/repo/info/lfs/* * Add request header checks to LFS BatchHandler / PostHandler * Implement LFS basic authentication * Rework JWT secret generation / load * Implement LFS SSH token authentication with JWT Specification: https://github.com/github/git-lfs/tree/master/docs/api * Integrate LFS settings into install process * Remove LFS objects when repository is deleted Only removes objects from content store when deleted repo is the only referencing repository * Make LFS module stateless Fixes bug where LFS would not work after installation without restarting Gitea * Change 500 'Internal Server Error' to 400 'Bad Request' * Change sql query to xorm call * Remove unneeded type from LFS module * Change internal imports to code.gitea.io/gitea/ * Add Gitea authors copyright * Change basic auth realm to "gitea-lfs" * Add unique indexes to LFS model * Use xorm count function in LFS check on repository delete * Return io.ReadCloser from content store and close after usage * Add LFS info to runWeb() * Export LFS content store base path * LFS file download from UI * Work around git-lfs client issue with unauthenticated requests Returning a dummy Authorization header for unauthenticated requests lets git-lfs client skip asking for auth credentials See: https://github.com/github/git-lfs/issues/1088 * Fix unauthenticated UI downloads from public repositories * Authentication check order, Finish LFS file view logic * Ignore LFS hooks if installed for current OS user Fixes Gitea UI actions for repositories tracking LFS files. Checks for minimum needed git version by parsing the semantic version string. * Hide LFS metafile diff from commit view, marking as binary * Show LFS notice if file in commit view is tracked * Add notbefore/nbf JWT claim * Correct lint suggestions - comments for structs and functions - Add comments to LFS model - Function comment for GetRandomBytesAsBase64 - LFS server function comments and lint variable suggestion * Move secret generation code out of conditional Ensures no LFS code may run with an empty secret * Do not hand out JWT tokens if LFS server support is disabled
8 years ago
  1. package lfs
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "errors"
  7. "io"
  8. "os"
  9. "path/filepath"
  10. )
  11. var (
  12. errHashMismatch = errors.New("Content hash does not match OID")
  13. errSizeMismatch = errors.New("Content size does not match")
  14. )
  15. // ContentStore provides a simple file system based storage.
  16. type ContentStore struct {
  17. BasePath string
  18. }
  19. // Get takes a Meta object and retreives the content from the store, returning
  20. // it as an io.Reader. If fromByte > 0, the reader starts from that byte
  21. func (s *ContentStore) Get(meta *models.LFSMetaObject, fromByte int64) (io.ReadCloser, error) {
  22. path := filepath.Join(s.BasePath, transformKey(meta.Oid))
  23. f, err := os.Open(path)
  24. if err != nil {
  25. return nil, err
  26. }
  27. if fromByte > 0 {
  28. _, err = f.Seek(fromByte, os.SEEK_CUR)
  29. }
  30. return f, err
  31. }
  32. // Put takes a Meta object and an io.Reader and writes the content to the store.
  33. func (s *ContentStore) Put(meta *models.LFSMetaObject, r io.Reader) error {
  34. path := filepath.Join(s.BasePath, transformKey(meta.Oid))
  35. tmpPath := path + ".tmp"
  36. dir := filepath.Dir(path)
  37. if err := os.MkdirAll(dir, 0750); err != nil {
  38. return err
  39. }
  40. file, err := os.OpenFile(tmpPath, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0640)
  41. if err != nil {
  42. return err
  43. }
  44. defer os.Remove(tmpPath)
  45. hash := sha256.New()
  46. hw := io.MultiWriter(hash, file)
  47. written, err := io.Copy(hw, r)
  48. if err != nil {
  49. file.Close()
  50. return err
  51. }
  52. file.Close()
  53. if written != meta.Size {
  54. return errSizeMismatch
  55. }
  56. shaStr := hex.EncodeToString(hash.Sum(nil))
  57. if shaStr != meta.Oid {
  58. return errHashMismatch
  59. }
  60. if err := os.Rename(tmpPath, path); err != nil {
  61. return err
  62. }
  63. return nil
  64. }
  65. // Exists returns true if the object exists in the content store.
  66. func (s *ContentStore) Exists(meta *models.LFSMetaObject) bool {
  67. path := filepath.Join(s.BasePath, transformKey(meta.Oid))
  68. if _, err := os.Stat(path); os.IsNotExist(err) {
  69. return false
  70. }
  71. return true
  72. }
  73. func transformKey(key string) string {
  74. if len(key) < 5 {
  75. return key
  76. }
  77. return filepath.Join(key[0:2], key[2:4], key[4:len(key)])
  78. }