refactor: add logic to find any unpinned files of a child manifest if the root is pinned but the children aren't

This commit is contained in:
Derrick Hammer 2024-03-05 16:27:29 -05:00
parent 111d0a7ead
commit 6fb77d102a
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 28 additions and 6 deletions

View File

@ -844,7 +844,7 @@ func (s *S5API) accountPinDelete(jc jape.Context) {
jc.ResponseWriter.WriteHeader(http.StatusNoContent) jc.ResponseWriter.WriteHeader(http.StatusNoContent)
} }
func (s *S5API) getManifestCids(ctx context.Context, cid *encoding.CID) ([]*encoding.CID, error) { func (s *S5API) getManifestCids(ctx context.Context, cid *encoding.CID, addSelf bool) ([]*encoding.CID, error) {
var cids []*encoding.CID var cids []*encoding.CID
if middleware.CtxAborted(ctx) { if middleware.CtxAborted(ctx) {
@ -856,7 +856,9 @@ func (s *S5API) getManifestCids(ctx context.Context, cid *encoding.CID) ([]*enco
return nil, err return nil, err
} }
if addSelf {
cids = append(cids, cid) cids = append(cids, cid)
}
switch cid.Type { switch cid.Type {
case types.CIDTypeMetadataMedia: case types.CIDTypeMetadataMedia:
@ -888,7 +890,7 @@ func (s *S5API) getManifestCids(ctx context.Context, cid *encoding.CID) ([]*enco
return return
} }
childCids, err := s.getManifestCids(ctx, cid) childCids, err := s.getManifestCids(ctx, cid, true)
if err != nil { if err != nil {
s.logger.Error("Error getting child manifest CIDs", zap.Error(err)) s.logger.Error("Error getting child manifest CIDs", zap.Error(err))
return return
@ -916,7 +918,7 @@ func (s *S5API) getManifestCids(ctx context.Context, cid *encoding.CID) ([]*enco
return cids, nil return cids, nil
} }
func (s *S5API) accountPinManifest(jc jape.Context, userId uint, cid *encoding.CID) { func (s *S5API) accountPinManifest(jc jape.Context, userId uint, cid *encoding.CID, addSelf bool) {
type pinResult struct { type pinResult struct {
Success bool `json:"success"` Success bool `json:"success"`
Error error `json:"error,omitempty"` Error error `json:"error,omitempty"`
@ -928,7 +930,7 @@ func (s *S5API) accountPinManifest(jc jape.Context, userId uint, cid *encoding.C
cid *encoding.CID cid *encoding.CID
} }
cids, err := s.getManifestCids(jc.Request.Context(), cid) cids, err := s.getManifestCids(jc.Request.Context(), cid, addSelf)
if err != nil { if err != nil {
s.sendErrorResponse(jc, NewS5Error(ErrKeyInvalidOperation, err)) s.sendErrorResponse(jc, NewS5Error(ErrKeyInvalidOperation, err))
return return
@ -1040,7 +1042,7 @@ func (s *S5API) accountPin(jc jape.Context) {
if !found { if !found {
if isCidManifest(decodedCid) { if isCidManifest(decodedCid) {
s.accountPinManifest(jc, userID, decodedCid) s.accountPinManifest(jc, userID, decodedCid, true)
return return
} else { } else {
err = s.pinEntity(jc.Request.Context(), userID, decodedCid) err = s.pinEntity(jc.Request.Context(), userID, decodedCid)
@ -1049,6 +1051,26 @@ func (s *S5API) accountPin(jc jape.Context) {
return return
} }
} }
} else {
cids, err := s.getManifestCids(jc.Request.Context(), decodedCid, false)
if err != nil {
s.sendErrorResponse(jc, NewS5Error(ErrKeyStorageOperationFailed, err))
return
}
for _, cid := range cids {
if err := s.accounts.PinByHash(cid.Hash.HashBytes(), userID); err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
s.sendErrorResponse(jc, NewS5Error(ErrKeyStorageOperationFailed, err))
return
}
err := s.pinEntity(jc.Request.Context(), userID, cid)
if err != nil {
s.sendErrorResponse(jc, NewS5Error(ErrKeyStorageOperationFailed, err))
return
}
}
}
} }
jc.ResponseWriter.WriteHeader(http.StatusNoContent) jc.ResponseWriter.WriteHeader(http.StatusNoContent)