diff --git a/models/git/lfs.go b/models/git/lfs.go index 179da3120ae1c..52b0fa1a2890c 100644 --- a/models/git/lfs.go +++ b/models/git/lfs.go @@ -210,7 +210,7 @@ func CountLFSMetaObjects(repoID int64) (int64, error) { // LFSObjectAccessible checks if a provided Oid is accessible to the user func LFSObjectAccessible(user *user_model.User, oid string) (bool, error) { - if user.IsAdmin { + if user != nil && user.IsAdmin { count, err := db.GetEngine(db.DefaultContext).Count(&LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}}) return count > 0, err } diff --git a/services/lfs/server.go b/services/lfs/server.go index b868db39dbece..a1d3af4ac0449 100644 --- a/services/lfs/server.go +++ b/services/lfs/server.go @@ -244,6 +244,26 @@ func BatchHandler(ctx *context.Context) { responseObject = buildObjectResponse(rc, p, false, !exists, err) } else { var err *lfs_module.ObjectError + + if exists && meta == nil { + accessible, accessibleErr := git_model.LFSObjectAccessible(ctx.Doer, p.Oid) + if accessibleErr != nil { + log.Error("Unable to check if LFS MetaObject [%s] is accessible. Error: %v", p.Oid, err) + writeStatus(ctx, http.StatusInternalServerError) + return + } + if accessible { + _, newMetaObjErr := git_model.NewLFSMetaObject(&git_model.LFSMetaObject{Pointer: p, RepositoryID: repository.ID}) + if newMetaObjErr != nil { + log.Error("Unable to create LFS MetaObject [%s] for %s/%s. Error: %v", p.Oid, rc.User, rc.Repo, err) + writeStatus(ctx, http.StatusInternalServerError) + return + } + } else { + exists = false + } + } + if !exists || meta == nil { err = &lfs_module.ObjectError{ Code: http.StatusNotFound,