Compare commits

..

4 commits

10 changed files with 120 additions and 42 deletions

View file

@ -858,7 +858,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch8() {
"static_url": "http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/attachment/small/`+instanceAccount.AvatarMediaAttachment.ID+`.webp",`+` "static_url": "http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/attachment/small/`+instanceAccount.AvatarMediaAttachment.ID+`.webp",`+`
"thumbnail_static_type": "image/webp", "thumbnail_static_type": "image/webp",
"thumbnail_description": "A bouncing little green peglin.", "thumbnail_description": "A bouncing little green peglin.",
"blurhash": "LE9801Rl4Yt5%dWCV]t5Dmoex?WC" "blurhash": "LF9Hm*Rl4Yt5.4RlRSt5IXkBxsj["
}`, string(instanceV2ThumbnailJson)) }`, string(instanceV2ThumbnailJson))
// double extra special bonus: now update the image description without changing the image // double extra special bonus: now update the image description without changing the image

View file

@ -35,6 +35,8 @@
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
var pingMsg = []byte("ping!")
// StreamGETHandler swagger:operation GET /api/v1/streaming streamGet // StreamGETHandler swagger:operation GET /api/v1/streaming streamGet
// //
// Initiate a websocket connection for live streaming of statuses and notifications. // Initiate a websocket connection for live streaming of statuses and notifications.
@ -389,40 +391,57 @@ func (m *Module) writeToWSConn(
) { ) {
for { for {
// Wrap context with timeout to send a ping. // Wrap context with timeout to send a ping.
pingctx, cncl := context.WithTimeout(ctx, ping) pingCtx, cncl := context.WithTimeout(ctx, ping)
// Block on receipt of msg. // Block and wait for
msg, ok := stream.Recv(pingctx) // one of the following:
//
// - receipt of msg
// - timeout of pingCtx
// - stream closed.
msg, haveMsg := stream.Recv(pingCtx)
// Check if cancel because ping. // If ping context has timed
pinged := (pingctx.Err() != nil) // out, we should send a ping.
//
// In any case cancel pingCtx
// as we're done with it.
shouldPing := (pingCtx.Err() != nil)
cncl() cncl()
switch { switch {
case !ok && pinged: case !haveMsg && !shouldPing:
// The ping context timed out! // We have no message and we shouldn't
l.Trace("writing websocket ping") // send a ping; this means the stream
// has been closed from the client's end,
// so there's nothing further to do here.
l.Trace("no message and we shouldn't ping, returning...")
return
// Wrapped context time-out, send a keep-alive "ping". case haveMsg:
if err := wsConn.WriteControl(websocket.PingMessage, nil, time.Time{}); err != nil { // We have a message to stream.
l.Debugf("error writing websocket ping: %v", err) l.Tracef("writing websocket message: %+v", msg)
break
}
case !ok: if err := wsConn.WriteJSON(msg); err != nil {
// Stream was // If there's an error writing then drop the
// closed. // connection, as client may have disappeared
// suddenly; they can reconnect if necessary.
l.Debugf("error writing websocket message: %v", err)
return return
} }
l.Trace("writing websocket message: %+v", msg) case shouldPing:
// We have no message but we do
// need to send a keep-alive ping.
l.Trace("writing websocket ping")
// Received a new message from the processor. if err := wsConn.WriteControl(websocket.PingMessage, pingMsg, time.Time{}); err != nil {
if err := wsConn.WriteJSON(msg); err != nil { // If there's an error writing then drop the
l.Debugf("error writing websocket message: %v", err) // connection, as client may have disappeared
break // suddenly; they can reconnect if necessary.
l.Debugf("error writing websocket ping: %v", err)
return
}
} }
} }
l.Debug("finished websocket write")
} }

View file

