feat: new ui (#1455)

This commit is contained in:
Levi authored and GitHub committed 2026-09-05 19:53:41 +08:00
1 parent 3be8db664c
commit 177c225f16
742 files changed
+59951 -5934

No files matched your search

+3
View File
@@ -133,6 +133,8 @@ type Options struct {
Name string `json:"name"`
// Download file path
Path string `json:"path"`
// Use this path as the default download directory after creation succeeds
AsDefaultPath bool `json:"asDefaultPath,omitempty"`
// Select file indexes to download
SelectFiles []int `json:"selectFiles"`
// Extra info for specific fetcher
@@ -206,6 +208,7 @@ type DownloaderStoreConfig struct {
Script *ScriptConfig `json:"script"` // Script is the script execution configuration
AutoTorrent *AutoTorrentConfig `json:"autoTorrent"` // AutoTorrent is the auto torrent task creation configuration
Archive *ArchiveConfig `json:"archive"` // Archive is the archive extraction configuration
AutoStartTasks bool `json:"autoStartTasks"` // AutoStartTasks continues all unfinished tasks when the backend starts
AutoDeleteMissingFileTasks bool `json:"autoDeleteMissingFileTasks"` // AutoDeleteMissingFileTasks enables automatic deletion of tasks with missing files
}
+198
View File
@@ -0,0 +1,198 @@
package base
import (
"encoding/json"
"errors"
"fmt"
"math/bits"
)
const (
// PieceMapEncoding identifies the one-bit wire format used by PieceMap.
// One byte stores eight ordered pieces, least-significant bit first: piece
// 0 uses bit 0, piece 1 uses bit 1, and so on. A set bit means the piece is
// locally complete and verified; a clear bit means it is not complete.
PieceMapEncoding = "bitset-v1"
piecesPerByte = 8
)
var ErrPieceIndexOutOfRange = errors.New("piece index out of range")
// PieceMap stores the verified completion state of ordered pieces as a bitset.
// It deliberately excludes scheduler states such as downloading, hashing and
// failed: those states are transient and are not stable across pause/restart.
//
// Fields are private so PieceCount, CompletedPieces and packed bytes remain
// consistent. Use Add, Delete, Update and Get for mutation, and Bytes when a
// binary copy is required. JSON serializes Data as Base64.
type PieceMap struct {
pieceCount int
pieceSize int64
completedPieces int
data []byte
}
// NewPieceMap creates a map containing pieceCount incomplete pieces.
func NewPieceMap(pieceCount int, pieceSize int64) *PieceMap {
if pieceCount < 0 {
pieceCount = 0
}
return &PieceMap{
pieceCount: pieceCount,
pieceSize: pieceSize,
data: make([]byte, packedPieceByteCount(pieceCount)),
}
}
// NewPieceMapFromBytes restores a bitset-v1 map. Input bytes are copied and
// unused high bits in the last byte are cleared.
func NewPieceMapFromBytes(pieceCount int, pieceSize int64, data []byte) (*PieceMap, error) {
if pieceCount < 0 {
return nil, fmt.Errorf("%w: negative piece count", ErrPieceIndexOutOfRange)
}
expectedBytes := packedPieceByteCount(pieceCount)
if len(data) != expectedBytes {
return nil, fmt.Errorf("invalid piece data length: got %d, want %d", len(data), expectedBytes)
}
pieceMap := &PieceMap{
pieceCount: pieceCount,
pieceSize: pieceSize,
data: append([]byte(nil), data...),
}
pieceMap.clearUnusedBits()
for _, value := range pieceMap.data {
pieceMap.completedPieces += bits.OnesCount8(value)
}
return pieceMap, nil
}
// Clone returns an independent ordered snapshot.
func (m *PieceMap) Clone() *PieceMap {
if m == nil {
return nil
}
return &PieceMap{
pieceCount: m.pieceCount,
pieceSize: m.pieceSize,
completedPieces: m.completedPieces,
data: append([]byte(nil), m.data...),
}
}
// Add appends a piece while preserving all existing indexes.
func (m *PieceMap) Add(completed bool) {
if m.pieceCount%piecesPerByte == 0 {
m.data = append(m.data, 0)
}
m.pieceCount++
if completed {
m.setCompleted(m.pieceCount-1, true)
m.completedPieces++
}
}
// Delete removes a piece and shifts every following piece one index left.
func (m *PieceMap) Delete(index int) error {
if !m.validIndex(index) {
return ErrPieceIndexOutOfRange
}
if m.completedAt(index) {
m.completedPieces--
}
for current := index; current < m.pieceCount-1; current++ {
m.setCompleted(current, m.completedAt(current+1))
}
m.pieceCount--
m.data = m.data[:packedPieceByteCount(m.pieceCount)]
m.clearUnusedBits()
return nil
}
// Update changes whether the piece at index is complete and verified.
func (m *PieceMap) Update(index int, completed bool) error {
if !m.validIndex(index) {
return ErrPieceIndexOutOfRange
}
previous := m.completedAt(index)
if previous == completed {
return nil
}
if completed {
m.completedPieces++
} else {
m.completedPieces--
}
m.setCompleted(index, completed)
return nil
}
// Get reports whether the exact piece index is complete and verified.
func (m *PieceMap) Get(index int) (bool, error) {
if !m.validIndex(index) {
return false, ErrPieceIndexOutOfRange
}
return m.completedAt(index), nil
}
// Bytes returns an independent copy of the packed bitset-v1 bytes.
func (m *PieceMap) Bytes() []byte {
return append([]byte(nil), m.data...)
}
func (m *PieceMap) PieceCount() int { return m.pieceCount }
func (m *PieceMap) PieceSize() int64 { return m.pieceSize }
func (m *PieceMap) CompletedPieces() int { return m.completedPieces }
// MarshalJSON publishes the compact bytes without exposing mutable storage.
func (m PieceMap) MarshalJSON() ([]byte, error) {
type pieceMapJSON struct {
Encoding string `json:"encoding"`
PieceCount int `json:"pieceCount"`
PieceSize int64 `json:"pieceSize"`
CompletedPieces int `json:"completedPieces"`
Data []byte `json:"data"`
}
return json.Marshal(pieceMapJSON{
Encoding: PieceMapEncoding,
PieceCount: m.pieceCount,
PieceSize: m.pieceSize,
CompletedPieces: m.completedPieces,
Data: m.data,
})
}
func (m *PieceMap) validIndex(index int) bool {
return m != nil && index >= 0 && index < m.pieceCount
}
func (m *PieceMap) completedAt(index int) bool {
byteIndex, mask := piecePosition(index)
return m.data[byteIndex]&mask != 0
}
func (m *PieceMap) setCompleted(index int, completed bool) {
byteIndex, mask := piecePosition(index)
if completed {
m.data[byteIndex] |= mask
} else {
m.data[byteIndex] &^= mask
}
}
func (m *PieceMap) clearUnusedBits() {
if m.pieceCount == 0 || m.pieceCount%piecesPerByte == 0 {
return
}
usedBits := uint(m.pieceCount % piecesPerByte)
m.data[len(m.data)-1] &= byte(1<<usedBits) - 1
}
func piecePosition(index int) (byteIndex int, mask byte) {
return index / piecesPerByte, byte(1 << uint(index%piecesPerByte))
}
func packedPieceByteCount(pieceCount int) int {
return (pieceCount + piecesPerByte - 1) / piecesPerByte
}
+104
View File
@@ -0,0 +1,104 @@
package base
import (
"encoding/json"
"errors"
"testing"
)
func TestPieceMapCRUDAndBytes(t *testing.T) {
pieceMap := NewPieceMap(4, 16*1024)
_ = pieceMap.Update(0, true)
_ = pieceMap.Update(2, true)
if got, want := pieceMap.Bytes(), []byte{0b00000101}; len(got) != 1 || got[0] != want[0] {
t.Fatalf("Bytes() = %08b, want %08b", got, want)
}
if got := pieceMap.CompletedPieces(); got != 2 {
t.Fatalf("CompletedPieces() = %d, want 2", got)
}
pieceMap.Add(true)
if got := pieceMap.PieceCount(); got != 5 {
t.Fatalf("PieceCount() = %d, want 5", got)
}
if err := pieceMap.Delete(1); err != nil {
t.Fatalf("Delete: %v", err)
}
wantAfterDelete := []bool{true, true, false, true}
for index, want := range wantAfterDelete {
got, err := pieceMap.Get(index)
if err != nil || got != want {
t.Fatalf("Get(%d) = %v, %v; want %v", index, got, err, want)
}
}
if got := pieceMap.CompletedPieces(); got != 3 {
t.Fatalf("CompletedPieces() after delete = %d, want 3", got)
}
bytesCopy := pieceMap.Bytes()
bytesCopy[0] = 0
got, _ := pieceMap.Get(0)
if !got {
t.Fatal("Bytes returned mutable internal storage")
}
}
func TestPieceMapValidationAndRestore(t *testing.T) {
pieceMap, err := NewPieceMapFromBytes(10, 1024, []byte{0b10010001, 0xff})
if err != nil {
t.Fatalf("NewPieceMapFromBytes: %v", err)
}
// Only the low positions for pieces 8 and 9 belong to the map.
if got, want := pieceMap.Bytes()[1], byte(0b00000011); got != want {
t.Fatalf("last byte = %08b, want %08b", got, want)
}
if pieceMap.CompletedPieces() != 5 {
t.Fatalf("CompletedPieces() = %d, want 5", pieceMap.CompletedPieces())
}
if _, err := NewPieceMapFromBytes(10, 1024, []byte{0}); err == nil {
t.Fatal("expected invalid byte length error")
}
if err := pieceMap.Update(10, true); !errors.Is(err, ErrPieceIndexOutOfRange) {
t.Fatalf("Update out of range error = %v", err)
}
}
func TestPieceMapJSON(t *testing.T) {
pieceMap := NewPieceMap(4, 4096)
_ = pieceMap.Update(0, true)
_ = pieceMap.Update(2, true)
data, err := json.Marshal(pieceMap)
if err != nil {
t.Fatalf("Marshal: %v", err)
}
var got map[string]any
if err := json.Unmarshal(data, &got); err != nil {
t.Fatalf("Unmarshal JSON: %v", err)
}
if got["encoding"] != PieceMapEncoding || got["data"] != "BQ==" {
t.Fatalf("unexpected JSON: %s", data)
}
if got["pieceCount"] != float64(4) || got["pieceSize"] != float64(4096) || got["completedPieces"] != float64(2) {
t.Fatalf("unexpected metadata: %s", data)
}
}
func TestPieceMapCloneIsIndependent(t *testing.T) {
pieceMap := NewPieceMap(4, 4096)
_ = pieceMap.Update(0, true)
snapshot := pieceMap.Clone()
_ = snapshot.Update(1, true)
if got, _ := pieceMap.Get(1); got {
t.Fatal("Clone mutated the source map")
}
if snapshot.CompletedPieces() != 2 {
t.Fatalf("clone completed pieces = %d, want 2", snapshot.CompletedPieces())
}
if (*PieceMap)(nil).Clone() != nil {
t.Fatal("nil map clone must stay nil")
}
}
+21
View File
@@ -0,0 +1,21 @@
package base
// PeerStats is the common row returned by protocols that expose connected
// peers. Speeds contain payload bytes per second rather than protocol overhead.
type PeerStats struct {
Address string `json:"address"`
Client string `json:"client"`
DownloadSpeed int64 `json:"downloadSpeed"`
UploadSpeed int64 `json:"uploadSpeed"`
PieceCount int `json:"pieceCount"`
// Completion is the known remote data completion ratio in the range 0–1.
// It is omitted when a protocol cannot determine the remote total.
Completion *float64 `json:"completion,omitempty"`
// Relevance is the ratio of locally wanted, missing pieces that this peer
// claims to have. It is omitted when the local missing set is unavailable or empty.
Relevance *float64 `json:"relevance,omitempty"`
Source string `json:"source"`
// Transport is the active connection transport, for example tcp or utp.
// It is intentionally separate from Source, which describes peer discovery.
Transport string `json:"transport"`
}