Skip to content

Commit

Permalink
fix: delete media files and storage folder on media delete
Browse files Browse the repository at this point in the history
fixes #98
  • Loading branch information
sundowndev committed Feb 28, 2021
1 parent f198c8f commit e4757bc
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
18 changes: 18 additions & 0 deletions api/medias.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/dreamvo/gilfoyle/ent"
_ "github.com/dreamvo/gilfoyle/ent"
"github.com/dreamvo/gilfoyle/ent/media"
"github.com/dreamvo/gilfoyle/ent/mediafile"
"github.com/dreamvo/gilfoyle/ent/schema"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
Expand Down Expand Up @@ -127,6 +128,23 @@ func (s *Server) deleteMedia(ctx *gin.Context) {
return
}

if v.Status == media.StatusProcessing {
util.NewError(ctx, http.StatusForbidden, errors.New("you can't delete a media while it's in processing state"))
return
}

_, err = s.db.MediaFile.Delete().Where(mediafile.HasMediaWith(media.ID(parsedUUID))).Exec(context.Background())
if err != nil {
util.NewError(ctx, http.StatusInternalServerError, errors.Unwrap(err))
return
}

err = s.storage.Delete(context.Background(), parsedUUID.String())
if err != nil {
util.NewError(ctx, http.StatusInternalServerError, errors.Unwrap(err))
return
}

err = s.db.Media.DeleteOneID(parsedUUID).Exec(context.Background())
if err != nil {
util.NewError(ctx, http.StatusInternalServerError, errors.Unwrap(err))
Expand Down
63 changes: 57 additions & 6 deletions api/medias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"github.com/dreamvo/gilfoyle/api/util"
"github.com/dreamvo/gilfoyle/ent"
"github.com/dreamvo/gilfoyle/ent/enttest"
"github.com/dreamvo/gilfoyle/ent/media"
"github.com/dreamvo/gilfoyle/ent/schema"
"github.com/dreamvo/gilfoyle/x/testutils"
"github.com/dreamvo/gilfoyle/x/testutils/mocks"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
Expand Down Expand Up @@ -220,30 +222,79 @@ func TestMedias(t *testing.T) {
dbClient := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
defer func() { _ = dbClient.Close() }()

mockedStorage := new(mocks.MockedStorage)

s := NewServer(Options{
Database: dbClient,
Logger: zap.NewExample(),
Storage: mockedStorage,
})

v, _ := dbClient.Media.
m, err := dbClient.Media.
Create().
SetTitle("test").
SetStatus(schema.MediaStatusAwaitingUpload).
Save(context.Background())
assert.NoError(t, err)

res, err := testutils.Send(s.router, http.MethodDelete, "/medias/"+v.ID.String(), nil)
_, err = dbClient.MediaFile.
Create().
SetMedia(m).
SetRenditionName("low").
SetFormat("hls").
SetTargetBandwidth(14000000).
SetVideoBitrate(14000000).
SetResolutionWidth(1920).
SetResolutionHeight(1080).
SetDurationSeconds(5).
SetFramerate(30).
SetMediaType(schema.MediaFileTypeVideo).
Save(context.Background())
assert.NoError(t, err)

mockedStorage.On("Delete", m.ID.String()).Return(nil)

res, err := testutils.Send(s.router, http.MethodDelete, "/medias/"+m.ID.String(), nil)
assert.NoError(t, err, "should be equal")

assert.Equal(t, res.Result().StatusCode, 200, "should be equal")

res, err = testutils.Send(s.router, http.MethodDelete, "/medias/"+v.ID.String(), nil)
res, err = testutils.Send(s.router, http.MethodDelete, "/medias/"+m.ID.String(), nil)
assert.NoError(t, err, "should be equal")

var body util.ErrorResponse
_ = json.NewDecoder(res.Body).Decode(&body)

assert.Equal(t, 404, res.Code)
assert.Equal(t, "resource not found", body.Message)

count, err := dbClient.MediaFile.Query().Count(context.Background())
assert.NoError(t, err)
assert.Equal(t, 0, count)

mockedStorage.AssertExpectations(t)
})

t.Run("should not delete a media being processed", func(t *testing.T) {
dbClient := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
defer func() { _ = dbClient.Close() }()

s := NewServer(Options{
Database: dbClient,
Logger: zap.NewExample(),
})

v, err := dbClient.Media.
Create().
SetTitle("test").
SetStatus(schema.MediaStatusProcessing).
Save(context.Background())
assert.NoError(t, err)

res, err := testutils.Send(s.router, http.MethodDelete, "/medias/"+v.ID.String(), nil)
assert.NoError(t, err, "should be equal")

assert.Equal(t, res.Result().StatusCode, 403, "should be equal")
})

t.Run("should return error on invalid uid", func(t *testing.T) {
Expand Down Expand Up @@ -353,16 +404,16 @@ func TestMedias(t *testing.T) {
assert.NoError(t, err)

res, err := testutils.Send(s.router, http.MethodPatch, "/medias/"+m.ID.String(), CreateMedia{
Title: "test2",
Title: "foo",
})
assert.NoError(t, err)

var body util.DataResponse
_ = json.NewDecoder(res.Body).Decode(&body)

assert.Equal(t, 200, res.Result().StatusCode)
assert.Equal(t, "test2", body.Data.(map[string]interface{})["title"])
assert.Equal(t, "AwaitingUpload", body.Data.(map[string]interface{})["status"])
assert.Equal(t, "foo", body.Data.(map[string]interface{})["title"])
assert.Equal(t, media.StatusAwaitingUpload.String(), body.Data.(map[string]interface{})["status"])
})

t.Run("should return validation error", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion api/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// @Header 200 {string} Content-Type "application/octet-stream"
// @Param media_id path string true "Media identifier" validate(required)
// @Param filename path string true "HLS filename" validate(required)
// @Router /medias/{media_id}/stream/playlists/{filename} [get]
// @Router /medias/{media_id}/stream/{filename} [get]
func (s *Server) getMediaPlaylistFile(ctx *gin.Context) {
mediaUUID := ctx.Param("id")
filename := ctx.Param("filename")
Expand Down

0 comments on commit e4757bc

Please sign in to comment.