Add Image Diff for SVG files (#14867)

* Added type sniffer.

* Switched content detection from base to typesniffer.

* Added GuessContentType to Blob.

* Moved image info logic to client.
Added support for SVG images in diff.

* Restore old blocked svg behaviour.

* Added missing image formats.

* Execute image diff only when container is visible.

* add margin to spinner

* improve BIN tag on image diffs

* Default to render view.

* Show image diff on incomplete diff.

Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
KN4CK3R 2021-06-05 14:32:19 +02:00 committed by GitHub
parent 7979c3654e
commit 8e262104c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 449 additions and 441 deletions

View file

@ -37,8 +37,20 @@ func setCompareContext(ctx *context.Context, base *git.Commit, head *git.Commit,
ctx.Data["BaseCommit"] = base
ctx.Data["HeadCommit"] = head
ctx.Data["GetBlobByPathForCommit"] = func(commit *git.Commit, path string) *git.Blob {
if commit == nil {
return nil
}
blob, err := commit.GetBlobByPath(path)
if err != nil {
return nil
}
return blob
}
setPathsCompareContext(ctx, base, head, headTarget)
setImageCompareContext(ctx, base, head)
setImageCompareContext(ctx)
setCsvCompareContext(ctx)
}
@ -57,27 +69,18 @@ func setPathsCompareContext(ctx *context.Context, base *git.Commit, head *git.Co
}
// setImageCompareContext sets context data that is required by image compare template
func setImageCompareContext(ctx *context.Context, base *git.Commit, head *git.Commit) {
ctx.Data["IsImageFileInHead"] = head.IsImageFile
ctx.Data["IsImageFileInBase"] = base.IsImageFile
ctx.Data["ImageInfoBase"] = func(name string) *git.ImageMetaData {
if base == nil {
return nil
func setImageCompareContext(ctx *context.Context) {
ctx.Data["IsBlobAnImage"] = func(blob *git.Blob) bool {
if blob == nil {
return false
}
result, err := base.ImageInfo(name)
st, err := blob.GuessContentType()
if err != nil {
log.Error("ImageInfo failed: %v", err)
return nil
log.Error("GuessContentType failed: %v", err)
return false
}
return result
}
ctx.Data["ImageInfo"] = func(name string) *git.ImageMetaData {
result, err := head.ImageInfo(name)
if err != nil {
log.Error("ImageInfo failed: %v", err)
return nil
}
return result
return st.IsImage() && (setting.UI.SVG.Enabled || !st.IsSvgImage())
}
}