Skip to content

Commit 24279e1

Browse files
committed
Added size parameter. Create meta object on upload.
1 parent aff8fc8 commit 24279e1

File tree

2 files changed

+54
-21
lines changed

2 files changed

+54
-21
lines changed

routers/routes/web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,9 +1102,9 @@ func RegisterRoutes(m *web.Route) {
11021102
m.Group("/{reponame}", func() {
11031103
m.Group("/info/lfs", func() {
11041104
m.Post("/objects/batch", lfs.CheckAcceptMediaType, lfs.BatchHandler)
1105+
m.Put("/objects/{oid}/{size}", lfs.UploadHandler)
11051106
m.Get("/objects/{oid}/{filename}", lfs.DownloadHandler)
11061107
m.Get("/objects/{oid}", lfs.DownloadHandler)
1107-
m.Put("/objects/{oid}", lfs.UploadHandler)
11081108
m.Post("/verify", lfs.CheckAcceptMediaType, lfs.VerifyHandler)
11091109
m.Group("/locks", func() {
11101110
m.Get("/", lfs.GetListLockHandler)

services/lfs/server.go

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@ type Claims struct {
3939
jwt.StandardClaims
4040
}
4141

42-
// ObjectLink builds a URL linking to the object.
43-
func (rc *requestContext) ObjectLink(oid string) string {
44-
return setting.AppURL + path.Join(rc.User, rc.Repo+".git", "info/lfs/objects", oid)
42+
// DownloadLink builds a URL to download the object.
43+
func (rc *requestContext) DownloadLink(p lfs_module.Pointer) string {
44+
return setting.AppURL + path.Join(rc.User, rc.Repo+".git", "info/lfs/objects", p.Oid)
45+
}
46+
47+
// UploadLink builds a URL to upload the object.
48+
func (rc *requestContext) UploadLink(p lfs_module.Pointer) string {
49+
return setting.AppURL + path.Join(rc.User, rc.Repo+".git", "info/lfs/objects", p.Oid, strconv.FormatInt(p.Size, 10))
4550
}
4651

4752
// VerifyLink builds a URL for verifying the object.
48-
func (rc *requestContext) VerifyLink() string {
53+
func (rc *requestContext) VerifyLink(p lfs_module.Pointer) string {
4954
return setting.AppURL + path.Join(rc.User, rc.Repo+".git", "info/lfs/verify")
5055
}
5156

@@ -63,19 +68,12 @@ func CheckAcceptMediaType(ctx *context.Context) {
6368
func getAuthenticatedRepoAndMeta(ctx *context.Context, rc *requestContext, p lfs_module.Pointer, requireWrite bool) (*models.LFSMetaObject, *models.Repository) {
6469
if !p.IsValid() {
6570
log.Info("Attempt to access invalid LFS OID[%s] in %s/%s", p.Oid, rc.User, rc.Repo)
66-
writeStatus(ctx, http.StatusNotFound)
67-
return nil, nil
68-
}
69-
70-
repository, err := models.GetRepositoryByOwnerAndName(rc.User, rc.Repo)
71-
if err != nil {
72-
log.Error("Unable to get repository: %s/%s Error: %v", rc.User, rc.Repo, err)
73-
writeStatus(ctx, http.StatusNotFound)
71+
writeStatus(ctx, http.StatusUnprocessableEntity)
7472
return nil, nil
7573
}
7674

77-
if !authenticate(ctx, repository, rc.Authorization, requireWrite) {
78-
requireAuth(ctx)
75+
repository := getAuthenticatedRepository(ctx, rc, requireWrite)
76+
if repository == nil {
7977
return nil, nil
8078
}
8179

@@ -89,6 +87,22 @@ func getAuthenticatedRepoAndMeta(ctx *context.Context, rc *requestContext, p lfs
8987
return meta, repository
9088
}
9189

90+
func getAuthenticatedRepository(ctx *context.Context, rc *requestContext, requireWrite bool) *models.Repository {
91+
repository, err := models.GetRepositoryByOwnerAndName(rc.User, rc.Repo)
92+
if err != nil {
93+
log.Error("Unable to get repository: %s/%s Error: %v", rc.User, rc.Repo, err)
94+
writeStatus(ctx, http.StatusNotFound)
95+
return nil
96+
}
97+
98+
if !authenticate(ctx, repository, rc.Authorization, requireWrite) {
99+
requireAuth(ctx)
100+
return nil
101+
}
102+
103+
return repository
104+
}
105+
92106
// DownloadHandler gets the content from the content store
93107
func DownloadHandler(ctx *context.Context) {
94108
rc, p := unpack(ctx)
@@ -279,10 +293,29 @@ func BatchHandler(ctx *context.Context) {
279293

280294
// UploadHandler receives data from the client and puts it into the content store
281295
func UploadHandler(ctx *context.Context) {
282-
rc, p := unpack(ctx)
296+
rc := getRequestContext(ctx)
283297

284-
meta, repository := getAuthenticatedRepoAndMeta(ctx, rc, p, true)
285-
if meta == nil {
298+
p := lfs_module.Pointer{Oid: ctx.Params("oid")}
299+
var err error
300+
if p.Size, err = strconv.ParseInt(ctx.Params("size"), 10, 64); err != nil {
301+
writeStatusMessage(ctx, http.StatusUnprocessableEntity, err)
302+
}
303+
304+
if !p.IsValid() {
305+
log.Trace("Attempt to access invalid LFS OID[%s] in %s/%s", p.Oid, rc.User, rc.Repo)
306+
writeStatus(ctx, http.StatusUnprocessableEntity)
307+
return
308+
}
309+
310+
repository := getAuthenticatedRepository(ctx, rc, true)
311+
if repository == nil {
312+
return
313+
}
314+
315+
meta, err := models.NewLFSMetaObject(&models.LFSMetaObject{Pointer: p, RepositoryID: repository.ID})
316+
if err != nil {
317+
log.Error("Unable to create LFS MetaObject [%s] for %s/%s. Error: %v", p.Oid, rc.User, rc.Repo, err)
318+
writeStatus(ctx, http.StatusInternalServerError)
286319
return
287320
}
288321

@@ -361,11 +394,11 @@ func buildObjectResponse(rc *requestContext, pointer lfs_module.Pointer, downloa
361394
}
362395

363396
if download {
364-
rep.Actions["download"] = &lfs_module.Link{Href: rc.ObjectLink(pointer.Oid), Header: header}
397+
rep.Actions["download"] = &lfs_module.Link{Href: rc.DownloadLink(pointer), Header: header}
365398
}
366399
if upload {
367-
rep.Actions["upload"] = &lfs_module.Link{Href: rc.ObjectLink(pointer.Oid), Header: header}
368-
rep.Actions["verify"] = &lfs_module.Link{Href: rc.VerifyLink(), Header: verifyHeader}
400+
rep.Actions["upload"] = &lfs_module.Link{Href: rc.UploadLink(pointer), Header: header}
401+
rep.Actions["verify"] = &lfs_module.Link{Href: rc.VerifyLink(pointer), Header: verifyHeader}
369402
}
370403
}
371404
return rep

0 commit comments

Comments
 (0)