Add archive preview and new storage categories
Some checks failed
Automated Container Build / build-and-push (push) Failing after 32s
Some checks failed
Automated Container Build / build-and-push (push) Failing after 32s
This commit is contained in:
parent
a2b634517f
commit
6b7d0de208
6 changed files with 150 additions and 4 deletions
|
|
@ -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})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1255,6 +1255,10 @@ func categorizeFile(ext string) string {
|
|||
return "documents"
|
||||
case ".zip", ".tar", ".gz", ".rar", ".7z", ".bz2":
|
||||
return "archives"
|
||||
case ".js", ".ts", ".tsx", ".jsx", ".html", ".css", ".json", ".py", ".go", ".java", ".cpp", ".c", ".h", ".hpp", ".cs", ".php", ".rb", ".sh", ".yml", ".yaml", ".xml", ".rs", ".swift", ".kt":
|
||||
return "code"
|
||||
case ".stl", ".3mf", ".obj", ".step", ".stp", ".glb", ".gltf", ".blend":
|
||||
return "3d_models"
|
||||
default:
|
||||
return "other"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -212,6 +212,7 @@ func main() {
|
|||
files.Get("/info/*", fsHandler.GetExtendedFileInfo)
|
||||
files.Get("/thumbnail/:checksum", fsHandler.ServeThumbnail)
|
||||
files.Get("/download-folder", fsHandler.DownloadFolder)
|
||||
files.Get("/archive-contents", archiveHandler.GetArchiveContents)
|
||||
files.Post("/download-bulk", fsHandler.DownloadBulk)
|
||||
files.Get("/download", fsHandler.Download)
|
||||
files.Get("/download/*", fsHandler.Download)
|
||||
|
|
|
|||
Reference in a new issue