From 0a85711eadbf7ac73a522759618743dffbe17dd5 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 22 Mar 2024 18:01:12 -0400 Subject: [PATCH] feat: implement /s5/pin/:cid/status --- api/s5/messages.go | 5 +++++ api/s5/s5.go | 32 ++++++++++++++++++++++++++++++++ api/s5/swagger.yaml | 28 ++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/api/s5/messages.go b/api/s5/messages.go index 1f158a6..70ba4ee 100644 --- a/api/s5/messages.go +++ b/api/s5/messages.go @@ -133,3 +133,8 @@ type AccountPin struct { PinnedAt time.Time `json:"pinned_at"` MimeType string `json:"mime_type"` } + +type AccountPinStatusResponse struct { + Status models.ImportStatus `json:"status"` + Progress float64 `json:"progress"` +} diff --git a/api/s5/s5.go b/api/s5/s5.go index 7cebed9..41e6691 100644 --- a/api/s5/s5.go +++ b/api/s5/s5.go @@ -1141,7 +1141,39 @@ func (s *S5API) accountPin(jc jape.Context) { } func (s *S5API) accountPinStatus(jc jape.Context) { + var cid string + if err := jc.DecodeParam("cid", &cid); err != nil { + return + } + userID := middleware.GetUserFromContext(jc.Request.Context()) + + decodedCid, err := encoding.CIDFromString(cid) + if err != nil { + s.sendErrorResponse(jc, NewS5Error(ErrKeyInvalidOperation, err)) + return + } + + if err := s.accounts.PinByHash(decodedCid.Hash.HashBytes(), userID); err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + s.sendErrorResponse(jc, NewS5Error(ErrKeyResourceNotFound, err)) + return + } + s.sendErrorResponse(jc, NewS5Error(ErrKeyInternalError, err)) + return + } + + meta, err := s._import.GetImport(jc.Request.Context(), decodedCid.Hash.HashBytes()) + + if err != nil { + s.sendErrorResponse(jc, NewS5Error(ErrKeyResourceNotFound, err)) + return + } + + jc.Encode(&AccountPinStatusResponse{ + Status: meta.Status, + Progress: meta.Progress, + }) } func (s *S5API) pinEntity(ctx context.Context, userId uint, userIp string, cid *encoding.CID) error { diff --git a/api/s5/swagger.yaml b/api/s5/swagger.yaml index 637f6fb..7a948fe 100644 --- a/api/s5/swagger.yaml +++ b/api/s5/swagger.yaml @@ -679,6 +679,24 @@ paths: responses: '204': description: File pinned + /s5/pin/{cid}/status: + get: + summary: Retrieve pin status + tags: + - pin + parameters: + - name: cid + in: path + required: true + schema: + type: string + responses: + '200': + description: Pin status + content: + application/json: + schema: + $ref: '#/components/schemas/AccountPinStatusResponse' /s5/delete/{cid}: delete: summary: Delete a file. This will only unpin it from the account, and potentially delete it later if there are no more global pins. @@ -923,6 +941,16 @@ components: type: string mime_type: type: string + AccountPinStatusResponse: + type: object + required: + - status + - progress + properties: + status: + type: string + progress: + type: number RegistryQueryResponse: type: object properties: