Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lang/en/home.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"delete_policy": {
"delete_on_upload_succeed": "Delete on upload succeed",
"delete_on_upload_failed": "Delete on upload failed",
"delete_after_seeding": "Delete after seeding ends",
"delete_never": "Never delete",
"delete_always": "Always delete",
"upload_download_stream": "Download stream direct upload"
Expand Down
44 changes: 28 additions & 16 deletions src/pages/home/toolbar/OfflineDownload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,29 @@ const deletePolicies = [
"upload_download_stream",
"delete_on_upload_succeed",
"delete_on_upload_failed",
"delete_after_seeding",
"delete_never",
"delete_always",
] as const

type DeletePolicy = (typeof deletePolicies)[number]

const defaultDeletePolicy: DeletePolicy = "upload_download_stream"
const fallbackDeletePolicy: DeletePolicy = "delete_on_upload_succeed"

const supportsDeleteAfterSeeding = (tool: string) =>
tool === "qBittorrent" || tool === "Transmission"

const supportsDeletePolicy = (policy: DeletePolicy, tool: string) => {
if (policy === "upload_download_stream") {
return tool === "SimpleHttp"
}
if (policy === "delete_after_seeding") {
return supportsDeleteAfterSeeding(tool)
}
return true
}

function utf8Decode(data: Uint8Array): string {
return crypto.enc.Utf8.stringify(crypto.lib.WordArray.create(data))
}
Expand Down Expand Up @@ -58,14 +75,19 @@ export const OfflineDownload = () => {
return r.get("/public/offline_download_tools")
})
const [tool, setTool] = createSignal("")
const [deletePolicy, setDeletePolicy] = createSignal<DeletePolicy>(
"upload_download_stream",
)
const [deletePolicy, setDeletePolicy] =
createSignal<DeletePolicy>(defaultDeletePolicy)
const updateTool = (nextTool: string) => {
if (!supportsDeletePolicy(deletePolicy(), nextTool)) {
setDeletePolicy(fallbackDeletePolicy)
}
setTool(nextTool)
}
onMount(async () => {
const resp = await reqTool()
handleResp(resp, (data) => {
setTools(data)
setTool(data[0])
updateTool(data[0])
})
})

Expand Down Expand Up @@ -120,13 +142,7 @@ export const OfflineDownload = () => {
<SelectWrapper
value={tool()}
onChange={(v) => {
if (
v !== "SimpleHttp" &&
deletePolicy() === "upload_download_stream"
) {
setDeletePolicy("delete_on_upload_succeed")
}
setTool(v)
updateTool(v)
}}
options={tools().map((tool) => {
return { value: tool, label: tool }
Expand All @@ -140,11 +156,7 @@ export const OfflineDownload = () => {
value={deletePolicy()}
onChange={(v) => setDeletePolicy(v as DeletePolicy)}
options={deletePolicies
.filter((policy) =>
policy == "upload_download_stream"
? tool() === "SimpleHttp"
: true,
)
.filter((policy) => supportsDeletePolicy(policy, tool()))
.map((policy) => {
return {
value: policy,
Expand Down
64 changes: 46 additions & 18 deletions src/pages/home/toolbar/OfflineDownloadEnhanced.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,29 @@ const deletePolicies = [
"upload_download_stream",
"delete_on_upload_succeed",
"delete_on_upload_failed",
"delete_after_seeding",
"delete_never",
"delete_always",
] as const

type DeletePolicy = (typeof deletePolicies)[number]

const defaultDeletePolicy: DeletePolicy = "upload_download_stream"
const fallbackDeletePolicy: DeletePolicy = "delete_on_upload_succeed"

const supportsDeleteAfterSeeding = (tool: string) =>
tool === "qBittorrent" || tool === "Transmission"

const supportsDeletePolicy = (policy: DeletePolicy, tool: string) => {
if (policy === "upload_download_stream") {
return tool === "SimpleHttp"
}
if (policy === "delete_after_seeding") {
return supportsDeleteAfterSeeding(tool)
}
return true
}

// Tab 类型
type TabType = "link" | "bt"

Expand Down Expand Up @@ -105,9 +122,15 @@ export const OfflineDownloadEnhanced = () => {
return r.get("/public/offline_download_tools")
})
const [tool, setTool] = createSignal("")
const [deletePolicy, setDeletePolicy] = createSignal<DeletePolicy>(
"upload_download_stream",
)
const [deletePolicy, setDeletePolicy] =
createSignal<DeletePolicy>(defaultDeletePolicy)

const updateTool = (nextTool: string) => {
if (!supportsDeletePolicy(deletePolicy(), nextTool)) {
setDeletePolicy(fallbackDeletePolicy)
}
setTool(nextTool)
}

// 对话框状态
const { isOpen, onOpen, onClose } = createDisclosure()
Expand Down Expand Up @@ -236,7 +259,7 @@ export const OfflineDownloadEnhanced = () => {
if (shouldDisableSimpleHttp() && tool() === "SimpleHttp") {
const available = availableTools()
if (available.length > 0) {
setTool(available[0])
updateTool(available[0])
}
}
})
Expand All @@ -245,7 +268,7 @@ export const OfflineDownloadEnhanced = () => {
const resp = await reqTool()
handleResp(resp, (data) => {
setTools(data)
setTool(data[0])
updateTool(data[0])
})
})

Expand Down Expand Up @@ -494,7 +517,22 @@ export const OfflineDownloadEnhanced = () => {
return
}

// 正常离线下载:将 torrent 转为磁力链提交
// qBittorrent 直接提交原始 torrent 文件,避免 PT 私种转磁力后卡在获取元信息
if (tool() === "qBittorrent") {
const resp = await offlineDownload(
savePath(),
[],
tool(),
deletePolicy(),
[torrentData()],
)
handleRespWithNotifySuccess(resp, () => {
handleClose()
})
return
}

// 其它工具保持原行为:将 torrent 转为磁力链提交
const buffer = Uint8Array.from(atob(torrentData()), (c) =>
c.charCodeAt(0),
)
Expand Down Expand Up @@ -689,13 +727,7 @@ export const OfflineDownloadEnhanced = () => {
: tool()
}
onChange={(v) => {
if (
v !== "SimpleHttp" &&
deletePolicy() === "upload_download_stream"
) {
setDeletePolicy("delete_on_upload_succeed")
}
setTool(v)
updateTool(v)
}}
options={availableTools().map((t) => ({
value: t,
Expand Down Expand Up @@ -724,11 +756,7 @@ export const OfflineDownloadEnhanced = () => {
value={deletePolicy()}
onChange={(v) => setDeletePolicy(v as DeletePolicy)}
options={deletePolicies
.filter((policy) =>
policy === "upload_download_stream"
? tool() === "SimpleHttp"
: true,
)
.filter((policy) => supportsDeletePolicy(policy, tool()))
.map((policy) => ({
value: policy,
label: t(`home.toolbar.delete_policy.${policy}`),
Expand Down
9 changes: 8 additions & 1 deletion src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,15 @@ export const offlineDownload = (
urls: string[],
tool: string,
delete_policy: string,
torrent_data: string[] = [],
): PEmptyResp => {
return r.post(`/fs/add_offline_download`, { path, urls, tool, delete_policy })
return r.post(`/fs/add_offline_download`, {
path,
urls,
tool,
delete_policy,
...(torrent_data.length ? { torrent_data } : {}),
})
}

export const fetchText = async (
Expand Down