code refactoring
This commit is contained in:
@@ -3,7 +3,6 @@ package internal
|
||||
import (
|
||||
"encoding/gob"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -35,33 +34,6 @@ func (m *MemoryDB) Set(process *Process) string {
|
||||
return id
|
||||
}
|
||||
|
||||
// Update a process info/metadata, given the process id
|
||||
//
|
||||
// Deprecated: will be removed anytime soon.
|
||||
func (m *MemoryDB) UpdateInfo(id string, info DownloadInfo) error {
|
||||
entry, ok := m.table.Load(id)
|
||||
if ok {
|
||||
entry.(*Process).Info = info
|
||||
m.table.Store(id, entry)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("can't update row with id %s", id)
|
||||
}
|
||||
|
||||
// Update a process progress data, given the process id
|
||||
// Used for updating completition percentage or ETA.
|
||||
//
|
||||
// Deprecated: will be removed anytime soon.
|
||||
func (m *MemoryDB) UpdateProgress(id string, progress DownloadProgress) error {
|
||||
entry, ok := m.table.Load(id)
|
||||
if ok {
|
||||
entry.(*Process).Progress = progress
|
||||
m.table.Store(id, entry)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("can't update row with id %s", id)
|
||||
}
|
||||
|
||||
// Removes a process progress, given the process id
|
||||
func (m *MemoryDB) Delete(id string) {
|
||||
m.table.Delete(id)
|
||||
@@ -92,7 +64,7 @@ func (m *MemoryDB) All() *[]ProcessResponse {
|
||||
return &running
|
||||
}
|
||||
|
||||
// WIP: Persist the database in a single file named "session.dat"
|
||||
// Persist the database in a single file named "session.dat"
|
||||
func (m *MemoryDB) Persist() error {
|
||||
running := m.All()
|
||||
|
||||
@@ -115,17 +87,16 @@ func (m *MemoryDB) Persist() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// WIP: Restore a persisted state
|
||||
// Restore a persisted state
|
||||
func (m *MemoryDB) Restore(logger *slog.Logger) {
|
||||
fd, err := os.Open("session.dat")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
session := Session{}
|
||||
var session Session
|
||||
|
||||
err = gob.NewDecoder(fd).Decode(&session)
|
||||
if err != nil {
|
||||
if err := gob.NewDecoder(fd).Decode(&session); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -29,17 +29,15 @@ func PlaylistDetect(req DownloadRequest, mq *MessageQueue, db *MemoryDB, logger
|
||||
return err
|
||||
}
|
||||
|
||||
m := metadata{}
|
||||
var m metadata
|
||||
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
if err := cmd.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Info("decoding metadata", slog.String("url", req.URL))
|
||||
|
||||
err = json.NewDecoder(stdout).Decode(&m)
|
||||
if err != nil {
|
||||
if err := json.NewDecoder(stdout).Decode(&m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user