@@ -10,6 +10,8 @@ import (
1010 "net/http"
1111 "time"
1212
13+ "github.com/jiaozifs/jiaozifs/utils/hash"
14+
1315 logging "github.com/ipfs/go-log/v2"
1416
1517 "github.com/go-openapi/swag"
@@ -39,7 +41,7 @@ type ObjectController struct {
3941}
4042
4143func (oct ObjectController ) DeleteObject (ctx context.Context , w * api.JiaozifsResponse , r * http.Request , ownerName string , repositoryName string , params api.DeleteObjectParams ) { //nolint
42- user , err := auth .GetOperator (ctx )
44+ operator , err := auth .GetOperator (ctx )
4345 if err != nil {
4446 w .Error (err )
4547 return
@@ -51,7 +53,7 @@ func (oct ObjectController) DeleteObject(ctx context.Context, w *api.JiaozifsRes
5153 return
5254 }
5355
54- if user .Name != ownerName { //todo check permission
56+ if operator .Name != ownerName { //todo check permission
5557 w .Forbidden ()
5658 return
5759 }
@@ -73,6 +75,12 @@ func (oct ObjectController) DeleteObject(ctx context.Context, w *api.JiaozifsRes
7375 return
7476 }
7577
78+ wip , err := oct .Repo .WipRepo ().Get (ctx , models .NewGetWipParams ().SetCreatorID (operator .ID ).SetRepositoryID (repository .ID ).SetRefID (ref .ID ))
79+ if err != nil {
80+ w .Error (err )
81+ return
82+ }
83+
7684 workTree , err := versionmgr .NewWorkTree (ctx , oct .Repo .FileTreeRepo (), models .NewRootTreeEntry (commit .TreeHash ))
7785 if err != nil {
7886 w .Error (err )
@@ -81,11 +89,11 @@ func (oct ObjectController) DeleteObject(ctx context.Context, w *api.JiaozifsRes
8189
8290 err = workTree .RemoveEntry (ctx , params .Path )
8391 if errors .Is (err , versionmgr .ErrPathNotFound ) {
84- w .Code ( http . StatusNotFound )
92+ w .BadRequest ( fmt . Sprintf ( "path %s not found" , params . Path ) )
8593 return
8694 }
8795
88- err = oct .Repo .WipRepo ().UpdateByID (ctx , models .NewUpdateWipParams (params . WipID ).SetCurrentTree (workTree .Root ().Hash ()))
96+ err = oct .Repo .WipRepo ().UpdateByID (ctx , models .NewUpdateWipParams (wip . ID ).SetCurrentTree (workTree .Root ().Hash ()))
8997 if err != nil {
9098 w .Error (err )
9199 return
@@ -123,20 +131,28 @@ func (oct ObjectController) GetObject(ctx context.Context, w *api.JiaozifsRespon
123131 return
124132 }
125133
126- commit , err := oct .Repo .CommitRepo ().Commit (ctx , ref .CommitHash )
127- if err != nil {
128- w .Error (err )
129- return
134+ treeHash := hash .EmptyHash
135+ if ! ref .CommitHash .IsEmpty () {
136+ commit , err := oct .Repo .CommitRepo ().Commit (ctx , ref .CommitHash )
137+ if err != nil {
138+ w .Error (err )
139+ return
140+ }
141+ treeHash = commit .TreeHash
130142 }
131143
132- workTree , err := versionmgr .NewWorkTree (ctx , oct .Repo .FileTreeRepo (), models .NewRootTreeEntry (commit . TreeHash ))
144+ workTree , err := versionmgr .NewWorkTree (ctx , oct .Repo .FileTreeRepo (), models .NewRootTreeEntry (treeHash ))
133145 if err != nil {
134146 w .Error (err )
135147 return
136148 }
137149
138150 blob , name , err := workTree .FindBlob (ctx , params .Path )
139151 if err != nil {
152+ if errors .Is (err , versionmgr .ErrPathNotFound ) {
153+ w .BadRequest (fmt .Sprintf ("path %s not found" , params .Path ))
154+ return
155+ }
140156 w .Error (err )
141157 return
142158 }
@@ -160,7 +176,7 @@ func (oct ObjectController) GetObject(ctx context.Context, w *api.JiaozifsRespon
160176 w .Header ().Set ("Content-Length" , fmt .Sprint (blob .Size ))
161177 }
162178
163- etag := httputil .ETag (blob .Hash .Hex ())
179+ etag := httputil .ETag (blob .CheckSum .Hex ())
164180 w .Header ().Set ("ETag" , etag )
165181 lastModified := httputil .HeaderTimestamp (blob .CreatedAt )
166182 w .Header ().Set ("Last-Modified" , lastModified )
@@ -212,44 +228,40 @@ func (oct ObjectController) HeadObject(ctx context.Context, w *api.JiaozifsRespo
212228 return
213229 }
214230
215- commit , err := oct .Repo .CommitRepo ().Commit (ctx , ref .CommitHash )
216- if err != nil {
217- w .Error (err )
218- return
231+ treeHash := hash .EmptyHash
232+ if ! ref .CommitHash .IsEmpty () {
233+ commit , err := oct .Repo .CommitRepo ().Commit (ctx , ref .CommitHash )
234+ if err != nil {
235+ w .Error (err )
236+ return
237+ }
238+ treeHash = commit .TreeHash
219239 }
220240
221241 fileRepo := oct .Repo .FileTreeRepo ()
222- treeOp , err := versionmgr .NewWorkTree (ctx , fileRepo , models .NewRootTreeEntry (commit . TreeHash ))
242+ workTree , err := versionmgr .NewWorkTree (ctx , fileRepo , models .NewRootTreeEntry (treeHash ))
223243 if err != nil {
224244 w .Error (err )
225245 return
226246 }
227247
228- existNodes , missingPath , err := treeOp .MatchPath (ctx , params .Path )
229- if err != nil {
230- w .Error (err )
231- return
232- }
233- if len (missingPath ) == 0 {
234- w .Error (versionmgr .ErrPathNotFound )
235- return
236- }
237-
238- objectWithName := existNodes [len (existNodes )- 1 ]
239-
240- blob , err := fileRepo .Blob (ctx , objectWithName .Node ().Hash )
248+ blob , name , err := workTree .FindBlob (ctx , params .Path )
241249 if err != nil {
250+ if errors .Is (err , versionmgr .ErrPathNotFound ) {
251+ w .BadRequest (fmt .Sprintf ("path %s not found" , params .Path ))
252+ return
253+ }
242254 w .Error (err )
243255 return
244256 }
245257
246258 //lookup files
247- etag := httputil .ETag (objectWithName . Node (). Hash .Hex ())
259+ etag := httputil .ETag (blob . CheckSum .Hex ())
248260 w .Header ().Set ("ETag" , etag )
249- lastModified := httputil .HeaderTimestamp (objectWithName . Node () .CreatedAt )
261+ lastModified := httputil .HeaderTimestamp (blob .CreatedAt )
250262 w .Header ().Set ("Last-Modified" , lastModified )
251263 w .Header ().Set ("Accept-Ranges" , "bytes" )
252- w .Header ().Set ("Content-Type" , httputil .ExtensionsByType (objectWithName . Entry (). Name ))
264+ w .Header ().Set ("Content-Type" , httputil .ExtensionsByType (name ))
253265 // for security, make sure the browser and any proxies en route don't cache the response
254266 w .Header ().Set ("Cache-Control" , "no-store, must-revalidate" )
255267 w .Header ().Set ("Expires" , "0" )
@@ -315,7 +327,7 @@ func (oct ObjectController) UploadObject(ctx context.Context, w *api.JiaozifsRes
315327 }
316328 defer reader .Close () //nolint
317329
318- user , err := auth .GetOperator (ctx )
330+ operator , err := auth .GetOperator (ctx )
319331 if err != nil {
320332 w .Error (err )
321333 return
@@ -327,18 +339,30 @@ func (oct ObjectController) UploadObject(ctx context.Context, w *api.JiaozifsRes
327339 return
328340 }
329341
330- if user .Name != ownerName { //todo check permission
342+ if operator .Name != ownerName { //todo check permission
331343 w .Forbidden ()
332344 return
333345 }
334346
335- _ , err = oct .Repo .RepositoryRepo ().Get (ctx , models .NewGetRepoParams ().SetOwnerID (owner .ID ).SetName (repositoryName ))
347+ repository , err := oct .Repo .RepositoryRepo ().Get (ctx , models .NewGetRepoParams ().SetOwnerID (owner .ID ).SetName (repositoryName ))
348+ if err != nil {
349+ w .Error (err )
350+ return
351+ }
352+
353+ ref , err := oct .Repo .RefRepo ().Get (ctx , models .NewGetRefParams ().SetName (params .Branch ).SetRepositoryID (repository .ID ))
354+ if err != nil {
355+ w .Error (err )
356+ return
357+ }
358+
359+ wip , err := oct .Repo .WipRepo ().Get (ctx , models .NewGetWipParams ().SetCreatorID (operator .ID ).SetRepositoryID (repository .ID ).SetRefID (ref .ID ))
336360 if err != nil {
337361 w .Error (err )
338362 return
339363 }
340364
341- stash , err := oct .Repo .WipRepo ().Get (ctx , models .NewGetWipParams ().SetID (params . WipID ))
365+ stash , err := oct .Repo .WipRepo ().Get (ctx , models .NewGetWipParams ().SetID (wip . ID ))
342366 if err != nil {
343367 w .Error (err )
344368 return
@@ -351,6 +375,7 @@ func (oct ObjectController) UploadObject(ctx context.Context, w *api.JiaozifsRes
351375 return err
352376 }
353377
378+ // todo move write blob out of transaction
354379 blob , err := workingTree .WriteBlob (ctx , oct .BlockAdapter , reader , r .ContentLength , models .DefaultLeafProperty ())
355380 if err != nil {
356381 return err
@@ -361,7 +386,7 @@ func (oct ObjectController) UploadObject(ctx context.Context, w *api.JiaozifsRes
361386 return err
362387 }
363388 response = api.ObjectStats {
364- Checksum : blob .Hash .Hex (),
389+ Checksum : blob .CheckSum .Hex (),
365390 Mtime : time .Now ().Unix (),
366391 Path : params .Path ,
367392 PathMode : utils .Uint32 (uint32 (filemode .Regular )),
@@ -377,5 +402,5 @@ func (oct ObjectController) UploadObject(ctx context.Context, w *api.JiaozifsRes
377402 return
378403 }
379404
380- w .JSON (response )
405+ w .JSON (response , http . StatusCreated )
381406}
0 commit comments