feat(server): track upload status via manifest and /status API
- Persist per-box file metadata in a .warpbox.json manifest, including IDs and status fields (pending/uploading/complete/failed) - Add GET /box/:id/status to return current file states for clients polling upload progress - Update upload handling to mark failures and completion in the manifest and decorate responses - Add CSS states for loading/failed files and disable interactions for unavailable itemsfeat(server): track upload status via manifest and /status API - Persist per-box file metadata in a .warpbox.json manifest, including IDs and status fields (pending/uploading/complete/failed) - Add GET /box/:id/status to return current file states for clients polling upload progress - Update upload handling to mark failures and completion in the manifest and decorate responses - Add CSS states for loading/failed files and disable interactions for unavailable items
This commit is contained in:
@@ -3,6 +3,7 @@ package server
|
||||
import (
|
||||
"archive/zip"
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -13,20 +14,53 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/gin-contrib/gzip"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const uploadRoot = "data/uploads"
|
||||
const (
|
||||
uploadRoot = "data/uploads"
|
||||
boxManifestFile = ".warpbox.json"
|
||||
fileStatusFailed = "failed"
|
||||
fileStatusReady = "complete"
|
||||
fileStatusWait = "pending"
|
||||
fileStatusWork = "uploading"
|
||||
)
|
||||
|
||||
var boxManifestMu sync.Mutex
|
||||
|
||||
type boxFile struct {
|
||||
Name string
|
||||
Size int64
|
||||
SizeLabel string
|
||||
MimeType string
|
||||
IconPath string
|
||||
DownloadPath string
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Size int64 `json:"size"`
|
||||
SizeLabel string `json:"size_label"`
|
||||
MimeType string `json:"mime_type"`
|
||||
Status string `json:"status"`
|
||||
StatusLabel string `json:"status_label"`
|
||||
Title string `json:"title"`
|
||||
IconPath string `json:"icon_path"`
|
||||
DownloadPath string `json:"download_path"`
|
||||
UploadPath string `json:"upload_path"`
|
||||
IsComplete bool `json:"is_complete"`
|
||||
}
|
||||
|
||||
type boxManifest struct {
|
||||
Files []boxFile `json:"files"`
|
||||
}
|
||||
|
||||
type createBoxRequest struct {
|
||||
Files []createBoxFileRequest `json:"files"`
|
||||
}
|
||||
|
||||
type createBoxFileRequest struct {
|
||||
Name string `json:"name"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
type updateFileStatusRequest struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func Run(addr string) error {
|
||||
@@ -58,6 +92,25 @@ func Run(addr string) error {
|
||||
})
|
||||
})
|
||||
|
||||
router.GET("/box/:id/status", func(ctx *gin.Context) {
|
||||
boxID := ctx.Param("id")
|
||||
if !validBoxID(boxID) {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid box id"})
|
||||
return
|
||||
}
|
||||
|
||||
files, err := listBoxFiles(boxID)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusNotFound, gin.H{"error": "Box not found"})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"box_id": boxID,
|
||||
"files": files,
|
||||
})
|
||||
})
|
||||
|
||||
router.GET("/box/:id/download", func(ctx *gin.Context) {
|
||||
boxID := ctx.Param("id")
|
||||
if !validBoxID(boxID) {
|
||||
@@ -78,6 +131,10 @@ func Run(addr string) error {
|
||||
defer zipWriter.Close()
|
||||
|
||||
for _, file := range files {
|
||||
if !file.IsComplete {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := addFileToZip(zipWriter, boxID, file.Name); err != nil {
|
||||
ctx.Status(http.StatusInternalServerError)
|
||||
return
|
||||
@@ -119,12 +176,77 @@ func Run(addr string) error {
|
||||
return
|
||||
}
|
||||
|
||||
var request createBoxRequest
|
||||
if err := ctx.ShouldBindJSON(&request); err != nil && err != io.EOF {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid box payload"})
|
||||
return
|
||||
}
|
||||
|
||||
files, err := createBoxManifest(boxID, request.Files)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"box_id": boxID,
|
||||
"box_url": "/box/" + boxID,
|
||||
"files": files,
|
||||
})
|
||||
})
|
||||
|
||||
router.POST("/box/:id/files/:file_id/upload", func(ctx *gin.Context) {
|
||||
boxID := ctx.Param("id")
|
||||
fileID := ctx.Param("file_id")
|
||||
if !validBoxID(boxID) {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid box id"})
|
||||
return
|
||||
}
|
||||
|
||||
file, err := ctx.FormFile("file")
|
||||
if err != nil {
|
||||
markManifestFileStatus(boxID, fileID, fileStatusFailed)
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "No file received"})
|
||||
return
|
||||
}
|
||||
|
||||
savedFile, err := saveManifestUploadedFile(ctx, boxID, fileID, file)
|
||||
if err != nil {
|
||||
markManifestFileStatus(boxID, fileID, fileStatusFailed)
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"box_id": boxID,
|
||||
"box_url": "/box/" + boxID,
|
||||
"file": savedFile,
|
||||
})
|
||||
})
|
||||
|
||||
router.POST("/box/:id/files/:file_id/status", func(ctx *gin.Context) {
|
||||
boxID := ctx.Param("id")
|
||||
fileID := ctx.Param("file_id")
|
||||
if !validBoxID(boxID) {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid box id"})
|
||||
return
|
||||
}
|
||||
|
||||
var request updateFileStatusRequest
|
||||
if err := ctx.ShouldBindJSON(&request); err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid status payload"})
|
||||
return
|
||||
}
|
||||
|
||||
file, err := markManifestFileStatus(boxID, fileID, request.Status)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{"file": file})
|
||||
})
|
||||
|
||||
router.POST("/box/:id/upload", func(ctx *gin.Context) {
|
||||
boxID := ctx.Param("id")
|
||||
if !validBoxID(boxID) {
|
||||
@@ -209,6 +331,15 @@ func newBoxID() (string, error) {
|
||||
return hex.EncodeToString(bytes), nil
|
||||
}
|
||||
|
||||
func newFileID() (string, error) {
|
||||
bytes := make([]byte, 8)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return hex.EncodeToString(bytes), nil
|
||||
}
|
||||
|
||||
func validBoxID(boxID string) bool {
|
||||
if len(boxID) != 32 {
|
||||
return false
|
||||
@@ -227,7 +358,20 @@ func boxPath(boxID string) string {
|
||||
return filepath.Join(uploadRoot, boxID)
|
||||
}
|
||||
|
||||
func manifestPath(boxID string) string {
|
||||
return filepath.Join(boxPath(boxID), boxManifestFile)
|
||||
}
|
||||
|
||||
func listBoxFiles(boxID string) ([]boxFile, error) {
|
||||
if manifest, err := readBoxManifest(boxID); err == nil && len(manifest.Files) > 0 {
|
||||
files := make([]boxFile, 0, len(manifest.Files))
|
||||
for _, file := range manifest.Files {
|
||||
files = append(files, decorateBoxFile(boxID, file))
|
||||
}
|
||||
|
||||
return files, nil
|
||||
}
|
||||
|
||||
entries, err := os.ReadDir(boxPath(boxID))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -235,7 +379,7 @@ func listBoxFiles(boxID string) ([]boxFile, error) {
|
||||
|
||||
files := make([]boxFile, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
if entry.IsDir() || entry.Name() == boxManifestFile {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -246,19 +390,160 @@ func listBoxFiles(boxID string) ([]boxFile, error) {
|
||||
|
||||
name := entry.Name()
|
||||
mimeType := mimeTypeForFile(filepath.Join(boxPath(boxID), name), name)
|
||||
files = append(files, boxFile{
|
||||
Name: name,
|
||||
Size: info.Size(),
|
||||
SizeLabel: formatBytes(info.Size()),
|
||||
MimeType: mimeType,
|
||||
IconPath: iconForMimeType(mimeType, name),
|
||||
DownloadPath: "/box/" + boxID + "/files/" + url.PathEscape(name),
|
||||
})
|
||||
files = append(files, decorateBoxFile(boxID, boxFile{
|
||||
ID: name,
|
||||
Name: name,
|
||||
Size: info.Size(),
|
||||
MimeType: mimeType,
|
||||
Status: fileStatusReady,
|
||||
}))
|
||||
}
|
||||
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func createBoxManifest(boxID string, requests []createBoxFileRequest) ([]boxFile, error) {
|
||||
usedNames := make(map[string]int, len(requests))
|
||||
files := make([]boxFile, 0, len(requests))
|
||||
|
||||
for _, request := range requests {
|
||||
filename, ok := safeFilename(request.Name)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Invalid filename")
|
||||
}
|
||||
|
||||
filename = uniqueManifestFilename(filename, usedNames)
|
||||
fileID, err := newFileID()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not create file id")
|
||||
}
|
||||
|
||||
mimeType := mime.TypeByExtension(strings.ToLower(filepath.Ext(filename)))
|
||||
if mimeType == "" {
|
||||
mimeType = "application/octet-stream"
|
||||
}
|
||||
|
||||
files = append(files, boxFile{
|
||||
ID: fileID,
|
||||
Name: filename,
|
||||
Size: request.Size,
|
||||
MimeType: mimeType,
|
||||
Status: fileStatusWait,
|
||||
})
|
||||
}
|
||||
|
||||
manifest := boxManifest{Files: files}
|
||||
if err := writeBoxManifest(boxID, manifest); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
decoratedFiles := make([]boxFile, 0, len(files))
|
||||
for _, file := range files {
|
||||
decoratedFiles = append(decoratedFiles, decorateBoxFile(boxID, file))
|
||||
}
|
||||
|
||||
return decoratedFiles, nil
|
||||
}
|
||||
|
||||
func readBoxManifest(boxID string) (boxManifest, error) {
|
||||
boxManifestMu.Lock()
|
||||
defer boxManifestMu.Unlock()
|
||||
|
||||
return readBoxManifestUnlocked(boxID)
|
||||
}
|
||||
|
||||
func readBoxManifestUnlocked(boxID string) (boxManifest, error) {
|
||||
var manifest boxManifest
|
||||
data, err := os.ReadFile(manifestPath(boxID))
|
||||
if err != nil {
|
||||
return manifest, err
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, &manifest); err != nil {
|
||||
return manifest, err
|
||||
}
|
||||
|
||||
return manifest, nil
|
||||
}
|
||||
|
||||
func writeBoxManifest(boxID string, manifest boxManifest) error {
|
||||
boxManifestMu.Lock()
|
||||
defer boxManifestMu.Unlock()
|
||||
|
||||
return writeBoxManifestUnlocked(boxID, manifest)
|
||||
}
|
||||
|
||||
func writeBoxManifestUnlocked(boxID string, manifest boxManifest) error {
|
||||
data, err := json.MarshalIndent(manifest, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(manifestPath(boxID), data, 0644)
|
||||
}
|
||||
|
||||
func markManifestFileStatus(boxID string, fileID string, status string) (boxFile, error) {
|
||||
if status != fileStatusWait && status != fileStatusWork && status != fileStatusReady && status != fileStatusFailed {
|
||||
return boxFile{}, fmt.Errorf("Invalid file status")
|
||||
}
|
||||
|
||||
boxManifestMu.Lock()
|
||||
defer boxManifestMu.Unlock()
|
||||
|
||||
manifest, err := readBoxManifestUnlocked(boxID)
|
||||
if err != nil {
|
||||
return boxFile{}, err
|
||||
}
|
||||
|
||||
for index, file := range manifest.Files {
|
||||
if file.ID != fileID {
|
||||
continue
|
||||
}
|
||||
|
||||
manifest.Files[index].Status = status
|
||||
if err := writeBoxManifestUnlocked(boxID, manifest); err != nil {
|
||||
return boxFile{}, err
|
||||
}
|
||||
|
||||
return decorateBoxFile(boxID, manifest.Files[index]), nil
|
||||
}
|
||||
|
||||
return boxFile{}, fmt.Errorf("File not found")
|
||||
}
|
||||
|
||||
func decorateBoxFile(boxID string, file boxFile) boxFile {
|
||||
if file.MimeType == "" {
|
||||
file.MimeType = mimeTypeForFile(filepath.Join(boxPath(boxID), file.Name), file.Name)
|
||||
}
|
||||
|
||||
if file.SizeLabel == "" {
|
||||
file.SizeLabel = formatBytes(file.Size)
|
||||
}
|
||||
|
||||
file.IconPath = iconForMimeType(file.MimeType, file.Name)
|
||||
file.DownloadPath = "/box/" + boxID + "/files/" + url.PathEscape(file.Name)
|
||||
file.UploadPath = "/box/" + boxID + "/files/" + url.PathEscape(file.ID) + "/upload"
|
||||
file.IsComplete = file.Status == fileStatusReady
|
||||
|
||||
switch file.Status {
|
||||
case fileStatusReady:
|
||||
file.StatusLabel = "Ready"
|
||||
file.Title = "Download " + file.Name
|
||||
case fileStatusFailed:
|
||||
file.StatusLabel = "Failed"
|
||||
file.Title = "Failed to upload"
|
||||
case fileStatusWork:
|
||||
file.StatusLabel = "Loading"
|
||||
file.Title = "Loading"
|
||||
default:
|
||||
file.Status = fileStatusWait
|
||||
file.StatusLabel = "Waiting"
|
||||
file.Title = "Loading"
|
||||
}
|
||||
|
||||
return file
|
||||
}
|
||||
|
||||
func addFileToZip(zipWriter *zip.Writer, boxID string, filename string) error {
|
||||
path := filepath.Join(boxPath(boxID), filename)
|
||||
source, err := os.Open(path)
|
||||
@@ -276,6 +561,49 @@ func addFileToZip(zipWriter *zip.Writer, boxID string, filename string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func saveManifestUploadedFile(ctx *gin.Context, boxID string, fileID string, file *multipart.FileHeader) (boxFile, error) {
|
||||
boxManifestMu.Lock()
|
||||
defer boxManifestMu.Unlock()
|
||||
|
||||
manifest, err := readBoxManifestUnlocked(boxID)
|
||||
if err != nil {
|
||||
return boxFile{}, err
|
||||
}
|
||||
|
||||
fileIndex := -1
|
||||
for index, manifestFile := range manifest.Files {
|
||||
if manifestFile.ID == fileID {
|
||||
fileIndex = index
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if fileIndex < 0 {
|
||||
return boxFile{}, fmt.Errorf("File not found")
|
||||
}
|
||||
|
||||
filename := manifest.Files[fileIndex].Name
|
||||
if err := os.MkdirAll(boxPath(boxID), 0755); err != nil {
|
||||
return boxFile{}, fmt.Errorf("Could not prepare upload box")
|
||||
}
|
||||
|
||||
destination := filepath.Join(boxPath(boxID), filename)
|
||||
if err := ctx.SaveUploadedFile(file, destination); err != nil {
|
||||
manifest.Files[fileIndex].Status = fileStatusFailed
|
||||
writeBoxManifestUnlocked(boxID, manifest)
|
||||
return boxFile{}, fmt.Errorf("Could not save uploaded file")
|
||||
}
|
||||
|
||||
manifest.Files[fileIndex].Size = file.Size
|
||||
manifest.Files[fileIndex].MimeType = mimeTypeForFile(destination, filename)
|
||||
manifest.Files[fileIndex].Status = fileStatusReady
|
||||
if err := writeBoxManifestUnlocked(boxID, manifest); err != nil {
|
||||
return boxFile{}, err
|
||||
}
|
||||
|
||||
return decorateBoxFile(boxID, manifest.Files[fileIndex]), nil
|
||||
}
|
||||
|
||||
func saveUploadedFile(ctx *gin.Context, boxID string, file *multipart.FileHeader) (gin.H, error) {
|
||||
filename, ok := safeFilename(file.Filename)
|
||||
if !ok {
|
||||
@@ -320,6 +648,19 @@ func uniqueFilename(directory string, filename string) string {
|
||||
}
|
||||
}
|
||||
|
||||
func uniqueManifestFilename(filename string, usedNames map[string]int) string {
|
||||
count := usedNames[filename]
|
||||
usedNames[filename] = count + 1
|
||||
|
||||
if count == 0 {
|
||||
return filename
|
||||
}
|
||||
|
||||
extension := filepath.Ext(filename)
|
||||
base := strings.TrimSuffix(filename, extension)
|
||||
return fmt.Sprintf("%s-%d%s", base, count+1, extension)
|
||||
}
|
||||
|
||||
func mimeTypeForFile(path string, filename string) string {
|
||||
if mimeType := mime.TypeByExtension(strings.ToLower(filepath.Ext(filename))); mimeType != "" {
|
||||
return mimeType
|
||||
|
||||
Reference in New Issue
Block a user