Add archive preview and new storage categories
Some checks failed
Automated Container Build / build-and-push (push) Failing after 32s

This commit is contained in:
Elijah 2026-06-01 08:57:01 -07:00
parent a2b634517f
commit 6b7d0de208
6 changed files with 150 additions and 4 deletions

View file

@ -1,6 +1,7 @@
package handlers
import (
"archive/zip"
"fmt"
"os"
"path/filepath"
@ -9,6 +10,7 @@ import (
"git.elijahkuntz.com/Elijah/drive/config"
"git.elijahkuntz.com/Elijah/drive/database"
"git.elijahkuntz.com/Elijah/drive/workers"
"github.com/bodgit/sevenzip"
"github.com/gofiber/fiber/v2"
)
@ -130,4 +132,66 @@ func (h *ArchiveHandler) syncArchiveToDB(relPath string) {
`, cleanPath, filepath.Base(cleanPath), info.Size(), info.IsDir())
}
type ArchiveContentItem struct {
Name string `json:"name"`
Size uint64 `json:"size"`
Path string `json:"path"`
}
// GetArchiveContents lists files within a zip or 7z archive without extracting.
// GET /api/files/archive-contents
func (h *ArchiveHandler) GetArchiveContents(c *fiber.Ctx) error {
relPath := c.Query("path")
if relPath == "" {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "path is required"})
}
fullPath, err := resolveSafe(h.Config.StorageDir, relPath)
if err != nil {
return c.Status(fiber.StatusForbidden).JSON(fiber.Map{"error": err.Error()})
}
ext := strings.ToLower(filepath.Ext(fullPath))
var items []ArchiveContentItem
if ext == ".zip" {
r, err := zip.OpenReader(fullPath)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "failed to open zip file"})
}
defer r.Close()
for _, f := range r.File {
if f.FileInfo().IsDir() {
continue
}
items = append(items, ArchiveContentItem{
Name: f.FileInfo().Name(),
Size: f.UncompressedSize64,
Path: f.Name,
})
}
} else if ext == ".7z" {
r, err := sevenzip.OpenReader(fullPath)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "failed to open 7z file"})
}
defer r.Close()
for _, f := range r.File {
if f.FileInfo().IsDir() {
continue
}
items = append(items, ArchiveContentItem{
Name: f.FileInfo().Name(),
Size: f.UncompressedSize,
Path: f.Name,
})
}
} else {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "unsupported archive type"})
}
return c.JSON(fiber.Map{"items": items})
}