本站源代码
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

70 lines
1.7KB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package lfs
  5. import (
  6. "io"
  7. "strconv"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. // ReadPointerFile will return a partially filled LFSMetaObject if the provided reader is a pointer file
  14. func ReadPointerFile(reader io.Reader) (*models.LFSMetaObject, *[]byte) {
  15. if !setting.LFS.StartServer {
  16. return nil, nil
  17. }
  18. buf := make([]byte, 1024)
  19. n, _ := reader.Read(buf)
  20. buf = buf[:n]
  21. if isTextFile := base.IsTextFile(buf); !isTextFile {
  22. return nil, nil
  23. }
  24. return IsPointerFile(&buf), &buf
  25. }
  26. // IsPointerFile will return a partially filled LFSMetaObject if the provided byte slice is a pointer file
  27. func IsPointerFile(buf *[]byte) *models.LFSMetaObject {
  28. if !setting.LFS.StartServer {
  29. return nil
  30. }
  31. headString := string(*buf)
  32. if !strings.HasPrefix(headString, models.LFSMetaFileIdentifier) {
  33. return nil
  34. }
  35. splitLines := strings.Split(headString, "\n")
  36. if len(splitLines) < 3 {
  37. return nil
  38. }
  39. oid := strings.TrimPrefix(splitLines[1], models.LFSMetaFileOidPrefix)
  40. size, err := strconv.ParseInt(strings.TrimPrefix(splitLines[2], "size "), 10, 64)
  41. if len(oid) != 64 || err != nil {
  42. return nil
  43. }
  44. contentStore := &ContentStore{BasePath: setting.LFS.ContentPath}
  45. meta := &models.LFSMetaObject{Oid: oid, Size: size}
  46. if !contentStore.Exists(meta) {
  47. return nil
  48. }
  49. return meta
  50. }
  51. // ReadMetaObject will read a models.LFSMetaObject and return a reader
  52. func ReadMetaObject(meta *models.LFSMetaObject) (io.ReadCloser, error) {
  53. contentStore := &ContentStore{BasePath: setting.LFS.ContentPath}
  54. return contentStore.Get(meta, 0)
  55. }
上海开阖软件有限公司 沪ICP备12045867号-1