Fix findLatestScreenshot sorting by pre-fetching file info
The sort comparator was calling Info() and silently discarding errors, which could cause nil dereferences or incorrect ordering. Pre-fetch ModTime into a struct so the sort always has valid data. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
32b97d4f27
commit
202f771434
22
main.go
22
main.go
|
|
@ -30,17 +30,29 @@ func getDesktopPath() string {
|
||||||
return filepath.Join(home, "Desktop")
|
return filepath.Join(home, "Desktop")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type screenshotFile struct {
|
||||||
|
path string
|
||||||
|
modTime time.Time
|
||||||
|
}
|
||||||
|
|
||||||
func findLatestScreenshot(desktop string) (string, error) {
|
func findLatestScreenshot(desktop string) (string, error) {
|
||||||
entries, err := os.ReadDir(desktop)
|
entries, err := os.ReadDir(desktop)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
var screenshots []os.DirEntry
|
var screenshots []screenshotFile
|
||||||
for _, e := range entries {
|
for _, e := range entries {
|
||||||
name := e.Name()
|
name := e.Name()
|
||||||
if strings.HasPrefix(name, "Screenshot") && strings.HasSuffix(name, ".png") {
|
if strings.HasPrefix(name, "Screenshot") && strings.HasSuffix(name, ".png") {
|
||||||
screenshots = append(screenshots, e)
|
info, err := e.Info()
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
screenshots = append(screenshots, screenshotFile{
|
||||||
|
path: filepath.Join(desktop, name),
|
||||||
|
modTime: info.ModTime(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -50,12 +62,10 @@ func findLatestScreenshot(desktop string) (string, error) {
|
||||||
|
|
||||||
// Sort by modification time (newest first)
|
// Sort by modification time (newest first)
|
||||||
sort.Slice(screenshots, func(i, j int) bool {
|
sort.Slice(screenshots, func(i, j int) bool {
|
||||||
infoI, _ := screenshots[i].Info()
|
return screenshots[i].modTime.After(screenshots[j].modTime)
|
||||||
infoJ, _ := screenshots[j].Info()
|
|
||||||
return infoI.ModTime().After(infoJ.ModTime())
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return filepath.Join(desktop, screenshots[0].Name()), nil
|
return screenshots[0].path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveAndDelete(w http.ResponseWriter, path string) {
|
func serveAndDelete(w http.ResponseWriter, path string) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue