Add file info at top of post images

This commit is contained in:
Dominic Ferrando
2025-04-21 00:57:35 -04:00
parent 65a3c155ce
commit 4e4b5614e0
4 changed files with 79 additions and 3 deletions
+60
View File
@@ -8,6 +8,7 @@ import (
"log" "log"
"mime/multipart" "mime/multipart"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -111,3 +112,62 @@ func SumUniquePostIps(posts []database.Post) int {
} }
return len(uniqueIpHashes) return len(uniqueIpHashes)
} }
type PostImageInfo struct {
Size int64
Height int
Width int
}
func GetPostImageInfo(mediaPath string) PostImageInfo {
var result PostImageInfo
imagePath := path.Join(POST_IMG_FULL_PATH, mediaPath)
fileInfo, err := os.Stat(imagePath)
if err != nil {
log.Printf("Failed to get image file info at %s: %v", mediaPath, err)
return PostImageInfo{}
}
file, err := os.Open(imagePath)
if err != nil {
log.Printf("Failed to open image at %s: %v", mediaPath, err)
return PostImageInfo{}
}
defer file.Close()
cfg, _, err := image.DecodeConfig(file)
if err != nil {
log.Printf("Failed to decode image at %s: %v", mediaPath, err)
return PostImageInfo{}
}
result.Size = fileInfo.Size()
result.Width = cfg.Width
result.Height = cfg.Height
return result
}
func FormatBytes(bytes int64) string {
const (
KB = 1 << 10 // 1024
MB = 1 << 20
GB = 1 << 30
TB = 1 << 40
)
switch {
case bytes >= TB:
return fmt.Sprintf("%.2f TB", float64(bytes)/float64(TB))
case bytes >= GB:
return fmt.Sprintf("%.2f GB", float64(bytes)/float64(GB))
case bytes >= MB:
return fmt.Sprintf("%.2f MB", float64(bytes)/float64(MB))
case bytes >= KB:
return fmt.Sprintf("%.2f KB", float64(bytes)/float64(KB))
default:
return fmt.Sprintf("%d B", bytes)
}
}
+7 -1
View File
@@ -216,6 +216,12 @@ body {
max-width: 100%; max-width: 100%;
} }
.post-img-info {
display: inline-block;
font-size: 10px;
margin-left: 5px;
}
.post-body { .post-body {
margin-top: 10px; margin-top: 10px;
margin-left: 5px; margin-left: 5px;
@@ -253,7 +259,7 @@ body {
color: rgb(52, 52, 92); color: rgb(52, 52, 92);
text-decoration: underline; text-decoration: underline;
cursor: pointer; cursor: pointer;
margin-left: 8px; margin-left: 4px;
} }
.reply-link-header:hover { .reply-link-header:hover {
+1 -1
View File
@@ -75,7 +75,7 @@ templ Board(board database.Board) {
<option selected value="small">Small</option> <option selected value="small">Small</option>
<option value="large">Large</option> <option value="large">Large</option>
</select> </select>
<input oninput="applyCatalogSearch()" id="catalogSearch" style="float: right;" placeholder="Search"/> <input style="margin-left: 10px;" oninput="applyCatalogSearch()" id="catalogSearch" placeholder="Search"/>
</div> </div>
<hr/> <hr/>
<div <div
+11 -1
View File
@@ -9,16 +9,21 @@ import (
) )
templ PostOriginal(post database.Post, thread *database.Thread) { templ PostOriginal(post database.Post, thread *database.Thread) {
{{ imgInfo := util.GetPostImageInfo(post.MediaPath) }}
<article id={ fmt.Sprintf("post-%d", post.Id) } class="post-op"> <article id={ fmt.Sprintf("post-%d", post.Id) } class="post-op">
<div> <div>
File: File:
<a href={ templ.URL("/static/img/posts/full/" + post.MediaPath) } class="post-filename"> <a href={ templ.URL("/static/img/posts/full/" + post.MediaPath) } class="post-filename">
{ post.MediaPath } { post.MediaPath }
</a> </a>
<div class="post-img-info">
(<span>{ util.FormatBytes(imgInfo.Size) }</span>,
<span>{ strconv.Itoa(imgInfo.Width) }</span>x<span>{ strconv.Itoa(imgInfo.Height) }</span>)
</div>
</div> </div>
<img onclick="togglePostImage(this)" loading="lazy" src={ fmt.Sprintf("/static/img/posts/thumb/%s", post.MediaPath) <img onclick="togglePostImage(this)" loading="lazy" src={ fmt.Sprintf("/static/img/posts/thumb/%s", post.MediaPath)
} class="post-img" /> } class="post-img" />
<header class="post-header"> <header style="margin-top: 10px;" class="post-header">
<h1 class="thread-subject">{ thread.Subject } -</h1> <h1 class="thread-subject">{ thread.Subject } -</h1>
<span class="post-author">{ post.Author }</span> <span class="post-author">{ post.Author }</span>
<span class="post-date">{ post.CreatedAt.Format("01/02/2006") }</span> <span class="post-date">{ post.CreatedAt.Format("01/02/2006") }</span>
@@ -36,6 +41,7 @@ templ PostOriginal(post database.Post, thread *database.Thread) {
} }
templ PostReply(post database.Post) { templ PostReply(post database.Post) {
{{ imgInfo := util.GetPostImageInfo(post.MediaPath) }}
<article id={ fmt.Sprintf("post-%d", post.Id) } class="post"> <article id={ fmt.Sprintf("post-%d", post.Id) } class="post">
<header class="post-header"> <header class="post-header">
<span class="post-author">{ post.Author }</span> <span class="post-author">{ post.Author }</span>
@@ -54,6 +60,10 @@ templ PostReply(post database.Post) {
<a href={ templ.URL("/static/img/posts/full/" + post.MediaPath) } class="post-filename"> <a href={ templ.URL("/static/img/posts/full/" + post.MediaPath) } class="post-filename">
{ post.MediaPath } { post.MediaPath }
</a> </a>
<div class="post-img-info">
(<span>{ util.FormatBytes(imgInfo.Size) }</span>,
<span>{ strconv.Itoa(imgInfo.Width) }</span>x<span>{ strconv.Itoa(imgInfo.Height) }</span>)
</div>
</div> </div>
<img onclick="togglePostImage(this)" loading="lazy" src={ fmt.Sprintf("/static/img/posts/thumb/%s", <img onclick="togglePostImage(this)" loading="lazy" src={ fmt.Sprintf("/static/img/posts/thumb/%s",
post.MediaPath) } class="post-img" /> post.MediaPath) } class="post-img" />