Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show image size on view page #25884

Merged
merged 9 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ import (
gocontext "context"
"encoding/base64"
"fmt"
"image"
"io"
"net/http"
"net/url"
"path"
"strings"
"time"

_ "image/gif" // for processing gif images
_ "image/jpeg" // for processing jpeg images
_ "image/png" // for processing png images

activities_model "code.gitea.io/gitea/models/activities"
admin_model "code.gitea.io/gitea/models/admin"
asymkey_model "code.gitea.io/gitea/models/asymkey"
Expand Down Expand Up @@ -44,6 +49,9 @@ import (
issue_service "code.gitea.io/gitea/services/issue"

"github.com/nektos/act/pkg/model"

_ "golang.org/x/image/bmp" // for processing bmp images
_ "golang.org/x/image/webp" // for processing webp images
)

const (
Expand Down Expand Up @@ -578,6 +586,15 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
}
}

if fInfo.st.IsImage() && !fInfo.st.IsSvgImage() {
img, _, err := image.DecodeConfig(bytes.NewReader(buf))
if err == nil {
// There are Image formats go can't decode
// Instead of throwing an error in that case, we show the size only when we can decode
ctx.Data["ImageSize"] = fmt.Sprintf("%dx%dpx", img.Width, img.Height)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why ' !fInfo.st.IsSvgImage()' not svg?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

svg is text-based instead of binary, and vector-based instead of line-by-line, so it doesn't really have a "size".
It is only limited by the view box that can be arbitrarily scaled.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not entirely correct. SVGs can have height and width, and if they are absent, the size of the viewport is used IIRC. If even that is absent, browser will use 300×150 IIRC. Still I guess it's enough if this PR handles only raster images for now.

}
}

if ctx.Repo.CanEnableEditor(ctx, ctx.Doer) {
if lfsLock != nil && lfsLock.OwnerID != ctx.Doer.ID {
ctx.Data["CanDeleteFile"] = false
Expand Down
5 changes: 5 additions & 0 deletions templates/repo/file_info.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@
{{.locale.Tr "repo.executable_file"}}
</div>
{{end}}
{{if .ImageSize}}
<div class="file-info-entry">
{{.ImageSize}}
</div>
{{end}}
</div>