1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-14 12:16:24 -07:00

Don't insert upload in AttachEmbed, since it gets inserted later

This commit is contained in:
Eggbertx 2025-03-15 22:28:51 -07:00
parent 309c8cede9
commit 2facfe68fc
3 changed files with 23 additions and 16 deletions

View file

@ -631,6 +631,11 @@ type PostConfig struct {
AllowDiceRerolls bool
}
// HasEmbedMatchers returns true if the board has embed handlers configured
func (pc *PostConfig) HasEmbedMatchers() bool {
return len(pc.EmbedMatchers) > 0
}
// GetEmbedVideoID returns the site ID, and video ID for the given URL if it is compatible with any
// configured embed handlers. It returns an error if none are found
func (pc *PostConfig) GetEmbedVideoID(url string) (string, string, error) {

View file

@ -21,10 +21,10 @@ type EmbedVideo struct {
ThumbHeight int
}
// AttachEmbed checks if the post contains an embedded media URL from the form (if applicable) and if it is valid.
// AttachEmbedFromRequest checks if the post contains an embedded media URL from the form (if applicable) and if it is valid.
// It returns true if the post contains an embedded media URL, an error if the URL is invalid or some other error occurred.
// It attaches the embed as a pseudo-upload in the database if the URL is valid.
func AttachEmbed(request *http.Request, post *gcsql.Post, boardCfg *config.BoardConfig, warnEv, errEv *zerolog.Event, requestOpts ...*gcsql.RequestOptions) (*gcsql.Upload, error) {
func AttachEmbedFromRequest(request *http.Request, boardCfg *config.BoardConfig, warnEv, errEv *zerolog.Event) (*gcsql.Upload, error) {
url := request.PostFormValue("embed")
if url == "" {
return nil, nil
@ -43,14 +43,8 @@ func AttachEmbed(request *http.Request, post *gcsql.Post, boardCfg *config.Board
upload := &gcsql.Upload{
Filename: "embed:" + handlerID,
OriginalFilename: videoID,
PostID: post.ID,
ThumbnailWidth: boardCfg.ThumbWidth,
ThumbnailHeight: boardCfg.ThumbHeight,
}
if err = post.AddAttachment(upload, requestOpts...); err != nil {
errEv.Err(err).Caller().Msg("Failed to attach embed")
return nil, err
}
return upload, nil
}

View file

@ -94,7 +94,7 @@ var (
ThumbnailURLTemplate: "https://vumbnail.com/{{.VideoID}}.jpg",
},
"rawvideo": {
URLRegex: `^https?://\S+\.\S+/\S+/(\S+\.(?:mp4|webm))`,
URLRegex: `^https?://\S+\.\S+/\S+/(\S+\.(?:mp4|webm))$`,
EmbedTemplate: `<video class="embed" controls><source src="{{.VideoID}}" type="video/mp4"></video>`,
VideoIDSubmatchIndex: intPointer(0),
},
@ -151,7 +151,8 @@ func embedTestRunner(t *testing.T, tc *embedTestCase, boardCfg *config.BoardConf
t.FailNow()
}
embedUpload, err := AttachEmbed(generateEmbedRequest(tc.url), &gcsql.Post{ID: 1}, boardCfg, warnEv, errEv)
post := &gcsql.Post{ID: 1}
embedUpload, err := AttachEmbedFromRequest(generateEmbedRequest(tc.url), boardCfg, warnEv, errEv)
if tc.expectError {
assert.Error(t, err)
return
@ -166,8 +167,10 @@ func embedTestRunner(t *testing.T, tc *embedTestCase, boardCfg *config.BoardConf
if !assert.NotNil(t, embedUpload) {
t.FailNow()
}
assert.Equal(t, tc.expectUpload.Filename, embedUpload.Filename)
assert.Equal(t, tc.expectUpload.OriginalFilename, embedUpload.OriginalFilename)
assert.NoError(t, post.AddAttachment(embedUpload))
assert.Equal(t, embedUpload.ID, 99)
assert.NoError(t, mock.ExpectationsWereMet())
}
@ -226,9 +229,12 @@ func TestOnlyAllowOneEmbed(t *testing.T) {
mock.ExpectPrepare(prepStr).
ExpectQuery().WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"filename", "dir"}).AddRow("file.png", "test"))
_, err = AttachEmbed(generateEmbedRequest("https://www.youtube.com/watch?v=123456"),
&gcsql.Post{ID: 1}, boardCfg, warnEv, errEv)
assert.ErrorIs(t, err, gcsql.ErrUploadAlreadyAttached)
embed, err := AttachEmbedFromRequest(generateEmbedRequest("https://www.youtube.com/watch?v=123456"), boardCfg, warnEv, errEv)
if !assert.NoError(t, err) {
t.FailNow()
}
post := &gcsql.Post{ID: 1}
assert.ErrorIs(t, post.AddAttachment(embed), gcsql.ErrUploadAlreadyAttached)
if !assert.NoError(t, mock.ExpectationsWereMet()) {
t.FailNow()
}
@ -237,8 +243,10 @@ func TestOnlyAllowOneEmbed(t *testing.T) {
mock.ExpectPrepare(prepStr).
ExpectQuery().WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"filename", "dir"}).AddRow("embed:youtube", "test"))
_, err = AttachEmbed(generateEmbedRequest("https://www.youtube.com/watch?v=123456"),
&gcsql.Post{ID: 1}, boardCfg, warnEv, errEv)
assert.ErrorIs(t, err, gcsql.ErrEmbedAlreadyAttached)
embed, err = AttachEmbedFromRequest(generateEmbedRequest("https://www.youtube.com/watch?v=123456"), boardCfg, warnEv, errEv)
if !assert.NoError(t, err) {
t.FailNow()
}
assert.ErrorIs(t, post.AddAttachment(embed), gcsql.ErrEmbedAlreadyAttached)
assert.NoError(t, mock.ExpectationsWereMet())
}