Save file names as current timestamp

This commit is contained in:
Dominic Ferrando
2025-04-19 19:51:23 -04:00
parent 62b9113509
commit 10ab0edfc8
2 changed files with 12 additions and 9 deletions
+11 -8
View File
@@ -12,6 +12,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"time"
"github.com/disintegration/imaging" "github.com/disintegration/imaging"
"github.com/dominicf2001/comfychan/internal/database" "github.com/dominicf2001/comfychan/internal/database"
@@ -28,9 +29,9 @@ const POST_IMG_THUMB_PATH = "web/static/img/posts/thumb"
var dev = true var dev = true
func savePostFile(file *multipart.File, header *multipart.FileHeader) error { func savePostFile(file *multipart.File, filename string) error {
dstPathFull := filepath.Join(POST_IMG_FULL_PATH, header.Filename) dstPathFull := filepath.Join(POST_IMG_FULL_PATH, filename)
dstPathThumb := filepath.Join(POST_IMG_THUMB_PATH, header.Filename) dstPathThumb := filepath.Join(POST_IMG_THUMB_PATH, filename)
// FULL // FULL
if err := os.MkdirAll(POST_IMG_FULL_PATH, 0755); err != nil { if err := os.MkdirAll(POST_IMG_FULL_PATH, 0755); err != nil {
@@ -66,7 +67,7 @@ func savePostFile(file *multipart.File, header *multipart.FileHeader) error {
return err return err
} }
thumb := imaging.Resize(img, 200, 0, imaging.Lanczos) thumb := imaging.Resize(img, 300, 0, imaging.Lanczos)
if err = imaging.Save(thumb, dstPathThumb); err != nil { if err = imaging.Save(thumb, dstPathThumb); err != nil {
log.Printf("imaging.Save: %v", err) log.Printf("imaging.Save: %v", err)
return err return err
@@ -192,13 +193,14 @@ func main() {
} }
defer file.Close() defer file.Close()
if err := savePostFile(&file, header); err != nil { filename := strconv.FormatInt(time.Now().UnixNano(), 10) + filepath.Ext(header.Filename)
if err := savePostFile(&file, filename); err != nil {
http.Error(w, "Failed to save file", http.StatusInternalServerError) http.Error(w, "Failed to save file", http.StatusInternalServerError)
log.Printf("savePostFile: %v", err) log.Printf("savePostFile: %v", err)
return return
} }
if err := database.PutThread(db, slug, subject, body, header.Filename); err != nil { if err := database.PutThread(db, slug, subject, body, filename); err != nil {
http.Error(w, "Failed to create thread", http.StatusInternalServerError) http.Error(w, "Failed to create thread", http.StatusInternalServerError)
log.Printf("PutThread: %v", err) log.Printf("PutThread: %v", err)
return return
@@ -234,12 +236,13 @@ func main() {
} }
} else { } else {
defer file.Close() defer file.Close()
if err := savePostFile(&file, header); err != nil { filename := strconv.FormatInt(time.Now().UnixNano(), 10) + filepath.Ext(header.Filename)
if err := savePostFile(&file, filename); err != nil {
http.Error(w, "Failed to save file", http.StatusInternalServerError) http.Error(w, "Failed to save file", http.StatusInternalServerError)
log.Printf("savePostFile: %v", err) log.Printf("savePostFile: %v", err)
return return
} }
mediaPath = header.Filename mediaPath = filename
} }
if err := database.PutPost(db, threadId, body, mediaPath); err != nil { if err := database.PutPost(db, threadId, body, mediaPath); err != nil {
+1 -1
View File
@@ -152,6 +152,7 @@ body {
font-weight: bold; font-weight: bold;
color: #0F0C5D; color: #0F0C5D;
margin-right: 5px; margin-right: 5px;
float: left;
} }
.post-op { .post-op {
@@ -174,7 +175,6 @@ body {
} }
.post-header { .post-header {
display: flex;
margin-bottom: 10px; margin-bottom: 10px;
} }