aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Library/NetSocket/FileProgress.swift
blob: 1cc022851e350195cb42b31a6149b9c87817883d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// NetSocketProgress
// Dustin Mierau • @mierau
// MIT License

import Foundation

public extension NetSocket {
  
  /// Progress information for file uploads/downloads
  struct FileProgress: Sendable {
    /// Number of bytes sent/received so far
    public let sent: Int
    /// Total file size (may be nil if unknown)
    public let total: Int?
    /// Smoothed transfer rate in bytes per second (EMA), if enough samples collected
    public let bytesPerSecond: Double?
    /// Estimated time remaining (seconds) based on smoothed rate, if available
    public let estimatedTimeRemaining: TimeInterval?
    
    public init(sent: Int, total: Int?, bytesPerSecond: Double? = nil, estimatedTimeRemaining: TimeInterval? = nil) {
      self.sent = sent
      self.total = total
      self.bytesPerSecond = bytesPerSecond
      self.estimatedTimeRemaining = estimatedTimeRemaining
    }
    
    /// Format transfer speed in human-readable format
    ///
    /// Automatically selects appropriate unit (B/sec, KB/sec, MB/sec, GB/sec)
    /// based on the magnitude of the speed.
    ///
    /// - Returns: Formatted string like "45KB/sec", "5B/sec", "12.5MB/sec", or nil if speed unavailable
    ///
    /// Example:
    /// ```swift
    /// if let speedString = progress.formattedSpeed {
    ///   print(speedString)  // "2.5MB/sec"
    /// }
    /// ```
    public var formattedSpeed: String? {
      guard let bytesPerSecond = bytesPerSecond, bytesPerSecond > 0 else { return nil }
      
      let kb = 1024.0
      let mb = kb * 1024.0
      let gb = mb * 1024.0
      
      if bytesPerSecond >= gb {
        return String(format: "%.1fGB/sec", bytesPerSecond / gb)
      } else if bytesPerSecond >= mb {
        return String(format: "%.1fMB/sec", bytesPerSecond / mb)
      } else if bytesPerSecond >= kb {
        return String(format: "%.0fKB/sec", bytesPerSecond / kb)
      } else {
        return String(format: "%.0fB/sec", bytesPerSecond)
      }
    }
  }
}