@ -78,23 +78,17 @@ func ffmpegGenerateWebpThumb(ctx context.Context, inpath, outpath string, width,
// (NOT as libwebp_anim). // (NOT as libwebp_anim).
"-codec:v", "libwebp", "-codec:v", "libwebp",
// Select thumb from first 7 frames. // Only one frame
// (in particular <= 7 reduced memory usage, marginally) "-frames:v", "1",
// (thumb filter: https://ffmpeg.org/ffmpeg-filters.html#thumbnail)
"-filter:v", "thumbnail=n=7,"+
// Scale to dimensions // Scale to dimensions
// (scale filter: https://ffmpeg.org/ffmpeg-filters.html#scale) // (scale filter: https://ffmpeg.org/ffmpeg-filters.html#scale)
"scale="+strconv.Itoa(width)+ "-filter:v", "scale="+strconv.Itoa(width)+":"+strconv.Itoa(height)+","+
":"+strconv.Itoa(height)+","+
// Attempt to use original pixel format // Attempt to use original pixel format
// (format filter: https://ffmpeg.org/ffmpeg-filters.html#format) // (format filter: https://ffmpeg.org/ffmpeg-filters.html#format)
"format=pix_fmts="+pixfmt, "format=pix_fmts="+pixfmt,
// Only one frame
"-frames:v", "1",
// Quality not specified, // Quality not specified,
// i.e. use default which // i.e. use default which
// should be 75% webp quality. // should be 75% webp quality.

View file

@ -428,7 +428,7 @@ func (suite *ManagerTestSuite) TestSlothVineProcess() {
suite.Equal("video/mp4", attachment.File.ContentType) suite.Equal("video/mp4", attachment.File.ContentType)
suite.Equal("image/webp", attachment.Thumbnail.ContentType) suite.Equal("image/webp", attachment.Thumbnail.ContentType)
suite.Equal(312453, attachment.File.FileSize) suite.Equal(312453, attachment.File.FileSize)
suite.Equal(5648, attachment.Thumbnail.FileSize) suite.Equal(5598, attachment.Thumbnail.FileSize)
suite.Equal("LgIYH}xtNsofxtfPW.j[_4axn+of", attachment.Blurhash) suite.Equal("LgIYH}xtNsofxtfPW.j[_4axn+of", attachment.Blurhash)
// now make sure the attachment is in the database // now make sure the attachment is in the database
@ -441,6 +441,71 @@ func (suite *ManagerTestSuite) TestSlothVineProcess() {
equalFiles(suite.T(), suite.state.Storage, dbAttachment.Thumbnail.Path, "./test/test-mp4-thumbnail.webp") equalFiles(suite.T(), suite.state.Storage, dbAttachment.Thumbnail.Path, "./test/test-mp4-thumbnail.webp")
} }
func (suite *ManagerTestSuite) TestAnimatedGifProcess() {
ctx := context.Background()
data := func(_ context.Context) (io.ReadCloser, error) {
// load bytes from a test image
b, err := os.ReadFile("./test/clock-original.gif")
if err != nil {
panic(err)
}
return io.NopCloser(bytes.NewBuffer(b)), nil
}
accountID := "01FS1X72SK9ZPW0J1QQ68BD264"
// process the media with no additional info provided
processing, err := suite.manager.CreateMedia(ctx,
accountID,
data,
media.AdditionalMediaInfo{},
)
suite.NoError(err)
suite.NotNil(processing)
// do a blocking call to fetch the attachment
attachment, err := processing.Load(ctx)
suite.NoError(err)
suite.NotNil(attachment)
// make sure it's got the stuff set on it that we expect
// the attachment ID and accountID we expect
suite.Equal(processing.ID(), attachment.ID)
suite.Equal(accountID, attachment.AccountID)
// file meta should be correctly derived from the image
suite.EqualValues(gtsmodel.Original{
Width: 528,
Height: 528,
Size: 278784,
Aspect: 1,
Duration: util.Ptr(float32(8.58)),
Framerate: util.Ptr(float32(16)),
Bitrate: util.Ptr(uint64(114092)),
}, attachment.FileMeta.Original)
suite.EqualValues(gtsmodel.Small{
Width: 512,
Height: 512,
Size: 262144,
Aspect: 1,
}, attachment.FileMeta.Small)
suite.Equal("image/gif", attachment.File.ContentType)
suite.Equal("image/webp", attachment.Thumbnail.ContentType)
suite.Equal(122364, attachment.File.FileSize)
suite.Equal(12962, attachment.Thumbnail.FileSize)
suite.Equal("LmKUZkt700ofoffQofj[00WBj[WB", attachment.Blurhash)
// now make sure the attachment is in the database
dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachment.ID)
suite.NoError(err)
suite.NotNil(dbAttachment)
// ensure the files contain the expected data.
equalFiles(suite.T(), suite.state.Storage, dbAttachment.File.Path, "./test/clock-processed.gif")
equalFiles(suite.T(), suite.state.Storage, dbAttachment.Thumbnail.Path, "./test/clock-thumbnail.webp")
}
func (suite *ManagerTestSuite) TestLongerMp4Process() { func (suite *ManagerTestSuite) TestLongerMp4Process() {
ctx := context.Background() ctx := context.Background()
@ -488,8 +553,8 @@ func (suite *ManagerTestSuite) TestLongerMp4Process() {
suite.Equal("video/mp4", attachment.File.ContentType) suite.Equal("video/mp4", attachment.File.ContentType)
suite.Equal("image/webp", attachment.Thumbnail.ContentType) suite.Equal("image/webp", attachment.Thumbnail.ContentType)
suite.Equal(109569, attachment.File.FileSize) suite.Equal(109569, attachment.File.FileSize)
suite.Equal(2976, attachment.Thumbnail.FileSize) suite.Equal(2958, attachment.Thumbnail.FileSize)
suite.Equal("LIQJfl_3IU?b~qM{ofayWBWVofRj", attachment.Blurhash) suite.Equal("LIQ9}}_3IU?b~qM{ofayWBWVofRj", attachment.Blurhash)
// now make sure the attachment is in the database // now make sure the attachment is in the database
dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachment.ID) dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachment.ID)
@ -548,8 +613,8 @@ func (suite *ManagerTestSuite) TestBirdnestMp4Process() {
suite.Equal("video/mp4", attachment.File.ContentType) suite.Equal("video/mp4", attachment.File.ContentType)
suite.Equal("image/webp", attachment.Thumbnail.ContentType) suite.Equal("image/webp", attachment.Thumbnail.ContentType)
suite.Equal(1409625, attachment.File.FileSize) suite.Equal(1409625, attachment.File.FileSize)
suite.Equal(14478, attachment.Thumbnail.FileSize) suite.Equal(15056, attachment.Thumbnail.FileSize)
suite.Equal("LLF$qyaeRO.9DgM_RPaetkV@WCMw", attachment.Blurhash) suite.Equal("LLF$nqafRO.9DgM_RPadtkV@WCMx", attachment.Blurhash)
// now make sure the attachment is in the database // now make sure the attachment is in the database
dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachment.ID) dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachment.ID)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.5 KiB

After

Width:  |  Height:  |  Size: 5.5 KiB