Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ID-91] Expose delete user audio record API #92

Merged
merged 7 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Expose delete user audio record API [#91](https://github.com/rokwire/content-building-block/issues/91)

## [1.3.0] - 2024-01-11
### Added
Expand Down
5 changes: 5 additions & 0 deletions core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Services interface {

UploadVoiceRecord(userID string, bytes []byte) error
GetVoiceRecord(userID string) ([]byte, error)
DeleteVoiceRecord(userID string) error

GetTwitterPosts(userID string, twitterQueryParams string, force bool) (map[string]interface{}, error)
}
Expand Down Expand Up @@ -169,6 +170,10 @@ func (s *servicesImpl) GetVoiceRecord(userID string) ([]byte, error) {
return s.app.getVoiceRecord(userID)
}

func (s *servicesImpl) DeleteVoiceRecord(userID string) error {
return s.app.deleteVoiceRecord(userID)
}

func (s *servicesImpl) GetTwitterPosts(userID string, twitterQueryParams string, force bool) (map[string]interface{}, error) {
return s.app.getTwitterPosts(userID, twitterQueryParams, force)
}
Expand Down
9 changes: 9 additions & 0 deletions core/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,15 @@ func (app *Application) getVoiceRecord(userID string) ([]byte, error) {
return fileContent, nil
}

func (app *Application) deleteVoiceRecord(userID string) error {
err := app.awsAdapter.DeleteUserVoiceRecord(userID)
if err != nil {
return err
}

return nil
}

func (app *Application) getTwitterPosts(userID string, twitterQueryParams string, force bool) (map[string]interface{}, error) {
var err error
posts := app.cacheAdapter.GetTwitterPosts(userID, twitterQueryParams)
Expand Down
22 changes: 22 additions & 0 deletions driven/awsstorage/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,28 @@ func (a *Adapter) LoadUserVoiceRecord(accountID string) ([]byte, error) {
return buffer.Bytes(), nil
}

// DeleteUserVoiceRecord deletes the voice record for the user
func (a *Adapter) DeleteUserVoiceRecord(accountID string) error {
s, err := a.createS3Session()
if err != nil {
log.Printf("Could not create S3 session")
return err
}

key := fmt.Sprintf("names-records/%s.m4a", accountID)

session := s3.New(s)
_, err = session.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(a.config.S3UsersAudiosBucket),
Key: aws.String(key),
})
if err != nil {
return err
}

return nil
}

func (a *Adapter) prepareKey(path string, preferredFileName *string) string {
var fileName string
if preferredFileName == nil {
Expand Down
1 change: 1 addition & 0 deletions driver/web/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (we Adapter) Start() {

contentRouter.HandleFunc("/voice_record", we.coreAuthWrapFunc(we.apisHandler.StoreVoiceRecord, we.auth.coreAuth.userAuth)).Methods("POST")
contentRouter.HandleFunc("/voice_record", we.coreAuthWrapFunc(we.apisHandler.GetVoiceRecord, we.auth.coreAuth.userAuth)).Methods("GET")
contentRouter.HandleFunc("/voice_record", we.coreAuthWrapFunc(we.apisHandler.DeleteVoiceRecord, we.auth.coreAuth.userAuth)).Methods("DELETE")

// handle student guide client apis
contentRouter.HandleFunc("/student_guides", we.coreAuthWrapFunc(we.apisHandler.GetStudentGuides, we.auth.coreAuth.standardAuth)).Methods("GET")
Expand Down
23 changes: 20 additions & 3 deletions driver/web/docs/gen/def.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1918,8 +1918,8 @@ paths:
post:
tags:
- Apis
summary: Upload a voice record file
description: Accept m4a file
summary: Upload a user voice record as a file
description: Accept m4a file with key 'voiceRecord'. It overrides the current audio.
requestBody:
content:
multipart/form-data:
Expand All @@ -1940,7 +1940,7 @@ paths:
get:
tags:
- Apis
summary: Get voice record file
summary: Get the user voice record as a file
responses:
'200':
description: Success
Expand All @@ -1951,6 +1951,23 @@ paths:
format: binary
'404':
description: File not found
delete:
tags:
- Apis
summary: Delete the user voice record
description: |
Delete the user voice record
security:
- bearerAuth: []
responses:
'200':
description: Success
'400':
description: Bad request
'401':
description: Unauthorized
'500':
description: Internal error
/student_guides:
get:
tags:
Expand Down
25 changes: 21 additions & 4 deletions driver/web/docs/resources/apis/voice-record.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
post:
tags:
- Apis
summary: Upload a voice record file
description: Accept m4a file
summary: Upload a user voice record as a file
description: Accept m4a file with key 'voiceRecord'. It overrides the current audio.
requestBody:
content:
multipart/form-data:
Expand All @@ -23,7 +23,7 @@ post:
get:
tags:
- Apis
summary: Get voice record file
summary: Get the user voice record as a file
responses:
'200':
description: Success
Expand All @@ -33,4 +33,21 @@ get:
type: string
format: binary
'404':
description: File not found
description: File not found
delete:
tags:
- Apis
summary: Delete the user voice record
description: |
Delete the user voice record
security:
- bearerAuth: []
responses:
200:
description: Success
400:
description: Bad request
401:
description: Unauthorized
500:
description: Internal error
20 changes: 18 additions & 2 deletions driver/web/rest/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ func (h ApisHandler) StoreVoiceRecord(claims *tokenauth.Claims, w http.ResponseW
return
}

if mime.String() != "audio/mp4" && mime.String() != "audio/x-m4a" {
log.Print("Invalid file type\n")
if mime.String() != "audio/mp4" && mime.String() != "audio/x-m4a" && mime.String() != "audio/m4a" {
log.Printf("Invalid file type - %s\n", mime.String())
http.Error(w, "Invalid file type. Expected m4a!", http.StatusBadRequest)
return
}
Expand Down Expand Up @@ -276,6 +276,22 @@ func (h ApisHandler) GetVoiceRecord(claims *tokenauth.Claims, w http.ResponseWri
w.Write(fileBytes)
}

// DeleteVoiceRecord deletes the user voice record
func (h ApisHandler) DeleteVoiceRecord(claims *tokenauth.Claims, w http.ResponseWriter, r *http.Request) {

err := h.app.Services.DeleteVoiceRecord(claims.Subject)
if err != nil {
if err != nil {
log.Printf("error on delete AWS voice audio file: %s", err)
}
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("Success"))
}

// GetStudentGuides retrieves all student guides
// @Description Retrieves all student guides
// @Tags Client
Expand Down
Loading