Compare commits
5 commits
15d856aa39
...
f7991a3706
Author | SHA1 | Date | |
---|---|---|---|
f7991a3706 | |||
0d0314b98d | |||
602c858379 | |||
ffc86f9092 | |||
3464b39d39 |
|
@ -858,7 +858,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch8() {
|
|||
"static_url": "http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/attachment/small/`+instanceAccount.AvatarMediaAttachment.ID+`.webp",`+`
|
||||
"thumbnail_static_type": "image/webp",
|
||||
"thumbnail_description": "A bouncing little green peglin.",
|
||||
"blurhash": "LE9801Rl4Yt5%dWCV]t5Dmoex?WC"
|
||||
"blurhash": "LF9Hm*Rl4Yt5.4RlRSt5IXkBxsj["
|
||||
}`, string(instanceV2ThumbnailJson))
|
||||
|
||||
// double extra special bonus: now update the image description without changing the image
|
||||
|
|
|
@ -35,6 +35,8 @@
|
|||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
var pingMsg = []byte("ping!")
|
||||
|
||||
// StreamGETHandler swagger:operation GET /api/v1/streaming streamGet
|
||||
//
|
||||
// Initiate a websocket connection for live streaming of statuses and notifications.
|
||||
|
@ -389,40 +391,57 @@ func (m *Module) writeToWSConn(
|
|||
) {
|
||||
for {
|
||||
// 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.
|
||||
msg, ok := stream.Recv(pingctx)
|
||||
// Block and wait for
|
||||
// one of the following:
|
||||
//
|
||||
// - receipt of msg
|
||||
// - timeout of pingCtx
|
||||
// - stream closed.
|
||||
msg, haveMsg := stream.Recv(pingCtx)
|
||||
|
||||
// Check if cancel because ping.
|
||||
pinged := (pingctx.Err() != nil)
|
||||
// If ping context has timed
|
||||
// out, we should send a ping.
|
||||
//
|
||||
// In any case cancel pingCtx
|
||||
// as we're done with it.
|
||||
shouldPing := (pingCtx.Err() != nil)
|
||||
cncl()
|
||||
|
||||
switch {
|
||||
case !ok && pinged:
|
||||
// The ping context timed out!
|
||||
l.Trace("writing websocket ping")
|
||||
case !haveMsg && !shouldPing:
|
||||
// We have no message and we shouldn't
|
||||
// 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".
|
||||
if err := wsConn.WriteControl(websocket.PingMessage, nil, time.Time{}); err != nil {
|
||||
l.Debugf("error writing websocket ping: %v", err)
|
||||
break
|
||||
}
|
||||
case haveMsg:
|
||||
// We have a message to stream.
|
||||
l.Tracef("writing websocket message: %+v", msg)
|
||||
|
||||
case !ok:
|
||||
// Stream was
|
||||
// closed.
|
||||
if err := wsConn.WriteJSON(msg); err != nil {
|
||||
// If there's an error writing then drop the
|
||||
// connection, as client may have disappeared
|
||||
// suddenly; they can reconnect if necessary.
|
||||
l.Debugf("error writing websocket message: %v", err)
|
||||
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.WriteJSON(msg); err != nil {
|
||||
l.Debugf("error writing websocket message: %v", err)
|
||||
break
|
||||
if err := wsConn.WriteControl(websocket.PingMessage, pingMsg, time.Time{}); err != nil {
|
||||
// If there's an error writing then drop the
|
||||
// connection, as client may have disappeared
|
||||
// suddenly; they can reconnect if necessary.
|
||||
l.Debugf("error writing websocket ping: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
l.Debug("finished websocket write")
|
||||
}
|
||||
|
|
|
@ -23,13 +23,44 @@
|
|||
"context"
|
||||
"database/sql/driver"
|
||||
|
||||
"codeberg.org/gruf/go-bytesize"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
"github.com/tetratelabs/wazero"
|
||||
|
||||
"github.com/ncruces/go-sqlite3"
|
||||
sqlite3driver "github.com/ncruces/go-sqlite3/driver"
|
||||
_ "github.com/ncruces/go-sqlite3/embed" // embed wasm binary
|
||||
_ "github.com/ncruces/go-sqlite3/vfs/memdb" // include memdb vfs
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Initialize WASM runtime config.
|
||||
cfg := wazero.NewRuntimeConfig()
|
||||
|
||||
// The size of a page in WASM.
|
||||
const pageSize = uint32(^uint16(0))
|
||||
|
||||
// The following check is compile-time:
|
||||
//
|
||||
// Size of a pointer on this platform,
|
||||
// i.e. are we running on 32 / 64 bit.
|
||||
//
|
||||
// See: https://github.com/ncruces/go-sqlite3/issues/168#issuecomment-2412429221
|
||||
const ptrsz = 32 << (^uintptr(0) >> 63)
|
||||
switch ptrsz {
|
||||
case 32:
|
||||
const memoryLimit = uint32(32*bytesize.MiB) / pageSize
|
||||
cfg = cfg.WithMemoryLimitPages(memoryLimit)
|
||||
case 64:
|
||||
const memoryLimit = uint32(256*bytesize.MiB) / pageSize
|
||||
cfg = cfg.WithMemoryLimitPages(memoryLimit)
|
||||
}
|
||||
|
||||
// Set runtime config before
|
||||
// initialize func gets called.
|
||||
sqlite3.RuntimeConfig = cfg
|
||||
}
|
||||
|
||||
// Driver is our own wrapper around the
|
||||
// driver.SQLite{} type in order to wrap
|
||||
// further SQL types with our own
|
||||
|
|
|
@ -78,23 +78,17 @@ func ffmpegGenerateWebpThumb(ctx context.Context, inpath, outpath string, width,
|
|||
// (NOT as libwebp_anim).
|
||||
"-codec:v", "libwebp",
|
||||
|
||||
// Select thumb from first 7 frames.
|
||||
// (in particular <= 7 reduced memory usage, marginally)
|
||||
// (thumb filter: https://ffmpeg.org/ffmpeg-filters.html#thumbnail)
|
||||
"-filter:v", "thumbnail=n=7,"+
|
||||
// Only one frame
|
||||
"-frames:v", "1",
|
||||
|
||||
// Scale to dimensions
|
||||
// (scale filter: https://ffmpeg.org/ffmpeg-filters.html#scale)
|
||||
"scale="+strconv.Itoa(width)+
|
||||
":"+strconv.Itoa(height)+","+
|
||||
"-filter:v", "scale="+strconv.Itoa(width)+":"+strconv.Itoa(height)+","+
|
||||
|
||||
// Attempt to use original pixel format
|
||||
// (format filter: https://ffmpeg.org/ffmpeg-filters.html#format)
|
||||
"format=pix_fmts="+pixfmt,
|
||||
|
||||
// Only one frame
|
||||
"-frames:v", "1",
|
||||
|
||||
// Quality not specified,
|
||||
// i.e. use default which
|
||||
// should be 75% webp quality.
|
||||
|
|
|
@ -428,7 +428,7 @@ func (suite *ManagerTestSuite) TestSlothVineProcess() {
|
|||
suite.Equal("video/mp4", attachment.File.ContentType)
|
||||
suite.Equal("image/webp", attachment.Thumbnail.ContentType)
|
||||
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)
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
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() {
|
||||
ctx := context.Background()
|
||||
|
||||
|
@ -488,8 +553,8 @@ func (suite *ManagerTestSuite) TestLongerMp4Process() {
|
|||
suite.Equal("video/mp4", attachment.File.ContentType)
|
||||
suite.Equal("image/webp", attachment.Thumbnail.ContentType)
|
||||
suite.Equal(109569, attachment.File.FileSize)
|
||||
suite.Equal(2976, attachment.Thumbnail.FileSize)
|
||||
suite.Equal("LIQJfl_3IU?b~qM{ofayWBWVofRj", attachment.Blurhash)
|
||||
suite.Equal(2958, attachment.Thumbnail.FileSize)
|
||||
suite.Equal("LIQ9}}_3IU?b~qM{ofayWBWVofRj", attachment.Blurhash)
|
||||
|
||||
// now make sure the attachment is in the database
|
||||
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("image/webp", attachment.Thumbnail.ContentType)
|
||||
suite.Equal(1409625, attachment.File.FileSize)
|
||||
suite.Equal(14478, attachment.Thumbnail.FileSize)
|
||||
suite.Equal("LLF$qyaeRO.9DgM_RPaetkV@WCMw", attachment.Blurhash)
|
||||
suite.Equal(15056, attachment.Thumbnail.FileSize)
|
||||
suite.Equal("LLF$nqafRO.9DgM_RPadtkV@WCMx", attachment.Blurhash)
|
||||
|
||||
// now make sure the attachment is in the database
|
||||
dbAttachment, err := suite.db.GetAttachmentByID(ctx, attachment.ID)
|
||||
|
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 15 KiB |
BIN
internal/media/test/clock-original.gif
Normal file
After Width: | Height: | Size: 120 KiB |
BIN
internal/media/test/clock-processed.gif
Normal file
After Width: | Height: | Size: 120 KiB |
BIN
internal/media/test/clock-thumbnail.webp
Normal file
After Width: | Height: | Size: 13 KiB |
Before Width: | Height: | Size: 2.9 KiB After Width: | Height: | Size: 2.9 KiB |
Before Width: | Height: | Size: 5.5 KiB After Width: | Height: | Size: 5.5 KiB |