@@ -39,13 +39,18 @@ type Claims struct {
39
39
jwt.StandardClaims
40
40
}
41
41
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 ))
45
50
}
46
51
47
52
// VerifyLink builds a URL for verifying the object.
48
- func (rc * requestContext ) VerifyLink () string {
53
+ func (rc * requestContext ) VerifyLink (p lfs_module. Pointer ) string {
49
54
return setting .AppURL + path .Join (rc .User , rc .Repo + ".git" , "info/lfs/verify" )
50
55
}
51
56
@@ -63,19 +68,12 @@ func CheckAcceptMediaType(ctx *context.Context) {
63
68
func getAuthenticatedRepoAndMeta (ctx * context.Context , rc * requestContext , p lfs_module.Pointer , requireWrite bool ) (* models.LFSMetaObject , * models.Repository ) {
64
69
if ! p .IsValid () {
65
70
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 )
74
72
return nil , nil
75
73
}
76
74
77
- if ! authenticate (ctx , repository , rc . Authorization , requireWrite ) {
78
- requireAuth ( ctx )
75
+ repository := getAuthenticatedRepository (ctx , rc , requireWrite )
76
+ if repository == nil {
79
77
return nil , nil
80
78
}
81
79
@@ -89,6 +87,22 @@ func getAuthenticatedRepoAndMeta(ctx *context.Context, rc *requestContext, p lfs
89
87
return meta , repository
90
88
}
91
89
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
+
92
106
// DownloadHandler gets the content from the content store
93
107
func DownloadHandler (ctx * context.Context ) {
94
108
rc , p := unpack (ctx )
@@ -279,10 +293,29 @@ func BatchHandler(ctx *context.Context) {
279
293
280
294
// UploadHandler receives data from the client and puts it into the content store
281
295
func UploadHandler (ctx * context.Context ) {
282
- rc , p := unpack (ctx )
296
+ rc := getRequestContext (ctx )
283
297
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 )
286
319
return
287
320
}
288
321
@@ -361,11 +394,11 @@ func buildObjectResponse(rc *requestContext, pointer lfs_module.Pointer, downloa
361
394
}
362
395
363
396
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 }
365
398
}
366
399
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 }
369
402
}
370
403
}
371
404
return rep
0 commit comments