优化扫描
This commit is contained in:
@@ -7,15 +7,25 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type ScanStatus struct {
|
||||
Running bool `json:"running"`
|
||||
LibraryID int `json:"library_id"`
|
||||
Report *service.ScanReport `json:"report,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
StartedAt *time.Time `json:"started_at,omitempty"`
|
||||
FinishedAt *time.Time `json:"finished_at,omitempty"`
|
||||
}
|
||||
|
||||
type LibraryController struct {
|
||||
libraryService *service.LibraryService
|
||||
coverService *service.CoverService
|
||||
scanMutex sync.Mutex
|
||||
isScanning bool
|
||||
scanStatus ScanStatus
|
||||
}
|
||||
|
||||
func NewLibraryController(libraryService *service.LibraryService, coverSvc *service.CoverService) *LibraryController {
|
||||
@@ -112,49 +122,91 @@ func (c *LibraryController) UpdatePath(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *LibraryController) Scan(w http.ResponseWriter, r *http.Request) {
|
||||
c.scanMutex.Lock()
|
||||
if c.isScanning {
|
||||
c.scanMutex.Unlock()
|
||||
jsonError(w, "Another scan is already in progress", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
c.isScanning = true
|
||||
c.scanMutex.Unlock()
|
||||
|
||||
idParam := chi.URLParam(r, "id")
|
||||
id, err := strconv.Atoi(idParam)
|
||||
if err != nil {
|
||||
c.scanMutex.Lock()
|
||||
c.isScanning = false
|
||||
c.scanMutex.Unlock()
|
||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer func() {
|
||||
c.scanMutex.Lock()
|
||||
c.isScanning = false
|
||||
c.scanMutex.Unlock()
|
||||
}()
|
||||
c.scanMutex.Lock()
|
||||
if c.scanStatus.Running {
|
||||
c.scanMutex.Unlock()
|
||||
jsonError(w, "Another scan is already in progress", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
|
||||
report, err := c.libraryService.Scan(id)
|
||||
startedAt := time.Now()
|
||||
c.scanStatus = ScanStatus{
|
||||
Running: true,
|
||||
LibraryID: id,
|
||||
Report: &service.ScanReport{},
|
||||
StartedAt: &startedAt,
|
||||
}
|
||||
startedStatus := c.cloneScanStatusLocked()
|
||||
c.scanMutex.Unlock()
|
||||
|
||||
go func() {
|
||||
report, err := c.libraryService.Scan(id, func(progress service.ScanReport) {
|
||||
c.scanMutex.Lock()
|
||||
if c.scanStatus.LibraryID == id {
|
||||
progressCopy := progress
|
||||
c.scanStatus.Report = &progressCopy
|
||||
}
|
||||
c.scanMutex.Unlock()
|
||||
})
|
||||
|
||||
c.scanMutex.Lock()
|
||||
defer c.scanMutex.Unlock()
|
||||
|
||||
finishedAt := time.Now()
|
||||
c.scanStatus.Running = false
|
||||
c.scanStatus.FinishedAt = &finishedAt
|
||||
if report != nil {
|
||||
reportCopy := *report
|
||||
c.scanStatus.Report = &reportCopy
|
||||
}
|
||||
if err != nil {
|
||||
c.scanStatus.Error = err.Error()
|
||||
log.Printf("Scan failed for library %d: %v", id, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.scanStatus.Error = ""
|
||||
log.Printf("Scan completed for library %d: %+v", id, report)
|
||||
}()
|
||||
|
||||
jsonMsg(w, "Library scan started.")
|
||||
jsonResponse(w, startedStatus, http.StatusAccepted)
|
||||
}
|
||||
|
||||
func (c *LibraryController) GetScanStatus(w http.ResponseWriter, r *http.Request) {
|
||||
idParam := chi.URLParam(r, "id")
|
||||
id, err := strconv.Atoi(idParam)
|
||||
if err != nil {
|
||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
c.scanMutex.Lock()
|
||||
scanning := c.isScanning
|
||||
status := c.cloneScanStatusLocked()
|
||||
c.scanMutex.Unlock()
|
||||
jsonResponse(w, map[string]bool{"scanning": scanning}, http.StatusOK)
|
||||
|
||||
if status.LibraryID != id {
|
||||
jsonResponse(w, ScanStatus{LibraryID: id, Report: &service.ScanReport{}}, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
jsonResponse(w, status, http.StatusOK)
|
||||
}
|
||||
|
||||
func (c *LibraryController) cloneScanStatusLocked() ScanStatus {
|
||||
status := c.scanStatus
|
||||
if c.scanStatus.Report != nil {
|
||||
reportCopy := *c.scanStatus.Report
|
||||
reportCopy.FailedFiles = append([]string(nil), c.scanStatus.Report.FailedFiles...)
|
||||
status.Report = &reportCopy
|
||||
}
|
||||
return status
|
||||
}
|
||||
|
||||
func (c *LibraryController) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user