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")
|
||||
}
|
||||
|
||||
type screenshotFile struct {
|
||||
path string
|
||||
modTime time.Time
|
||||
}
|
||||
|
||||
func findLatestScreenshot(desktop string) (string, error) {
|
||||
entries, err := os.ReadDir(desktop)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var screenshots []os.DirEntry
|
||||
var screenshots []screenshotFile
|
||||
for _, e := range entries {
|
||||
name := e.Name()
|
||||
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.Slice(screenshots, func(i, j int) bool {
|
||||
infoI, _ := screenshots[i].Info()
|
||||
infoJ, _ := screenshots[j].Info()
|
||||
return infoI.ModTime().After(infoJ.ModTime())
|
||||
return screenshots[i].modTime.After(screenshots[j].modTime)
|
||||
})
|
||||
|
||||
return filepath.Join(desktop, screenshots[0].Name()), nil
|
||||
return screenshots[0].path, nil
|
||||
}
|
||||
|
||||
func serveAndDelete(w http.ResponseWriter, path string) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue