Skip to content

Commit

Permalink
Debrid: Use universal cached IDs
Browse files Browse the repository at this point in the history
Different services can send different statuses for if a file is
cached or not. Therefore, make this scoped to the debrid service
rather than expecting everything to state "downloaded".

Also it feels pretty blank if the disclosure groups are gone when
a cloud array is empty, so remove those checks.

Signed-off-by: kingbri <[email protected]>
  • Loading branch information
bdashore3 committed Jun 17, 2024
1 parent 78f2aff commit 70b628b
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 14 deletions.
5 changes: 3 additions & 2 deletions Ferrite/API/AllDebridWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class AllDebrid: PollingDebridSource, ObservableObject {
let id = "AllDebrid"
let abbreviation = "AD"
let website = "https://alldebrid.com"
let cachedStatus: [String] = ["Ready"]
var authTask: Task<Void, Error>?

@Published var authProcessing: Bool = false
Expand Down Expand Up @@ -224,7 +225,7 @@ class AllDebrid: PollingDebridSource, ObservableObject {
func getRestrictedFile(magnet: Magnet, ia: DebridIA?, iaFile: DebridIAFile?) async throws -> (restrictedFile: DebridIAFile?, newIA: DebridIA?) {
let selectedMagnetId: String

if let existingMagnet = cloudMagnets.first(where: { $0.hash == magnet.hash && $0.status == "Ready" }) {
if let existingMagnet = cloudMagnets.first(where: { $0.hash == magnet.hash && cachedStatus.contains($0.status) }) {
selectedMagnetId = existingMagnet.id
} else {
let magnetId = try await addMagnet(magnet: magnet)
Expand Down Expand Up @@ -317,7 +318,7 @@ class AllDebrid: PollingDebridSource, ObservableObject {
DebridCloudMagnet(
id: String(magnetResponse.id),
fileName: magnetResponse.filename,
status: magnetResponse.status == "Ready" ? "downloaded" : magnetResponse.status,
status: magnetResponse.status,
hash: magnetResponse.hash,
links: magnetResponse.links.map(\.link)
)
Expand Down
5 changes: 3 additions & 2 deletions Ferrite/API/OffCloudWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class OffCloud: DebridSource, ObservableObject {
let description: String? = "OffCloud is a debrid service that is used for downloads and media playback. " +
"You must pay to access this service. \n\n" +
"This service does not inform if a magnet link is a batch before downloading."
let cachedStatus: [String] = ["downloaded"]

@Published var authProcessing: Bool = false
var isLoggedIn: Bool {
Expand Down Expand Up @@ -143,12 +144,12 @@ class OffCloud: DebridSource, ObservableObject {
let selectedCloudMagnet: DebridCloudMagnet

// Don't queue a new job if the magnet already exists in the user's account
if let existingCloudMagnet = cloudMagnets.first(where: { $0.hash == magnet.hash && $0.status == "downloaded" }) {
if let existingCloudMagnet = cloudMagnets.first(where: { $0.hash == magnet.hash && cachedStatus.contains($0.status) }) {
selectedCloudMagnet = existingCloudMagnet
} else {
let cloudDownloadResponse = try await offcloudDownload(magnet: magnet)

guard cloudDownloadResponse.status == "downloaded" else {
guard cachedStatus.contains(cloudDownloadResponse.status) else {
throw DebridError.IsCaching
}

Expand Down
1 change: 1 addition & 0 deletions Ferrite/API/RealDebridWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class RealDebrid: PollingDebridSource, ObservableObject {
let id = "RealDebrid"
let abbreviation = "RD"
let website = "https://real-debrid.com"
let cachedStatus: [String] = ["downloaded"]
var authTask: Task<Void, Error>?

@Published var authProcessing: Bool = false
Expand Down
5 changes: 3 additions & 2 deletions Ferrite/API/TorBoxWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TorBox: DebridSource, ObservableObject {
let website = "https://torbox.app"
let description: String? = "TorBox is a debrid service that is used for downloads and media playback with seeding. " +
"Both free and paid plans are available."
let cachedStatus: [String] = ["cached", "completed"]

@Published var authProcessing: Bool = false
var isLoggedIn: Bool {
Expand Down Expand Up @@ -155,7 +156,7 @@ class TorBox: DebridSource, ObservableObject {
}

// If the user magnet isn't saved, it's considered as caching
guard filteredCloudMagnet.downloadState == "cached" || filteredCloudMagnet.downloadState == "completed" else {
guard cachedStatus.contains(filteredCloudMagnet.downloadState) else {
throw DebridError.IsCaching
}

Expand Down Expand Up @@ -245,7 +246,7 @@ class TorBox: DebridSource, ObservableObject {
DebridCloudMagnet(
id: String(cloudMagnet.id),
fileName: cloudMagnet.name,
status: cloudMagnet.downloadState == "cached" || cloudMagnet.downloadState == "completed" ? "downloaded" : cloudMagnet.downloadState,
status: cloudMagnet.downloadState,
hash: cloudMagnet.hash,
links: cloudMagnet.files.map { String($0.id) }
)
Expand Down
5 changes: 5 additions & 0 deletions Ferrite/Protocols/Debrid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ protocol DebridSource: AnyObservableObject {
var abbreviation: String { get }
var website: String { get }
var description: String? { get }
var cachedStatus: [String] { get }

// Auth variables
var authProcessing: Bool { get set }
Expand Down Expand Up @@ -59,6 +60,10 @@ extension DebridSource {
var description: String? {
nil
}

var cachedStatus: [String] {
[]
}
}

protocol PollingDebridSource: DebridSource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct CloudMagnetView: View {
searchText.isEmpty ? true : $0.fileName.lowercased().contains(searchText.lowercased())
}, id: \.self) { cloudMagnet in
Button {
if cloudMagnet.status == "downloaded", !cloudMagnet.links.isEmpty {
if debridSource.cachedStatus.contains(cloudMagnet.status), !cloudMagnet.links.isEmpty {
navModel.resultFromCloud = true
navModel.selectedTitle = cloudMagnet.fileName

Expand Down
9 changes: 2 additions & 7 deletions Ferrite/Views/ComponentViews/Library/DebridCloudView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ struct DebridCloudView: View {

var body: some View {
List {
if !debridSource.cloudDownloads.isEmpty {
CloudDownloadView(debridSource: debridSource, searchText: $searchText)
}

if !debridSource.cloudMagnets.isEmpty {
CloudMagnetView(debridSource: debridSource, searchText: $searchText)
}
CloudDownloadView(debridSource: debridSource, searchText: $searchText)
CloudMagnetView(debridSource: debridSource, searchText: $searchText)
}
.listStyle(.plain)
.task {
Expand Down

0 comments on commit 70b628b

Please sign in to comment.