Skip to content

Commit

Permalink
Show image size on view page
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobDev committed Jul 14, 2023
1 parent 61c9268 commit e60808a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,7 @@ audio_not_supported_in_browser = Your browser does not support the HTML5 'audio'
stored_lfs = Stored with Git LFS
symbolic_link = Symbolic link
executable_file = Executable File
image_size = Size: %s
commit_graph = Commit Graph
commit_graph.select = Select branches
commit_graph.hide_pr_refs = Hide Pull Requests
Expand Down
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"

Check failure on line 20 in routers/web/repo/view.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)

Check failure on line 20 in routers/web/repo/view.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)

Check failure on line 20 in routers/web/repo/view.go

View workflow job for this annotation

GitHub Actions / lint-backend

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)
_ "image/jpeg"
_ "image/png"

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"

Check failure on line 53 in routers/web/repo/view.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)

Check failure on line 53 in routers/web/repo/view.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)

Check failure on line 53 in routers/web/repo/view.go

View workflow job for this annotation

GitHub Actions / lint-backend

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)
_ "golang.org/x/image/webp"
)

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%d", img.Width, img.Height)
}
}

if ctx.Repo.CanEnableEditor(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">
{{.locale.Tr "repo.image_size" .ImageSize}}
</div>
{{end}}
</div>

0 comments on commit e60808a

Please sign in to comment.