Add resumable downloads, download paths, queue controls and history

This commit is contained in:
SirBlob committed 2026-11-01 13:36:29 -05:00
1 parent 434f6441da
commit 6027cfabb9
43 files changed
+2138 -291

No files matched your search

+17 -1
View File
@@ -3,22 +3,27 @@ package preferences
import (
"errors"
"fmt"
"path/filepath"
)
const (
gigabyte = int64(1) << 30
maxConcurrentLimit = 10
maxConnectionsLimit = 16
maxReservedGigabytes = 100000
maxDownloadPaths = 50
)
var ErrInvalid = errors.New("invalid settings")
type Preferences struct {
MaxConcurrentDownloads int `json:"maxConcurrentDownloads"`
ConnectionsPerDownload int `json:"connectionsPerDownload"`
MinFreeGB int `json:"minFreeGB"`
AllowedPorts []uint16 `json:"allowedPorts"`
AutoExtract bool `json:"autoExtract"`
CleanupAfterMove bool `json:"cleanupAfterMove"`
DownloadPaths []string `json:"downloadPaths"`
}
func (preferences Preferences) MinFreeBytes() int64 {
@@ -29,7 +34,10 @@ func (preferences Preferences) Validate() error {
if preferences.MaxConcurrentDownloads < 1 || preferences.MaxConcurrentDownloads > maxConcurrentLimit {
return fmt.Errorf("%w: parallel downloads must be between 1 and %d", ErrInvalid, maxConcurrentLimit)
}
if preferences.MinFreeGB < 0 || preferences.MinFreeGB > maxReservedGigabytes {
if preferences.ConnectionsPerDownload < 1 || preferences.ConnectionsPerDownload > maxConnectionsLimit {
return fmt.Errorf("%w: connections per download must be between 1 and %d", ErrInvalid, maxConnectionsLimit)
}
if preferences.MinFreeGB < 0|| preferences.MinFreeGB > maxReservedGigabytes {
return fmt.Errorf("%w: free space to keep must be between 0 and %d GB", ErrInvalid, maxReservedGigabytes)
}
for _, port := range preferences.AllowedPorts {
@@ -37,5 +45,13 @@ func (preferences Preferences) Validate() error {
return fmt.Errorf("%w: port 0 is not allowed", ErrInvalid)
}
}
if len(preferences.DownloadPaths) > maxDownloadPaths {
return fmt.Errorf("%w: at most %d download paths are allowed", ErrInvalid, maxDownloadPaths)
}
for _, downloadPath := range preferences.DownloadPaths {
if !filepath.IsAbs(downloadPath) {
return fmt.Errorf("%w: download path %q must be an absolute path", ErrInvalid, downloadPath)
}
}
return nil
}
+7 -20
View File
@@ -6,9 +6,10 @@ import (
"fmt"
"io/fs"
"os"
"path/filepath"
"slices"
"sync"
"ddc/internal/files"
)
type Store struct {
@@ -47,6 +48,10 @@ func (store *Store) Get() Preferences {
if current.AllowedPorts == nil {
current.AllowedPorts = []uint16{}
}
current.DownloadPaths = slices.Clone(current.DownloadPaths)
if current.DownloadPaths == nil {
current.DownloadPaths = []string{}
}
return current
}
@@ -61,27 +66,9 @@ func (store *Store) Update(next Preferences) error {
store.mutex.Lock()
defer store.mutex.Unlock()
if err := writeAtomically(store.path, data); err != nil {
if err := files.WriteAtomically(store.path, data); err != nil {
return err
}
store.current = next
return nil
}
func writeAtomically(path string, data []byte) error {
temporary, err := os.CreateTemp(filepath.Dir(path), ".settings-*.tmp")
if err != nil {
return err
}
_, writeErr := temporary.Write(data)
closeErr := temporary.Close()
if writeErr != nil || closeErr != nil {
os.Remove(temporary.Name())
return errors.Join(writeErr, closeErr)
}
if err := os.Rename(temporary.Name(), path); err != nil {
os.Remove(temporary.Name())
return err
}
return nil
}