Add standalone desktop workflow
This commit is contained in:
@@ -8,6 +8,28 @@ import (
|
||||
"jukebox_maker/internal/disk"
|
||||
)
|
||||
|
||||
func (s *Server) diskResponse(info disk.DiskInfo) map[string]any {
|
||||
item := map[string]any{
|
||||
"state": info.State,
|
||||
"disk_id": info.DiskID,
|
||||
"total_bytes": info.TotalBytes,
|
||||
"free_bytes": info.FreeBytes,
|
||||
"mount_path": info.MountPath,
|
||||
}
|
||||
if info.DiskID != "" {
|
||||
if s.deps.OnDiskInit != nil {
|
||||
s.deps.OnDiskInit(info.MountPath, info.DiskID)
|
||||
}
|
||||
if lastCopiedAt, ok, err := s.deps.Copier.LastCopiedAt(info.DiskID); err == nil && ok {
|
||||
item["last_copied_at"] = lastCopiedAt.Format(time.RFC3339)
|
||||
}
|
||||
if t, ok := s.deps.Tasks.ActiveTaskByDisk(info.DiskID); ok {
|
||||
item["active_task_id"] = t.ID
|
||||
}
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
func (s *Server) handleDiskStatus(w http.ResponseWriter, r *http.Request) {
|
||||
type response struct {
|
||||
State disk.DiskState `json:"state"`
|
||||
@@ -29,12 +51,12 @@ func (s *Server) handleDiskStatus(w http.ResponseWriter, r *http.Request) {
|
||||
FreeBytes: info.FreeBytes,
|
||||
MountPath: info.MountPath,
|
||||
}
|
||||
if info.DiskID != "" {
|
||||
if lastCopiedAt, ok, err := s.deps.Copier.LastCopiedAt(info.DiskID); err == nil && ok {
|
||||
item.LastCopiedAt = lastCopiedAt.Format(time.RFC3339)
|
||||
if payload := s.diskResponse(info); payload != nil {
|
||||
if v, ok := payload["last_copied_at"].(string); ok {
|
||||
item.LastCopiedAt = v
|
||||
}
|
||||
if t, ok := s.deps.Tasks.ActiveTaskByDisk(info.DiskID); ok {
|
||||
item.ActiveTaskID = t.ID
|
||||
if v, ok := payload["active_task_id"].(string); ok {
|
||||
item.ActiveTaskID = v
|
||||
}
|
||||
}
|
||||
resp = append(resp, item)
|
||||
@@ -43,6 +65,16 @@ func (s *Server) handleDiskStatus(w http.ResponseWriter, r *http.Request) {
|
||||
jsonOK(w, map[string]any{"items": resp})
|
||||
}
|
||||
|
||||
func (s *Server) handleDiskProbe(w http.ResponseWriter, r *http.Request) {
|
||||
mountPath := r.URL.Query().Get("mount_path")
|
||||
info, err := s.deps.ProbeDisk(mountPath)
|
||||
if err != nil {
|
||||
jsonErr(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
jsonOK(w, s.diskResponse(info))
|
||||
}
|
||||
|
||||
func (s *Server) handleDiskInit(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
MountPath string `json:"mount_path"`
|
||||
@@ -52,9 +84,9 @@ func (s *Server) handleDiskInit(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
info, ok := s.deps.Watcher.DiskByMountPath(req.MountPath)
|
||||
if !ok {
|
||||
jsonErr(w, http.StatusNotFound, "disk not found")
|
||||
info, err := s.deps.ProbeDisk(req.MountPath)
|
||||
if err != nil {
|
||||
jsonErr(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
if info.State == disk.DiskAbsent {
|
||||
@@ -76,7 +108,8 @@ func (s *Server) handleDiskInit(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
s.deps.OnDiskInit(info.MountPath, diskID)
|
||||
s.deps.Watcher.ProbeNow()
|
||||
if s.deps.OnDiskInit != nil {
|
||||
s.deps.OnDiskInit(info.MountPath, diskID)
|
||||
}
|
||||
jsonOK(w, map[string]string{"disk_id": diskID})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user