diff --git a/backend/handlers/onlyoffice.go b/backend/handlers/onlyoffice.go index f2c2461..78003c2 100644 --- a/backend/handlers/onlyoffice.go +++ b/backend/handlers/onlyoffice.go @@ -165,6 +165,59 @@ func (h *OnlyOfficeHandler) CreateBlank(c *fiber.Ctx) error { }) } +// ListOfficeFiles returns all .docx or .pptx files across the entire storage directory. +// GET /api/onlyoffice/files?type=docx|pptx +func (h *OnlyOfficeHandler) ListOfficeFiles(c *fiber.Ctx) error { + fileType := c.Query("type", "docx") // "docx" or "pptx" + ext := ".docx" + if fileType == "pptx" { + ext = ".pptx" + } + + type OfficeFile struct { + Name string `json:"name"` + Path string `json:"path"` + Size int64 `json:"size"` + ModTime string `json:"mod_time"` + } + + var results []OfficeFile + + filepath.Walk(h.Config.StorageDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return nil + } + + if info.IsDir() { + base := filepath.Base(path) + if strings.HasPrefix(base, ".") && path != h.Config.StorageDir { + return filepath.SkipDir + } + return nil + } + + if strings.HasSuffix(strings.ToLower(info.Name()), ext) { + relPath, _ := filepath.Rel(h.Config.StorageDir, path) + results = append(results, OfficeFile{ + Name: info.Name(), + Path: filepath.ToSlash(relPath), + Size: info.Size(), + ModTime: info.ModTime().UTC().Format("2006-01-02T15:04:05Z"), + }) + } + return nil + }) + + if results == nil { + results = []OfficeFile{} + } + + return c.JSON(fiber.Map{ + "files": results, + "count": len(results), + }) +} + // createBlankOfficeFile creates a minimal valid .docx or .pptx file. // These formats are ZIP archives containing XML files following the // Office Open XML (OOXML) standard.